From d9828934901dd3bd26906922c81c9dc11acaf3a3 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 18:45:04 +0000 Subject: [PATCH] refactor(test): use env helper for web auto-reply timezone test --- ...onnects-after-connection-close.e2e.test.ts | 155 +++++++++--------- 1 file changed, 77 insertions(+), 78 deletions(-) diff --git a/src/web/auto-reply.web-auto-reply.reconnects-after-connection-close.e2e.test.ts b/src/web/auto-reply.web-auto-reply.reconnects-after-connection-close.e2e.test.ts index 2c677cd890e..bfdd513ee96 100644 --- a/src/web/auto-reply.web-auto-reply.reconnects-after-connection-close.e2e.test.ts +++ b/src/web/auto-reply.web-auto-reply.reconnects-after-connection-close.e2e.test.ts @@ -1,5 +1,6 @@ import { beforeAll, describe, expect, it, vi } from "vitest"; import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { installWebAutoReplyTestHomeHooks, installWebAutoReplyUnitTestHooks, @@ -233,92 +234,90 @@ describe("web auto-reply", () => { }); it("processes inbound messages without batching and preserves timestamps", async () => { - const originalTz = process.env.TZ; - process.env.TZ = "Europe/Vienna"; + await withEnvAsync({ TZ: "Europe/Vienna" }, async () => { + const originalMax = process.getMaxListeners(); + process.setMaxListeners?.(1); // force low to confirm bump - const originalMax = process.getMaxListeners(); - process.setMaxListeners?.(1); // force low to confirm bump + const store = await makeSessionStore({ + main: { sessionId: "sid", updatedAt: Date.now() }, + }); - const store = await makeSessionStore({ - main: { sessionId: "sid", updatedAt: Date.now() }, - }); + try { + const sendMedia = vi.fn(); + const reply = vi.fn().mockResolvedValue(undefined); + const sendComposing = vi.fn(); + const resolver = vi.fn().mockResolvedValue({ text: "ok" }); - try { - const sendMedia = vi.fn(); - const reply = vi.fn().mockResolvedValue(undefined); - const sendComposing = vi.fn(); - const resolver = vi.fn().mockResolvedValue({ text: "ok" }); + let capturedOnMessage: + | ((msg: import("./inbound.js").WebInboundMessage) => Promise) + | undefined; + const listenerFactory = async (opts: { + onMessage: (msg: import("./inbound.js").WebInboundMessage) => Promise; + }) => { + capturedOnMessage = opts.onMessage; + return { close: vi.fn() }; + }; - let capturedOnMessage: - | ((msg: import("./inbound.js").WebInboundMessage) => Promise) - | undefined; - const listenerFactory = async (opts: { - onMessage: (msg: import("./inbound.js").WebInboundMessage) => Promise; - }) => { - capturedOnMessage = opts.onMessage; - return { close: vi.fn() }; - }; - - setLoadConfigMock(() => ({ - agents: { - defaults: { - envelopeTimezone: "utc", + setLoadConfigMock(() => ({ + agents: { + defaults: { + envelopeTimezone: "utc", + }, }, - }, - session: { store: store.storePath }, - })); + session: { store: store.storePath }, + })); - await monitorWebChannel(false, listenerFactory as never, false, resolver); - expect(capturedOnMessage).toBeDefined(); + await monitorWebChannel(false, listenerFactory as never, false, resolver); + expect(capturedOnMessage).toBeDefined(); - // Two messages from the same sender with fixed timestamps - await capturedOnMessage?.( - makeInboundMessage({ - body: "first", - from: "+1", - to: "+2", - id: "m1", - timestamp: 1735689600000, // Jan 1 2025 00:00:00 UTC - sendComposing, - reply, - sendMedia, - }), - ); - await capturedOnMessage?.( - makeInboundMessage({ - body: "second", - from: "+1", - to: "+2", - id: "m2", - timestamp: 1735693200000, // Jan 1 2025 01:00:00 UTC - sendComposing, - reply, - sendMedia, - }), - ); + // Two messages from the same sender with fixed timestamps + await capturedOnMessage?.( + makeInboundMessage({ + body: "first", + from: "+1", + to: "+2", + id: "m1", + timestamp: 1735689600000, // Jan 1 2025 00:00:00 UTC + sendComposing, + reply, + sendMedia, + }), + ); + await capturedOnMessage?.( + makeInboundMessage({ + body: "second", + from: "+1", + to: "+2", + id: "m2", + timestamp: 1735693200000, // Jan 1 2025 01:00:00 UTC + sendComposing, + reply, + sendMedia, + }), + ); - expect(resolver).toHaveBeenCalledTimes(2); - const firstArgs = resolver.mock.calls[0][0]; - const secondArgs = resolver.mock.calls[1][0]; - const firstTimestamp = formatEnvelopeTimestamp(new Date("2025-01-01T00:00:00Z")); - const secondTimestamp = formatEnvelopeTimestamp(new Date("2025-01-01T01:00:00Z")); - const firstPattern = escapeRegExp(firstTimestamp); - const secondPattern = escapeRegExp(secondTimestamp); - expect(firstArgs.Body).toMatch( - new RegExp(`\\[WhatsApp \\+1 (\\+\\d+[smhd] )?${firstPattern}\\] \\[openclaw\\] first`), - ); - expect(firstArgs.Body).not.toContain("second"); - expect(secondArgs.Body).toMatch( - new RegExp(`\\[WhatsApp \\+1 (\\+\\d+[smhd] )?${secondPattern}\\] \\[openclaw\\] second`), - ); - expect(secondArgs.Body).not.toContain("first"); + expect(resolver).toHaveBeenCalledTimes(2); + const firstArgs = resolver.mock.calls[0][0]; + const secondArgs = resolver.mock.calls[1][0]; + const firstTimestamp = formatEnvelopeTimestamp(new Date("2025-01-01T00:00:00Z")); + const secondTimestamp = formatEnvelopeTimestamp(new Date("2025-01-01T01:00:00Z")); + const firstPattern = escapeRegExp(firstTimestamp); + const secondPattern = escapeRegExp(secondTimestamp); + expect(firstArgs.Body).toMatch( + new RegExp(`\\[WhatsApp \\+1 (\\+\\d+[smhd] )?${firstPattern}\\] \\[openclaw\\] first`), + ); + expect(firstArgs.Body).not.toContain("second"); + expect(secondArgs.Body).toMatch( + new RegExp(`\\[WhatsApp \\+1 (\\+\\d+[smhd] )?${secondPattern}\\] \\[openclaw\\] second`), + ); + expect(secondArgs.Body).not.toContain("first"); - // Max listeners bumped to avoid warnings in multi-instance test runs - expect(process.getMaxListeners?.()).toBeGreaterThanOrEqual(50); - } finally { - process.setMaxListeners?.(originalMax); - process.env.TZ = originalTz; - await store.cleanup(); - } + // Max listeners bumped to avoid warnings in multi-instance test runs + expect(process.getMaxListeners?.()).toBeGreaterThanOrEqual(50); + } finally { + process.setMaxListeners?.(originalMax); + await store.cleanup(); + } + }); }); });