|
| 1 | +import type { TracedEvent } from './types' |
| 2 | + |
| 3 | +import { useLogg } from '@guiiai/logg' |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +import { createEventBus } from './index' |
| 7 | + |
| 8 | +describe('eventBus', () => { |
| 9 | + const createTestBus = () => |
| 10 | + createEventBus({ |
| 11 | + logger: useLogg('test'), |
| 12 | + config: { historySize: 100 }, |
| 13 | + }) |
| 14 | + |
| 15 | + describe('emit', () => { |
| 16 | + it('should create an event with auto-generated id and timestamp', () => { |
| 17 | + const bus = createTestBus() |
| 18 | + |
| 19 | + const event = bus.emit({ |
| 20 | + type: 'test:event', |
| 21 | + payload: { foo: 'bar' }, |
| 22 | + traceId: 'trace-1', |
| 23 | + source: { component: 'test' }, |
| 24 | + }) |
| 25 | + |
| 26 | + expect(event.id).toBeDefined() |
| 27 | + expect(event.id.length).toBe(12) |
| 28 | + expect(event.traceId).toBe('trace-1') |
| 29 | + expect(event.type).toBe('test:event') |
| 30 | + expect(event.payload).toEqual({ foo: 'bar' }) |
| 31 | + expect(event.timestamp).toBeGreaterThan(0) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should generate traceId if not provided', () => { |
| 35 | + const bus = createTestBus() |
| 36 | + |
| 37 | + const event = bus.emit({ |
| 38 | + type: 'test:event', |
| 39 | + payload: {}, |
| 40 | + source: { component: 'test' }, |
| 41 | + }) |
| 42 | + |
| 43 | + expect(event.traceId).toBeDefined() |
| 44 | + expect(event.traceId.length).toBe(16) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should freeze the event (immutable)', () => { |
| 48 | + const bus = createTestBus() |
| 49 | + |
| 50 | + const event = bus.emit({ |
| 51 | + type: 'test:event', |
| 52 | + payload: { mutable: 'data' }, |
| 53 | + source: { component: 'test' }, |
| 54 | + }) |
| 55 | + |
| 56 | + expect(Object.isFrozen(event)).toBe(true) |
| 57 | + expect(Object.isFrozen(event.payload)).toBe(true) |
| 58 | + expect(Object.isFrozen(event.source)).toBe(true) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should deep freeze nested objects in payload', () => { |
| 62 | + const bus = createTestBus() |
| 63 | + |
| 64 | + const event = bus.emit({ |
| 65 | + type: 'test:event', |
| 66 | + payload: { |
| 67 | + level1: { |
| 68 | + level2: { |
| 69 | + value: 42, |
| 70 | + }, |
| 71 | + }, |
| 72 | + array: [{ item: 1 }, { item: 2 }], |
| 73 | + }, |
| 74 | + source: { component: 'test' }, |
| 75 | + }) |
| 76 | + |
| 77 | + expect(Object.isFrozen(event.payload)).toBe(true) |
| 78 | + expect(Object.isFrozen((event.payload as any).level1)).toBe(true) |
| 79 | + expect(Object.isFrozen((event.payload as any).level1.level2)).toBe(true) |
| 80 | + expect(Object.isFrozen((event.payload as any).array)).toBe(true) |
| 81 | + expect(Object.isFrozen((event.payload as any).array[0])).toBe(true) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + describe('emitChild', () => { |
| 86 | + it('should inherit traceId and set parentId', () => { |
| 87 | + const bus = createTestBus() |
| 88 | + |
| 89 | + const parent = bus.emit({ |
| 90 | + type: 'parent:event', |
| 91 | + payload: {}, |
| 92 | + source: { component: 'test' }, |
| 93 | + }) |
| 94 | + |
| 95 | + const child = bus.emitChild(parent, { |
| 96 | + type: 'child:event', |
| 97 | + payload: { derived: true }, |
| 98 | + source: { component: 'test' }, |
| 99 | + }) |
| 100 | + |
| 101 | + expect(child.traceId).toBe(parent.traceId) |
| 102 | + expect(child.parentId).toBe(parent.id) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + describe('subscribe', () => { |
| 107 | + it('should call handler for matching events', () => { |
| 108 | + const bus = createTestBus() |
| 109 | + const handler = vi.fn() |
| 110 | + |
| 111 | + bus.subscribe('test:event', handler) |
| 112 | + bus.emit({ |
| 113 | + type: 'test:event', |
| 114 | + payload: { data: 123 }, |
| 115 | + source: { component: 'test' }, |
| 116 | + }) |
| 117 | + |
| 118 | + expect(handler).toHaveBeenCalledTimes(1) |
| 119 | + expect(handler.mock.calls[0][0].payload).toEqual({ data: 123 }) |
| 120 | + }) |
| 121 | + |
| 122 | + it('should support wildcard patterns', () => { |
| 123 | + const bus = createTestBus() |
| 124 | + const handler = vi.fn() |
| 125 | + |
| 126 | + bus.subscribe('raw:*', handler) |
| 127 | + |
| 128 | + bus.emit({ |
| 129 | + type: 'raw:sighted:punch', |
| 130 | + payload: {}, |
| 131 | + source: { component: 'test' }, |
| 132 | + }) |
| 133 | + bus.emit({ |
| 134 | + type: 'raw:heard:sound', |
| 135 | + payload: {}, |
| 136 | + source: { component: 'test' }, |
| 137 | + }) |
| 138 | + bus.emit({ |
| 139 | + type: 'signal:attention', |
| 140 | + payload: {}, |
| 141 | + source: { component: 'test' }, |
| 142 | + }) |
| 143 | + |
| 144 | + expect(handler).toHaveBeenCalledTimes(2) |
| 145 | + }) |
| 146 | + |
| 147 | + it('should return unsubscribe function', () => { |
| 148 | + const bus = createTestBus() |
| 149 | + const handler = vi.fn() |
| 150 | + |
| 151 | + const unsub = bus.subscribe('test:*', handler) |
| 152 | + |
| 153 | + bus.emit({ |
| 154 | + type: 'test:one', |
| 155 | + payload: {}, |
| 156 | + source: { component: 'test' }, |
| 157 | + }) |
| 158 | + expect(handler).toHaveBeenCalledTimes(1) |
| 159 | + |
| 160 | + unsub() |
| 161 | + |
| 162 | + bus.emit({ |
| 163 | + type: 'test:two', |
| 164 | + payload: {}, |
| 165 | + source: { component: 'test' }, |
| 166 | + }) |
| 167 | + expect(handler).toHaveBeenCalledTimes(1) // Still 1 |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + describe('trace context propagation', () => { |
| 172 | + it('should propagate trace context in handlers', () => { |
| 173 | + const bus = createTestBus() |
| 174 | + let childEvent: TracedEvent | undefined |
| 175 | + |
| 176 | + bus.subscribe('parent:event', () => { |
| 177 | + // Emit within handler - should inherit context |
| 178 | + childEvent = bus.emit({ |
| 179 | + type: 'child:event', |
| 180 | + payload: {}, |
| 181 | + source: { component: 'handler' }, |
| 182 | + }) |
| 183 | + }) |
| 184 | + |
| 185 | + const parent = bus.emit({ |
| 186 | + type: 'parent:event', |
| 187 | + payload: {}, |
| 188 | + source: { component: 'test' }, |
| 189 | + }) |
| 190 | + |
| 191 | + expect(childEvent).toBeDefined() |
| 192 | + expect(childEvent!.traceId).toBe(parent.traceId) |
| 193 | + expect(childEvent!.parentId).toBe(parent.id) |
| 194 | + }) |
| 195 | + }) |
| 196 | + |
| 197 | + describe('history', () => { |
| 198 | + it('should store events in history', () => { |
| 199 | + const bus = createTestBus() |
| 200 | + |
| 201 | + bus.emit({ type: 'e1', payload: {}, source: { component: 'test' } }) |
| 202 | + bus.emit({ type: 'e2', payload: {}, source: { component: 'test' } }) |
| 203 | + bus.emit({ type: 'e3', payload: {}, source: { component: 'test' } }) |
| 204 | + |
| 205 | + const history = bus.getHistory() |
| 206 | + expect(history.length).toBe(3) |
| 207 | + expect(history[0].type).toBe('e1') |
| 208 | + expect(history[2].type).toBe('e3') |
| 209 | + }) |
| 210 | + |
| 211 | + it('should respect historySize limit (ring buffer)', () => { |
| 212 | + const bus = createEventBus({ |
| 213 | + logger: useLogg('test'), |
| 214 | + config: { historySize: 3 }, |
| 215 | + }) |
| 216 | + |
| 217 | + bus.emit({ type: 'e1', payload: {}, source: { component: 'test' } }) |
| 218 | + bus.emit({ type: 'e2', payload: {}, source: { component: 'test' } }) |
| 219 | + bus.emit({ type: 'e3', payload: {}, source: { component: 'test' } }) |
| 220 | + bus.emit({ type: 'e4', payload: {}, source: { component: 'test' } }) |
| 221 | + |
| 222 | + const history = bus.getHistory() |
| 223 | + expect(history.length).toBe(3) |
| 224 | + // Oldest event (e1) should be evicted |
| 225 | + expect(history.map(e => e.type)).toEqual(['e2', 'e3', 'e4']) |
| 226 | + }) |
| 227 | + }) |
| 228 | + |
| 229 | + describe('getEventsByTrace', () => { |
| 230 | + it('should filter events by traceId', () => { |
| 231 | + const bus = createTestBus() |
| 232 | + |
| 233 | + const e1 = bus.emit({ type: 'a', payload: {}, source: { component: 'test' } }) |
| 234 | + bus.emitChild(e1, { type: 'b', payload: {}, source: { component: 'test' } }) |
| 235 | + bus.emit({ type: 'c', payload: {}, source: { component: 'test' } }) // Different trace |
| 236 | + |
| 237 | + const trace = bus.getEventsByTrace(e1.traceId) |
| 238 | + expect(trace.length).toBe(2) |
| 239 | + expect(trace.map(e => e.type)).toEqual(['a', 'b']) |
| 240 | + }) |
| 241 | + }) |
| 242 | +}) |
0 commit comments