test: remove whatsapp session store paths

This commit is contained in:
Peter Steinberger
2026-05-08 10:11:57 +01:00
parent 51b0f22d82
commit 3d45802eb7
5 changed files with 57 additions and 35 deletions

View File

@@ -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<string, unknown> = {},
): Promise<{ storePath: string; cleanup: () => Promise<void> }> {
): Promise<{ cleanup: () => Promise<void> }> {
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,
};
}

View File

@@ -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({

View File

@@ -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<string, unknown> = {},
): Promise<{ storePath: string; cleanup: () => Promise<void> }> {
): Promise<{ cleanup: () => Promise<void> }> {
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<void>> = [];
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",

View File

@@ -72,7 +72,6 @@ vi.mock("./runtime-api.js", () => ({
recordSessionMetaFromInbound: async () => {},
resolveChannelContextVisibilityMode: () => "standard",
resolveInboundSessionEnvelopeContext: () => ({
storePath: "/tmp/sessions.json",
envelopeOptions: {},
previousTimestamp: undefined,
}),

View File

@@ -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 {