|
1 | 1 | 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"; |
2 | 5 | import { resolveApiKey, resolveApiKeyWithSource } from "../src/auth/resolver.ts"; |
3 | 6 |
|
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]])); |
5 | 9 |
|
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]; |
10 | 14 | } |
11 | 15 |
|
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 | +}); |
19 | 20 |
|
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", () => { |
21 | 31 | process.env.COMMAND_CODE_API_KEY = " cc_env_key_1234567890 "; |
22 | 32 | const resolved = resolveApiKeyWithSource(); |
23 | 33 | expect(resolved.key).toBe("cc_env_key_1234567890"); |
24 | 34 | expect(resolved.source).toBe("env:COMMAND_CODE_API_KEY"); |
25 | 35 | }); |
26 | 36 |
|
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; |
28 | 50 | 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"); |
33 | 62 | }); |
34 | 63 |
|
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 | + |
37 | 82 | 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")}`); |
40 | 85 | }); |
41 | 86 |
|
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" '; |
43 | 89 | const key = resolveApiKey(); |
44 | 90 | expect(key).toBe(key.trim()); |
45 | 91 | expect(key.startsWith('"') || key.startsWith("'")).toBe(false); |
46 | 92 | }); |
47 | 93 |
|
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); |
52 | 99 | }); |
53 | 100 | }); |
0 commit comments