diff --git a/src/hooks/background-notification/hook.test.ts b/src/hooks/background-notification/hook.test.ts new file mode 100644 index 000000000..c8566a39d --- /dev/null +++ b/src/hooks/background-notification/hook.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, test, mock } from "bun:test" + +import { createBackgroundNotificationHook } from "./hook" + +describe("createBackgroundNotificationHook", () => { + test("#given unsupported event type #when event handler runs #then it does not forward to manager", async () => { + //#given + const handleEvent = mock(() => {}) + const hook = createBackgroundNotificationHook({ + handleEvent, + injectPendingNotificationsIntoChatMessage: () => {}, + } as never) + + //#when + await hook.event({ event: { type: "message.removed", properties: { sessionID: "ses-1" } } }) + + //#then + expect(handleEvent).not.toHaveBeenCalled() + }) + + test("#given supported event type #when event handler runs #then it forwards to manager", async () => { + //#given + const handleEvent = mock(() => {}) + const hook = createBackgroundNotificationHook({ + handleEvent, + injectPendingNotificationsIntoChatMessage: () => {}, + } as never) + + const event = { type: "message.part.delta", properties: { sessionID: "ses-1", field: "text", delta: "x" } } + + //#when + await hook.event({ event }) + + //#then + expect(handleEvent).toHaveBeenCalledWith(event) + }) +}) diff --git a/src/hooks/background-notification/hook.ts b/src/hooks/background-notification/hook.ts index 3f40ffadb..e52b7beb4 100644 --- a/src/hooks/background-notification/hook.ts +++ b/src/hooks/background-notification/hook.ts @@ -17,6 +17,16 @@ interface ChatMessageOutput { parts: Array<{ type: string; text?: string; [key: string]: unknown }> } +const FORWARDED_EVENT_TYPES = new Set([ + "message.updated", + "message.part.updated", + "message.part.delta", + "session.idle", + "session.error", + "session.deleted", + "session.status", +]) + /** * Background notification hook - handles event routing to BackgroundManager. * @@ -25,6 +35,7 @@ interface ChatMessageOutput { */ export function createBackgroundNotificationHook(manager: BackgroundManager) { const eventHandler = async ({ event }: EventInput) => { + if (!FORWARDED_EVENT_TYPES.has(event.type)) return manager.handleEvent(event) }