|
| 1 | +import { describe, test, expect, beforeEach } from "bun:test"; |
| 2 | +import { EventBus, getEventBus, removeEventBus, getAllEventBuses } from "../transport/event-bus"; |
| 3 | + |
| 4 | +describe("EventBus", () => { |
| 5 | + let bus: EventBus; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + bus = new EventBus(); |
| 9 | + }); |
| 10 | + |
| 11 | + describe("publish", () => { |
| 12 | + test("publishes event with seqNum starting at 1", () => { |
| 13 | + const event = bus.publish({ |
| 14 | + id: "e1", |
| 15 | + sessionId: "s1", |
| 16 | + type: "user", |
| 17 | + payload: { content: "hello" }, |
| 18 | + direction: "outbound", |
| 19 | + }); |
| 20 | + expect(event.seqNum).toBe(1); |
| 21 | + expect(event.createdAt).toBeGreaterThan(0); |
| 22 | + }); |
| 23 | + |
| 24 | + test("increments seqNum on each publish", () => { |
| 25 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }); |
| 26 | + bus.publish({ id: "e2", sessionId: "s1", type: "assistant", payload: {}, direction: "inbound" }); |
| 27 | + const event = bus.publish({ id: "e3", sessionId: "s1", type: "result", payload: {}, direction: "inbound" }); |
| 28 | + expect(event.seqNum).toBe(3); |
| 29 | + }); |
| 30 | + |
| 31 | + test("throws when publishing to a closed bus", () => { |
| 32 | + bus.close(); |
| 33 | + expect(() => |
| 34 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }), |
| 35 | + ).toThrow("EventBus is closed"); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("subscribe", () => { |
| 40 | + test("receives published events", () => { |
| 41 | + const received: unknown[] = []; |
| 42 | + bus.subscribe((event) => received.push(event)); |
| 43 | + |
| 44 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: { content: "hi" }, direction: "outbound" }); |
| 45 | + expect(received).toHaveLength(1); |
| 46 | + expect((received[0] as any).payload).toEqual({ content: "hi" }); |
| 47 | + }); |
| 48 | + |
| 49 | + test("unsubscribe stops receiving events", () => { |
| 50 | + const received: unknown[] = []; |
| 51 | + const unsub = bus.subscribe((event) => received.push(event)); |
| 52 | + unsub(); |
| 53 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }); |
| 54 | + expect(received).toHaveLength(0); |
| 55 | + }); |
| 56 | + |
| 57 | + test("multiple subscribers all receive events", () => { |
| 58 | + const r1: unknown[] = []; |
| 59 | + const r2: unknown[] = []; |
| 60 | + bus.subscribe((e) => r1.push(e)); |
| 61 | + bus.subscribe((e) => r2.push(e)); |
| 62 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }); |
| 63 | + expect(r1).toHaveLength(1); |
| 64 | + expect(r2).toHaveLength(1); |
| 65 | + }); |
| 66 | + |
| 67 | + test("subscriber error does not affect other subscribers", () => { |
| 68 | + const received: unknown[] = []; |
| 69 | + bus.subscribe(() => { |
| 70 | + throw new Error("boom"); |
| 71 | + }); |
| 72 | + bus.subscribe((e) => received.push(e)); |
| 73 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }); |
| 74 | + expect(received).toHaveLength(1); |
| 75 | + }); |
| 76 | + |
| 77 | + test("subscriberCount", () => { |
| 78 | + expect(bus.subscriberCount()).toBe(0); |
| 79 | + const unsub1 = bus.subscribe(() => {}); |
| 80 | + expect(bus.subscriberCount()).toBe(1); |
| 81 | + const unsub2 = bus.subscribe(() => {}); |
| 82 | + expect(bus.subscriberCount()).toBe(2); |
| 83 | + unsub1(); |
| 84 | + expect(bus.subscriberCount()).toBe(1); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("getEventsSince", () => { |
| 89 | + test("returns events after given seqNum", () => { |
| 90 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }); |
| 91 | + bus.publish({ id: "e2", sessionId: "s1", type: "assistant", payload: {}, direction: "inbound" }); |
| 92 | + bus.publish({ id: "e3", sessionId: "s1", type: "result", payload: {}, direction: "inbound" }); |
| 93 | + |
| 94 | + const events = bus.getEventsSince(1); |
| 95 | + expect(events).toHaveLength(2); |
| 96 | + expect(events[0].seqNum).toBe(2); |
| 97 | + expect(events[1].seqNum).toBe(3); |
| 98 | + }); |
| 99 | + |
| 100 | + test("returns empty for seqNum beyond last", () => { |
| 101 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }); |
| 102 | + expect(bus.getEventsSince(1)).toHaveLength(0); |
| 103 | + }); |
| 104 | + |
| 105 | + test("returns all events when seqNum is 0", () => { |
| 106 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }); |
| 107 | + bus.publish({ id: "e2", sessionId: "s1", type: "assistant", payload: {}, direction: "inbound" }); |
| 108 | + expect(bus.getEventsSince(0)).toHaveLength(2); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("getLastSeqNum", () => { |
| 113 | + test("returns 0 for empty bus", () => { |
| 114 | + expect(bus.getLastSeqNum()).toBe(0); |
| 115 | + }); |
| 116 | + |
| 117 | + test("returns last seqNum after publishes", () => { |
| 118 | + bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }); |
| 119 | + bus.publish({ id: "e2", sessionId: "s1", type: "user", payload: {}, direction: "outbound" }); |
| 120 | + expect(bus.getLastSeqNum()).toBe(2); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe("close", () => { |
| 125 | + test("clears subscribers and prevents publishing", () => { |
| 126 | + bus.subscribe(() => {}); |
| 127 | + bus.close(); |
| 128 | + expect(bus.subscriberCount()).toBe(0); |
| 129 | + expect(() => bus.publish({ id: "e1", sessionId: "s1", type: "user", payload: {}, direction: "outbound" })).toThrow(); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe("EventBus registry", () => { |
| 135 | + beforeEach(() => { |
| 136 | + // Clean up global registry |
| 137 | + for (const [key] of getAllEventBuses()) { |
| 138 | + removeEventBus(key); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + describe("getEventBus", () => { |
| 143 | + test("creates new bus for unknown session", () => { |
| 144 | + const bus = getEventBus("s1"); |
| 145 | + expect(bus).toBeInstanceOf(EventBus); |
| 146 | + expect(getAllEventBuses().has("s1")).toBe(true); |
| 147 | + }); |
| 148 | + |
| 149 | + test("returns same bus for same session", () => { |
| 150 | + const bus1 = getEventBus("s1"); |
| 151 | + const bus2 = getEventBus("s1"); |
| 152 | + expect(bus1).toBe(bus2); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe("removeEventBus", () => { |
| 157 | + test("removes and closes bus", () => { |
| 158 | + const bus = getEventBus("s2"); |
| 159 | + removeEventBus("s2"); |
| 160 | + expect(getAllEventBuses().has("s2")).toBe(false); |
| 161 | + expect(() => bus.publish({ id: "e1", sessionId: "s2", type: "user", payload: {}, direction: "outbound" })).toThrow(); |
| 162 | + }); |
| 163 | + |
| 164 | + test("no-op for non-existent bus", () => { |
| 165 | + expect(() => removeEventBus("nonexistent")).not.toThrow(); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + describe("getAllEventBuses", () => { |
| 170 | + test("returns all registered buses", () => { |
| 171 | + getEventBus("a"); |
| 172 | + getEventBus("b"); |
| 173 | + expect(getAllEventBuses().size).toBeGreaterThanOrEqual(2); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments