|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import type { OpenClawConfig } from "../../config/config.js"; |
| 6 | +import type { SessionEntry } from "../../config/sessions.js"; |
| 7 | +import type { HookRunner } from "../../plugins/hooks.js"; |
| 8 | + |
| 9 | +const hookRunnerMocks = vi.hoisted(() => ({ |
| 10 | + hasHooks: vi.fn<HookRunner["hasHooks"]>(), |
| 11 | + runSessionStart: vi.fn<HookRunner["runSessionStart"]>(), |
| 12 | + runSessionEnd: vi.fn<HookRunner["runSessionEnd"]>(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../../plugins/hook-runner-global.js", () => ({ |
| 16 | + getGlobalHookRunner: () => |
| 17 | + ({ |
| 18 | + hasHooks: hookRunnerMocks.hasHooks, |
| 19 | + runSessionStart: hookRunnerMocks.runSessionStart, |
| 20 | + runSessionEnd: hookRunnerMocks.runSessionEnd, |
| 21 | + }) as unknown as HookRunner, |
| 22 | +})); |
| 23 | + |
| 24 | +const { initSessionState } = await import("./session.js"); |
| 25 | + |
| 26 | +async function createStorePath(prefix: string): Promise<string> { |
| 27 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), `${prefix}-`)); |
| 28 | + return path.join(root, "sessions.json"); |
| 29 | +} |
| 30 | + |
| 31 | +async function writeStore( |
| 32 | + storePath: string, |
| 33 | + store: Record<string, SessionEntry | Record<string, unknown>>, |
| 34 | +): Promise<void> { |
| 35 | + await fs.mkdir(path.dirname(storePath), { recursive: true }); |
| 36 | + await fs.writeFile(storePath, JSON.stringify(store), "utf-8"); |
| 37 | +} |
| 38 | + |
| 39 | +describe("session hook context wiring", () => { |
| 40 | + beforeEach(() => { |
| 41 | + hookRunnerMocks.hasHooks.mockReset(); |
| 42 | + hookRunnerMocks.runSessionStart.mockReset(); |
| 43 | + hookRunnerMocks.runSessionEnd.mockReset(); |
| 44 | + hookRunnerMocks.runSessionStart.mockResolvedValue(undefined); |
| 45 | + hookRunnerMocks.runSessionEnd.mockResolvedValue(undefined); |
| 46 | + hookRunnerMocks.hasHooks.mockImplementation( |
| 47 | + (hookName) => hookName === "session_start" || hookName === "session_end", |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + vi.restoreAllMocks(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("passes sessionKey to session_start hook context", async () => { |
| 56 | + const sessionKey = "agent:main:telegram:direct:123"; |
| 57 | + const storePath = await createStorePath("openclaw-session-hook-start"); |
| 58 | + await writeStore(storePath, {}); |
| 59 | + const cfg = { session: { store: storePath } } as OpenClawConfig; |
| 60 | + |
| 61 | + await initSessionState({ |
| 62 | + ctx: { Body: "hello", SessionKey: sessionKey }, |
| 63 | + cfg, |
| 64 | + commandAuthorized: true, |
| 65 | + }); |
| 66 | + |
| 67 | + await vi.waitFor(() => expect(hookRunnerMocks.runSessionStart).toHaveBeenCalledTimes(1)); |
| 68 | + const [event, context] = hookRunnerMocks.runSessionStart.mock.calls[0] ?? []; |
| 69 | + expect(event).toMatchObject({ sessionKey }); |
| 70 | + expect(context).toMatchObject({ sessionKey }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("passes sessionKey to session_end hook context on reset", async () => { |
| 74 | + const sessionKey = "agent:main:telegram:direct:123"; |
| 75 | + const storePath = await createStorePath("openclaw-session-hook-end"); |
| 76 | + await writeStore(storePath, { |
| 77 | + [sessionKey]: { |
| 78 | + sessionId: "old-session", |
| 79 | + updatedAt: Date.now(), |
| 80 | + }, |
| 81 | + }); |
| 82 | + const cfg = { session: { store: storePath } } as OpenClawConfig; |
| 83 | + |
| 84 | + await initSessionState({ |
| 85 | + ctx: { Body: "/new", SessionKey: sessionKey }, |
| 86 | + cfg, |
| 87 | + commandAuthorized: true, |
| 88 | + }); |
| 89 | + |
| 90 | + await vi.waitFor(() => expect(hookRunnerMocks.runSessionEnd).toHaveBeenCalledTimes(1)); |
| 91 | + const [event, context] = hookRunnerMocks.runSessionEnd.mock.calls[0] ?? []; |
| 92 | + expect(event).toMatchObject({ sessionKey }); |
| 93 | + expect(context).toMatchObject({ sessionKey }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments