Files
redefined-designs/backend/tests/unit/anthropicClient.test.ts
bermudalambandClaude Opus 5 6c6aaa46eb fix(intake): send the workspace id an identity-linked key requires (#271)
Every draft in QA failed with a 400: "anthropic-workspace-id is required when authenticating with an identity-linked API key". A key issued against a workspace, rather than standing alone, is refused unless the request names the workspace it acts in — and the client was constructed with an API key and nothing else.

Nothing about a key's shape says which kind it is, so no amount of configuration checking would have caught this. Only a real call would, which is exactly what #223's task 8 existed to make.

Sent only when ANTHROPIC_WORKSPACE_ID is set. Plenty of keys need no workspace, and sending an empty header would turn the ordinary case into a different error rather than leaving it working. Both compose files carry it with an empty default so an unset variable cannot fail a deploy, and the cutover doc goes from fifteen interpolated names to sixteen — checked against the file, and every name in the list now matches one in the compose.

The failure handling needed no change and got none. The submission kept its photos, the draft recorded ai_error, and the review queue showed the reason. A model call failing must never lose somebody's consignment, and it did not.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-02 17:25:03 -05:00

71 lines
2.3 KiB
TypeScript

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<string, string> } })._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');
});
});