test: dedupe anthropic transport mock reads

This commit is contained in:
Peter Steinberger
2026-05-12 19:17:25 +01:00
parent 7fd1f5c73c
commit ef8e6650ae

View File

@@ -74,6 +74,16 @@ function latestAnthropicRequestHeaders() {
return new Headers(latestAnthropicRequest().init?.headers);
}
function guardedFetchCall(
callIndex = 0,
): [unknown, { method?: unknown; headers?: HeadersInit } | undefined] {
const call = guardedFetchMock.mock.calls[callIndex];
if (!call) {
throw new Error(`expected guarded fetch call ${callIndex + 1}`);
}
return call as [unknown, { method?: unknown; headers?: HeadersInit } | undefined];
}
function requireRecord(value: unknown, label: string): Record<string, unknown> {
if (!value || typeof value !== "object" || Array.isArray(value)) {
throw new Error(`Expected ${label}`);
@@ -178,7 +188,7 @@ describe("anthropic transport stream", () => {
);
expect(buildGuardedModelFetchMock).toHaveBeenCalledWith(model);
const [url, init] = guardedFetchMock.mock.calls.at(0) ?? [];
const [url, init] = guardedFetchCall();
expect(url).toBe("https://api.anthropic.com/v1/messages");
expect(init?.method).toBe("POST");
const headers = new Headers(init?.headers);
@@ -241,8 +251,9 @@ describe("anthropic transport stream", () => {
} as AnthropicStreamOptions,
);
expect(guardedFetchMock.mock.calls.at(0)?.[0]).toBe("https://custom-proxy.example/v1/messages");
expect(guardedFetchMock.mock.calls.at(0)?.[1]?.method).toBe("POST");
const [url, init] = guardedFetchCall();
expect(url).toBe("https://custom-proxy.example/v1/messages");
expect(init?.method).toBe("POST");
expect(latestAnthropicRequestHeaders().get("anthropic-beta")).toBeNull();
});
@@ -468,8 +479,9 @@ describe("anthropic transport stream", () => {
);
const result = await stream.result();
expect(guardedFetchMock.mock.calls.at(0)?.[0]).toBe("https://api.anthropic.com/v1/messages");
const headers = new Headers(guardedFetchMock.mock.calls.at(0)?.[1]?.headers);
const [url, init] = guardedFetchCall();
expect(url).toBe("https://api.anthropic.com/v1/messages");
const headers = new Headers(init?.headers);
expect(headers.get("authorization")).toBe("Bearer sk-ant-oat-example");
expect(headers.get("x-app")).toBe("cli");
expect(headers.get("user-agent")).toContain("claude-cli/");