|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { Agent } from "./agent"; |
| 3 | + |
| 4 | +interface TestableAgent { |
| 5 | + _configureLlmGateway( |
| 6 | + overrideUrl?: string, |
| 7 | + ): Promise<{ gatewayUrl: string; apiKey: string } | null>; |
| 8 | +} |
| 9 | + |
| 10 | +const ENV_KEYS_UNDER_TEST = [ |
| 11 | + "ANTHROPIC_BASE_URL", |
| 12 | + "ANTHROPIC_AUTH_TOKEN", |
| 13 | + "ANTHROPIC_CUSTOM_HEADERS", |
| 14 | + "OPENAI_BASE_URL", |
| 15 | + "OPENAI_API_KEY", |
| 16 | +] as const; |
| 17 | + |
| 18 | +describe("Agent._configureLlmGateway", () => { |
| 19 | + const originalEnv: Partial<Record<string, string | undefined>> = {}; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + for (const key of ENV_KEYS_UNDER_TEST) { |
| 23 | + originalEnv[key] = process.env[key]; |
| 24 | + delete process.env[key]; |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + for (const key of ENV_KEYS_UNDER_TEST) { |
| 30 | + const value = originalEnv[key]; |
| 31 | + if (value === undefined) { |
| 32 | + delete process.env[key]; |
| 33 | + } else { |
| 34 | + process.env[key] = value; |
| 35 | + } |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + const buildAgent = (): TestableAgent => |
| 40 | + new Agent({ |
| 41 | + skipLogPersistence: true, |
| 42 | + posthog: { |
| 43 | + apiUrl: "https://us.posthog.com", |
| 44 | + getApiKey: vi.fn().mockResolvedValue("test-token"), |
| 45 | + projectId: 99, |
| 46 | + }, |
| 47 | + }) as unknown as TestableAgent; |
| 48 | + |
| 49 | + it("forwards the team_id as an x-posthog-property header", async () => { |
| 50 | + await buildAgent()._configureLlmGateway(); |
| 51 | + |
| 52 | + expect(process.env.ANTHROPIC_CUSTOM_HEADERS).toBe( |
| 53 | + "x-posthog-property-team_id: 99", |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it("preserves pre-existing custom headers and dedupes the team_id line", async () => { |
| 58 | + process.env.ANTHROPIC_CUSTOM_HEADERS = [ |
| 59 | + "x-posthog-property-team_id: 1", |
| 60 | + "x-posthog-use-bedrock-fallback: true", |
| 61 | + ].join("\n"); |
| 62 | + |
| 63 | + await buildAgent()._configureLlmGateway(); |
| 64 | + |
| 65 | + expect(process.env.ANTHROPIC_CUSTOM_HEADERS).toBe( |
| 66 | + [ |
| 67 | + "x-posthog-property-team_id: 99", |
| 68 | + "x-posthog-use-bedrock-fallback: true", |
| 69 | + ].join("\n"), |
| 70 | + ); |
| 71 | + }); |
| 72 | +}); |
0 commit comments