refactor(test): dedupe session reset policy setup

This commit is contained in:
Peter Steinberger
2026-02-15 00:36:34 +00:00
parent 8181f51dbd
commit d75bcc27f9
+11 -38
View File
@@ -1,7 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import { saveSessionStore } from "../../config/sessions.js";
import { initSessionState } from "./session.js";
@@ -21,7 +21,7 @@ afterAll(async () => {
async function makeCaseDir(prefix: string): Promise<string> {
const dir = path.join(suiteRoot, `${prefix}${++suiteCase}`);
await fs.mkdir(dir, { recursive: true });
await fs.mkdir(dir);
return dir;
}
@@ -29,7 +29,7 @@ describe("initSessionState thread forking", () => {
it("forks a new session from the parent session file", async () => {
const root = await makeCaseDir("openclaw-thread-session-");
const sessionsDir = path.join(root, "sessions");
await fs.mkdir(sessionsDir, { recursive: true });
await fs.mkdir(sessionsDir);
const parentSessionId = "parent-session";
const parentSessionFile = path.join(sessionsDir, "parent.jsonl");
@@ -258,10 +258,16 @@ describe("initSessionState RawBody", () => {
});
describe("initSessionState reset policy", () => {
it("defaults to daily reset at 4am local time", async () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it("defaults to daily reset at 4am local time", async () => {
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try {
const root = await makeCaseDir("openclaw-reset-daily-");
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:whatsapp:dm:s1";
@@ -283,15 +289,10 @@ describe("initSessionState reset policy", () => {
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
it("treats sessions as stale before the daily reset when updated before yesterday's boundary", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 3, 0, 0));
try {
const root = await makeCaseDir("openclaw-reset-daily-edge-");
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:whatsapp:dm:s-edge";
@@ -313,15 +314,10 @@ describe("initSessionState reset policy", () => {
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
it("expires sessions when idle timeout wins over daily reset", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 30, 0));
try {
const root = await makeCaseDir("openclaw-reset-idle-");
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:whatsapp:dm:s2";
@@ -348,15 +344,10 @@ describe("initSessionState reset policy", () => {
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
it("uses per-type overrides for thread sessions", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try {
const root = await makeCaseDir("openclaw-reset-thread-");
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:slack:channel:c1:thread:123";
@@ -384,15 +375,10 @@ describe("initSessionState reset policy", () => {
expect(result.isNewSession).toBe(false);
expect(result.sessionId).toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
it("detects thread sessions without thread key suffix", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try {
const root = await makeCaseDir("openclaw-reset-thread-nosuffix-");
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:discord:channel:c1";
@@ -419,15 +405,10 @@ describe("initSessionState reset policy", () => {
expect(result.isNewSession).toBe(false);
expect(result.sessionId).toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
it("defaults to daily resets when only resetByType is configured", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try {
const root = await makeCaseDir("openclaw-reset-type-default-");
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:whatsapp:dm:s4";
@@ -454,15 +435,10 @@ describe("initSessionState reset policy", () => {
expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
it("keeps legacy idleMinutes behavior without reset config", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try {
const root = await makeCaseDir("openclaw-reset-legacy-");
const storePath = path.join(root, "sessions.json");
const sessionKey = "agent:main:whatsapp:dm:s3";
@@ -489,9 +465,6 @@ describe("initSessionState reset policy", () => {
expect(result.isNewSession).toBe(false);
expect(result.sessionId).toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
});
});