From 3d45802eb74e489baad97784376b99188d05b7cb Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 8 May 2026 10:11:57 +0100 Subject: [PATCH] test: remove whatsapp session store paths --- .../whatsapp/src/auto-reply.test-harness.ts | 20 +++++-- ...to-reply.web-auto-reply.last-route.test.ts | 12 ++-- .../monitor/group-activation.test.ts | 58 +++++++++++-------- .../process-message.audio-preflight.test.ts | 1 - .../auto-reply/web-auto-reply-utils.test.ts | 1 - 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/extensions/whatsapp/src/auto-reply.test-harness.ts b/extensions/whatsapp/src/auto-reply.test-harness.ts index b022d2a6c2d..3755188749e 100644 --- a/extensions/whatsapp/src/auto-reply.test-harness.ts +++ b/extensions/whatsapp/src/auto-reply.test-harness.ts @@ -5,6 +5,7 @@ import os from "node:os"; import path from "node:path"; import { resetInboundDedupe } from "openclaw/plugin-sdk/reply-dedupe"; import { resetLogger, setLoggerOverride } from "openclaw/plugin-sdk/runtime-env"; +import { upsertSessionEntry } from "openclaw/plugin-sdk/session-store-runtime"; import { mockPinnedHostnameResolution } from "openclaw/plugin-sdk/test-env"; import { afterAll, afterEach, beforeAll, beforeEach, vi, type Mock } from "vitest"; import type { WebChannelStatus } from "./auto-reply/types.js"; @@ -187,15 +188,26 @@ export function installWebAutoReplyTestHomeHooks() { export async function makeSessionStore( entries: Record = {}, -): Promise<{ storePath: string; cleanup: () => Promise }> { +): Promise<{ cleanup: () => Promise }> { const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-")); - const storePath = path.join(dir, "sessions.json"); - await fs.writeFile(storePath, JSON.stringify(entries)); + const previousStateDir = process.env.OPENCLAW_STATE_DIR; + process.env.OPENCLAW_STATE_DIR = dir; + for (const [sessionKey, entry] of Object.entries(entries)) { + upsertSessionEntry({ + agentId: "main", + sessionKey, + entry: entry as never, + }); + } const cleanup = async () => { + if (previousStateDir === undefined) { + delete process.env.OPENCLAW_STATE_DIR; + } else { + process.env.OPENCLAW_STATE_DIR = previousStateDir; + } await rmDirWithRetries(dir); }; return { - storePath, cleanup, }; } diff --git a/extensions/whatsapp/src/auto-reply.web-auto-reply.last-route.test.ts b/extensions/whatsapp/src/auto-reply.web-auto-reply.last-route.test.ts index 90f9cf31b22..4f9f3ac8580 100644 --- a/extensions/whatsapp/src/auto-reply.web-auto-reply.last-route.test.ts +++ b/extensions/whatsapp/src/auto-reply.web-auto-reply.last-route.test.ts @@ -23,10 +23,10 @@ vi.mock("./auto-reply/monitor/last-route.js", async () => { }; }); -function makeCfg(storePath: string): OpenClawConfig { +function makeCfg(): OpenClawConfig { return { channels: { whatsapp: { allowFrom: ["*"] } }, - session: { store: storePath }, + session: {}, }; } @@ -62,9 +62,9 @@ function createHandlerForTest(opts: { cfg: OpenClawConfig; replyResolver: unknow return { handler, backgroundTasks }; } -function createLastRouteHarness(storePath: string) { +function createLastRouteHarness() { const replyResolver = vi.fn().mockResolvedValue(undefined); - const cfg = makeCfg(storePath); + const cfg = makeCfg(); return createHandlerForTest({ cfg, replyResolver }); } @@ -120,7 +120,7 @@ describe("web auto-reply last-route", () => { [mainSessionKey]: { sessionId: "sid", updatedAt: now - 1 }, }); - const { handler, backgroundTasks } = createLastRouteHarness(store.storePath); + const { handler, backgroundTasks } = createLastRouteHarness(); await handler( buildInboundMessage({ @@ -152,7 +152,7 @@ describe("web auto-reply last-route", () => { [groupSessionKey]: { sessionId: "sid", updatedAt: now - 1 }, }); - const { handler, backgroundTasks } = createLastRouteHarness(store.storePath); + const { handler, backgroundTasks } = createLastRouteHarness(); await handler( buildInboundMessage({ diff --git a/extensions/whatsapp/src/auto-reply/monitor/group-activation.test.ts b/extensions/whatsapp/src/auto-reply/monitor/group-activation.test.ts index a38a0d95fb9..40139ff72d5 100644 --- a/extensions/whatsapp/src/auto-reply/monitor/group-activation.test.ts +++ b/extensions/whatsapp/src/auto-reply/monitor/group-activation.test.ts @@ -2,7 +2,7 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; -import { loadSessionStore } from "../config.runtime.js"; +import { getSessionEntry, upsertSessionEntry } from "../config.runtime.js"; import { resolveGroupActivationFor } from "./group-activation.js"; const GROUP_CONVERSATION_ID = "123@g.us"; @@ -17,19 +17,24 @@ type SessionStoreEntry = { async function makeSessionStore( entries: Record = {}, -): Promise<{ storePath: string; cleanup: () => Promise }> { +): Promise<{ cleanup: () => Promise }> { const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-")); - const storePath = path.join(dir, "sessions.json"); - await fs.writeFile(storePath, JSON.stringify(entries)); + process.env.OPENCLAW_STATE_DIR = dir; + for (const [sessionKey, entry] of Object.entries(entries)) { + upsertSessionEntry({ + agentId: "main", + sessionKey, + entry: entry as never, + }); + } return { - storePath, cleanup: async () => { await fs.rm(dir, { recursive: true, force: true }); }, }; } -const resolveWorkGroupActivation = (storePath: string) => +const resolveWorkGroupActivation = () => resolveGroupActivationFor({ cfg: { channels: { @@ -39,7 +44,7 @@ const resolveWorkGroupActivation = (storePath: string) => }, }, }, - session: { store: storePath }, + session: {}, } as never, accountId: "work", agentId: "main", @@ -48,36 +53,43 @@ const resolveWorkGroupActivation = (storePath: string) => }); const expectWorkGroupActivationEntry = async ( - storePath: string, assertEntry?: (entry: SessionStoreEntry | undefined) => void, ) => { await vi.waitFor(() => { - const scopedEntry = loadSessionStore(storePath)[WORK_GROUP_SESSION_KEY]; + const scopedEntry = getSessionEntry({ + agentId: "main", + sessionKey: WORK_GROUP_SESSION_KEY, + }); expect(scopedEntry?.groupActivation).toBe("always"); assertEntry?.(scopedEntry); }); }; const expectResolvedWorkGroupActivation = async ( - storePath: string, assertEntry?: (entry: SessionStoreEntry | undefined) => void, ) => { - const activation = await resolveWorkGroupActivation(storePath); + const activation = await resolveWorkGroupActivation(); expect(activation).toBe("always"); - await expectWorkGroupActivationEntry(storePath, assertEntry); + await expectWorkGroupActivationEntry(assertEntry); }; describe("resolveGroupActivationFor", () => { const cleanups: Array<() => Promise> = []; + const originalStateDir = process.env.OPENCLAW_STATE_DIR; afterEach(async () => { while (cleanups.length > 0) { await cleanups.pop()?.(); } + if (originalStateDir === undefined) { + delete process.env.OPENCLAW_STATE_DIR; + } else { + process.env.OPENCLAW_STATE_DIR = originalStateDir; + } }); it("reads legacy named-account group activation and backfills the scoped key", async () => { - const { storePath, cleanup } = await makeSessionStore({ + const { cleanup } = await makeSessionStore({ [LEGACY_GROUP_SESSION_KEY]: { groupActivation: "always", sessionId: "legacy-session", @@ -86,14 +98,14 @@ describe("resolveGroupActivationFor", () => { }); cleanups.push(cleanup); - await expectResolvedWorkGroupActivation(storePath, (scopedEntry) => { - expect(scopedEntry?.sessionId).toBeUndefined(); - expect(scopedEntry?.updatedAt).toBeUndefined(); + await expectResolvedWorkGroupActivation((scopedEntry) => { + expect(typeof scopedEntry?.sessionId).toBe("string"); + expect(typeof scopedEntry?.updatedAt).toBe("number"); }); }); it("preserves legacy group activation when the scoped entry already exists without activation", async () => { - const { storePath, cleanup } = await makeSessionStore({ + const { cleanup } = await makeSessionStore({ [LEGACY_GROUP_SESSION_KEY]: { groupActivation: "always", }, @@ -103,13 +115,13 @@ describe("resolveGroupActivationFor", () => { }); cleanups.push(cleanup); - await expectResolvedWorkGroupActivation(storePath, (scopedEntry) => { + await expectResolvedWorkGroupActivation((scopedEntry) => { expect(scopedEntry?.sessionId).toBe("scoped-session"); }); }); it("does not wake the default account from an activation-only legacy group entry in multi-account setups", async () => { - const { storePath, cleanup } = await makeSessionStore({ + const { cleanup } = await makeSessionStore({ [LEGACY_GROUP_SESSION_KEY]: { groupActivation: "always", }, @@ -129,7 +141,7 @@ describe("resolveGroupActivationFor", () => { }, }, }, - session: { store: storePath }, + session: {}, } as never; const workActivation = await resolveGroupActivationFor({ @@ -151,11 +163,11 @@ describe("resolveGroupActivationFor", () => { }); expect(defaultActivation).toBe("mention"); - await expectWorkGroupActivationEntry(storePath); + await expectWorkGroupActivationEntry(); }); it("does not treat mixed-case default account keys as named accounts", async () => { - const { storePath, cleanup } = await makeSessionStore({ + const { cleanup } = await makeSessionStore({ [LEGACY_GROUP_SESSION_KEY]: { groupActivation: "always", }, @@ -176,7 +188,7 @@ describe("resolveGroupActivationFor", () => { }, }, }, - session: { store: storePath }, + session: {}, } as never, accountId: "default", agentId: "main", diff --git a/extensions/whatsapp/src/auto-reply/monitor/process-message.audio-preflight.test.ts b/extensions/whatsapp/src/auto-reply/monitor/process-message.audio-preflight.test.ts index 13d548bd53e..ff58c1a3d36 100644 --- a/extensions/whatsapp/src/auto-reply/monitor/process-message.audio-preflight.test.ts +++ b/extensions/whatsapp/src/auto-reply/monitor/process-message.audio-preflight.test.ts @@ -72,7 +72,6 @@ vi.mock("./runtime-api.js", () => ({ recordSessionMetaFromInbound: async () => {}, resolveChannelContextVisibilityMode: () => "standard", resolveInboundSessionEnvelopeContext: () => ({ - storePath: "/tmp/sessions.json", envelopeOptions: {}, previousTimestamp: undefined, }), diff --git a/extensions/whatsapp/src/auto-reply/web-auto-reply-utils.test.ts b/extensions/whatsapp/src/auto-reply/web-auto-reply-utils.test.ts index 1033069e3b6..3c7e9917e45 100644 --- a/extensions/whatsapp/src/auto-reply/web-auto-reply-utils.test.ts +++ b/extensions/whatsapp/src/auto-reply/web-auto-reply-utils.test.ts @@ -260,7 +260,6 @@ describe("getSessionSnapshot", () => { await withTempDir("openclaw-snapshot-", async (root) => { const previousStateDir = process.env.OPENCLAW_STATE_DIR; process.env.OPENCLAW_STATE_DIR = root; - const storePath = path.join(root, "agents", "main", "sessions", "sessions.json"); const sessionKey = "agent:main:whatsapp:dm:s1"; try {