Skip to content

Commit 3dccb66

Browse files
committed
fix(tests): make the suite hermetic so CI passes without host credentials
Two tests asserted against the developer machine instead of a controlled environment, so the first CI run failed on a clean runner: - auth resolver > resolver reads a real usable key on this host: required an actual key on the host. Replaced with hermetic coverage of the cascade: environment priority and quote stripping, explicit COMMANDCODE_KEY_FILE use, precedence of the env var over the file, a missing or empty override file not short-circuiting, and XDG config key file resolution against a temp directory. - http.request hook > injects the API key only for the real Command Code origin: depended on a resolvable host key to assert the bearer header. The hook tests now set a fixed COMMAND_CODE_API_KEY in beforeEach and restore the environment in afterEach. Verified by simulating a clean runner (isolated HOME, no key in the environment): 61 pass, 3 skip, 0 fail, and a clean typecheck.
1 parent 4211384 commit 3dccb66

2 files changed

Lines changed: 82 additions & 28 deletions

File tree

‎tests/auth.test.ts‎

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,100 @@
11
import { describe, expect, test, beforeEach, afterEach } from "bun:test";
2+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
25
import { resolveApiKey, resolveApiKeyWithSource } from "../src/auth/resolver.ts";
36

4-
const ORIGINAL_ENV = { ...process.env };
7+
const MANAGED_VARS = ["COMMAND_CODE_API_KEY", "COMMANDCODE_KEY_FILE", "XDG_CONFIG_HOME"] as const;
8+
const ORIGINAL = new Map<string, string | undefined>(MANAGED_VARS.map((k) => [k, process.env[k]]));
59

