mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 14:51:42 +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 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,25 +87,26 @@ 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 () => {
|
|
||||||
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"));
|
|
||||||
});
|
|
||||||
|
|
||||||
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(
|
const mentionTargets = resolveMentionTargets(
|
||||||
makeMsg({
|
makeMsg({
|
||||||
body: "ping",
|
body: "ping",
|
||||||
@@ -118,13 +128,14 @@ describe("resolveMentionTargets with @lid mapping", () => {
|
|||||||
expect(selfTargets.selfE164).toBe("+1777");
|
expect(selfTargets.selfE164).toBe("+1777");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("getSessionSnapshot", () => {
|
describe("getSessionSnapshot", () => {
|
||||||
it("uses channel reset overrides when configured", async () => {
|
it("uses channel reset overrides when configured", async () => {
|
||||||
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";
|
||||||
|
|
||||||
@@ -154,6 +165,7 @@ describe("getSessionSnapshot", () => {
|
|||||||
expect(snapshot.resetPolicy.idleMinutes).toBe(360);
|
expect(snapshot.resetPolicy.idleMinutes).toBe(360);
|
||||||
expect(snapshot.fresh).toBe(true);
|
expect(snapshot.fresh).toBe(true);
|
||||||
expect(snapshot.dailyResetAt).toBeUndefined();
|
expect(snapshot.dailyResetAt).toBeUndefined();
|
||||||
|
});
|
||||||
} finally {
|
} finally {
|
||||||
vi.useRealTimers();
|
vi.useRealTimers();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user