Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
3// Regression coverage for issue #667: importing this module must not generate a
4// random value in global scope. Edge runtimes (e.g. Cloudflare Workers) forbid
5// random-value generation at module-evaluation time, so eager seeding of the
6// runtime id crashed the Worker simply by importing `@tanstack/ai`'s `chat()`,
7// which pulls in the devtools middleware → this module.
8//
9// These tests live in a dedicated file (rather than envelope.test.ts) so the
10// module can be imported lazily via dynamic import after `vi.resetModules()`,
11// letting us observe behavior at the exact moment of module evaluation.
12describe('runtime id generation is deferred to first use (issue #667)', () => {
13 beforeEach(() => {
14 vi.resetModules()
15 Reflect.deleteProperty(globalThis, '__TANSTACK_AI_DEVTOOLS_RUNTIME_ID__')
16 })
17
18 afterEach(() => {
19 vi.restoreAllMocks()
20 Reflect.deleteProperty(globalThis, '__TANSTACK_AI_DEVTOOLS_RUNTIME_ID__')
21 })
22
23 it('does not generate a random value at module import time', async () => {
24 const randomUUID = vi.spyOn(globalThis.crypto, 'randomUUID')
25 const mathRandom = vi.spyOn(Math, 'random')
26
27 const mod = await import('../src/envelope')
28
29 // The crux of the bug: simply evaluating the module must touch no RNG.
30 expect(randomUUID).not.toHaveBeenCalled()
31 expect(mathRandom).not.toHaveBeenCalled()
32 expect(globalThis.__TANSTACK_AI_DEVTOOLS_RUNTIME_ID__).toBeUndefined()
33
34 // The id is generated lazily, on first use, inside a handler/call.
35 const id = mod.getAIDevtoolsRuntimeId()
36 expect(randomUUID).toHaveBeenCalledTimes(1)
37 expect(typeof id).toBe('string')
38 expect(id.length).toBeGreaterThan(0)
39 expect(globalThis.__TANSTACK_AI_DEVTOOLS_RUNTIME_ID__).toBe(id)
40 })
41
42 it('also defers generation when reached via createAIDevtoolsEventEnvelope', async () => {
43 const randomUUID = vi.spyOn(globalThis.crypto, 'randomUUID')
44
45 const mod = await import('../src/envelope')
46 expect(randomUUID).not.toHaveBeenCalled()
47
48 const envelope = mod.createAIDevtoolsEventEnvelope({
49 eventType: 'run:started',
50 source: 'server',
51 visibility: 'server-internal',
52 timestamp: 1,
53 })
54
55 expect(randomUUID).toHaveBeenCalled()
56 expect(envelope.runtimeId).toBe(mod.getAIDevtoolsRuntimeId())
57 })
58
59 it('reuses a runtime id already present on the global without regenerating', async () => {
60 const randomUUID = vi.spyOn(globalThis.crypto, 'randomUUID')
61 const mathRandom = vi.spyOn(Math, 'random')
62 globalThis.__TANSTACK_AI_DEVTOOLS_RUNTIME_ID__ = 'preseeded-runtime-id'
63
64 const mod = await import('../src/envelope')
65
66 expect(mod.getAIDevtoolsRuntimeId()).toBe('preseeded-runtime-id')
67 expect(randomUUID).not.toHaveBeenCalled()
68 expect(mathRandom).not.toHaveBeenCalled()
69 })
70
71 it('memoizes after first resolution so the id is stable across calls', async () => {
72 const mod = await import('../src/envelope')
73
74 const first = mod.getAIDevtoolsRuntimeId()
75 const second = mod.getAIDevtoolsRuntimeId()
76 const envelope = mod.createAIDevtoolsEventEnvelope({
77 eventType: 'run:started',
78 source: 'server',
79 visibility: 'server-internal',
80 timestamp: 1,
81 })
82
83 expect(second).toBe(first)
84 expect(envelope.runtimeId).toBe(first)
85 })
86
87 it('falls back to Math.random when crypto.randomUUID is unavailable', async () => {
88 const cryptoDescriptor = Object.getOwnPropertyDescriptor(
89 globalThis,
90 'crypto',
91 )
92 // Simulate a runtime without Web Crypto (forces the Math.random fallback).
93 Object.defineProperty(globalThis, 'crypto', {
94 configurable: true,
95 value: undefined,
96 })
97 const mathRandom = vi.spyOn(Math, 'random')
98
99 try {
100 const mod = await import('../src/envelope')
101 expect(mathRandom).not.toHaveBeenCalled()
102
103 const id = mod.getAIDevtoolsRuntimeId()
104 expect(mathRandom).toHaveBeenCalled()
105 expect(typeof id).toBe('string')
106 expect(id.length).toBeGreaterThan(0)
107 } finally {
108 if (cryptoDescriptor) {
109 Object.defineProperty(globalThis, 'crypto', cryptoDescriptor)
110 }
111 }
112 })
113})