test: guard utility mock call helpers · openclaw/openclaw@d3edbaa
steipete
·
2026-05-12
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,14 @@ vi.mock("./random-token.js", () => ({
|
59 | 59 | randomToken: randomTokenMock, |
60 | 60 | })); |
61 | 61 | |
| 62 | +function firstReplaceConfigRequest(): unknown { |
| 63 | +const [call] = replaceConfigFileMock.mock.calls; |
| 64 | +if (!call) { |
| 65 | +throw new Error("expected config replace call"); |
| 66 | +} |
| 67 | +return call[0]; |
| 68 | +} |
| 69 | + |
62 | 70 | describe("resolveGatewayInstallToken", () => { |
63 | 71 | beforeEach(() => { |
64 | 72 | vi.clearAllMocks(); |
@@ -193,7 +201,7 @@ describe("resolveGatewayInstallToken", () => {
|
193 | 201 | |
194 | 202 | expect(result.warnings.join("\n")).toContain("saving to config"); |
195 | 203 | expect(replaceConfigFileMock).toHaveBeenCalledOnce(); |
196 | | -expect(replaceConfigFileMock.mock.calls[0]?.[0]).toStrictEqual({ |
| 204 | +expect(firstReplaceConfigRequest()).toStrictEqual({ |
197 | 205 | nextConfig: { |
198 | 206 | gateway: { |
199 | 207 | auth: { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -21,7 +21,11 @@ function createRuntime(): RuntimeEnv {
|
21 | 21 | } |
22 | 22 | |
23 | 23 | function readJsonLog(runtime: RuntimeEnv): unknown { |
24 | | -return JSON.parse(String(vi.mocked(runtime.log).mock.calls[0]?.[0])); |
| 24 | +const [call] = vi.mocked(runtime.log).mock.calls; |
| 25 | +if (!call) { |
| 26 | +throw new Error("expected runtime log call"); |
| 27 | +} |
| 28 | +return JSON.parse(String(call[0])); |
25 | 29 | } |
26 | 30 | |
27 | 31 | async function withTaskJsonStateDir(run: () => Promise<void>): Promise<void> { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -62,7 +62,11 @@ const createWritableStreamMock = () => {
|
62 | 62 | }; |
63 | 63 | |
64 | 64 | function requireFirstWrite(write: ReturnType<typeof vi.fn>): string { |
65 | | -const value = write.mock.calls[0]?.[0]; |
| 65 | +const [call] = write.mock.calls; |
| 66 | +if (!call) { |
| 67 | +throw new Error("expected systemd status write"); |
| 68 | +} |
| 69 | +const [value] = call; |
66 | 70 | if (value === undefined) { |
67 | 71 | throw new Error("expected systemd status write"); |
68 | 72 | } |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,11 @@ import { describe, expect, it, vi } from "vitest";
|
2 | 2 | import { fireAndForgetBoundedHook, fireAndForgetHook } from "./fire-and-forget.js"; |
3 | 3 | |
4 | 4 | function requireFirstLog(logger: ReturnType<typeof vi.fn>): string { |
5 | | -const message = logger.mock.calls[0]?.[0]; |
| 5 | +const [call] = logger.mock.calls; |
| 6 | +if (!call) { |
| 7 | +throw new Error("expected log call"); |
| 8 | +} |
| 9 | +const [message] = call; |
6 | 10 | if (typeof message !== "string") { |
7 | 11 | throw new Error("expected string log message"); |
8 | 12 | } |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。