mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 15:51:22 +00:00
test(web): dedupe temp dir setup in web auto-reply utils tests
This commit is contained in:
@@ -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>): WebInboundMsg =>
|
||||
...overrides,
|
||||
}) 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", () => {
|
||||
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<typeof getSessionSnapshot>[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<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 {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user