import assert from "node:assert/strict" import test, { before, after } from "node:test" import { existsSync, mkdtempSync, mkdirSync, readdirSync, readFileSync, rmSync, } from "node:fs" import { join } from "node:path" import { tmpdir } from "node:os" import { pathToFileURL } from "node:url" // Tier-2 coverage: drive each plugin hook with a fake OpenCode client. // Runs a single real proxy instance for the whole file; the OS cleans it up // when the Node test runner exits (registerCleanup is wired to process exit). let hooks let fakeHomeDir let logEntries = [] let previousEnv = {} let meridianModelMapperPromise let plugin let secondHooks async function loadMeridianModelMapper() { if (meridianModelMapperPromise) return meridianModelMapperPromise const distDir = join(process.cwd(), "node_modules", "@rynfar", "meridian", "dist") const mapperFile = readdirSync(distDir).find((entry) => { if (!entry.endsWith(".js")) return false return readFileSync(join(distDir, entry), "utf8").includes( "function mapModelToClaudeModel", ) }) assert.ok(mapperFile, "expected Meridian dist to expose mapModelToClaudeModel") meridianModelMapperPromise = import( pathToFileURL(join(distDir, mapperFile)).href ) return meridianModelMapperPromise } function makeClient() { return { app: { log: async ({ body }) => { // Capture log output so we can assert against it if needed. logEntries.push(body) return {} }, }, } } before(async () => { // Isolate ~/.config/meridian so tests don't read the developer's real files. fakeHomeDir = mkdtempSync(join(tmpdir(), "owc-hooks-")) mkdirSync(join(fakeHomeDir, ".config", "meridian"), { recursive: true }) previousEnv = { HOME: process.env.HOME, USERPROFILE: process.env.USERPROFILE, CLAUDE_PROXY_PORT: process.env.CLAUDE_PROXY_PORT, MERIDIAN_WORKDIR: process.env.MERIDIAN_WORKDIR, CLAUDE_PROXY_WORKDIR: process.env.CLAUDE_PROXY_WORKDIR, } process.env.HOME = fakeHomeDir process.env.USERPROFILE = fakeHomeDir delete process.env.MERIDIAN_WORKDIR delete process.env.CLAUDE_PROXY_WORKDIR // Use a random OS-assigned port so multiple runs don't collide. process.env.CLAUDE_PROXY_PORT = "0" ;({ default: plugin } = await import( `../../dist/index.js?t=${Date.now()}${Math.random()}` )) ;[hooks, secondHooks] = await Promise.all([ plugin.server({ client: makeClient(), directory: fakeHomeDir, worktree: fakeHomeDir }), plugin.server({ client: makeClient(), directory: `${fakeHomeDir}/second`, worktree: fakeHomeDir }), ]) }) test("v1 project instances share the in-flight and already running proxy", async () => { const thirdHooks = await plugin.server({ client: makeClient() }) const urls = [] for (const instance of [hooks, secondHooks, thirdHooks]) { const config = { provider: { anthropic: {} } } await instance.config(config) urls.push(config.provider.anthropic.options.baseURL) } assert.equal(new Set(urls).size, 1) assert.equal(logEntries.filter((entry) => entry.message.startsWith("proxy ready at http://")).length, 1) await hooks.config({ agent: { custom: { mode: "subagent" } } }) const output = { headers: {} } await secondHooks["chat.headers"]({ sessionID: "separate-project", agent: "custom", model: { providerID: "anthropic" }, message: { id: "msg" }, }, output) assert.equal(output.headers["x-opencode-agent-mode"], "primary", "agent config stays local to each plugin instance") }) after(async () => { // plugin.server() starts the proxy internally but doesn't return its // handle, so we trigger its registerCleanup() hook by emitting SIGINT // and waiting a short tick for the async close() to drain the event loop. // Without this, the open server keeps node:test from exiting. process.emit("SIGINT") await new Promise((resolve) => setTimeout(resolve, 250)) for (const [key, value] of Object.entries(previousEnv)) { if (value === undefined) delete process.env[key] else process.env[key] = value } rmSync(fakeHomeDir, { recursive: true, force: true }) }) // --------------------------------------------------------------------------- // config hook // --------------------------------------------------------------------------- test("config hook rewrites provider.anthropic.options.baseURL to the proxy URL", async () => { const input = { provider: { anthropic: { options: { baseURL: "https://api.anthropic.com" } } }, } await hooks.config(input) assert.match(input.provider.anthropic.options.baseURL, /^http:\/\/.+:\d+$/) }) test("config hook creates options when missing on the anthropic provider", async () => { const input = { provider: { anthropic: {} } } await hooks.config(input) assert.ok(input.provider.anthropic.options) assert.match(input.provider.anthropic.options.baseURL, /^http:\/\//) }) test("config hook is a no-op when no anthropic provider exists", async () => { const input = { provider: { openai: { options: { baseURL: "other" } } } } await hooks.config(input) assert.deepEqual(input, { provider: { openai: { options: { baseURL: "other" } } }, }) }) // --------------------------------------------------------------------------- // system prompt handling // --------------------------------------------------------------------------- test("system.transform scrubs OpenCode fingerprints and keeps user context", async () => { const opencodePrompt = [ "You are OpenCode, the best coding agent on the planet.", "", "If the user asks for help or wants to give feedback inform them of the following:", "- To give feedback, users should report the issue at", " https://github.com/anomalyco/opencode", "", "When the user directly asks about OpenCode, use docs from https://opencode.ai/docs", "", "It is best for the user if OpenCode honestly applies rigorous standards.", "Keep this tool guidance.", ].join("\n") const env = [ "You are powered by the model named claude-opus-4-6. The exact model ID is anthropic/claude-opus-4-6", "Here is some useful information about the environment you are running in:", "", " Working directory: /tmp/project", "", ].join("\n") const agents = "# Fake agents marker\nproject-specific instructions here." const output = { system: [opencodePrompt, env, agents] } await hooks["experimental.chat.system.transform"]( { model: { providerID: "anthropic" } }, output, ) assert.equal(output.system.length, 1) const scrubbed = output.system[0] assert.match(scrubbed, /You are an expert coding assistant/) assert.match(scrubbed, /Keep this tool guidance/) assert.match(scrubbed, /Fake agents marker/) assert.match(scrubbed, /the assistant honestly applies/) assert.doesNotMatch(scrubbed, /OpenCode/) assert.doesNotMatch(scrubbed, /github\.com\/anomalyco\/opencode/) assert.doesNotMatch(scrubbed, /opencode\.ai\/docs/) assert.doesNotMatch(scrubbed, /powered by the model named/) assert.doesNotMatch(scrubbed, /useful information about the environment/) // The scrub runs client-side here, before Meridian sees the prompt. The // bare working-directory line is the only place Meridian can read the // project path from to pick the SDK session's cwd, so it must survive. assert.match(scrubbed, /\n\s*Working directory: \/tmp\/project\n<\/env>/) }) test("system.transform ignores non-anthropic providers", async () => { const system = [ "You are OpenCode, You and the user share the same workspace.", "\nWorking directory: /tmp/project\n", ] const output = { system: [...system] } await hooks["experimental.chat.system.transform"]( { model: { providerID: "openai" } }, output, ) assert.deepEqual(output.system, system) }) test("plugin does not mutate Meridian sdk-features.json", () => { assert.equal( existsSync(join(fakeHomeDir, ".config", "meridian", "sdk-features.json")), false, ) }) test("plugin does not pin MERIDIAN_WORKDIR, leaving per-session cwd resolution to Meridian", () => { // Pinning MERIDIAN_WORKDIR at plugin init takes highest precedence in // Meridian's resolveSdkWorkingDirectory and defeats the per-session // adapterCwd extracted from the client. Long-lived hosts (e.g. the // OpenCode desktop app) serve many projects from one process, so the // plugin must not pin a process-wide working directory. assert.equal(process.env.MERIDIAN_WORKDIR, undefined) assert.equal(process.env.CLAUDE_PROXY_WORKDIR, undefined) }) // --------------------------------------------------------------------------- // chat.headers — strip anthropic-beta, add OpenCode/Meridian headers // --------------------------------------------------------------------------- test("chat.message marks the observed message ID as human for chat.headers", async () => { const messageId = "msg-human" await hooks["chat.message"]( { sessionID: "sess-123" }, { message: { id: messageId } }, ) const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", model: { providerID: "anthropic" }, message: { id: messageId }, }, output, ) assert.equal(output.headers["x-opencode-request-kind"], "human") }) test("chat.headers keeps an observed message ID human across repeated calls in the same session", async () => { const messageId = "msg-repeated-human" const sessionID = "sess-123" await hooks["chat.message"]( { sessionID }, { message: { id: messageId } }, ) for (const _ of [0, 1]) { const output = { headers: {} } await hooks["chat.headers"]( { sessionID, model: { providerID: "anthropic" }, message: { id: messageId }, }, output, ) assert.equal(output.headers["x-opencode-request-kind"], "human") } }) test("chat.headers marks the same message ID in another session as synthetic", async () => { const messageId = "msg-session-scoped" await hooks["chat.message"]( { sessionID: "sess-human" }, { message: { id: messageId } }, ) const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-other", model: { providerID: "anthropic" }, message: { id: messageId }, }, output, ) assert.equal(output.headers["x-opencode-request-kind"], "synthetic") }) test("chat.headers marks an unobserved message ID as synthetic", async () => { const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", model: { providerID: "anthropic" }, message: { id: "msg-unobserved-synthetic" }, }, output, ) assert.equal(output.headers["x-opencode-request-kind"], "synthetic") }) test("turn-origin metadata does not change non-anthropic chat.headers behavior", async () => { await hooks["chat.message"]( { sessionID: "sess-123" }, { message: { id: "msg-non-anthropic" } }, ) const output = { headers: { "anthropic-beta": "still-here" } } await hooks["chat.headers"]( { sessionID: "sess-123", model: { providerID: "openai" }, message: { id: "msg-non-anthropic" }, }, output, ) assert.deepEqual(output.headers, { "anthropic-beta": "still-here" }) }) test("chat.headers strips anthropic-beta and adds session + request IDs", async () => { const output = { headers: { "anthropic-beta": "some-flag", keep: "me" } } await hooks["chat.headers"]( { sessionID: "sess-123", model: { providerID: "anthropic" }, message: { id: "msg-abc" }, }, output, ) assert.equal(output.headers["anthropic-beta"], undefined) assert.equal(output.headers["x-opencode-session"], "sess-123") assert.equal(output.headers["x-opencode-request"], "msg-abc") assert.equal(output.headers["x-opencode-agent-mode"], "primary") assert.equal(output.headers.keep, "me", "other headers should be preserved") }) test("chat.headers strips non-ASCII before mode lookup", async () => { await hooks.config({ agent: { explore: { mode: "subagent" }, }, }) const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", agent: "explore\u200b", model: { providerID: "anthropic" }, message: { id: "msg-abc" }, }, output, ) assert.equal(output.headers["x-opencode-agent-mode"], "subagent") assert.equal(output.headers["x-opencode-agent-name"], "explore") }) test("chat.headers detaches title and summary requests from the session lease", async () => { // OpenCode titles a fresh session on the same session id, concurrently with // the user's first prompt; Meridian's per-session turn lease then rejects // whichever request waited. Title requests must carry no session header // and declare subagent mode so Meridian treats them as independent. for (const agent of ["title", { name: "title", mode: "primary" }, "summary"]) { // A session header already present (provider config, an earlier hook) // must be removed, not merely left unset. const output = { headers: { "X-OpenCode-Session": "stale", "x-session-affinity": "stale", }, } await hooks["chat.headers"]( { sessionID: "sess-123", model: { providerID: "anthropic" }, message: { id: "msg-title" }, agent, }, output, ) const name = typeof agent === "string" ? agent : agent.name assert.equal(output.headers["x-opencode-session"], undefined) assert.equal(output.headers["X-OpenCode-Session"], undefined) assert.equal(output.headers["x-session-affinity"], undefined) assert.equal(output.headers["x-meridian-source"], `subagent-${name}`) assert.equal(output.headers["x-opencode-agent-mode"], "subagent") assert.equal(output.headers["x-opencode-agent-name"], name) assert.equal(output.headers["x-opencode-request"], "msg-title") } }) test("chat.headers replaces a stale session header on attached requests", async () => { const output = { headers: { "X-Opencode-Session": "stale", "x-session-id": "stale" } } await hooks["chat.headers"]( { sessionID: "sess-123", model: { providerID: "anthropic" }, message: { id: "msg-abc" }, agent: "build", }, output, ) assert.equal(output.headers["x-opencode-session"], "sess-123") assert.equal(output.headers["X-Opencode-Session"], undefined) assert.equal(output.headers["x-session-id"], undefined) assert.equal(output.headers["x-meridian-source"], undefined) }) test("chat.headers knows OpenCode's built-in subagents without config", async () => { const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", model: { providerID: "anthropic" }, message: { id: "msg-abc" }, agent: "explore", }, output, ) assert.equal(output.headers["x-opencode-agent-mode"], "subagent") assert.equal(output.headers["x-opencode-session"], "sess-123") }) test("chat.headers maps agent mode 'all' to primary for Meridian", async () => { const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", model: { providerID: "anthropic" }, message: { id: "msg-abc" }, agent: { name: "helper", mode: "all" }, }, output, ) assert.equal(output.headers["x-opencode-agent-mode"], "primary") }) test("chat.headers reads mode from runtime agent objects", async () => { const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", agent: { name: "runtime-only", mode: "subagent" }, model: { providerID: "anthropic" }, message: { id: "msg-abc" }, }, output, ) assert.equal(output.headers["x-opencode-agent-mode"], "subagent") assert.equal(output.headers["x-opencode-agent-name"], "runtime-only") }) test("chat.headers prefers runtime agent mode over cached config", async () => { await hooks.config({ agent: { explore: { mode: "primary" }, }, }) const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", agent: { name: "explore", mode: "subagent" }, model: { providerID: "anthropic" }, message: { id: "msg-abc" }, }, output, ) assert.equal(output.headers["x-opencode-agent-mode"], "subagent") assert.equal(output.headers["x-opencode-agent-name"], "explore") }) test("chat.headers keeps exact subagent mode without an agent name", async () => { const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", agent: { mode: "subagent" }, model: { providerID: "anthropic" }, message: { id: "msg-abc" }, }, output, ) assert.equal(output.headers["x-opencode-agent-mode"], "subagent") assert.equal(output.headers["x-opencode-agent-name"], "unknown") }) test("chat.headers subagent mode selects Meridian non-extended tier", async () => { const { mapModelToClaudeModel } = await loadMeridianModelMapper() const subagentOutput = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", agent: { name: "explore", mode: "subagent" }, model: { providerID: "anthropic" }, message: { id: "msg-subagent" }, }, subagentOutput, ) const primaryOutput = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", agent: { name: "build", mode: "primary" }, model: { providerID: "anthropic" }, message: { id: "msg-primary" }, }, primaryOutput, ) assert.equal( mapModelToClaudeModel( "claude-opus-4-7", undefined, subagentOutput.headers["x-opencode-agent-mode"], ), "opus", ) assert.equal( mapModelToClaudeModel( "claude-opus-4-7", undefined, primaryOutput.headers["x-opencode-agent-mode"], ), "opus[1m]", ) }) test("chat.headers resolves string agent modes from OpenCode config", async () => { await hooks.config({ agent: { explore: { mode: "subagent" }, build: { mode: "primary" }, }, }) const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "sess-123", agent: "explore", model: { providerID: "anthropic" }, message: { id: "msg-abc", agent: "explore" }, }, output, ) assert.equal(output.headers["x-opencode-agent-mode"], "subagent") }) test("chat.headers is safe when anthropic-beta header was never present", async () => { const output = { headers: {} } await hooks["chat.headers"]( { sessionID: "s", model: { providerID: "anthropic" }, message: { id: "m" }, }, output, ) assert.equal(output.headers["x-opencode-session"], "s") assert.equal(output.headers["x-opencode-request"], "m") assert.equal(output.headers["x-opencode-agent-mode"], "primary") }) test("chat.headers is a no-op for non-anthropic providers", async () => { const output = { headers: { "anthropic-beta": "still-here" } } await hooks["chat.headers"]( { sessionID: "s", model: { providerID: "openai" }, message: { id: "m" }, }, output, ) assert.deepEqual(output.headers, { "anthropic-beta": "still-here" }) }) // --------------------------------------------------------------------------- // Logger instrumentation // --------------------------------------------------------------------------- test("plugin logs 'proxy ready' during startup", () => { // The before-hook already invoked plugin.server(). One of the startup log // entries should announce the proxy URL. assert.ok( logEntries.some( (e) => e.service === "opencode-with-claude" && typeof e.message === "string" && e.message.startsWith("proxy ready at http://"), ), "expected a 'proxy ready at ...' log entry from startup", ) })