fix: stabilize internal hooks singleton registry (#32292) (thanks @Drickon)

This commit is contained in:
Peter Steinberger
2026-03-03 00:26:58 +00:00
parent d0a3743abd
commit 68e982ec80
2 changed files with 20 additions and 0 deletions

View File

@@ -142,6 +142,25 @@ describe("hooks", () => {
const event = createInternalHookEvent("command", "new", "test-session");
await expect(triggerInternalHook(event)).resolves.not.toThrow();
});
it("stores handlers in the global singleton registry", async () => {
const globalHooks = globalThis as typeof globalThis & {
__openclaw_internal_hook_handlers__?: Map<string, Array<(event: unknown) => unknown>>;
};
const handler = vi.fn();
registerInternalHook("command:new", handler);
const event = createInternalHookEvent("command", "new", "test-session");
await triggerInternalHook(event);
expect(handler).toHaveBeenCalledWith(event);
expect(globalHooks.__openclaw_internal_hook_handlers__?.has("command:new")).toBe(true);
const injectedHandler = vi.fn();
globalHooks.__openclaw_internal_hook_handlers__?.set("command:new", [injectedHandler]);
await triggerInternalHook(event);
expect(injectedHandler).toHaveBeenCalledWith(event);
});
});
describe("createInternalHookEvent", () => {