refactor(test): dedupe env setup across suites

This commit is contained in:
Peter Steinberger
2026-02-21 13:12:53 +00:00
parent f903603722
commit 7724abeee0
5 changed files with 43 additions and 122 deletions

View File

@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { withEnv } from "../test-utils/env.js";
import {
buildGroupDisplayName,
deriveSessionKey,
@@ -33,6 +34,9 @@ describe("sessions", () => {
await fs.rm(fixtureRoot, { recursive: true, force: true });
});
const withStateDir = <T>(stateDir: string, fn: () => T): T =>
withEnv({ OPENCLAW_STATE_DIR: stateDir }, fn);
it("returns normalized per-sender key", () => {
expect(deriveSessionKey("per-sender", { From: "whatsapp:+1555" })).toBe("+1555");
});
@@ -428,9 +432,7 @@ describe("sessions", () => {
});
it("includes topic ids in session transcript filenames", () => {
const prev = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = "/custom/state";
try {
withStateDir("/custom/state", () => {
const sessionFile = resolveSessionTranscriptPath("sess-1", "main", 123);
expect(sessionFile).toBe(
path.join(
@@ -441,39 +443,23 @@ describe("sessions", () => {
"sess-1-topic-123.jsonl",
),
);
} finally {
if (prev === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = prev;
}
}
});
});
it("uses agent id when resolving session file fallback paths", () => {
const prev = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = "/custom/state";
try {
withStateDir("/custom/state", () => {
const sessionFile = resolveSessionFilePath("sess-2", undefined, {
agentId: "codex",
});
expect(sessionFile).toBe(
path.join(path.resolve("/custom/state"), "agents", "codex", "sessions", "sess-2.jsonl"),
);
} finally {
if (prev === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = prev;
}
}
});
});
it("resolves cross-agent absolute sessionFile paths", () => {
const prev = process.env.OPENCLAW_STATE_DIR;
const stateDir = path.resolve("/home/user/.openclaw");
process.env.OPENCLAW_STATE_DIR = stateDir;
try {
withStateDir(stateDir, () => {
const bot2Session = path.join(stateDir, "agents", "bot2", "sessions", "sess-1.jsonl");
// Agent bot1 resolves a sessionFile that belongs to agent bot2
const sessionFile = resolveSessionFilePath(
@@ -482,19 +468,11 @@ describe("sessions", () => {
{ agentId: "bot1" },
);
expect(sessionFile).toBe(bot2Session);
} finally {
if (prev === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = prev;
}
}
});
});
it("resolves cross-agent paths when OPENCLAW_STATE_DIR differs from stored paths", () => {
const prev = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = path.resolve("/different/state");
try {
withStateDir(path.resolve("/different/state"), () => {
const originalBase = path.resolve("/original/state");
const bot2Session = path.join(originalBase, "agents", "bot2", "sessions", "sess-1.jsonl");
// sessionFile was created under a different state dir than current env
@@ -504,19 +482,11 @@ describe("sessions", () => {
{ agentId: "bot1" },
);
expect(sessionFile).toBe(bot2Session);
} finally {
if (prev === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = prev;
}
}
});
});
it("rejects absolute sessionFile paths outside agent sessions directories", () => {
const prev = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = path.resolve("/home/user/.openclaw");
try {
withStateDir(path.resolve("/home/user/.openclaw"), () => {
expect(() =>
resolveSessionFilePath(
"sess-1",
@@ -524,13 +494,7 @@ describe("sessions", () => {
{ agentId: "bot1" },
),
).toThrow(/within sessions directory/);
} finally {
if (prev === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = prev;
}
}
});
});
it("updateSessionStoreEntry merges concurrent patches", async () => {