import { getAnthropicClient, resetAnthropicClient } from '../../src/intake/anthropicClient'; const KEY = 'sk-ant-test-key'; beforeEach(() => { resetAnthropicClient(); delete process.env.ANTHROPIC_API_KEY; delete process.env.ANTHROPIC_WORKSPACE_ID; }); afterAll(() => { resetAnthropicClient(); }); /** The SDK stores what it was given, so the header is readable back off it. */ function headerOf(client: unknown): string | undefined { const headers = (client as { _options?: { defaultHeaders?: Record } })._options ?.defaultHeaders; return headers?.['anthropic-workspace-id']; } describe('getAnthropicClient', () => { it('is null without a key, because unconfigured is a working configuration', () => { expect(getAnthropicClient()).toBeNull(); }); it('is null for a key that is only whitespace', () => { process.env.ANTHROPIC_API_KEY = ' '; expect(getAnthropicClient()).toBeNull(); }); it('builds a client from a key', () => { process.env.ANTHROPIC_API_KEY = KEY; expect(getAnthropicClient()).not.toBeNull(); }); /** * An identity-linked key is refused with a 400 unless the request names the * workspace it acts in. Nothing about a key's shape says which kind it is, so * this only showed up on a real call (#271). */ it('sends the workspace id when one is configured', () => { process.env.ANTHROPIC_API_KEY = KEY; process.env.ANTHROPIC_WORKSPACE_ID = 'wrkspc_abc123'; expect(headerOf(getAnthropicClient())).toBe('wrkspc_abc123'); }); // Plenty of keys need no workspace. Sending an empty header would turn the // ordinary case into a different error rather than leaving it working. it('sends no workspace header when none is configured', () => { process.env.ANTHROPIC_API_KEY = KEY; expect(headerOf(getAnthropicClient())).toBeUndefined(); }); it('sends no workspace header for a value that is only whitespace', () => { process.env.ANTHROPIC_API_KEY = KEY; process.env.ANTHROPIC_WORKSPACE_ID = ' '; expect(headerOf(getAnthropicClient())).toBeUndefined(); }); it('trims a workspace id that arrived with padding', () => { process.env.ANTHROPIC_API_KEY = KEY; process.env.ANTHROPIC_WORKSPACE_ID = ' wrkspc_abc123 '; expect(headerOf(getAnthropicClient())).toBe('wrkspc_abc123'); }); });