test: speed up heavy suites with shared fixtures

This commit is contained in:
Peter Steinberger
2026-03-02 21:58:19 +00:00
parent 6358aae024
commit 3beb1b9da9
12 changed files with 227 additions and 151 deletions

View File

@@ -5,7 +5,6 @@ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi }
import { buildModelAliasIndex } from "../../agents/model-selection.js";
import type { OpenClawConfig } from "../../config/config.js";
import type { SessionEntry } from "../../config/sessions.js";
import { saveSessionStore } from "../../config/sessions.js";
import { formatZonedTimestamp } from "../../infra/format-time/format-datetime.ts";
import { enqueueSystemEvent, resetSystemEventsForTest } from "../../infra/system-events.js";
import { applyResetModelOverride } from "./session-reset-model.js";
@@ -51,6 +50,14 @@ async function makeStorePath(prefix: string): Promise<string> {
const createStorePath = makeStorePath;
async function writeSessionStoreFast(
storePath: string,
store: Record<string, SessionEntry | Record<string, unknown>>,
): Promise<void> {
await fs.mkdir(path.dirname(storePath), { recursive: true });
await fs.writeFile(storePath, JSON.stringify(store), "utf-8");
}
describe("initSessionState thread forking", () => {
it("forks a new session from the parent session file", async () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
@@ -89,7 +96,7 @@ describe("initSessionState thread forking", () => {
const storePath = path.join(root, "sessions.json");
const parentSessionKey = "agent:main:slack:channel:c1";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[parentSessionKey]: {
sessionId: parentSessionId,
sessionFile: parentSessionFile,
@@ -175,7 +182,7 @@ describe("initSessionState thread forking", () => {
const storePath = path.join(root, "sessions.json");
const parentSessionKey = "agent:main:slack:channel:c1";
const threadSessionKey = "agent:main:slack:channel:c1:thread:123";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[parentSessionKey]: {
sessionId: parentSessionId,
sessionFile: parentSessionFile,
@@ -256,7 +263,7 @@ describe("initSessionState thread forking", () => {
const storePath = path.join(root, "sessions.json");
const parentSessionKey = "agent:main:slack:channel:c1";
// Set totalTokens well above PARENT_FORK_MAX_TOKENS (100_000)
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[parentSessionKey]: {
sessionId: parentSessionId,
sessionFile: parentSessionFile,
@@ -324,7 +331,7 @@ describe("initSessionState thread forking", () => {
const storePath = path.join(root, "sessions.json");
const parentSessionKey = "agent:main:slack:channel:c1";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[parentSessionKey]: {
sessionId: parentSessionId,
sessionFile: parentSessionFile,
@@ -461,7 +468,7 @@ describe("initSessionState RawBody", () => {
vi.stubEnv("OPENCLAW_STATE_DIR", stateDir);
try {
await fs.mkdir(path.dirname(storePath), { recursive: true });
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId,
sessionFile,
@@ -507,7 +514,7 @@ describe("initSessionState reset policy", () => {
const sessionKey = "agent:main:whatsapp:dm:s1";
const existingSessionId = "daily-session-id";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
@@ -532,7 +539,7 @@ describe("initSessionState reset policy", () => {
const sessionKey = "agent:main:whatsapp:dm:s-edge";
const existingSessionId = "daily-edge-session";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 17, 3, 30, 0).getTime(),
@@ -557,7 +564,7 @@ describe("initSessionState reset policy", () => {
const sessionKey = "agent:main:whatsapp:dm:s2";
const existingSessionId = "idle-session-id";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 4, 45, 0).getTime(),
@@ -587,7 +594,7 @@ describe("initSessionState reset policy", () => {
const sessionKey = "agent:main:slack:channel:c1:thread:123";
const existingSessionId = "thread-session-id";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
@@ -618,7 +625,7 @@ describe("initSessionState reset policy", () => {
const sessionKey = "agent:main:discord:channel:c1";
const existingSessionId = "thread-nosuffix";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
@@ -648,7 +655,7 @@ describe("initSessionState reset policy", () => {
const sessionKey = "agent:main:whatsapp:dm:s4";
const existingSessionId = "type-default-session";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
@@ -678,7 +685,7 @@ describe("initSessionState reset policy", () => {
const sessionKey = "agent:main:whatsapp:dm:s3";
const existingSessionId = "legacy-session-id";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 30, 0).getTime(),
@@ -710,7 +717,7 @@ describe("initSessionState channel reset overrides", () => {
const sessionId = "session-override";
const updatedAt = Date.now() - (10080 - 1) * 60_000;
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId,
updatedAt,
@@ -747,7 +754,7 @@ describe("initSessionState reset triggers in WhatsApp groups", () => {
sessionKey: string;
sessionId: string;
}): Promise<void> {
await saveSessionStore(params.storePath, {
await writeSessionStoreFast(params.storePath, {
[params.sessionKey]: {
sessionId: params.sessionId,
updatedAt: Date.now(),
@@ -840,7 +847,7 @@ describe("initSessionState reset triggers in Slack channels", () => {
sessionKey: string;
sessionId: string;
}): Promise<void> {
await saveSessionStore(params.storePath, {
await writeSessionStoreFast(params.storePath, {
[params.sessionKey]: {
sessionId: params.sessionId,
updatedAt: Date.now(),
@@ -989,7 +996,7 @@ describe("initSessionState preserves behavior overrides across /new and /reset",
sessionId: string;
overrides: Record<string, unknown>;
}): Promise<void> {
await saveSessionStore(params.storePath, {
await writeSessionStoreFast(params.storePath, {
[params.sessionKey]: {
sessionId: params.sessionId,
updatedAt: Date.now(),
@@ -1390,7 +1397,7 @@ describe("initSessionState stale threadId fallback", () => {
describe("initSessionState dmScope delivery migration", () => {
it("retires stale main-session delivery route when dmScope uses per-channel DM keys", async () => {
const storePath = await createStorePath("dm-scope-retire-main-route-");
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
"agent:main:main": {
sessionId: "legacy-main",
updatedAt: Date.now(),
@@ -1436,7 +1443,7 @@ describe("initSessionState dmScope delivery migration", () => {
it("keeps legacy main-session delivery route when current DM target does not match", async () => {
const storePath = await createStorePath("dm-scope-keep-main-route-");
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
"agent:main:main": {
sessionId: "legacy-main",
updatedAt: Date.now(),
@@ -1483,7 +1490,7 @@ describe("initSessionState internal channel routing preservation", () => {
it("keeps persisted external lastChannel when OriginatingChannel is internal webchat", async () => {
const storePath = await createStorePath("preserve-external-channel-");
const sessionKey = "agent:main:telegram:group:12345";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId: "sess-1",
updatedAt: Date.now(),
@@ -1517,7 +1524,7 @@ describe("initSessionState internal channel routing preservation", () => {
it("keeps persisted external route when OriginatingChannel is non-deliverable", async () => {
const storePath = await createStorePath("preserve-nondeliverable-route-");
const sessionKey = "agent:main:discord:channel:24680";
await saveSessionStore(storePath, {
await writeSessionStoreFast(storePath, {
[sessionKey]: {
sessionId: "sess-2",
updatedAt: Date.now(),