@@ -0,0 +1,105 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { join } from "node:path";
5+
6+const connectionModuleUrl = new URL(
7+ "../session/db/connection.ts",
8+ import.meta.url,
9+).href;
10+const prepareModuleUrl = new URL("./turn-prepare-messages.ts", import.meta.url)
11+ .href;
12+
13+function runInHome(
14+ home: string,
15+ script: string,
16+): {
17+ exitCode: number;
18+ stdout: string;
19+ stderr: string;
20+} {
21+ const scriptPath = join(
22+ tmpdir(),
23+ `mc-turn-prepare-test-${process.pid}-${Date.now()}.mjs`,
24+ );
25+ writeFileSync(scriptPath, script);
26+ try {
27+ const result = Bun.spawnSync([process.execPath, scriptPath], {
28+ env: { ...process.env, HOME: home },
29+ stdout: "pipe",
30+ stderr: "pipe",
31+ });
32+ return {
33+ exitCode: result.exitCode,
34+ stdout: result.stdout.toString(),
35+ stderr: result.stderr.toString(),
36+ };
37+ } finally {
38+ rmSync(scriptPath, { force: true });
39+ }
40+}
41+
42+describe("prepareTurnMessages Anthropic OAuth identity handling", () => {
43+ let fakeHome = "";
44+
45+ beforeEach(() => {
46+ fakeHome = mkdtempSync(join(tmpdir(), "mc-turn-prepare-home-"));
47+ mkdirSync(join(fakeHome, ".config", "mini-coder"), { recursive: true });
48+ });
49+
50+ afterEach(() => {
51+ rmSync(fakeHome, { recursive: true, force: true });
52+ });
53+
54+ test("keeps Claude Code identity on direct Anthropic OAuth, but not on zen/claude", () => {
55+ const result = runInHome(
56+ fakeHome,
57+ `
58+ import { getDb } from ${JSON.stringify(connectionModuleUrl)};
59+ import { prepareTurnMessages } from ${JSON.stringify(prepareModuleUrl)};
60+
61+ const db = getDb();
62+ db.run(
63+ "INSERT INTO oauth_tokens (provider, access_token, refresh_token, expires_at, updated_at) VALUES (?, ?, ?, ?, ?)",
64+ ["anthropic", "token", "refresh", ${Date.now() + 60_000}, ${Date.now()}],
65+ );
66+
67+ const anthropic = prepareTurnMessages({
68+ messages: [{ role: "user", content: "hi" }],
69+ modelString: "anthropic/claude-sonnet-4-6",
70+ toolCount: 0,
71+ systemPrompt: "sys",
72+ });
73+ const zen = prepareTurnMessages({
74+ messages: [{ role: "user", content: "hi" }],
75+ modelString: "zen/claude-sonnet-4-6",
76+ toolCount: 0,
77+ systemPrompt: "sys",
78+ });
79+
80+ console.log(JSON.stringify({
81+ anthropicMessages: anthropic.messages,
82+ anthropicSystemPrompt: anthropic.systemPrompt,
83+ zenMessages: zen.messages,
84+ zenSystemPrompt: zen.systemPrompt,
85+ }));
86+ `,
87+ );
88+
89+ expect(result.exitCode).toBe(0);
90+ expect(result.stderr).toBe("");
91+ expect(JSON.parse(result.stdout)).toEqual({
92+ anthropicMessages: [
93+ {
94+ role: "system",
95+ content: "You are Claude Code, Anthropic's official CLI for Claude.",
96+ },
97+ { role: "system", content: "sys" },
98+ { role: "user", content: "hi" },
99+ ],
100+ anthropicSystemPrompt: undefined,
101+ zenMessages: [{ role: "user", content: "hi" }],
102+ zenSystemPrompt: "sys",
103+ });
104+ });
105+});