mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 17:01:53 +03:00
perf(test): speed up sessions suite
This commit is contained in:
+31
-14
@@ -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 { describe, expect, it } from "vitest";
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||||
import { sleep } from "../utils.js";
|
import { sleep } from "../utils.js";
|
||||||
import {
|
import {
|
||||||
buildGroupDisplayName,
|
buildGroupDisplayName,
|
||||||
@@ -17,6 +17,23 @@ import {
|
|||||||
} from "./sessions.js";
|
} from "./sessions.js";
|
||||||
|
|
||||||
describe("sessions", () => {
|
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", () => {
|
it("returns normalized per-sender key", () => {
|
||||||
expect(deriveSessionKey("per-sender", { From: "whatsapp:+1555" })).toBe("+1555");
|
expect(deriveSessionKey("per-sender", { From: "whatsapp:+1555" })).toBe("+1555");
|
||||||
});
|
});
|
||||||
@@ -95,7 +112,7 @@ describe("sessions", () => {
|
|||||||
|
|
||||||
it("updateLastRoute persists channel and target", async () => {
|
it("updateLastRoute persists channel and target", async () => {
|
||||||
const mainSessionKey = "agent:main:main";
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
storePath,
|
storePath,
|
||||||
@@ -148,7 +165,7 @@ describe("sessions", () => {
|
|||||||
|
|
||||||
it("updateLastRoute prefers explicit deliveryContext", async () => {
|
it("updateLastRoute prefers explicit deliveryContext", async () => {
|
||||||
const mainSessionKey = "agent:main:main";
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(storePath, "{}", "utf-8");
|
await fs.writeFile(storePath, "{}", "utf-8");
|
||||||
|
|
||||||
@@ -178,7 +195,7 @@ describe("sessions", () => {
|
|||||||
|
|
||||||
it("updateLastRoute clears threadId when explicit route omits threadId", async () => {
|
it("updateLastRoute clears threadId when explicit route omits threadId", async () => {
|
||||||
const mainSessionKey = "agent:main:main";
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
storePath,
|
storePath,
|
||||||
@@ -222,7 +239,7 @@ describe("sessions", () => {
|
|||||||
|
|
||||||
it("updateLastRoute records origin + group metadata when ctx is provided", async () => {
|
it("updateLastRoute records origin + group metadata when ctx is provided", async () => {
|
||||||
const sessionKey = "agent:main:whatsapp:group:123@g.us";
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(storePath, "{}", "utf-8");
|
await fs.writeFile(storePath, "{}", "utf-8");
|
||||||
|
|
||||||
@@ -252,7 +269,7 @@ describe("sessions", () => {
|
|||||||
|
|
||||||
it("updateSessionStoreEntry preserves existing fields when patching", async () => {
|
it("updateSessionStoreEntry preserves existing fields when patching", async () => {
|
||||||
const sessionKey = "agent:main:main";
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
storePath,
|
storePath,
|
||||||
@@ -282,7 +299,7 @@ describe("sessions", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("updateSessionStore preserves concurrent additions", async () => {
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(storePath, "{}", "utf-8");
|
await fs.writeFile(storePath, "{}", "utf-8");
|
||||||
|
|
||||||
@@ -301,7 +318,7 @@ describe("sessions", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("recovers from array-backed session stores", async () => {
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(storePath, "[]", "utf-8");
|
await fs.writeFile(storePath, "[]", "utf-8");
|
||||||
|
|
||||||
@@ -317,7 +334,7 @@ describe("sessions", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("normalizes last route fields on write", async () => {
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(storePath, "{}", "utf-8");
|
await fs.writeFile(storePath, "{}", "utf-8");
|
||||||
|
|
||||||
@@ -343,7 +360,7 @@ describe("sessions", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("updateSessionStore keeps deletions when concurrent writes happen", async () => {
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
storePath,
|
storePath,
|
||||||
@@ -375,7 +392,7 @@ describe("sessions", () => {
|
|||||||
|
|
||||||
it("loadSessionStore auto-migrates legacy provider keys to channel keys", async () => {
|
it("loadSessionStore auto-migrates legacy provider keys to channel keys", async () => {
|
||||||
const mainSessionKey = "agent:main:main";
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
storePath,
|
storePath,
|
||||||
@@ -455,7 +472,7 @@ describe("sessions", () => {
|
|||||||
|
|
||||||
it("updateSessionStoreEntry merges concurrent patches", async () => {
|
it("updateSessionStoreEntry merges concurrent patches", async () => {
|
||||||
const mainSessionKey = "agent:main:main";
|
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");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
storePath,
|
storePath,
|
||||||
@@ -478,7 +495,7 @@ describe("sessions", () => {
|
|||||||
storePath,
|
storePath,
|
||||||
sessionKey: mainSessionKey,
|
sessionKey: mainSessionKey,
|
||||||
update: async () => {
|
update: async () => {
|
||||||
await sleep(50);
|
await sleep(10);
|
||||||
return { modelOverride: "anthropic/claude-opus-4-5" };
|
return { modelOverride: "anthropic/claude-opus-4-5" };
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@@ -486,7 +503,7 @@ describe("sessions", () => {
|
|||||||
storePath,
|
storePath,
|
||||||
sessionKey: mainSessionKey,
|
sessionKey: mainSessionKey,
|
||||||
update: async () => {
|
update: async () => {
|
||||||
await sleep(10);
|
await sleep(1);
|
||||||
return { thinkingLevel: "high" };
|
return { thinkingLevel: "high" };
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user