test: guard extension mock helpers · openclaw/openclaw@e17dfb0
steipete
·
2026-05-12
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -318,8 +318,10 @@ function mockCallArg<T>(
|
318 | 318 | _type?: (value: unknown) => value is T, |
319 | 319 | ): T { |
320 | 320 | const call = callIndex < 0 ? mock.mock.calls.at(callIndex) : mock.mock.calls[callIndex]; |
321 | | -expect(call).toBeDefined(); |
322 | | -return call?.[argIndex] as T; |
| 321 | +if (!call) { |
| 322 | +throw new Error(`Expected mock call at index ${callIndex}`); |
| 323 | +} |
| 324 | +return call[argIndex] as T; |
323 | 325 | } |
324 | 326 | |
325 | 327 | function lastMockCallArg<T>( |
@@ -362,14 +364,13 @@ function externalContentDetails(
|
362 | 364 | targetId?: unknown; |
363 | 365 | } |
364 | 366 | | undefined; |
365 | | -expect(details).toBeDefined(); |
366 | 367 | if (!details) { |
367 | 368 | throw new Error("Expected browser tool result details"); |
368 | 369 | } |
369 | | -expect(details?.ok).toBe(true); |
370 | | -expect(details?.externalContent?.untrusted).toBe(true); |
371 | | -expect(details?.externalContent?.source).toBe("browser"); |
372 | | -expect(details?.externalContent?.kind).toBe(kind); |
| 370 | +expect(details.ok).toBe(true); |
| 371 | +expect(details.externalContent?.untrusted).toBe(true); |
| 372 | +expect(details.externalContent?.source).toBe("browser"); |
| 373 | +expect(details.externalContent?.kind).toBe(kind); |
373 | 374 | return details; |
374 | 375 | } |
375 | 376 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -115,8 +115,10 @@ function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
115 | 115 | function callArg(mock: unknown, callIndex: number, argIndex: number, label: string) { |
116 | 116 | const calls = (mock as { mock?: { calls?: Array<Array<unknown>> } }).mock?.calls ?? []; |
117 | 117 | const call = calls.at(callIndex); |
118 | | -expect(call, label).toBeDefined(); |
119 | | -return call?.[argIndex]; |
| 118 | +if (!call) { |
| 119 | +throw new Error(`Expected ${label}`); |
| 120 | +} |
| 121 | +return call[argIndex]; |
120 | 122 | } |
121 | 123 | |
122 | 124 | function expectExistingSessionProfile(value: unknown) { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -86,8 +86,10 @@ function callArg(
|
86 | 86 | label: string, |
87 | 87 | ) { |
88 | 88 | const call = mock.mock.calls.at(callIndex); |
89 | | -expect(call, label).toBeDefined(); |
90 | | -return call?.[argIndex]; |
| 89 | +if (!call) { |
| 90 | +throw new Error(`Expected ${label}`); |
| 91 | +} |
| 92 | +return call[argIndex]; |
91 | 93 | } |
92 | 94 | |
93 | 95 | function expectDynamicSpec( |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -42,7 +42,6 @@ function requireMockCall(mock: unknown, index: number, label: string): unknown[]
|
42 | 42 | throw new Error(`${label} did not expose mock calls`); |
43 | 43 | } |
44 | 44 | const call = calls[index]; |
45 | | -expect(call).toBeDefined(); |
46 | 45 | if (!call) { |
47 | 46 | throw new Error(`missing ${label} call ${index + 1}`); |
48 | 47 | } |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -54,11 +54,13 @@ function fetchRequest(fetchMock: ReturnType<typeof vi.fn>): {
|
54 | 54 | } { |
55 | 55 | const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit | undefined]; |
56 | 56 | expect(typeof url).toBe("string"); |
57 | | -expect(init).toBeDefined(); |
| 57 | +if (!init) { |
| 58 | +throw new Error("Expected fetch init"); |
| 59 | +} |
58 | 60 | return { |
59 | | -body: typeof init?.body === "string" ? init.body : undefined, |
60 | | -headers: init?.headers, |
61 | | -method: init?.method, |
| 61 | +body: typeof init.body === "string" ? init.body : undefined, |
| 62 | +headers: init.headers, |
| 63 | +method: init.method, |
62 | 64 | url, |
63 | 65 | }; |
64 | 66 | } |
@@ -69,7 +71,9 @@ function postJsonRequestOptions(spy: unknown): {
|
69 | 71 | ssrfPolicy?: { allowRfc2544BenchmarkRange?: boolean }; |
70 | 72 | } { |
71 | 73 | const options = (spy as { mock?: { calls?: Array<[unknown]> } }).mock?.calls?.[0]?.[0]; |
72 | | -expect(options).toBeDefined(); |
| 74 | +if (!options) { |
| 75 | +throw new Error("Expected postJsonRequest options"); |
| 76 | +} |
73 | 77 | return options as { |
74 | 78 | allowPrivateNetwork?: boolean; |
75 | 79 | pinDns?: boolean; |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -35,14 +35,18 @@ type GenerateContentRequest = {
|
35 | 35 | function lastGoogleGenAIConfig(): GoogleGenAIConfig { |
36 | 36 | const calls = createGoogleGenAIMock.mock.calls as unknown[][]; |
37 | 37 | const config = calls.at(-1)?.[0]; |
38 | | -expect(config).toBeDefined(); |
| 38 | +if (!config) { |
| 39 | +throw new Error("Expected GoogleGenAI config"); |
| 40 | +} |
39 | 41 | return config as GoogleGenAIConfig; |
40 | 42 | } |
41 | 43 | |
42 | 44 | function firstGenerateContentRequest(): GenerateContentRequest { |
43 | 45 | const calls = generateContentMock.mock.calls as unknown[][]; |
44 | 46 | const request = calls[0]?.[0]; |
45 | | -expect(request).toBeDefined(); |
| 47 | +if (!request) { |
| 48 | +throw new Error("Expected generateContent request"); |
| 49 | +} |
46 | 50 | return request as GenerateContentRequest; |
47 | 51 | } |
48 | 52 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -59,7 +59,9 @@ function lastConnectParams(): MockGoogleLiveConnectParams {
|
59 | 59 | |
60 | 60 | function sentAudio(index = 0): { data?: unknown; mimeType?: unknown } { |
61 | 61 | const audio = session.sendRealtimeInput.mock.calls[index]?.[0]?.audio; |
62 | | -expect(audio).toBeDefined(); |
| 62 | +if (!audio) { |
| 63 | +throw new Error(`Expected sent audio at index ${index}`); |
| 64 | +} |
63 | 65 | return audio as { data?: unknown; mimeType?: unknown }; |
64 | 66 | } |
65 | 67 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -140,7 +140,9 @@ function mockOpenAIImageApiResponse(params: {
|
140 | 140 | |
141 | 141 | function firstMockArg(mocked: unknown): Record<string, unknown> { |
142 | 142 | const arg = (mocked as { mock?: { calls?: unknown[][] } }).mock?.calls?.[0]?.[0]; |
143 | | -expect(arg).toBeDefined(); |
| 143 | +if (!arg || typeof arg !== "object") { |
| 144 | +throw new Error("Expected first mock argument"); |
| 145 | +} |
144 | 146 | return arg as Record<string, unknown>; |
145 | 147 | } |
146 | 148 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -15,11 +15,13 @@ describe("opencode provider plugin", () => {
|
15 | 15 | }); |
16 | 16 | |
17 | 17 | const mediaProvider = mediaProviders.find((provider) => provider.id === "opencode"); |
18 | | -expect(mediaProvider).toBeDefined(); |
19 | | -expect(mediaProvider?.capabilities).toEqual(["image"]); |
20 | | -expect(mediaProvider?.defaultModels).toEqual({ image: "gpt-5-nano" }); |
21 | | -expect(typeof mediaProvider?.describeImage).toBe("function"); |
22 | | -expect(typeof mediaProvider?.describeImages).toBe("function"); |
| 18 | +if (!mediaProvider) { |
| 19 | +throw new Error("Expected opencode media provider"); |
| 20 | +} |
| 21 | +expect(mediaProvider.capabilities).toEqual(["image"]); |
| 22 | +expect(mediaProvider.defaultModels).toEqual({ image: "gpt-5-nano" }); |
| 23 | +expect(typeof mediaProvider.describeImage).toBe("function"); |
| 24 | +expect(typeof mediaProvider.describeImages).toBe("function"); |
23 | 25 | }); |
24 | 26 | |
25 | 27 | it("owns passthrough-gemini replay policy for Gemini-backed models", async () => { |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。