Open History to see the last commit
1import { renderHook } from '@octanejs/testing-library'
2import { beforeEach, describe, expect, it, vi } from 'vitest'
3import { useChat } from '../../src/use-chat.tsrx'
4import { useGeneration } from '../../src/use-generation.tsrx'
5import { createMockConnectionAdapter } from './test-utils'
6import type { AIDevtoolsClientMetadata } from '@tanstack/ai-client'
7
8// The hooks type `devtools` as display options (`name` only), so a caller can
9// reach `framework` / `hookName` only by forwarding a wider metadata object.
10const foreignMetadata: AIDevtoolsClientMetadata = {
11 framework: 'react',
12 hookName: 'somethingElse',
13}
14
15/**
16 * These hooks tag the TanStack AI Devtools bridge with `framework: 'octane'` so
17 * the devtools attribute activity to this binding rather than to React. The tag
18 * is hardcoded, which means it has to win over anything a caller passes in
19 * `devtools` — `useGeneration` in particular is documented as public API for
20 * custom generation types, so callers do supply their own `devtools` metadata.
21 *
22 * Capture the options each bridge factory receives and assert the identity
23 * survives, then delegate to the real factory so the clients still work.
24 */
25const capturedGeneration: Array<Record<string, any>> = []
26const capturedChat: Array<Record<string, any>> = []
27
28vi.mock('@tanstack/ai-client/devtools', async (importOriginal) => {
29 const actual =
30 await importOriginal<typeof import('@tanstack/ai-client/devtools')>()
31 return {
32 ...actual,
33 createGenerationDevtoolsBridge: (options: any) => {
34 capturedGeneration.push(options)
35 return actual.createGenerationDevtoolsBridge(options)
36 },
37 createChatDevtoolsBridge: (options: any) => {
38 capturedChat.push(options)
39 return actual.createChatDevtoolsBridge(options)
40 },
41 }
42})
43
44describe('devtools identification', () => {
45 beforeEach(() => {
46 capturedGeneration.length = 0
47 capturedChat.length = 0
48 })
49
50 it('tags useGeneration as octane', () => {
51 renderHook(() =>
52 useGeneration({
53 threadId: 'thread-1',
54 connection: createMockConnectionAdapter(),
55 }),
56 )
57
58 expect(capturedGeneration[0]?.metadata).toMatchObject({
59 framework: 'octane',
60 hookName: 'useGeneration',
61 })
62 })
63
64 it('keeps the octane identity when a caller supplies its own devtools metadata', () => {
65 renderHook(() =>
66 useGeneration({
67 threadId: 'thread-1',
68 connection: createMockConnectionAdapter(),
69 devtools: foreignMetadata,
70 }),
71 )
72
73 // A caller must not be able to misattribute this binding.
74 expect(capturedGeneration[0]?.metadata).toMatchObject({
75 framework: 'octane',
76 hookName: 'useGeneration',
77 })
78 })
79
80 it('preserves unrelated caller devtools metadata', () => {
81 renderHook(() =>
82 useGeneration({
83 threadId: 'thread-1',
84 connection: createMockConnectionAdapter(),
85 devtools: { name: 'Custom generation' },
86 }),
87 )
88
89 expect(capturedGeneration[0]?.metadata).toMatchObject({
90 framework: 'octane',
91 name: 'Custom generation',
92 })
93 })
94
95 it('tags useChat as octane and resists caller override', () => {
96 renderHook(() =>
97 useChat({
98 threadId: 'thread-1',
99 connection: createMockConnectionAdapter(),
100 devtools: foreignMetadata,
101 }),
102 )
103
104 expect(capturedChat[0]?.metadata).toMatchObject({
105 framework: 'octane',
106 hookName: 'useChat',
107 })
108 })
109})