





























@@ -150,6 +150,17 @@ function mockLogWebSelfIdCreds(me: Record<string, string>) {
150150};
151151}
152152153+function firstMockCall(
154+mock: { mock: { calls: Array<readonly unknown[]> } },
155+label: string,
156+): readonly unknown[] {
157+const call = mock.mock.calls[0];
158+if (!call) {
159+throw new Error(`expected ${label} call`);
160+}
161+return call;
162+}
163+153164function readLastSocketOptions(): {
154165agent?: unknown;
155166connectTimeoutMs?: number;
@@ -159,7 +170,10 @@ function readLastSocketOptions(): {
159170printQRInTerminal?: boolean;
160171logger?: { level?: string; trace?: unknown };
161172} {
162-const options = (baileys.makeWASocket as ReturnType<typeof vi.fn>).mock.calls.at(0)?.at(0);
173+const [options] = firstMockCall(
174+baileys.makeWASocket as ReturnType<typeof vi.fn>,
175+"Baileys socket creation",
176+);
163177if (typeof options !== "object" || options === null) {
164178throw new Error("expected Baileys socket options");
165179}
@@ -181,12 +195,19 @@ function requireValue<T>(value: T | undefined, label: string): T {
181195return value;
182196}
183197198+function requireString(value: unknown, label: string): string {
199+if (typeof value !== "string") {
200+throw new Error(`expected ${label}`);
201+}
202+return value;
203+}
204+184205function firstWriteFileCall(writeFileSpy: ReturnType<typeof vi.fn>): {
185206data: unknown;
186207options: { flag?: string; mode?: number };
187208path: string;
188209} {
189-const [filePath, data, options] = writeFileSpy.mock.calls.at(0) ?? [];
210+const [filePath, data, options] = firstMockCall(writeFileSpy, "fs.writeFile");
190211expect(typeof filePath).toBe("string");
191212return {
192213 data,
@@ -522,9 +543,9 @@ describe("web session", () => {
522543await waitForCredsSaveQueue();
523544524545expect(creds.copySpy).toHaveBeenCalledTimes(1);
525-const args = creds.copySpy.mock.calls.at(0) ?? [];
526-expect(String(args[0] ?? "")).toContain(creds.credsSuffix);
527-expect(String(args[1] ?? "")).toContain(backupSuffix);
546+const [sourcePath, backupPath] = firstMockCall(creds.copySpy, "creds backup copy");
547+expect(requireString(sourcePath, "creds backup source path")).toContain(creds.credsSuffix);
548+expect(requireString(backupPath, "creds backup target path")).toContain(backupSuffix);
528549expect(openMock.tempHandles).toHaveLength(1);
529550530551creds.restore();
@@ -561,10 +582,10 @@ describe("web session", () => {
561582expect(openMock.dirHandles).toHaveLength(1);
562583expect(openMock.dirHandles[0]?.sync).toHaveBeenCalledTimes(1);
563584const writePath = openMock.tempHandles[0]?.filePath;
564-const renameArgs = renameSpy.mock.calls.at(0) ?? [];
585+const [, renameTarget] = firstMockCall(renameSpy, "creds atomic rename");
565586expect(typeof writePath).toBe("string");
566587expect(writePath).toContain(".creds.");
567-expect(String(renameArgs[1] ?? "")).toContain(
588+expect(requireString(renameTarget, "creds rename target path")).toContain(
568589path.join("/tmp", "openclaw-oauth", "whatsapp", "default", "creds.json"),
569590);
570591} finally {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。