test: guard provider usage mock calls

This commit is contained in:
Peter Steinberger
2026-05-11 23:51:03 +01:00
parent 7c81310b99
commit 995d5ef16f
3 changed files with 55 additions and 20 deletions

View File

@@ -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);

View File

@@ -7,6 +7,16 @@ import {
parseFiniteNumber,
} from "./provider-usage.fetch.shared.js";
function requireFetchCall(
mock: ReturnType<typeof vi.fn>,
): [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" });

View File

@@ -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");