test: guard provider usage mock calls · openclaw/openclaw@995d5ef
steipete
·
2026-05-12
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,22 @@ vi.mock("./model-selection.runtime.js", () => ({
|
24 | 24 | resolveCommitmentDefaultModelRef: resolveDefaultModelMock, |
25 | 25 | })); |
26 | 26 | |
| 27 | +function requireFirstEmbeddedPiRequest(): { |
| 28 | +provider?: string; |
| 29 | +model?: string; |
| 30 | +disableTools?: boolean; |
| 31 | +} { |
| 32 | +const [call] = runEmbeddedPiAgentMock.mock.calls; |
| 33 | +if (!call) { |
| 34 | +throw new Error("expected embedded PI agent extraction request"); |
| 35 | +} |
| 36 | +const [request] = call; |
| 37 | +if (!request || typeof request !== "object" || Array.isArray(request)) { |
| 38 | +throw new Error("expected embedded PI agent extraction request"); |
| 39 | +} |
| 40 | +return request as { provider?: string; model?: string; disableTools?: boolean }; |
| 41 | +} |
| 42 | + |
27 | 43 | describe("commitment extraction runtime", () => { |
28 | 44 | const tmpDirs: string[] = []; |
29 | 45 | const nowMs = Date.parse("2026-04-29T16:00:00.000Z"); |
@@ -204,12 +220,7 @@ describe("commitment extraction runtime", () => {
|
204 | 220 | await expect(drainCommitmentExtractionQueue()).resolves.toBe(1); |
205 | 221 | expect(resolveDefaultModelMock).toHaveBeenCalledWith({ cfg, agentId: "main" }); |
206 | 222 | expect(runEmbeddedPiAgentMock).toHaveBeenCalledTimes(1); |
207 | | -const request = runEmbeddedPiAgentMock.mock.calls[0]?.[0] as |
208 | | -| { provider?: string; model?: string; disableTools?: boolean } |
209 | | -| undefined; |
210 | | -if (!request) { |
211 | | -throw new Error("Expected embedded PI agent extraction request"); |
212 | | -} |
| 223 | +const request = requireFirstEmbeddedPiRequest(); |
213 | 224 | expect(request.provider).toBe("openai-codex"); |
214 | 225 | expect(request.model).toBe("gpt-5.5"); |
215 | 226 | expect(request.disableTools).toBe(true); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,16 @@ import {
|
7 | 7 | parseFiniteNumber, |
8 | 8 | } from "./provider-usage.fetch.shared.js"; |
9 | 9 | |
| 10 | +function requireFetchCall( |
| 11 | +mock: ReturnType<typeof vi.fn>, |
| 12 | +): [URL | RequestInfo, RequestInit | undefined] { |
| 13 | +const [call] = mock.mock.calls; |
| 14 | +if (!call) { |
| 15 | +throw new Error("expected fetch call"); |
| 16 | +} |
| 17 | +return call as [URL | RequestInfo, RequestInit | undefined]; |
| 18 | +} |
| 19 | + |
10 | 20 | describe("provider usage fetch shared helpers", () => { |
11 | 21 | afterEach(() => { |
12 | 22 | vi.restoreAllMocks(); |
@@ -48,7 +58,7 @@ describe("provider usage fetch shared helpers", () => {
|
48 | 58 | ); |
49 | 59 | |
50 | 60 | expect(fetchFnMock).toHaveBeenCalledOnce(); |
51 | | -const [input, init] = fetchFnMock.mock.calls[0] ?? []; |
| 61 | +const [input, init] = requireFetchCall(fetchFnMock); |
52 | 62 | expect(input).toBe("https://example.com/usage"); |
53 | 63 | expect(init?.method).toBe("POST"); |
54 | 64 | expect(init?.headers).toEqual({ authorization: "Bearer test" }); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -22,6 +22,32 @@ let loadProviderUsageSummary: typeof import("./provider-usage.load.js").loadProv
|
22 | 22 | |
23 | 23 | const usageNow = Date.UTC(2026, 0, 7, 0, 0, 0); |
24 | 24 | |
| 25 | +function requireFirstPluginUsageCall(): { |
| 26 | +provider?: unknown; |
| 27 | +context?: { |
| 28 | +provider?: unknown; |
| 29 | +token?: unknown; |
| 30 | +timeoutMs?: unknown; |
| 31 | +}; |
| 32 | +} { |
| 33 | +const [call] = resolveProviderUsageSnapshotWithPluginMock.mock.calls; |
| 34 | +if (!call) { |
| 35 | +throw new Error("expected provider usage plugin call"); |
| 36 | +} |
| 37 | +const [pluginCall] = call; |
| 38 | +if (!pluginCall || typeof pluginCall !== "object" || Array.isArray(pluginCall)) { |
| 39 | +throw new Error("expected provider usage plugin call"); |
| 40 | +} |
| 41 | +return pluginCall as { |
| 42 | +provider?: unknown; |
| 43 | +context?: { |
| 44 | +provider?: unknown; |
| 45 | +token?: unknown; |
| 46 | +timeoutMs?: unknown; |
| 47 | +}; |
| 48 | +}; |
| 49 | +} |
| 50 | + |
25 | 51 | describe("provider-usage.load plugin boundary", () => { |
26 | 52 | beforeAll(async () => { |
27 | 53 | ({ loadProviderUsageSummary } = await import("./provider-usage.load.js")); |
@@ -61,19 +87,7 @@ describe("provider-usage.load plugin boundary", () => {
|
61 | 87 | |
62 | 88 | expect(mockFetch).not.toHaveBeenCalled(); |
63 | 89 | expect(resolveProviderUsageSnapshotWithPluginMock).toHaveBeenCalledOnce(); |
64 | | -const pluginCall = resolveProviderUsageSnapshotWithPluginMock.mock.calls[0]?.[0] as |
65 | | -| { |
66 | | -provider?: unknown; |
67 | | -context?: { |
68 | | -provider?: unknown; |
69 | | -token?: unknown; |
70 | | -timeoutMs?: unknown; |
71 | | -}; |
72 | | -} |
73 | | -| undefined; |
74 | | -if (!pluginCall) { |
75 | | -throw new Error("missing provider usage plugin call"); |
76 | | -} |
| 90 | +const pluginCall = requireFirstPluginUsageCall(); |
77 | 91 | expect(pluginCall.provider).toBe("github-copilot"); |
78 | 92 | expect(pluginCall.context?.provider).toBe("github-copilot"); |
79 | 93 | expect(pluginCall.context?.token).toBe("copilot-token"); |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。