From 995d5ef16f5dba4d4f8a07b9b4202ed833320367 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 11 May 2026 23:51:03 +0100 Subject: [PATCH] test: guard provider usage mock calls --- src/commitments/runtime.test.ts | 23 ++++++++--- src/infra/provider-usage.fetch.shared.test.ts | 12 +++++- src/infra/provider-usage.load.plugin.test.ts | 40 +++++++++++++------ 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/commitments/runtime.test.ts b/src/commitments/runtime.test.ts index a629157c290..330994d72d5 100644 --- a/src/commitments/runtime.test.ts +++ b/src/commitments/runtime.test.ts @@ -24,6 +24,22 @@ vi.mock("./model-selection.runtime.js", () => ({ resolveCommitmentDefaultModelRef: resolveDefaultModelMock, })); +function requireFirstEmbeddedPiRequest(): { + provider?: string; + model?: string; + disableTools?: boolean; +} { + const [call] = runEmbeddedPiAgentMock.mock.calls; + if (!call) { + throw new Error("expected embedded PI agent extraction request"); + } + const [request] = call; + if (!request || typeof request !== "object" || Array.isArray(request)) { + throw new Error("expected embedded PI agent extraction request"); + } + return request as { provider?: string; model?: string; disableTools?: boolean }; +} + describe("commitment extraction runtime", () => { const tmpDirs: string[] = []; const nowMs = Date.parse("2026-04-29T16:00:00.000Z"); @@ -204,12 +220,7 @@ describe("commitment extraction runtime", () => { await expect(drainCommitmentExtractionQueue()).resolves.toBe(1); expect(resolveDefaultModelMock).toHaveBeenCalledWith({ cfg, agentId: "main" }); expect(runEmbeddedPiAgentMock).toHaveBeenCalledTimes(1); - const request = runEmbeddedPiAgentMock.mock.calls[0]?.[0] as - | { provider?: string; model?: string; disableTools?: boolean } - | undefined; - if (!request) { - throw new Error("Expected embedded PI agent extraction request"); - } + const request = requireFirstEmbeddedPiRequest(); expect(request.provider).toBe("openai-codex"); expect(request.model).toBe("gpt-5.5"); expect(request.disableTools).toBe(true); diff --git a/src/infra/provider-usage.fetch.shared.test.ts b/src/infra/provider-usage.fetch.shared.test.ts index a80229640d1..5bfb29ed0e6 100644 --- a/src/infra/provider-usage.fetch.shared.test.ts +++ b/src/infra/provider-usage.fetch.shared.test.ts @@ -7,6 +7,16 @@ import { parseFiniteNumber, } from "./provider-usage.fetch.shared.js"; +function requireFetchCall( + mock: ReturnType, +): [URL | RequestInfo, RequestInit | undefined] { + const [call] = mock.mock.calls; + if (!call) { + throw new Error("expected fetch call"); + } + return call as [URL | RequestInfo, RequestInit | undefined]; +} + describe("provider usage fetch shared helpers", () => { afterEach(() => { vi.restoreAllMocks(); @@ -48,7 +58,7 @@ describe("provider usage fetch shared helpers", () => { ); expect(fetchFnMock).toHaveBeenCalledOnce(); - const [input, init] = fetchFnMock.mock.calls[0] ?? []; + const [input, init] = requireFetchCall(fetchFnMock); expect(input).toBe("https://example.com/usage"); expect(init?.method).toBe("POST"); expect(init?.headers).toEqual({ authorization: "Bearer test" }); diff --git a/src/infra/provider-usage.load.plugin.test.ts b/src/infra/provider-usage.load.plugin.test.ts index 36d3dd1d384..4b1a6c04324 100644 --- a/src/infra/provider-usage.load.plugin.test.ts +++ b/src/infra/provider-usage.load.plugin.test.ts @@ -22,6 +22,32 @@ let loadProviderUsageSummary: typeof import("./provider-usage.load.js").loadProv const usageNow = Date.UTC(2026, 0, 7, 0, 0, 0); +function requireFirstPluginUsageCall(): { + provider?: unknown; + context?: { + provider?: unknown; + token?: unknown; + timeoutMs?: unknown; + }; +} { + const [call] = resolveProviderUsageSnapshotWithPluginMock.mock.calls; + if (!call) { + throw new Error("expected provider usage plugin call"); + } + const [pluginCall] = call; + if (!pluginCall || typeof pluginCall !== "object" || Array.isArray(pluginCall)) { + throw new Error("expected provider usage plugin call"); + } + return pluginCall as { + provider?: unknown; + context?: { + provider?: unknown; + token?: unknown; + timeoutMs?: unknown; + }; + }; +} + describe("provider-usage.load plugin boundary", () => { beforeAll(async () => { ({ loadProviderUsageSummary } = await import("./provider-usage.load.js")); @@ -61,19 +87,7 @@ describe("provider-usage.load plugin boundary", () => { expect(mockFetch).not.toHaveBeenCalled(); expect(resolveProviderUsageSnapshotWithPluginMock).toHaveBeenCalledOnce(); - const pluginCall = resolveProviderUsageSnapshotWithPluginMock.mock.calls[0]?.[0] as - | { - provider?: unknown; - context?: { - provider?: unknown; - token?: unknown; - timeoutMs?: unknown; - }; - } - | undefined; - if (!pluginCall) { - throw new Error("missing provider usage plugin call"); - } + const pluginCall = requireFirstPluginUsageCall(); expect(pluginCall.provider).toBe("github-copilot"); expect(pluginCall.context?.provider).toBe("github-copilot"); expect(pluginCall.context?.token).toBe("copilot-token");