diff --git a/src/auto-reply/reply/session.test.ts b/src/auto-reply/reply/session.test.ts index ba239f610..b12156037 100644 --- a/src/auto-reply/reply/session.test.ts +++ b/src/auto-reply/reply/session.test.ts @@ -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 { 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,240 +258,213 @@ 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"; - const existingSessionId = "daily-session-id"; + const root = await makeCaseDir("openclaw-reset-daily-"); + const storePath = path.join(root, "sessions.json"); + const sessionKey = "agent:main:whatsapp:dm:s1"; + const existingSessionId = "daily-session-id"; - await saveSessionStore(storePath, { - [sessionKey]: { - sessionId: existingSessionId, - updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), - }, - }); + await saveSessionStore(storePath, { + [sessionKey]: { + sessionId: existingSessionId, + updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), + }, + }); - const cfg = { session: { store: storePath } } as OpenClawConfig; - const result = await initSessionState({ - ctx: { Body: "hello", SessionKey: sessionKey }, - cfg, - commandAuthorized: true, - }); + const cfg = { session: { store: storePath } } as OpenClawConfig; + const result = await initSessionState({ + ctx: { Body: "hello", SessionKey: sessionKey }, + cfg, + commandAuthorized: true, + }); - expect(result.isNewSession).toBe(true); - expect(result.sessionId).not.toBe(existingSessionId); - } finally { - vi.useRealTimers(); - } + expect(result.isNewSession).toBe(true); + expect(result.sessionId).not.toBe(existingSessionId); }); 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"; - const existingSessionId = "daily-edge-session"; + const root = await makeCaseDir("openclaw-reset-daily-edge-"); + const storePath = path.join(root, "sessions.json"); + const sessionKey = "agent:main:whatsapp:dm:s-edge"; + const existingSessionId = "daily-edge-session"; - await saveSessionStore(storePath, { - [sessionKey]: { - sessionId: existingSessionId, - updatedAt: new Date(2026, 0, 17, 3, 30, 0).getTime(), - }, - }); + await saveSessionStore(storePath, { + [sessionKey]: { + sessionId: existingSessionId, + updatedAt: new Date(2026, 0, 17, 3, 30, 0).getTime(), + }, + }); - const cfg = { session: { store: storePath } } as OpenClawConfig; - const result = await initSessionState({ - ctx: { Body: "hello", SessionKey: sessionKey }, - cfg, - commandAuthorized: true, - }); + const cfg = { session: { store: storePath } } as OpenClawConfig; + const result = await initSessionState({ + ctx: { Body: "hello", SessionKey: sessionKey }, + cfg, + commandAuthorized: true, + }); - expect(result.isNewSession).toBe(true); - expect(result.sessionId).not.toBe(existingSessionId); - } finally { - vi.useRealTimers(); - } + expect(result.isNewSession).toBe(true); + expect(result.sessionId).not.toBe(existingSessionId); }); 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"; - const existingSessionId = "idle-session-id"; + const root = await makeCaseDir("openclaw-reset-idle-"); + const storePath = path.join(root, "sessions.json"); + const sessionKey = "agent:main:whatsapp:dm:s2"; + const existingSessionId = "idle-session-id"; - await saveSessionStore(storePath, { - [sessionKey]: { - sessionId: existingSessionId, - updatedAt: new Date(2026, 0, 18, 4, 45, 0).getTime(), - }, - }); + await saveSessionStore(storePath, { + [sessionKey]: { + sessionId: existingSessionId, + updatedAt: new Date(2026, 0, 18, 4, 45, 0).getTime(), + }, + }); - const cfg = { - session: { - store: storePath, - reset: { mode: "daily", atHour: 4, idleMinutes: 30 }, - }, - } as OpenClawConfig; - const result = await initSessionState({ - ctx: { Body: "hello", SessionKey: sessionKey }, - cfg, - commandAuthorized: true, - }); + const cfg = { + session: { + store: storePath, + reset: { mode: "daily", atHour: 4, idleMinutes: 30 }, + }, + } as OpenClawConfig; + const result = await initSessionState({ + ctx: { Body: "hello", SessionKey: sessionKey }, + cfg, + commandAuthorized: true, + }); - expect(result.isNewSession).toBe(true); - expect(result.sessionId).not.toBe(existingSessionId); - } finally { - vi.useRealTimers(); - } + expect(result.isNewSession).toBe(true); + expect(result.sessionId).not.toBe(existingSessionId); }); 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"; - const existingSessionId = "thread-session-id"; + const root = await makeCaseDir("openclaw-reset-thread-"); + const storePath = path.join(root, "sessions.json"); + const sessionKey = "agent:main:slack:channel:c1:thread:123"; + const existingSessionId = "thread-session-id"; - await saveSessionStore(storePath, { - [sessionKey]: { - sessionId: existingSessionId, - updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), - }, - }); + await saveSessionStore(storePath, { + [sessionKey]: { + sessionId: existingSessionId, + updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), + }, + }); - const cfg = { - session: { - store: storePath, - reset: { mode: "daily", atHour: 4 }, - resetByType: { thread: { mode: "idle", idleMinutes: 180 } }, - }, - } as OpenClawConfig; - const result = await initSessionState({ - ctx: { Body: "reply", SessionKey: sessionKey, ThreadLabel: "Slack thread" }, - cfg, - commandAuthorized: true, - }); + const cfg = { + session: { + store: storePath, + reset: { mode: "daily", atHour: 4 }, + resetByType: { thread: { mode: "idle", idleMinutes: 180 } }, + }, + } as OpenClawConfig; + const result = await initSessionState({ + ctx: { Body: "reply", SessionKey: sessionKey, ThreadLabel: "Slack thread" }, + cfg, + commandAuthorized: true, + }); - expect(result.isNewSession).toBe(false); - expect(result.sessionId).toBe(existingSessionId); - } finally { - vi.useRealTimers(); - } + expect(result.isNewSession).toBe(false); + expect(result.sessionId).toBe(existingSessionId); }); 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"; - const existingSessionId = "thread-nosuffix"; + const root = await makeCaseDir("openclaw-reset-thread-nosuffix-"); + const storePath = path.join(root, "sessions.json"); + const sessionKey = "agent:main:discord:channel:c1"; + const existingSessionId = "thread-nosuffix"; - await saveSessionStore(storePath, { - [sessionKey]: { - sessionId: existingSessionId, - updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), - }, - }); + await saveSessionStore(storePath, { + [sessionKey]: { + sessionId: existingSessionId, + updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), + }, + }); - const cfg = { - session: { - store: storePath, - resetByType: { thread: { mode: "idle", idleMinutes: 180 } }, - }, - } as OpenClawConfig; - const result = await initSessionState({ - ctx: { Body: "reply", SessionKey: sessionKey, ThreadLabel: "Discord thread" }, - cfg, - commandAuthorized: true, - }); + const cfg = { + session: { + store: storePath, + resetByType: { thread: { mode: "idle", idleMinutes: 180 } }, + }, + } as OpenClawConfig; + const result = await initSessionState({ + ctx: { Body: "reply", SessionKey: sessionKey, ThreadLabel: "Discord thread" }, + cfg, + commandAuthorized: true, + }); - expect(result.isNewSession).toBe(false); - expect(result.sessionId).toBe(existingSessionId); - } finally { - vi.useRealTimers(); - } + expect(result.isNewSession).toBe(false); + expect(result.sessionId).toBe(existingSessionId); }); 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"; - const existingSessionId = "type-default-session"; + const root = await makeCaseDir("openclaw-reset-type-default-"); + const storePath = path.join(root, "sessions.json"); + const sessionKey = "agent:main:whatsapp:dm:s4"; + const existingSessionId = "type-default-session"; - await saveSessionStore(storePath, { - [sessionKey]: { - sessionId: existingSessionId, - updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), - }, - }); + await saveSessionStore(storePath, { + [sessionKey]: { + sessionId: existingSessionId, + updatedAt: new Date(2026, 0, 18, 3, 0, 0).getTime(), + }, + }); - const cfg = { - session: { - store: storePath, - resetByType: { thread: { mode: "idle", idleMinutes: 60 } }, - }, - } as OpenClawConfig; - const result = await initSessionState({ - ctx: { Body: "hello", SessionKey: sessionKey }, - cfg, - commandAuthorized: true, - }); + const cfg = { + session: { + store: storePath, + resetByType: { thread: { mode: "idle", idleMinutes: 60 } }, + }, + } as OpenClawConfig; + const result = await initSessionState({ + ctx: { Body: "hello", SessionKey: sessionKey }, + cfg, + commandAuthorized: true, + }); - expect(result.isNewSession).toBe(true); - expect(result.sessionId).not.toBe(existingSessionId); - } finally { - vi.useRealTimers(); - } + expect(result.isNewSession).toBe(true); + expect(result.sessionId).not.toBe(existingSessionId); }); 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"; - const existingSessionId = "legacy-session-id"; + const root = await makeCaseDir("openclaw-reset-legacy-"); + const storePath = path.join(root, "sessions.json"); + const sessionKey = "agent:main:whatsapp:dm:s3"; + const existingSessionId = "legacy-session-id"; - await saveSessionStore(storePath, { - [sessionKey]: { - sessionId: existingSessionId, - updatedAt: new Date(2026, 0, 18, 3, 30, 0).getTime(), - }, - }); + await saveSessionStore(storePath, { + [sessionKey]: { + sessionId: existingSessionId, + updatedAt: new Date(2026, 0, 18, 3, 30, 0).getTime(), + }, + }); - const cfg = { - session: { - store: storePath, - idleMinutes: 240, - }, - } as OpenClawConfig; - const result = await initSessionState({ - ctx: { Body: "hello", SessionKey: sessionKey }, - cfg, - commandAuthorized: true, - }); + const cfg = { + session: { + store: storePath, + idleMinutes: 240, + }, + } as OpenClawConfig; + const result = await initSessionState({ + ctx: { Body: "hello", SessionKey: sessionKey }, + cfg, + commandAuthorized: true, + }); - expect(result.isNewSession).toBe(false); - expect(result.sessionId).toBe(existingSessionId); - } finally { - vi.useRealTimers(); - } + expect(result.isNewSession).toBe(false); + expect(result.sessionId).toBe(existingSessionId); }); });