Files
openclaw/src/plugins/hook-runner-global.test.ts
2026-05-08 17:06:54 +01:00

72 lines
2.4 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { createMockPluginRegistry } from "./hooks.test-helpers.js";
async function importHookRunnerGlobalModule() {
return import("./hook-runner-global.js");
}
type HookRunnerGlobalModule = Awaited<ReturnType<typeof importHookRunnerGlobalModule>>;
type HookRunner = NonNullable<ReturnType<HookRunnerGlobalModule["getGlobalHookRunner"]>>;
function expectGlobalHookRunner(
runner: ReturnType<HookRunnerGlobalModule["getGlobalHookRunner"]>,
): HookRunner {
expect(runner).toEqual(expect.objectContaining({ hasHooks: expect.any(Function) }));
if (runner === null) {
throw new Error("Expected global hook runner");
}
return runner;
}
async function expectGlobalRunnerState(expected: { hasRunner: boolean; registry?: unknown }) {
const mod = await importHookRunnerGlobalModule();
expect(mod.getGlobalHookRunner() === null).toBe(!expected.hasRunner);
if ("registry" in expected) {
expect(mod.getGlobalPluginRegistry()).toBe(expected.registry ?? null);
}
return mod;
}
afterEach(async () => {
const mod = await importHookRunnerGlobalModule();
mod.resetGlobalHookRunner();
});
describe("hook-runner-global", () => {
async function createInitializedModule() {
const modA = await importHookRunnerGlobalModule();
const registry = createMockPluginRegistry([{ hookName: "message_received", handler: vi.fn() }]);
modA.initializeGlobalHookRunner(registry);
return { modA, registry };
}
it("preserves the initialized runner across module reloads", async () => {
const { modA, registry } = await createInitializedModule();
expect(expectGlobalHookRunner(modA.getGlobalHookRunner()).hasHooks("message_received")).toBe(
true,
);
vi.resetModules();
const modB = await expectGlobalRunnerState({ hasRunner: true, registry });
expect(expectGlobalHookRunner(modB.getGlobalHookRunner()).hasHooks("message_received")).toBe(
true,
);
});
it("clears the shared state across module reloads", async () => {
await createInitializedModule();
vi.resetModules();
const modB = await expectGlobalRunnerState({ hasRunner: true });
modB.resetGlobalHookRunner();
expect(modB.getGlobalHookRunner()).toBeNull();
expect(modB.getGlobalPluginRegistry()).toBeNull();
vi.resetModules();
await expectGlobalRunnerState({ hasRunner: false });
});
});