Files
openclaw/src/plugins/hooks.correlation.test.ts
Vincent Koc 3bd2ee78b6 feat(plugins): expose hook correlation fields
Expose first-class hook correlation fields for plugin message and run lifecycle hooks, including frozen diagnostic trace copies for plugin-facing events.
2026-04-24 11:37:34 -07:00

56 lines
1.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { createHookRunner } from "./hooks.js";
import { addTestHook, TEST_PLUGIN_AGENT_CTX } from "./hooks.test-helpers.js";
import { createEmptyPluginRegistry, type PluginRegistry } from "./registry.js";
import type { PluginHookRegistration } from "./types.js";
describe("hook correlation fields", () => {
let registry: PluginRegistry;
beforeEach(() => {
registry = createEmptyPluginRegistry();
});
it("adds runId to legacy before_agent_start events from hook context", async () => {
const handler = vi.fn(() => undefined);
addTestHook({
registry,
pluginId: "plugin-a",
hookName: "before_agent_start",
handler: handler as PluginHookRegistration["handler"],
});
const runner = createHookRunner(registry);
await runner.runBeforeAgentStart({ prompt: "hello" }, TEST_PLUGIN_AGENT_CTX);
expect(handler).toHaveBeenCalledWith(
expect.objectContaining({ prompt: "hello", runId: "test-run-id" }),
TEST_PLUGIN_AGENT_CTX,
);
});
it("adds runId to agent_end events from hook context", async () => {
const handler = vi.fn(() => undefined);
addTestHook({
registry,
pluginId: "plugin-a",
hookName: "agent_end",
handler: handler as PluginHookRegistration["handler"],
});
const runner = createHookRunner(registry);
await runner.runAgentEnd(
{
messages: [],
success: true,
},
TEST_PLUGIN_AGENT_CTX,
);
expect(handler).toHaveBeenCalledWith(
expect.objectContaining({ messages: [], success: true, runId: "test-run-id" }),
TEST_PLUGIN_AGENT_CTX,
);
});
});