Open History to see the last commit
1import { describe, expect, test } from 'vitest'
2import {
3 generateRuntimeBridgeCode,
4 injectRuntimeBridge,
5 wireRuntimeBridgeChannels,
6} from './runtime-bridge'
7β
8describe('generateRuntimeBridgeCode', () => {
9 test('guards on import.meta.hot and an unset global target', () => {
10 const code = generateRuntimeBridgeCode()
11 expect(code).toContain('import.meta.hot')
12 expect(code).toContain('globalThis.__TANSTACK_EVENT_TARGET__')
13 expect(code).toContain('!globalThis.__TANSTACK_EVENT_TARGET__')
14 })
15β
16 test('completes the connect handshake locally', () => {
17 const code = generateRuntimeBridgeCode()
18 expect(code).toContain("'tanstack-connect'")
19 expect(code).toContain("'tanstack-connect-success'")
20 })
21β
22 test('forwards dispatched events to the dev server', () => {
23 const code = generateRuntimeBridgeCode()
24 expect(code).toContain("'tanstack-dispatch-event'")
25 expect(code).toContain("import.meta.hot.send('tsd:to-server'")
26 })
27β
28 test('receives dev-server events and redispatches them locally', () => {
29 const code = generateRuntimeBridgeCode()
30 expect(code).toContain("import.meta.hot.on('tsd:to-client'")
31 expect(code).toContain("'tanstack-devtools-global'")
32 })
33β
34 test('has no external imports', () => {
35 expect(generateRuntimeBridgeCode()).not.toContain('import ')
36 })
37})
38β
39describe('injectRuntimeBridge', () => {
40 const EVENT_CLIENT_ID =
41 '/repo/node_modules/@tanstack/devtools-event-client/dist/esm/index.js'
42 const EVENT_CLIENT_CODE = 'class EventClient { emit() {} }'
43β
44 test('injects into the event-client module in a server environment', () => {
45 const out = injectRuntimeBridge(EVENT_CLIENT_CODE, EVENT_CLIENT_ID, 'ssr')
46 expect(out).toBeDefined()
47 expect(out).toContain(EVENT_CLIENT_CODE)
48 expect(out).toContain('__tsdRuntimeBridge')
49 })
50β
51 test('matches the workspace source path too', () => {
52 const id = '/repo/packages/event-bus-client/src/plugin.ts'
53 expect(injectRuntimeBridge(EVENT_CLIENT_CODE, id, 'ssr')).toBeDefined()
54 })
55β
56 test('skips the client environment', () => {
57 expect(
58 injectRuntimeBridge(EVENT_CLIENT_CODE, EVENT_CLIENT_ID, 'client'),
59 ).toBeUndefined()
60 })
61β
62 test('skips when environment name is unknown (pre-Environment-API)', () => {
63 expect(
64 injectRuntimeBridge(EVENT_CLIENT_CODE, EVENT_CLIENT_ID, undefined),
65 ).toBeUndefined()
66 })
67β
68 test('skips modules that are not the event-client', () => {
69 expect(
70 injectRuntimeBridge('export const x = 1', '/repo/src/app.ts', 'ssr'),
71 ).toBeUndefined()
72 })
73β
74 test('skips event-client-pathed modules that lack the EventClient class', () => {
75 const id =
76 '/repo/node_modules/@tanstack/devtools-event-client/dist/esm/foo.js'
77 expect(injectRuntimeBridge('export const y = 2', id, 'ssr')).toBeUndefined()
78 })
79})
80β
81describe('wireRuntimeBridgeChannels', () => {
82 function makeEnv() {
83 type Handler = (data: any) => void
84 const handlers: Record<string, Handler> = {}
85 const removed: Array<{ event: string; cb: Handler }> = []
86 const sent: Array<{ event: string; data: any }> = []
87 return {
88 hot: {
89 on: (event: string, cb: Handler) => (handlers[event] = cb),
90 off: (event: string, cb: Handler) => removed.push({ event, cb }),
91 send: (event: string, data: any) => sent.push({ event, data }),
92 },
93 __handlers: handlers,
94 __removed: removed,
95 __sent: sent,
96 }
97 }
98β
99 test('worker event -> dispatches tanstack-dispatch-event on the target', () => {
100 const target = new EventTarget()
101 const ssr = makeEnv()
102 const server = { environments: { client: { hot: null }, ssr } }
103 const received: Array<any> = []
104 target.addEventListener('tanstack-dispatch-event', (e) =>
105 received.push((e as CustomEvent).detail),
106 )
107β
108 wireRuntimeBridgeChannels(server as any, () => target)
109 const evt = { type: 'q:foo', payload: 1 }
110 ssr.__handlers['tsd:to-server']!(evt)
111β
112 expect(received).toEqual([evt])
113 })
114β
115 test('target global event -> forwarded to the env via tsd:to-client', () => {
116 const target = new EventTarget()
117 const ssr = makeEnv()
118 const server = { environments: { ssr } }
119β
120 wireRuntimeBridgeChannels(server as any, () => target)
121 const evt = { type: 'q:bar', payload: 2 }
122 target.dispatchEvent(
123 new CustomEvent('tanstack-devtools-global', { detail: evt }),
124 )
125β
126 expect(ssr.__sent).toEqual([{ event: 'tsd:to-client', data: evt }])
127 })
128β
129 test('skips the client environment', () => {
130 const client = makeEnv()
131 const server = { environments: { client } }
132 wireRuntimeBridgeChannels(server as any, () => new EventTarget())
133 expect(client.__handlers['tsd:to-server']).toBeUndefined()
134 })
135β
136 test('teardown stops forwarding', () => {
137 const target = new EventTarget()
138 const ssr = makeEnv()
139 const server = { environments: { ssr } }
140 const teardown = wireRuntimeBridgeChannels(server as any, () => target)
141 teardown()
142 target.dispatchEvent(
143 new CustomEvent('tanstack-devtools-global', { detail: { type: 'x' } }),
144 )
145 expect(ssr.__sent).toEqual([])
146 })
147β
148 test('teardown removes the tsd:to-server handler via hot.off (I1)', () => {
149 const target = new EventTarget()
150 const ssr = makeEnv()
151 const server = { environments: { ssr } }
152 const teardown = wireRuntimeBridgeChannels(server as any, () => target)
153β
154 // Capture the registered handler reference before teardown.
155 const registeredHandler = ssr.__handlers['tsd:to-server']
156 expect(registeredHandler).toBeDefined()
157β
158 teardown()
159β
160 // hot.off must have been called with the exact same handler reference.
161 expect(ssr.__removed).toContainEqual({
162 event: 'tsd:to-server',
163 cb: registeredHandler,
164 })
165β
166 // Dispatching a worker event after teardown must not reach the target.
167 const received: Array<any> = []
168 target.addEventListener('tanstack-dispatch-event', (e) =>
169 received.push((e as CustomEvent).detail),
170 )
171 registeredHandler!({ type: 'post-teardown' })
172 // The handler still dispatches (it holds a closure over getTarget) but the
173 // important invariant is that hot.off was called so the real HMR channel
174 // will no longer invoke it on subsequent dev-server restarts.
175 expect(
176 ssr.__removed.filter((r) => r.event === 'tsd:to-server'),
177 ).toHaveLength(1)
178 })
179})