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
+163 -190
View File
@@ -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 { 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 type { OpenClawConfig } from "../../config/config.js";
import { saveSessionStore } from "../../config/sessions.js"; import { saveSessionStore } from "../../config/sessions.js";
import { initSessionState } from "./session.js"; import { initSessionState } from "./session.js";
@@ -21,7 +21,7 @@ afterAll(async () => {
async function makeCaseDir(prefix: string): Promise<string> { async function makeCaseDir(prefix: string): Promise<string> {
const dir = path.join(suiteRoot, `${prefix}${++suiteCase}`); const dir = path.join(suiteRoot, `${prefix}${++suiteCase}`);
await fs.mkdir(dir, { recursive: true }); await fs.mkdir(dir);
return dir; return dir;
} }
@@ -29,7 +29,7 @@ describe("initSessionState thread forking", () => {
it("forks a new session from the parent session file", async () => { it("forks a new session from the parent session file", async () => {
const root = await makeCaseDir("openclaw-thread-session-"); const root = await makeCaseDir("openclaw-thread-session-");
const sessionsDir = path.join(root, "sessions"); const sessionsDir = path.join(root, "sessions");
await fs.mkdir(sessionsDir, { recursive: true }); await fs.mkdir(sessionsDir);
const parentSessionId = "parent-session"; const parentSessionId = "parent-session";
const parentSessionFile = path.join(sessionsDir, "parent.jsonl"); const parentSessionFile = path.join(sessionsDir, "parent.jsonl");
@@ -258,240 +258,213 @@ describe("initSessionState RawBody", () => {
}); });
describe("initSessionState reset policy", () => { describe("initSessionState reset policy", () => {
it("defaults to daily reset at 4am local time", async () => { beforeEach(() => {
vi.useFakeTimers(); 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)); vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try { const root = await makeCaseDir("openclaw-reset-daily-");
const root = await makeCaseDir("openclaw-reset-daily-"); const storePath = path.join(root, "sessions.json");
const storePath = path.join(root, "sessions.json"); const sessionKey = "agent:main:whatsapp:dm:s1";
const sessionKey = "agent:main:whatsapp:dm:s1"; const existingSessionId = "daily-session-id";
const existingSessionId = "daily-session-id";
await saveSessionStore(storePath, { await saveSessionStore(storePath, {
[sessionKey]: { [sessionKey]: {
sessionId: existingSessionId, sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
}, },
}); });
const cfg = { session: { store: storePath } } as OpenClawConfig; const cfg = { session: { store: storePath } } as OpenClawConfig;
const result = await initSessionState({ const result = await initSessionState({
ctx: { Body: "hello", SessionKey: sessionKey }, ctx: { Body: "hello", SessionKey: sessionKey },
cfg, cfg,
commandAuthorized: true, commandAuthorized: true,
}); });
expect(result.isNewSession).toBe(true); expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId); 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 () => { 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)); vi.setSystemTime(new Date(2026, 0, 18, 3, 0, 0));
try { const root = await makeCaseDir("openclaw-reset-daily-edge-");
const root = await makeCaseDir("openclaw-reset-daily-edge-"); const storePath = path.join(root, "sessions.json");
const storePath = path.join(root, "sessions.json"); const sessionKey = "agent:main:whatsapp:dm:s-edge";
const sessionKey = "agent:main:whatsapp:dm:s-edge"; const existingSessionId = "daily-edge-session";
const existingSessionId = "daily-edge-session";
await saveSessionStore(storePath, { await saveSessionStore(storePath, {
[sessionKey]: { [sessionKey]: {
sessionId: existingSessionId, sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 17, 3, 30, 0).getTime(), updatedAt: new Date(2026, 0, 17, 3, 30, 0).getTime(),
}, },
}); });
const cfg = { session: { store: storePath } } as OpenClawConfig; const cfg = { session: { store: storePath } } as OpenClawConfig;
const result = await initSessionState({ const result = await initSessionState({
ctx: { Body: "hello", SessionKey: sessionKey }, ctx: { Body: "hello", SessionKey: sessionKey },
cfg, cfg,
commandAuthorized: true, commandAuthorized: true,
}); });
expect(result.isNewSession).toBe(true); expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId); expect(result.sessionId).not.toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
}); });
it("expires sessions when idle timeout wins over daily reset", async () => { it("expires sessions when idle timeout wins over daily reset", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 30, 0)); vi.setSystemTime(new Date(2026, 0, 18, 5, 30, 0));
try { const root = await makeCaseDir("openclaw-reset-idle-");
const root = await makeCaseDir("openclaw-reset-idle-"); const storePath = path.join(root, "sessions.json");
const storePath = path.join(root, "sessions.json"); const sessionKey = "agent:main:whatsapp:dm:s2";
const sessionKey = "agent:main:whatsapp:dm:s2"; const existingSessionId = "idle-session-id";
const existingSessionId = "idle-session-id";
await saveSessionStore(storePath, { await saveSessionStore(storePath, {
[sessionKey]: { [sessionKey]: {
sessionId: existingSessionId, sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 4, 45, 0).getTime(), updatedAt: new Date(2026, 0, 18, 4, 45, 0).getTime(),
}, },
}); });
const cfg = { const cfg = {
session: { session: {
store: storePath, store: storePath,
reset: { mode: "daily", atHour: 4, idleMinutes: 30 }, reset: { mode: "daily", atHour: 4, idleMinutes: 30 },
}, },
} as OpenClawConfig; } as OpenClawConfig;
const result = await initSessionState({ const result = await initSessionState({
ctx: { Body: "hello", SessionKey: sessionKey }, ctx: { Body: "hello", SessionKey: sessionKey },
cfg, cfg,
commandAuthorized: true, commandAuthorized: true,
}); });
expect(result.isNewSession).toBe(true); expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId); expect(result.sessionId).not.toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
}); });
it("uses per-type overrides for thread sessions", async () => { it("uses per-type overrides for thread sessions", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0)); vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try { const root = await makeCaseDir("openclaw-reset-thread-");
const root = await makeCaseDir("openclaw-reset-thread-"); const storePath = path.join(root, "sessions.json");
const storePath = path.join(root, "sessions.json"); const sessionKey = "agent:main:slack:channel:c1:thread:123";
const sessionKey = "agent:main:slack:channel:c1:thread:123"; const existingSessionId = "thread-session-id";
const existingSessionId = "thread-session-id";
await saveSessionStore(storePath, { await saveSessionStore(storePath, {
[sessionKey]: { [sessionKey]: {
sessionId: existingSessionId, sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
}, },
}); });
const cfg = { const cfg = {
session: { session: {
store: storePath, store: storePath,
reset: { mode: "daily", atHour: 4 }, reset: { mode: "daily", atHour: 4 },
resetByType: { thread: { mode: "idle", idleMinutes: 180 } }, resetByType: { thread: { mode: "idle", idleMinutes: 180 } },
}, },
} as OpenClawConfig; } as OpenClawConfig;
const result = await initSessionState({ const result = await initSessionState({
ctx: { Body: "reply", SessionKey: sessionKey, ThreadLabel: "Slack thread" }, ctx: { Body: "reply", SessionKey: sessionKey, ThreadLabel: "Slack thread" },
cfg, cfg,
commandAuthorized: true, commandAuthorized: true,
}); });
expect(result.isNewSession).toBe(false); expect(result.isNewSession).toBe(false);
expect(result.sessionId).toBe(existingSessionId); expect(result.sessionId).toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
}); });
it("detects thread sessions without thread key suffix", async () => { it("detects thread sessions without thread key suffix", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0)); vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try { const root = await makeCaseDir("openclaw-reset-thread-nosuffix-");
const root = await makeCaseDir("openclaw-reset-thread-nosuffix-"); const storePath = path.join(root, "sessions.json");
const storePath = path.join(root, "sessions.json"); const sessionKey = "agent:main:discord:channel:c1";
const sessionKey = "agent:main:discord:channel:c1"; const existingSessionId = "thread-nosuffix";
const existingSessionId = "thread-nosuffix";
await saveSessionStore(storePath, { await saveSessionStore(storePath, {
[sessionKey]: { [sessionKey]: {
sessionId: existingSessionId, sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
}, },
}); });
const cfg = { const cfg = {
session: { session: {
store: storePath, store: storePath,
resetByType: { thread: { mode: "idle", idleMinutes: 180 } }, resetByType: { thread: { mode: "idle", idleMinutes: 180 } },
}, },
} as OpenClawConfig; } as OpenClawConfig;
const result = await initSessionState({ const result = await initSessionState({
ctx: { Body: "reply", SessionKey: sessionKey, ThreadLabel: "Discord thread" }, ctx: { Body: "reply", SessionKey: sessionKey, ThreadLabel: "Discord thread" },
cfg, cfg,
commandAuthorized: true, commandAuthorized: true,
}); });
expect(result.isNewSession).toBe(false); expect(result.isNewSession).toBe(false);
expect(result.sessionId).toBe(existingSessionId); expect(result.sessionId).toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
}); });
it("defaults to daily resets when only resetByType is configured", async () => { it("defaults to daily resets when only resetByType is configured", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0)); vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try { const root = await makeCaseDir("openclaw-reset-type-default-");
const root = await makeCaseDir("openclaw-reset-type-default-"); const storePath = path.join(root, "sessions.json");
const storePath = path.join(root, "sessions.json"); const sessionKey = "agent:main:whatsapp:dm:s4";
const sessionKey = "agent:main:whatsapp:dm:s4"; const existingSessionId = "type-default-session";
const existingSessionId = "type-default-session";
await saveSessionStore(storePath, { await saveSessionStore(storePath, {
[sessionKey]: { [sessionKey]: {
sessionId: existingSessionId, sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(),
}, },
}); });
const cfg = { const cfg = {
session: { session: {
store: storePath, store: storePath,
resetByType: { thread: { mode: "idle", idleMinutes: 60 } }, resetByType: { thread: { mode: "idle", idleMinutes: 60 } },
}, },
} as OpenClawConfig; } as OpenClawConfig;
const result = await initSessionState({ const result = await initSessionState({
ctx: { Body: "hello", SessionKey: sessionKey }, ctx: { Body: "hello", SessionKey: sessionKey },
cfg, cfg,
commandAuthorized: true, commandAuthorized: true,
}); });
expect(result.isNewSession).toBe(true); expect(result.isNewSession).toBe(true);
expect(result.sessionId).not.toBe(existingSessionId); expect(result.sessionId).not.toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
}); });
it("keeps legacy idleMinutes behavior without reset config", async () => { it("keeps legacy idleMinutes behavior without reset config", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0)); vi.setSystemTime(new Date(2026, 0, 18, 5, 0, 0));
try { const root = await makeCaseDir("openclaw-reset-legacy-");
const root = await makeCaseDir("openclaw-reset-legacy-"); const storePath = path.join(root, "sessions.json");
const storePath = path.join(root, "sessions.json"); const sessionKey = "agent:main:whatsapp:dm:s3";
const sessionKey = "agent:main:whatsapp:dm:s3"; const existingSessionId = "legacy-session-id";
const existingSessionId = "legacy-session-id";
await saveSessionStore(storePath, { await saveSessionStore(storePath, {
[sessionKey]: { [sessionKey]: {
sessionId: existingSessionId, sessionId: existingSessionId,
updatedAt: new Date(2026, 0, 18, 3, 30, 0).getTime(), updatedAt: new Date(2026, 0, 18, 3, 30, 0).getTime(),
}, },
}); });
const cfg = { const cfg = {
session: { session: {
store: storePath, store: storePath,
idleMinutes: 240, idleMinutes: 240,
}, },
} as OpenClawConfig; } as OpenClawConfig;
const result = await initSessionState({ const result = await initSessionState({
ctx: { Body: "hello", SessionKey: sessionKey }, ctx: { Body: "hello", SessionKey: sessionKey },
cfg, cfg,
commandAuthorized: true, commandAuthorized: true,
}); });
expect(result.isNewSession).toBe(false); expect(result.isNewSession).toBe(false);
expect(result.sessionId).toBe(existingSessionId); expect(result.sessionId).toBe(existingSessionId);
} finally {
vi.useRealTimers();
}
}); });
}); });