6-
function resetEnv() {
7-
for (const k of ["COMMAND_CODE_API_KEY", "COMMANDCODE_KEY_FILE", "XDG_CONFIG_HOME"]) {
8-
delete process.env[k];
9-
}
10+
let scratch = "";
11+
12+
function clearManagedVars() {
13+
for (const k of MANAGED_VARS) delete process.env[k];
1014
}
1115

12-
describe("auth resolver", () => {
13-
beforeEach(resetEnv);
14-
afterEach(() => {
15-
resetEnv();
16-
process.env = { ...ORIGINAL_ENV };
17-
Object.assign(process.env, ORIGINAL_ENV);
18-
});
16+
beforeEach(() => {
17+
clearManagedVars();
18+
scratch = mkdtempSync(join(tmpdir(), "cc-auth-test-"));
19+
});
1920

20-
test("environment variable has highest priority and is trimmed", () => {
21+
afterEach(() => {
22+
clearManagedVars();
23+
for (const [k, v] of ORIGINAL) {
24+
if (v !== undefined) process.env[k] = v;
25+
}
26+
if (scratch) rmSync(scratch, { recursive: true, force: true });
27+
});
28+
29+
describe("auth resolver", () => {
30+
test("environment variable has the highest priority and is trimmed", () => {
2131
process.env.COMMAND_CODE_API_KEY = " cc_env_key_1234567890 ";
2232
const resolved = resolveApiKeyWithSource();
2333
expect(resolved.key).toBe("cc_env_key_1234567890");
2434
expect(resolved.source).toBe("env:COMMAND_CODE_API_KEY");
2535
});
2636

27-
test("falls back to a key file when the env var is absent", () => {
37+
test("strips surrounding quotes from the environment variable", () => {
38+
process.env.COMMAND_CODE_API_KEY = '"cc_quoted_key"';
39+
expect(resolveApiKey()).toBe("cc_quoted_key");
40+
41+
process.env.COMMAND_CODE_API_KEY = "'cc_single_quoted'";
42+
expect(resolveApiKey()).toBe("cc_single_quoted");
43+
});
44+
45+
test("explicit COMMANDCODE_KEY_FILE is used when it points at a real file", () => {
46+
const keyFile = join(scratch, "key.txt");
47+
writeFileSync(keyFile, "cc_file_key_abcdefghij\n", "utf8");
48+
49+
process.env.COMMANDCODE_KEY_FILE = keyFile;
2850
const resolved = resolveApiKeyWithSource();
29-
// On this host the key lives in ~/.config/commandcode/key.txt or /etc/environment.
30-
if (resolved.key) {
31-
expect(["file", "env"]).toContain(resolved.source.split(":")[0]!);
32-
}
51+
expect(resolved.key).toBe("cc_file_key_abcdefghij");
52+
expect(resolved.source).toBe(`file:${keyFile}`);
53+
});
54+
55+
test("the environment variable wins over a key file", () => {
56+
const keyFile = join(scratch, "key.txt");
57+
writeFileSync(keyFile, "cc_file_key", "utf8");
58+
59+
process.env.COMMANDCODE_KEY_FILE = keyFile;
60+
process.env.COMMAND_CODE_API_KEY = "cc_env_wins";
61+
expect(resolveApiKey()).toBe("cc_env_wins");
3362
});
3463

35-
test("explicit COMMANDCODE_KEY_FILE override is honoured", () => {
36-
process.env.COMMANDCODE_KEY_FILE = "/tmp/opencode/nonexistent-key-file.txt";
64+
test("a missing or empty override file does not short-circuit the cascade", () => {
65+
process.env.COMMANDCODE_KEY_FILE = join(scratch, "does-not-exist.txt");
66+
const missing = resolveApiKeyWithSource();
67+
expect(missing.source).not.toContain("does-not-exist.txt");
68+
69+
const emptyFile = join(scratch, "empty.txt");
70+
writeFileSync(emptyFile, " \n", "utf8");
71+
process.env.COMMANDCODE_KEY_FILE = emptyFile;
72+
const empty = resolveApiKeyWithSource();
73+
expect(empty.source).not.toBe(`file:${emptyFile}`);
74+
});
75+
76+
test("reads the XDG config key file when the higher-priority stores are absent", () => {
77+
const nested = join(scratch, "commandcode");
78+
mkdirSync(nested, { recursive: true });
79+
writeFileSync(join(nested, "key.txt"), "cc_xdg_key_zyxwvutsrq", "utf8");
80+
process.env.XDG_CONFIG_HOME = scratch;
81+
3782
const resolved = resolveApiKeyWithSource();
38-
// Missing override file must not short-circuit the cascade with garbage.
39-
expect(resolved.source).not.toContain("nonexistent-key-file");
83+
expect(resolved.key).toBe("cc_xdg_key_zyxwvutsrq");
84+
expect(resolved.source).toBe(`file:${join(nested, "key.txt")}`);
4085
});
4186

42-
test("never returns a key with surrounding quotes or whitespace", () => {
87+
test("never returns a key with surrounding whitespace or quotes", () => {
88+
process.env.COMMAND_CODE_API_KEY = ' "cc_padded" ';
4389
const key = resolveApiKey();
4490
expect(key).toBe(key.trim());
4591
expect(key.startsWith('"') || key.startsWith("'")).toBe(false);
4692
});
4793

48-
test("resolver reads a real usable key on this host", () => {
49-
const key = resolveApiKey();
50-
expect(typeof key).toBe("string");
51-
expect(key.length).toBeGreaterThan(10);
94+
test("reports a source identifier for every resolution outcome", () => {
95+
process.env.COMMAND_CODE_API_KEY = "cc_source_key";
96+
expect(resolveApiKeyWithSource().source.length).toBeGreaterThan(0);
97+
delete process.env.COMMAND_CODE_API_KEY;
98+
expect(resolveApiKeyWithSource().source.length).toBeGreaterThan(0);
5299
});
53100
});

‎tests/plugin.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, test, beforeEach } from "bun:test";
1+
import { describe, expect, test, beforeEach, afterEach } from "bun:test";
22
import plugin, { applyModelCatalog } from "../src/index.ts";
33
import { sanitizeCommandCodeMessages } from "../src/hooks/resilience.ts";
44

@@ -74,12 +74,19 @@ describe("plugin registration", () => {
7474

7575
describe("http.request hook behavior", () => {
7676
let harness: ReturnType<typeof makeCtx>;
77+
const MANAGED_KEY = "cc_test_key_1234567890";
7778

7879
beforeEach(async () => {
80+
// Deterministic credential so the hook tests never depend on the host setup.
81+
process.env.COMMAND_CODE_API_KEY = MANAGED_KEY;
7982
harness = makeCtx();
8083
await plugin.setup(harness.ctx);
8184
});
8285

86+
afterEach(() => {
87+
delete process.env.COMMAND_CODE_API_KEY;
88+
});
89+
8390
test("injects the API key only for the real Command Code origin", async () => {
8491
const ok = new Request("https://api.commandcode.ai/provider/v1/chat/completions", {
8592
method: "POST",

0 commit comments

Comments
 (0)