test: add idempotency tests for initializeGlobalHookRunner (#50025)

This commit is contained in:
Julian Streibel 2026-03-19 15:29:29 +01:00
parent c4a4050ce4
commit 57feef7ba6

View File

@ -46,4 +46,54 @@ describe("hook-runner-global", () => {
expect(modC.getGlobalHookRunner()).toBeNull();
expect(modC.getGlobalPluginRegistry()).toBeNull();
});
it("does not replace an existing runner that has typed hooks", async () => {
const mod = await importHookRunnerGlobalModule();
const registryWithHooks = createMockPluginRegistry([
{ hookName: "llm_input", handler: vi.fn() },
]);
const emptyRegistry = createMockPluginRegistry([]);
mod.initializeGlobalHookRunner(registryWithHooks);
const originalRunner = mod.getGlobalHookRunner();
expect(originalRunner?.hasHooks("llm_input")).toBe(true);
// Second call with empty registry should be a no-op
mod.initializeGlobalHookRunner(emptyRegistry);
expect(mod.getGlobalHookRunner()).toBe(originalRunner);
expect(mod.getGlobalPluginRegistry()).toBe(registryWithHooks);
expect(mod.getGlobalHookRunner()?.hasHooks("llm_input")).toBe(true);
});
it("allows replacing a runner that has no typed hooks", async () => {
const mod = await importHookRunnerGlobalModule();
const emptyRegistry = createMockPluginRegistry([]);
const registryWithHooks = createMockPluginRegistry([
{ hookName: "llm_input", handler: vi.fn() },
]);
mod.initializeGlobalHookRunner(emptyRegistry);
expect(mod.getGlobalHookRunner()?.hasHooks("llm_input")).toBe(false);
// Second call with hooks should replace
mod.initializeGlobalHookRunner(registryWithHooks);
expect(mod.getGlobalHookRunner()?.hasHooks("llm_input")).toBe(true);
expect(mod.getGlobalPluginRegistry()).toBe(registryWithHooks);
});
it("allows re-initialization after reset", async () => {
const mod = await importHookRunnerGlobalModule();
const registryA = createMockPluginRegistry([{ hookName: "llm_input", handler: vi.fn() }]);
const registryB = createMockPluginRegistry([{ hookName: "llm_output", handler: vi.fn() }]);
mod.initializeGlobalHookRunner(registryA);
expect(mod.getGlobalHookRunner()?.hasHooks("llm_input")).toBe(true);
mod.resetGlobalHookRunner();
expect(mod.getGlobalHookRunner()).toBeNull();
mod.initializeGlobalHookRunner(registryB);
expect(mod.getGlobalHookRunner()?.hasHooks("llm_output")).toBe(true);
expect(mod.getGlobalHookRunner()?.hasHooks("llm_input")).toBe(false);
});
});