Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import {
3 aiEventClient,
4 dispatchAIDevtoolsEvent,
5 emitAIDevtoolsEvent,
6} from '../src/index'
7import type { AIDevtoolsEventMap } from '../src/index'
8
9class TestCustomEvent<TDetail = unknown> extends Event {
10 readonly detail: TDetail
11
12 constructor(type: string, init?: CustomEventInit<TDetail>) {
13 super(type)
14 this.detail = init?.detail as TDetail
15 }
16}
17
18describe('AI devtools event emission', () => {
19 const originalWindow = globalThis.window
20 const originalCustomEvent = globalThis.CustomEvent
21
22 beforeEach(() => {
23 Object.defineProperty(globalThis, 'window', {
24 configurable: true,
25 value: new EventTarget(),
26 })
27 Object.defineProperty(globalThis, 'CustomEvent', {
28 configurable: true,
29 value: TestCustomEvent,
30 })
31 })
32
33 afterEach(() => {
34 vi.restoreAllMocks()
35
36 if (originalWindow) {
37 Object.defineProperty(globalThis, 'window', {
38 configurable: true,
39 value: originalWindow,
40 })
41 } else {
42 Reflect.deleteProperty(globalThis, 'window')
43 }
44
45 if (originalCustomEvent) {
46 Object.defineProperty(globalThis, 'CustomEvent', {
47 configurable: true,
48 value: originalCustomEvent,
49 })
50 } else {
51 Reflect.deleteProperty(globalThis, 'CustomEvent')
52 }
53 })
54
55 it('shares the event client through a global symbol singleton', () => {
56 expect(
57 Reflect.get(globalThis, Symbol.for('tanstack.ai.devtools.eventClient')),
58 ).toBe(aiEventClient)
59 })
60
61 it('routes events through the shared event client (single path, no duplicate DOM dispatch)', () => {
62 const eventClientEmit = vi
63 .spyOn(aiEventClient, 'emit')
64 .mockImplementation(() => undefined)
65 const dispatchedEvents: Array<Event> = []
66
67 window.addEventListener('tanstack-dispatch-event', (event) => {
68 dispatchedEvents.push(event)
69 })
70
71 const payload = {
72 eventId: 'event-1',
73 timestamp: 1,
74 hookId: 'hook-1',
75 hookName: 'useChat',
76 lifecycle: 'mounted',
77 } satisfies AIDevtoolsEventMap['hook:registered']
78
79 emitAIDevtoolsEvent('hook:registered', payload)
80
81 expect(eventClientEmit).toHaveBeenCalledWith('hook:registered', payload)
82 expect(eventClientEmit).toHaveBeenCalledTimes(1)
83 // emitAIDevtoolsEvent must not double-dispatch to the bus directly; the
84 // event client owns transport so on() listeners fire exactly once when a
85 // ClientEventBus is running.
86 expect(dispatchedEvents).toHaveLength(0)
87 })
88
89 it('can dispatch directly to the devtools bus without using the event client', () => {
90 const eventClientEmit = vi
91 .spyOn(aiEventClient, 'emit')
92 .mockImplementation(() => undefined)
93 const dispatchedEvents: Array<
94 CustomEvent<{
95 type: string
96 pluginId: string
97 payload: AIDevtoolsEventMap['devtools:request-state']
98 }>
99 > = []
100
101 window.addEventListener('tanstack-dispatch-event', (event) => {
102 dispatchedEvents.push(
103 event as CustomEvent<{
104 type: string
105 pluginId: string
106 payload: AIDevtoolsEventMap['devtools:request-state']
107 }>,
108 )
109 })
110
111 const payload = {
112 eventId: 'event-request-state',
113 timestamp: 2,
114 } satisfies AIDevtoolsEventMap['devtools:request-state']
115
116 dispatchAIDevtoolsEvent('devtools:request-state', payload)
117
118 expect(eventClientEmit).not.toHaveBeenCalled()
119 expect(dispatchedEvents).toHaveLength(1)
120 expect(dispatchedEvents[0]?.detail).toEqual({
121 type: 'tanstack-ai-devtools:devtools:request-state',
122 pluginId: 'tanstack-ai-devtools',
123 payload,
124 })
125 })
126})