|
| 1 | +import { getServerBasicAuthHeader, injectServerAuthIntoClient } from "./opencode-server-auth" |
| 2 | + |
| 3 | +describe("opencode-server-auth", () => { |
| 4 | + let originalEnv: Record<string, string | undefined> |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + originalEnv = { |
| 8 | + OPENCODE_SERVER_PASSWORD: process.env.OPENCODE_SERVER_PASSWORD, |
| 9 | + OPENCODE_SERVER_USERNAME: process.env.OPENCODE_SERVER_USERNAME, |
| 10 | + } |
| 11 | + }) |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + for (const [key, value] of Object.entries(originalEnv)) { |
| 15 | + if (value !== undefined) { |
| 16 | + process.env[key] = value |
| 17 | + } else { |
| 18 | + delete process.env[key] |
| 19 | + } |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + test("#given no server password #when building auth header #then returns undefined", () => { |
| 24 | + delete process.env.OPENCODE_SERVER_PASSWORD |
| 25 | + |
| 26 | + const result = getServerBasicAuthHeader() |
| 27 | + |
| 28 | + expect(result).toBeUndefined() |
| 29 | + }) |
| 30 | + |
| 31 | + test("#given server password without username #when building auth header #then uses default username", () => { |
| 32 | + process.env.OPENCODE_SERVER_PASSWORD = "secret" |
| 33 | + delete process.env.OPENCODE_SERVER_USERNAME |
| 34 | + |
| 35 | + const result = getServerBasicAuthHeader() |
| 36 | + |
| 37 | + expect(result).toBe("Basic b3BlbmNvZGU6c2VjcmV0") |
| 38 | + }) |
| 39 | + |
| 40 | + test("#given server password and username #when building auth header #then uses provided username", () => { |
| 41 | + process.env.OPENCODE_SERVER_PASSWORD = "secret" |
| 42 | + process.env.OPENCODE_SERVER_USERNAME = "dan" |
| 43 | + |
| 44 | + const result = getServerBasicAuthHeader() |
| 45 | + |
| 46 | + expect(result).toBe("Basic ZGFuOnNlY3JldA==") |
| 47 | + }) |
| 48 | + |
| 49 | + test("#given server password #when injecting into client #then updates client headers", () => { |
| 50 | + process.env.OPENCODE_SERVER_PASSWORD = "secret" |
| 51 | + delete process.env.OPENCODE_SERVER_USERNAME |
| 52 | + |
| 53 | + let receivedConfig: { headers: Record<string, string> } | undefined |
| 54 | + const client = { |
| 55 | + _client: { |
| 56 | + setConfig: (config: { headers: Record<string, string> }) => { |
| 57 | + receivedConfig = config |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + injectServerAuthIntoClient(client) |
| 63 | + |
| 64 | + expect(receivedConfig).toEqual({ |
| 65 | + headers: { |
| 66 | + Authorization: "Basic b3BlbmNvZGU6c2VjcmV0", |
| 67 | + }, |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + test("#given server password #when client has no _client #then throws error", () => { |
| 72 | + process.env.OPENCODE_SERVER_PASSWORD = "secret" |
| 73 | + const client = {} |
| 74 | + |
| 75 | + expect(() => injectServerAuthIntoClient(client)).toThrow( |
| 76 | + "[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client structure is incompatible" |
| 77 | + ) |
| 78 | + }) |
| 79 | + |
| 80 | + test("#given server password #when client._client has no setConfig #then throws error", () => { |
| 81 | + process.env.OPENCODE_SERVER_PASSWORD = "secret" |
| 82 | + const client = { _client: {} } |
| 83 | + |
| 84 | + expect(() => injectServerAuthIntoClient(client)).toThrow( |
| 85 | + "[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client._client.setConfig is not a function" |
| 86 | + ) |
| 87 | + }) |
| 88 | + |
| 89 | + test("#given no server password #when client is invalid #then does not throw", () => { |
| 90 | + delete process.env.OPENCODE_SERVER_PASSWORD |
| 91 | + const client = {} |
| 92 | + |
| 93 | + expect(() => injectServerAuthIntoClient(client)).not.toThrow() |
| 94 | + }) |
| 95 | +}) |
0 commit comments