From 66951e52e61bc95432784459de28c747e8e03f7d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 21:59:27 +0000 Subject: [PATCH] perf(test): speed up sessions suite --- src/config/sessions.test.ts | 45 +++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/config/sessions.test.ts b/src/config/sessions.test.ts index d46c7e97ce7..686a46d3740 100644 --- a/src/config/sessions.test.ts +++ b/src/config/sessions.test.ts @@ -1,7 +1,7 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { describe, expect, it } from "vitest"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { sleep } from "../utils.js"; import { buildGroupDisplayName, @@ -17,6 +17,23 @@ import { } from "./sessions.js"; describe("sessions", () => { + let fixtureRoot = ""; + let fixtureCount = 0; + + const createCaseDir = async (prefix: string) => { + const dir = path.join(fixtureRoot, `${prefix}-${fixtureCount++}`); + await fs.mkdir(dir, { recursive: true }); + return dir; + }; + + beforeAll(async () => { + fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-suite-")); + }); + + afterAll(async () => { + await fs.rm(fixtureRoot, { recursive: true, force: true }); + }); + it("returns normalized per-sender key", () => { expect(deriveSessionKey("per-sender", { From: "whatsapp:+1555" })).toBe("+1555"); }); @@ -95,7 +112,7 @@ describe("sessions", () => { it("updateLastRoute persists channel and target", async () => { const mainSessionKey = "agent:main:main"; - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateLastRoute"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile( storePath, @@ -148,7 +165,7 @@ describe("sessions", () => { it("updateLastRoute prefers explicit deliveryContext", async () => { const mainSessionKey = "agent:main:main"; - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateLastRoute"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile(storePath, "{}", "utf-8"); @@ -178,7 +195,7 @@ describe("sessions", () => { it("updateLastRoute clears threadId when explicit route omits threadId", async () => { const mainSessionKey = "agent:main:main"; - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateLastRoute"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile( storePath, @@ -222,7 +239,7 @@ describe("sessions", () => { it("updateLastRoute records origin + group metadata when ctx is provided", async () => { const sessionKey = "agent:main:whatsapp:group:123@g.us"; - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateLastRoute"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile(storePath, "{}", "utf-8"); @@ -252,7 +269,7 @@ describe("sessions", () => { it("updateSessionStoreEntry preserves existing fields when patching", async () => { const sessionKey = "agent:main:main"; - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateSessionStoreEntry"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile( storePath, @@ -282,7 +299,7 @@ describe("sessions", () => { }); it("updateSessionStore preserves concurrent additions", async () => { - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateSessionStore"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile(storePath, "{}", "utf-8"); @@ -301,7 +318,7 @@ describe("sessions", () => { }); it("recovers from array-backed session stores", async () => { - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateSessionStore"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile(storePath, "[]", "utf-8"); @@ -317,7 +334,7 @@ describe("sessions", () => { }); it("normalizes last route fields on write", async () => { - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateSessionStore"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile(storePath, "{}", "utf-8"); @@ -343,7 +360,7 @@ describe("sessions", () => { }); it("updateSessionStore keeps deletions when concurrent writes happen", async () => { - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateSessionStore"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile( storePath, @@ -375,7 +392,7 @@ describe("sessions", () => { it("loadSessionStore auto-migrates legacy provider keys to channel keys", async () => { const mainSessionKey = "agent:main:main"; - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("loadSessionStore"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile( storePath, @@ -455,7 +472,7 @@ describe("sessions", () => { it("updateSessionStoreEntry merges concurrent patches", async () => { const mainSessionKey = "agent:main:main"; - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-")); + const dir = await createCaseDir("updateSessionStoreEntry"); const storePath = path.join(dir, "sessions.json"); await fs.writeFile( storePath, @@ -478,7 +495,7 @@ describe("sessions", () => { storePath, sessionKey: mainSessionKey, update: async () => { - await sleep(50); + await sleep(10); return { modelOverride: "anthropic/claude-opus-4-5" }; }, }), @@ -486,7 +503,7 @@ describe("sessions", () => { storePath, sessionKey: mainSessionKey, update: async () => { - await sleep(10); + await sleep(1); return { thinkingLevel: "high" }; }, }),