|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { |
| 3 | + CHAT_TASKS_CHANGED_EVENT, |
| 4 | + CHAT_TOOLS_ACTIVATE_EVENT, |
| 5 | +} from "./director.js"; |
| 6 | +import { handleChatDirectorEvent } from "./chat-event-subscribers.js"; |
| 7 | +import type { Task } from "./tasks.js"; |
| 8 | + |
| 9 | +function makeLog() { |
| 10 | + const calls: { message: string; fields?: Record<string, unknown> }[] = []; |
| 11 | + return { |
| 12 | + calls, |
| 13 | + log: (message: string, fields?: Record<string, unknown>): void => { |
| 14 | + calls.push(fields !== undefined ? { message, fields } : { message }); |
| 15 | + }, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +describe("handleChatDirectorEvent", () => { |
| 20 | + test("dispatches a valid tasks-changed payload without logging", () => { |
| 21 | + const seen: Task[][] = []; |
| 22 | + const log = makeLog(); |
| 23 | + const handled = handleChatDirectorEvent( |
| 24 | + { |
| 25 | + type: CHAT_TASKS_CHANGED_EVENT, |
| 26 | + data: { tasks: [{ id: "t1", title: "work", status: "doing" }] }, |
| 27 | + }, |
| 28 | + { |
| 29 | + onTasksChanged: (tasks) => seen.push(tasks), |
| 30 | + onToolsActivate: () => { |
| 31 | + throw new Error("unexpected tools-activate dispatch"); |
| 32 | + }, |
| 33 | + }, |
| 34 | + log.log, |
| 35 | + ); |
| 36 | + expect(handled).toBe(true); |
| 37 | + expect(seen).toEqual([[{ id: "t1", title: "work", status: "doing" }]]); |
| 38 | + expect(log.calls).toEqual([]); |
| 39 | + }); |
| 40 | + |
| 41 | + test("dispatches a valid tools-activate payload without logging", () => { |
| 42 | + const seen: string[][] = []; |
| 43 | + const log = makeLog(); |
| 44 | + const handled = handleChatDirectorEvent( |
| 45 | + { type: CHAT_TOOLS_ACTIVATE_EVENT, data: { names: ["lsp"] } }, |
| 46 | + { |
| 47 | + onTasksChanged: () => { |
| 48 | + throw new Error("unexpected tasks-changed dispatch"); |
| 49 | + }, |
| 50 | + onToolsActivate: (names) => seen.push([...names]), |
| 51 | + }, |
| 52 | + log.log, |
| 53 | + ); |
| 54 | + expect(handled).toBe(true); |
| 55 | + expect(seen).toEqual([["lsp"]]); |
| 56 | + expect(log.calls).toEqual([]); |
| 57 | + }); |
| 58 | + |
| 59 | + test("drops an invalid tasks payload with a debug log naming the failure", () => { |
| 60 | + let dispatched = false; |
| 61 | + const log = makeLog(); |
| 62 | + const handled = handleChatDirectorEvent( |
| 63 | + { type: CHAT_TASKS_CHANGED_EVENT, data: { tasks: "not-a-list" } }, |
| 64 | + { |
| 65 | + onTasksChanged: () => { |
| 66 | + dispatched = true; |
| 67 | + }, |
| 68 | + onToolsActivate: () => { |
| 69 | + dispatched = true; |
| 70 | + }, |
| 71 | + }, |
| 72 | + log.log, |
| 73 | + ); |
| 74 | + expect(handled).toBe(true); |
| 75 | + expect(dispatched).toBe(false); |
| 76 | + expect(log.calls).toHaveLength(1); |
| 77 | + expect(log.calls[0]?.message).toMatch(/tasks-changed/); |
| 78 | + expect(typeof log.calls[0]?.fields?.["error"]).toBe("string"); |
| 79 | + }); |
| 80 | + |
| 81 | + test("drops an invalid tools payload with a debug log naming the failure", () => { |
| 82 | + let dispatched = false; |
| 83 | + const log = makeLog(); |
| 84 | + const handled = handleChatDirectorEvent( |
| 85 | + { type: CHAT_TOOLS_ACTIVATE_EVENT, data: { names: [42] } }, |
| 86 | + { |
| 87 | + onTasksChanged: () => { |
| 88 | + dispatched = true; |
| 89 | + }, |
| 90 | + onToolsActivate: () => { |
| 91 | + dispatched = true; |
| 92 | + }, |
| 93 | + }, |
| 94 | + log.log, |
| 95 | + ); |
| 96 | + expect(handled).toBe(true); |
| 97 | + expect(dispatched).toBe(false); |
| 98 | + expect(log.calls).toHaveLength(1); |
| 99 | + expect(log.calls[0]?.message).toMatch(/tools-activate/); |
| 100 | + expect(typeof log.calls[0]?.fields?.["error"]).toBe("string"); |
| 101 | + }); |
| 102 | + |
| 103 | + test("ignores unrelated events without logging or dispatching", () => { |
| 104 | + let dispatched = false; |
| 105 | + const log = makeLog(); |
| 106 | + const handled = handleChatDirectorEvent( |
| 107 | + { type: "inference.done", data: {} }, |
| 108 | + { |
| 109 | + onTasksChanged: () => { |
| 110 | + dispatched = true; |
| 111 | + }, |
| 112 | + onToolsActivate: () => { |
| 113 | + dispatched = true; |
| 114 | + }, |
| 115 | + }, |
| 116 | + log.log, |
| 117 | + ); |
| 118 | + expect(handled).toBe(false); |
| 119 | + expect(dispatched).toBe(false); |
| 120 | + expect(log.calls).toEqual([]); |
| 121 | + }); |
| 122 | +}); |
0 commit comments