Skip to content

Commit c0715db

Browse files
committed
fix: add session hook context regression tests (openclaw#26394) (thanks @tempeste)
1 parent 20c15cc commit c0715db

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Docs: https://docs.openclaw.ai
1414
- Plugin SDK/channel extensibility: expose `channelRuntime` on `ChannelGatewayContext` so external channel plugins can access shared runtime helpers (reply/routing/session/text/media/commands) without internal imports. (#25462) Thanks @guxiaobo.
1515
- Plugin runtime/events: expose `runtime.events.onAgentEvent` and `runtime.events.onSessionTranscriptUpdate` for extension-side subscriptions, and isolate transcript-listener failures so one faulty listener cannot break the entire update fanout. (#16044) Thanks @scifantastic.
1616
- Plugin runtime/system: expose `runtime.system.requestHeartbeatNow(...)` so extensions can wake targeted sessions immediately after enqueueing system events. (#19464) Thanks @AustinEral.
17+
- Plugin hooks/session lifecycle: include `sessionKey` in `session_start`/`session_end` hook events and contexts so plugins can correlate lifecycle callbacks with routing identity. (#26394) Thanks @tempeste.
1718
- Sessions/Attachments: add inline file attachment support for `sessions_spawn` (subagent runtime only) with base64/utf8 encoding, transcript content redaction, lifecycle cleanup, and configurable limits via `tools.sessions_spawn.attachments`. (#16761) Thanks @napetrov.
1819
- Tools/PDF analysis: add a first-class `pdf` tool with native Anthropic and Google PDF provider support, extraction fallback for non-native models, configurable defaults (`agents.defaults.pdfModel`, `pdfMaxBytesMb`, `pdfMaxPages`), and docs/tests covering routing, validation, and registration. (#31319) Thanks @tyler6204.
1920
- Zalo Personal plugin (`@openclaw/zalouser`): rebuilt channel runtime to use native `zca-js` integration in-process, removing external CLI transport usage and keeping QR/login + send/listen flows fully inside OpenClaw.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)