Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

envelope.test.ts

126 lines3.6 KB

1import { describe, expect, it } from 'vitest'
2import {
3 createAIDevtoolsEventEnvelope,
4 getAIDevtoolsDedupeKey,
5 getAIDevtoolsRuntimeId,
6} from '../src/envelope'
7​
8describe('AI devtools event envelope', () => {
9 it('creates a provenance-aware envelope with encoded identity parts', () => {
10 const event = createAIDevtoolsEventEnvelope({
11 eventType: 'hook:registered',
12 source: 'client',
13 visibility: 'client-state',
14 clientId: 'client-1',
15 requestId: 'request-1',
16 streamId: 'stream-1',
17 hookId: 'hook-1',
18 threadId: 'thread-1',
19 runId: 'run-1',
20 messageId: 'msg-1',
21 toolCallId: 'tool-1',
22 sequence: 2,
23 correlationId: 'correlation-1',
24 relatedEventId: 'event-parent',
25 timestamp: 1234,
26 })
27​
28 expect(event).toMatchObject({
29 eventType: 'hook:registered',
30 source: 'client',
31 visibility: 'client-state',
32 clientId: 'client-1',
33 requestId: 'request-1',
34 streamId: 'stream-1',
35 hookId: 'hook-1',
36 threadId: 'thread-1',
37 runId: 'run-1',
38 messageId: 'msg-1',
39 toolCallId: 'tool-1',
40 sequence: 2,
41 correlationId: 'correlation-1',
42 relatedEventId: 'event-parent',
43 timestamp: 1234,
44 })
45 expect(event.eventId).toContain('client:hook:registered')
46 expect(event.eventId).toContain('value-client-1')
47 expect(event.eventId).toContain('value-hook-1')
48 expect(event.eventId).toContain('value-thread-1')
49 expect(event.eventId).toContain('value-run-1')
50 expect(event.eventId).toContain(
51 `value-${encodeURIComponent(getAIDevtoolsRuntimeId())}`,
52 )
53 expect(event.runtimeId).toBe(getAIDevtoolsRuntimeId())
54 })
55​
56 it('does not overwrite an explicit event id', () => {
57 const event = createAIDevtoolsEventEnvelope({
58 eventId: 'event-explicit',
59 eventType: 'run:started',
60 source: 'server',
61 visibility: 'server-internal',
62 timestamp: 1,
63 })
64​
65 expect(event.eventId).toBe('event-explicit')
66 })
67​
68 it('generates an event id when explicit event id is empty', () => {
69 const event = createAIDevtoolsEventEnvelope({
70 eventId: '',
71 eventType: 'run:started',
72 source: 'server',
73 visibility: 'server-internal',
74 timestamp: 1,
75 })
76​
77 expect(event.eventId).not.toBe('')
78 expect(event.eventId).toContain('server:run:started')
79 })
80​
81 it('uses eventId as the primary dedupe key', () => {
82 const key = getAIDevtoolsDedupeKey({
83 eventId: 'event-1',
84 eventType: 'hook:registered',
85 source: 'client',
86 visibility: 'client-state',
87 timestamp: 1,
88 })
89​
90 expect(key).toBe('event:event-1')
91 })
92​
93 it('falls back to a composite dedupe key when eventId is missing', () => {
94 const key = getAIDevtoolsDedupeKey({
95 eventType: 'tools:call:completed',
96 source: 'client',
97 visibility: 'user-visible',
98 clientId: 'client-1',
99 requestId: 'request-1',
100 streamId: 'stream-1',
101 hookId: 'hook-1',
102 threadId: 'thread-1',
103 runId: 'run-1',
104 messageId: 'msg-1',
105 toolCallId: 'tool-1',
106 sequence: 2,
107 timestamp: 12345,
108 })
109​
110 expect(key).toBe(
111 'fallback:client:tools:call:completed:user-visible:value-client-1:value-request-1:value-stream-1:value-hook-1:value-thread-1:value-run-1:value-msg-1:value-tool-1:value-2:12000',
112 )
113 })
114​
115 it('encodes separators and URL-sensitive characters in generated ids', () => {
116 const event = createAIDevtoolsEventEnvelope({
117 eventType: 'hook:registered',
118 source: 'client',
119 visibility: 'client-state',
120 hookId: 'hook:with/slash and space',
121 timestamp: 1,
122 })
123​
124 expect(event.eventId).toContain('value-hook%3Awith%2Fslash%20and%20space')
125 })
126})