






















@@ -165,6 +165,27 @@ function requireValue<T>(value: T | undefined, label: string): T {
165165return value;
166166}
167167168+function firstWriteFileCall(writeFileSpy: ReturnType<typeof vi.fn>): {
169+data: unknown;
170+options: { flag?: string; mode?: number };
171+path: string;
172+} {
173+const [filePath, data, options] = writeFileSpy.mock.calls[0] ?? [];
174+expect(typeof filePath).toBe("string");
175+return {
176+ data,
177+options: (options ?? {}) as { flag?: string; mode?: number },
178+path: filePath as string,
179+};
180+}
181+182+function expectRuntimeLogContaining(
183+runtime: { log: ReturnType<typeof vi.fn> },
184+text: string,
185+): void {
186+expect(runtime.log.mock.calls.some(([message]) => String(message).includes(text))).toBe(true);
187+}
188+168189describe("web session", () => {
169190beforeAll(async () => {
170191({
@@ -198,13 +219,11 @@ describe("web session", () => {
198219199220await createWaSocket(true, false, { authDir });
200221const makeWASocket = baileys.makeWASocket as ReturnType<typeof vi.fn>;
201-expect(makeWASocket).toHaveBeenCalledWith(
202-expect.objectContaining({
203-printQRInTerminal: false,
204- ...DEFAULT_WHATSAPP_SOCKET_TIMING,
205-}),
206-);
207222const passed = makeWASocket.mock.calls[0][0];
223+expect(passed.printQRInTerminal).toBe(false);
224+expect(passed.keepAliveIntervalMs).toBe(DEFAULT_WHATSAPP_SOCKET_TIMING.keepAliveIntervalMs);
225+expect(passed.connectTimeoutMs).toBe(DEFAULT_WHATSAPP_SOCKET_TIMING.connectTimeoutMs);
226+expect(passed.defaultQueryTimeoutMs).toBe(DEFAULT_WHATSAPP_SOCKET_TIMING.defaultQueryTimeoutMs);
208227const passedLogger = (passed as { logger?: { level?: string; trace?: unknown } }).logger;
209228expect(passedLogger?.level).toBe("silent");
210229if (typeof passedLogger?.trace !== "function") {
@@ -213,11 +232,11 @@ describe("web session", () => {
213232passedLogger.trace("ignored");
214233await emitCredsUpdate(authDir);
215234216-expect(openMock.writeFileSpy).toHaveBeenCalledWith(
217- expect.stringContaining(path.join(authDir, ".creds.")),
218- expect.any(String),
219- expect.objectContaining({ mode: 0o600, flag: "wx" }),
220-);
235+const write = firstWriteFileCall(openMock.writeFileSpy);
236+expect(write.path).toContain(path.join(authDir, ".creds."));
237+expect(typeof write.data).toBe("string");
238+expect(write.options.mode).toBe(0o600);
239+expect(write.options.flag).toBe("wx");
221240openMock.restore();
222241});
223242@@ -228,13 +247,10 @@ describe("web session", () => {
228247defaultQueryTimeoutMs: 120_000,
229248});
230249231-expect(baileys.makeWASocket).toHaveBeenCalledWith(
232-expect.objectContaining({
233-keepAliveIntervalMs: 10_000,
234-connectTimeoutMs: 90_000,
235-defaultQueryTimeoutMs: 120_000,
236-}),
237-);
250+const passed = (baileys.makeWASocket as ReturnType<typeof vi.fn>).mock.calls[0]?.[0];
251+expect(passed.keepAliveIntervalMs).toBe(10_000);
252+expect(passed.connectTimeoutMs).toBe(90_000);
253+expect(passed.defaultQueryTimeoutMs).toBe(120_000);
238254});
239255240256it("uses ambient env proxy agent when HTTPS_PROXY is configured", async () => {
@@ -326,9 +342,7 @@ describe("web session", () => {
326342327343logWebSelfId("/tmp/wa-creds", runtime as never, true);
328344329-expect(runtime.log).toHaveBeenCalledWith(
330-expect.stringContaining("Web Channel: +12345 (jid 12345@s.whatsapp.net)"),
331-);
345+expectRuntimeLogContaining(runtime, "Web Channel: +12345 (jid 12345@s.whatsapp.net)");
332346creds.restore();
333347});
334348@@ -345,8 +359,9 @@ describe("web session", () => {
345359346360logWebSelfId("/tmp/wa-creds", runtime as never, true);
347361348-expect(runtime.log).toHaveBeenCalledWith(
349-expect.stringContaining("Web Channel: +12345 (jid 12345@s.whatsapp.net, lid 777@lid)"),
362+expectRuntimeLogContaining(
363+runtime,
364+"Web Channel: +12345 (jid 12345@s.whatsapp.net, lid 777@lid)",
350365);
351366creds.restore();
352367});
@@ -515,13 +530,13 @@ describe("web session", () => {
515530me: { id: "123@s.whatsapp.net" },
516531});
517532518-expect(openMock.writeFileSpy).toHaveBeenCalledWith(
519-expect.stringContaining(
520-path.join("/tmp", "openclaw-oauth", "whatsapp", "default", ".creds."),
521-),
522-expect.any(String),
523-expect.objectContaining({ mode: 0o600, flag: "wx" }),
533+const write = firstWriteFileCall(openMock.writeFileSpy);
534+expect(write.path).toContain(
535+path.join("/tmp", "openclaw-oauth", "whatsapp", "default", ".creds."),
524536);
537+expect(typeof write.data).toBe("string");
538+expect(write.options.mode).toBe(0o600);
539+expect(write.options.flag).toBe("wx");
525540expect(openMock.tempHandles).toHaveLength(1);
526541expect(openMock.tempHandles[0]?.sync).toHaveBeenCalledTimes(1);
527542expect(openMock.tempHandles[0]?.close).toHaveBeenCalledTimes(1);
@@ -585,7 +600,7 @@ describe("web session", () => {
585600586601expect(renameSpy).toHaveBeenCalledOnce();
587602const parsedCreds = JSON.parse(raw) as unknown;
588-expect(parsedCreds).toMatchObject(originalCreds);
603+expect(parsedCreds).toEqual(originalCreds);
589604expect(tempEntries).toHaveLength(0);
590605591606renameSpy.mockRestore();
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。