test(web): dedupe temp dir setup in web auto-reply utils tests

This commit is contained in:
Peter Steinberger
2026-02-21 19:19:36 +00:00
parent b703ea3675
commit 626d8e9f62

View File

@@ -1,7 +1,7 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import os from "node:os"; import os from "node:os";
import path from "node:path"; 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 { saveSessionStore } from "../../config/sessions.js";
import { isBotMentionedFromTargets, resolveMentionTargets } from "./mentions.js"; import { isBotMentionedFromTargets, resolveMentionTargets } from "./mentions.js";
import { getSessionSnapshot } from "./session-snapshot.js"; import { getSessionSnapshot } from "./session-snapshot.js";
@@ -24,6 +24,15 @@ const makeMsg = (overrides: Partial<WebInboundMsg>): WebInboundMsg =>
...overrides, ...overrides,
}) as WebInboundMsg; }) as WebInboundMsg;
async function withTempDir<T>(prefix: string, run: (dir: string) => Promise<T>): Promise<T> {
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", () => { describe("isBotMentionedFromTargets", () => {
const mentionCfg = { mentionRegexes: [/\bopenclaw\b/i] }; const mentionCfg = { mentionRegexes: [/\bopenclaw\b/i] };
@@ -78,44 +87,46 @@ describe("isBotMentionedFromTargets", () => {
const targetsText = resolveMentionTargets(msgTextMention); const targetsText = resolveMentionTargets(msgTextMention);
expect(isBotMentionedFromTargets(msgTextMention, cfg, targetsText)).toBe(true); 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", () => { 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 () => { const mentionTargets = resolveMentionTargets(
authDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-lid-mapping-")); makeMsg({
await fs.writeFile(path.join(authDir, "lid-mapping-777_reverse.json"), JSON.stringify("+1777")); body: "ping",
}); mentionedJids: ["777@lid"],
selfE164: "+15551234567",
selfJid: "15551234567@s.whatsapp.net",
}),
authDir,
);
expect(mentionTargets.normalizedMentions).toContain("+1777");
afterAll(async () => { const selfTargets = resolveMentionTargets(
if (!authDir) { makeMsg({
return; body: "ping",
} selfJid: "777@lid",
await fs.rm(authDir, { recursive: true, force: true }); }),
authDir = ""; authDir,
}); );
expect(selfTargets.selfE164).toBe("+1777");
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");
}); });
}); });
@@ -124,36 +135,37 @@ describe("getSessionSnapshot", () => {
vi.useFakeTimers(); vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0)); vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try { try {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-snapshot-")); await withTempDir("openclaw-snapshot-", async (root) => {
const storePath = path.join(root, "sessions.json"); const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:whatsapp:dm:s1"; const sessionKey = "agent:main:whatsapp:dm:s1";
await saveSessionStore(storePath, { await saveSessionStore(storePath, {
[sessionKey]: { [sessionKey]: {
sessionId: "snapshot-session", sessionId: "snapshot-session",
updatedAt: new Date(2026, 0, 18, 3, 30, 0).getTime(), updatedAt: new Date(2026, 0, 18, 3, 30, 0).getTime(),
lastChannel: "whatsapp", lastChannel: "whatsapp",
},
});
const cfg = {
session: {
store: storePath,
reset: { mode: "daily", atHour: 4, idleMinutes: 240 },
resetByChannel: {
whatsapp: { mode: "idle", idleMinutes: 360 },
}, },
}, });
} as Parameters<typeof getSessionSnapshot>[0];
const snapshot = getSessionSnapshot(cfg, "whatsapp:+15550001111", true, { const cfg = {
sessionKey, session: {
store: storePath,
reset: { mode: "daily", atHour: 4, idleMinutes: 240 },
resetByChannel: {
whatsapp: { mode: "idle", idleMinutes: 360 },
},
},
} as Parameters<typeof getSessionSnapshot>[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 { } finally {
vi.useRealTimers(); vi.useRealTimers();
} }