test: guard mock lookup helpers · openclaw/openclaw@bc92264
steipete
·
2026-05-12
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -107,15 +107,22 @@ function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
107 | 107 | |
108 | 108 | function mockCalls(mock: unknown, label: string): Array<Array<unknown>> { |
109 | 109 | const mockState = (mock as { mock?: { calls?: Array<Array<unknown>> } }).mock; |
110 | | -expect(mockState, `${label}.mock`).toBeDefined(); |
111 | | -expect(Array.isArray(mockState?.calls), `${label}.mock.calls`).toBe(true); |
112 | | -return mockState?.calls ?? []; |
| 110 | +if (!mockState) { |
| 111 | +throw new Error(`Expected ${label}.mock`); |
| 112 | +} |
| 113 | +const calls = mockState.calls; |
| 114 | +if (!Array.isArray(calls)) { |
| 115 | +throw new Error(`Expected ${label}.mock.calls`); |
| 116 | +} |
| 117 | +return calls; |
113 | 118 | } |
114 | 119 | |
115 | 120 | function findMockCall(mock: unknown, label: string, predicate: (call: Array<unknown>) => boolean) { |
116 | 121 | const call = mockCalls(mock, label).find(predicate); |
117 | | -expect(call, label).toBeDefined(); |
118 | | -return call as Array<unknown>; |
| 122 | +if (!call) { |
| 123 | +throw new Error(`Expected ${label}`); |
| 124 | +} |
| 125 | +return call; |
119 | 126 | } |
120 | 127 | |
121 | 128 | function responsePayload(respond: unknown): Record<string, unknown> { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -30,7 +30,6 @@ function expectModelFields(
|
30 | 30 | model: ProviderRuntimeModel | undefined, |
31 | 31 | fields: Partial<ProviderRuntimeModel>, |
32 | 32 | ) { |
33 | | -expect(model).toBeDefined(); |
34 | 33 | if (!model) { |
35 | 34 | throw new Error("expected provider model"); |
36 | 35 | } |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -42,11 +42,13 @@ describe("opencode-go provider plugin", () => {
|
42 | 42 | }); |
43 | 43 | |
44 | 44 | const mediaProvider = mediaProviders.find((provider) => provider.id === "opencode-go"); |
45 | | -expect(mediaProvider).toBeDefined(); |
46 | | -expect(mediaProvider?.capabilities).toEqual(["image"]); |
47 | | -expect(mediaProvider?.defaultModels).toEqual({ image: "kimi-k2.6" }); |
48 | | -expect(typeof mediaProvider?.describeImage).toBe("function"); |
49 | | -expect(typeof mediaProvider?.describeImages).toBe("function"); |
| 45 | +if (!mediaProvider) { |
| 46 | +throw new Error("Expected opencode-go media provider"); |
| 47 | +} |
| 48 | +expect(mediaProvider.capabilities).toEqual(["image"]); |
| 49 | +expect(mediaProvider.defaultModels).toEqual({ image: "kimi-k2.6" }); |
| 50 | +expect(typeof mediaProvider.describeImage).toBe("function"); |
| 51 | +expect(typeof mediaProvider.describeImages).toBe("function"); |
50 | 52 | }); |
51 | 53 | |
52 | 54 | it("owns passthrough-gemini replay policy for Gemini-backed models", async () => { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -258,7 +258,6 @@ describe("openrouter video generation provider", () => {
|
258 | 258 | }); |
259 | 259 | expect(rows).toHaveLength(1); |
260 | 260 | const row = rows[0]; |
261 | | -expect(row).toBeDefined(); |
262 | 261 | if (!row) { |
263 | 262 | throw new Error("expected OpenRouter catalog row"); |
264 | 263 | } |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -73,7 +73,6 @@ describe("handleSlackAction", () => {
|
73 | 73 | |
74 | 74 | function requireSlackSendCall(index: number) { |
75 | 75 | const call = sendSlackMessage.mock.calls[index] as unknown[] | undefined; |
76 | | -expect(call).toBeDefined(); |
77 | 76 | if (!call) { |
78 | 77 | throw new Error(`missing Slack send call ${index + 1}`); |
79 | 78 | } |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -349,8 +349,10 @@ function gatewayRequests(): Array<{ method?: string; params?: Record<string, unk
|
349 | 349 | |
350 | 350 | function gatewayRequest(method: string): { method?: string; params?: Record<string, unknown> } { |
351 | 351 | const request = gatewayRequests().find((candidate) => candidate.method === method); |
352 | | -expect(request).toBeDefined(); |
353 | | -return request as { method?: string; params?: Record<string, unknown> }; |
| 352 | +if (!request) { |
| 353 | +throw new Error(`Expected gateway request for ${method}`); |
| 354 | +} |
| 355 | +return request; |
354 | 356 | } |
355 | 357 | |
356 | 358 | function expectGatewayMethodNotCalled(method: string): void { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -109,7 +109,9 @@ function expectRecordFields(record: Record<string, unknown>, fields: Record<stri
|
109 | 109 | |
110 | 110 | function requireMockArg(mock: typeof runCliAgentMock, callIndex: number, label: string) { |
111 | 111 | const arg = mock.mock.calls[callIndex]?.[0]; |
112 | | -expect(arg).toBeDefined(); |
| 112 | +if (arg === undefined) { |
| 113 | +throw new Error(`Expected mock argument for ${label}`); |
| 114 | +} |
113 | 115 | return requireRecord(arg, label); |
114 | 116 | } |
115 | 117 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -19,8 +19,10 @@ vi.mock("./hooks.js", async () => {
|
19 | 19 | |
20 | 20 | function expectRetryAfterHeader(setHeader: ReturnType<typeof vi.fn>): void { |
21 | 21 | const retryAfterCall = setHeader.mock.calls.find(([name]) => name === "Retry-After"); |
22 | | -expect(retryAfterCall).toBeDefined(); |
23 | | -const retryAfterValue = retryAfterCall?.[1]; |
| 22 | +if (!retryAfterCall) { |
| 23 | +throw new Error("Expected Retry-After header call"); |
| 24 | +} |
| 25 | +const retryAfterValue = retryAfterCall[1]; |
24 | 26 | expect(typeof retryAfterValue).toBe("string"); |
25 | 27 | expect(Number.parseInt(String(retryAfterValue), 10)).toBeGreaterThan(0); |
26 | 28 | } |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -59,16 +59,18 @@ function requireArray(value: unknown, label: string): unknown[] {
|
59 | 59 | |
60 | 60 | function mockCall(source: MockCallSource, index: number, label: string) { |
61 | 61 | const call = source.mock.calls[index]; |
62 | | -expect(call, label).toBeDefined(); |
| 62 | +if (!call) { |
| 63 | +throw new Error(`Expected ${label}`); |
| 64 | +} |
63 | 65 | return call; |
64 | 66 | } |
65 | 67 | |
66 | 68 | function responseCall(source: MockCallSource, index = 0) { |
67 | 69 | const call = mockCall(source, index, `response call ${index}`); |
68 | 70 | return { |
69 | | -ok: call?.[0], |
70 | | -result: call?.[1], |
71 | | -error: call?.[2], |
| 71 | +ok: call[0], |
| 72 | +result: call[1], |
| 73 | +error: call[2], |
72 | 74 | }; |
73 | 75 | } |
74 | 76 | |
@@ -87,8 +89,10 @@ function acceptedResult(source: MockCallSource) {
|
87 | 89 | ? (result as Record<string, unknown>).status === "accepted" |
88 | 90 | : false; |
89 | 91 | }); |
90 | | -expect(call, "accepted response call").toBeDefined(); |
91 | | -return requireRecord(call?.[1], "accepted response result"); |
| 92 | +if (!call) { |
| 93 | +throw new Error("Expected accepted response call"); |
| 94 | +} |
| 95 | +return requireRecord(call[1], "accepted response result"); |
92 | 96 | } |
93 | 97 | |
94 | 98 | function acceptedApprovalId(source: MockCallSource) { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,9 @@ function createInvokeParams(params: Record<string, unknown>) {
|
55 | 55 | |
56 | 56 | function firstMockArg(mock: { mock: { calls: unknown[][] } }, label: string): unknown { |
57 | 57 | const arg = mock.mock.calls[0]?.[0]; |
58 | | -expect(arg, label).toBeDefined(); |
| 58 | +if (arg === undefined) { |
| 59 | +throw new Error(`Expected ${label}`); |
| 60 | +} |
59 | 61 | return arg; |
60 | 62 | } |
61 | 63 | |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。