From 626d8e9f6245490289db0e395ba827a2d4d047f7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 19:19:36 +0000 Subject: [PATCH] test(web): dedupe temp dir setup in web auto-reply utils tests --- .../auto-reply/web-auto-reply-utils.test.ts | 132 ++++++++++-------- 1 file changed, 72 insertions(+), 60 deletions(-) diff --git a/src/web/auto-reply/web-auto-reply-utils.test.ts b/src/web/auto-reply/web-auto-reply-utils.test.ts index 6e98d4a9068..30228929264 100644 --- a/src/web/auto-reply/web-auto-reply-utils.test.ts +++ b/src/web/auto-reply/web-auto-reply-utils.test.ts @@ -1,7 +1,7 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { saveSessionStore } from "../../config/sessions.js"; import { isBotMentionedFromTargets, resolveMentionTargets } from "./mentions.js"; import { getSessionSnapshot } from "./session-snapshot.js"; @@ -24,6 +24,15 @@ const makeMsg = (overrides: Partial): WebInboundMsg => ...overrides, }) as WebInboundMsg; +async function withTempDir(prefix: string, run: (dir: string) => Promise): Promise { + const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix)); + try { + return await run(dir); + } finally { + await fs.rm(dir, { recursive: true, force: true }); + } +} + describe("isBotMentionedFromTargets", () => { const mentionCfg = { mentionRegexes: [/\bopenclaw\b/i] }; @@ -78,44 +87,46 @@ describe("isBotMentionedFromTargets", () => { const targetsText = resolveMentionTargets(msgTextMention); expect(isBotMentionedFromTargets(msgTextMention, cfg, targetsText)).toBe(true); }); + + it("matches fallback number mentions when regexes do not match", () => { + const msg = makeMsg({ + body: "please check +1 555 123 4567", + selfE164: "+15551234567", + selfJid: "15551234567@s.whatsapp.net", + }); + const targets = resolveMentionTargets(msg); + expect(isBotMentionedFromTargets(msg, { mentionRegexes: [] }, targets)).toBe(true); + }); }); describe("resolveMentionTargets with @lid mapping", () => { - let authDir = ""; + it("uses @lid reverse mapping for mentions and self identity", async () => { + await withTempDir("openclaw-lid-mapping-", async (authDir) => { + await fs.writeFile( + path.join(authDir, "lid-mapping-777_reverse.json"), + JSON.stringify("+1777"), + ); - beforeAll(async () => { - authDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-lid-mapping-")); - await fs.writeFile(path.join(authDir, "lid-mapping-777_reverse.json"), JSON.stringify("+1777")); - }); + const mentionTargets = resolveMentionTargets( + makeMsg({ + body: "ping", + mentionedJids: ["777@lid"], + selfE164: "+15551234567", + selfJid: "15551234567@s.whatsapp.net", + }), + authDir, + ); + expect(mentionTargets.normalizedMentions).toContain("+1777"); - afterAll(async () => { - if (!authDir) { - return; - } - await fs.rm(authDir, { recursive: true, force: true }); - authDir = ""; - }); - - it("uses @lid reverse mapping for mentions and self identity", () => { - const mentionTargets = resolveMentionTargets( - makeMsg({ - body: "ping", - mentionedJids: ["777@lid"], - selfE164: "+15551234567", - selfJid: "15551234567@s.whatsapp.net", - }), - authDir, - ); - expect(mentionTargets.normalizedMentions).toContain("+1777"); - - const selfTargets = resolveMentionTargets( - makeMsg({ - body: "ping", - selfJid: "777@lid", - }), - authDir, - ); - expect(selfTargets.selfE164).toBe("+1777"); + const selfTargets = resolveMentionTargets( + makeMsg({ + body: "ping", + selfJid: "777@lid", + }), + authDir, + ); + expect(selfTargets.selfE164).toBe("+1777"); + }); }); }); @@ -124,36 +135,37 @@ describe("getSessionSnapshot", () => { vi.useFakeTimers(); vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0)); try { - const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-snapshot-")); - const storePath = path.join(root, "sessions.json"); - const sessionKey = "agent:main:whatsapp:dm:s1"; + await withTempDir("openclaw-snapshot-", async (root) => { + const storePath = path.join(root, "sessions.json"); + const sessionKey = "agent:main:whatsapp:dm:s1"; - await saveSessionStore(storePath, { - [sessionKey]: { - sessionId: "snapshot-session", - updatedAt: new Date(2026, 0, 18, 3, 30, 0).getTime(), - lastChannel: "whatsapp", - }, - }); - - const cfg = { - session: { - store: storePath, - reset: { mode: "daily", atHour: 4, idleMinutes: 240 }, - resetByChannel: { - whatsapp: { mode: "idle", idleMinutes: 360 }, + await saveSessionStore(storePath, { + [sessionKey]: { + sessionId: "snapshot-session", + updatedAt: new Date(2026, 0, 18, 3, 30, 0).getTime(), + lastChannel: "whatsapp", }, - }, - } as Parameters[0]; + }); - const snapshot = getSessionSnapshot(cfg, "whatsapp:+15550001111", true, { - sessionKey, + const cfg = { + session: { + store: storePath, + reset: { mode: "daily", atHour: 4, idleMinutes: 240 }, + resetByChannel: { + whatsapp: { mode: "idle", idleMinutes: 360 }, + }, + }, + } as Parameters[0]; + + const snapshot = getSessionSnapshot(cfg, "whatsapp:+15550001111", true, { + sessionKey, + }); + + expect(snapshot.resetPolicy.mode).toBe("idle"); + expect(snapshot.resetPolicy.idleMinutes).toBe(360); + expect(snapshot.fresh).toBe(true); + expect(snapshot.dailyResetAt).toBeUndefined(); }); - - expect(snapshot.resetPolicy.mode).toBe("idle"); - expect(snapshot.resetPolicy.idleMinutes).toBe(360); - expect(snapshot.fresh).toBe(true); - expect(snapshot.dailyResetAt).toBeUndefined(); } finally { vi.useRealTimers(); }