config.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import fs from "node:fs/promises"
  2. import path from "node:path"
  3. import { expect } from "bun:test"
  4. import { Config } from "@opencode-ai/schema/config"
  5. import { Effect, Schema } from "effect"
  6. import { HttpServer } from "effect/unstable/http"
  7. import { tmpdir } from "../../core/test/fixture/tmpdir"
  8. import { it } from "../../core/test/lib/effect"
  9. import { ServerProcess } from "../src/process"
  10. it.live("returns ordered config entries for the requested directory", () =>
  11. Effect.acquireUseRelease(
  12. Effect.promise(() => tmpdir("opencode-config-endpoint-")),
  13. (tmp) =>
  14. Effect.gen(function* () {
  15. const global = path.join(tmp.path, "global")
  16. const project = path.join(tmp.path, "project")
  17. const config = path.join(project, "opencode.json")
  18. yield* Effect.promise(() =>
  19. Promise.all([fs.mkdir(global, { recursive: true }), fs.mkdir(project, { recursive: true })]),
  20. )
  21. yield* Effect.promise(() =>
  22. fs.writeFile(
  23. config,
  24. JSON.stringify({
  25. permissions: [
  26. { action: "shell", resource: "*", effect: "ask" },
  27. { action: "shell", resource: "git status", effect: "allow" },
  28. ],
  29. mcp: { servers: { docs: { type: "remote", url: "https://example.com/mcp" } } },
  30. }),
  31. ),
  32. )
  33. const server = yield* ServerProcess.start<never, never>({
  34. hostname: "127.0.0.1",
  35. port: 0,
  36. password: "secret",
  37. app: { version: "test-version" },
  38. database: { path: ":memory:" },
  39. config: { directory: global },
  40. fs: { filewatcher: false },
  41. })
  42. const url = new URL("/api/config", HttpServer.formatAddress(server.address))
  43. url.searchParams.set("location[directory]", project)
  44. const response = yield* Effect.promise(() =>
  45. fetch(url, { headers: { authorization: `Basic ${btoa("opencode:secret")}` } }),
  46. )
  47. const body: unknown = yield* Effect.promise(() => response.json())
  48. const entries = Schema.decodeUnknownSync(Schema.Array(Config.Entry))(body)
  49. expect(response.status).toBe(200)
  50. expect(Array.isArray(entries)).toBe(true)
  51. const document = entries.find(
  52. (entry): entry is Config.Document => entry.type === "document" && entry.path === config,
  53. )
  54. expect(document?.info.permissions).toEqual([
  55. { action: "shell", resource: "*", effect: "ask" },
  56. { action: "shell", resource: "git status", effect: "allow" },
  57. ])
  58. expect(entries.some((entry) => entry.type === "file" && entry.path === config)).toBe(true)
  59. if (!Array.isArray(body)) throw new Error("Expected a config entry array")
  60. const raw = body.find((entry) => isRecord(entry) && entry["type"] === "document" && entry["path"] === config)
  61. if (!isRecord(raw) || !isRecord(raw["info"])) throw new Error("Expected a config document")
  62. expect(raw["info"]).not.toHaveProperty("default_agent")
  63. expect(raw["info"]).not.toHaveProperty("model")
  64. const mcp = raw["info"]["mcp"]
  65. if (!isRecord(mcp) || !isRecord(mcp["servers"]) || !isRecord(mcp["servers"]["docs"]))
  66. throw new Error("Expected an MCP server config")
  67. expect(mcp["servers"]["docs"]).not.toHaveProperty("headers")
  68. expect(mcp["servers"]["docs"]).not.toHaveProperty("oauth")
  69. }),
  70. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  71. ),
  72. )
  73. function isRecord(value: unknown): value is Record<string, unknown> {
  74. return typeof value === "object" && value !== null && !Array.isArray(value)
  75. }