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