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

devtools-identification.test.ts

72 lines1.8 KB

1import { renderHook } from '@testing-library/react'
2import { beforeEach, describe, expect, it, vi } from 'vitest'
3import { useGenerateImage } from '../src/use-generate-image'
4import { useGeneration } from '../src/use-generation'
5​
6const { captureMetadata } = vi.hoisted(() => ({
7 captureMetadata: vi.fn(),
8}))
9​
10vi.mock('@tanstack/ai-client/devtools', async (importOriginal) => {
11 const actual =
12 await importOriginal<typeof import('@tanstack/ai-client/devtools')>()
13 const createGenerationDevtoolsBridge: typeof actual.createGenerationDevtoolsBridge =
14 (options) => {
15 captureMetadata(options.metadata)
16 return actual.createGenerationDevtoolsBridge(options)
17 }
18​
19 return {
20 ...actual,
21 createGenerationDevtoolsBridge,
22 }
23})
24​
25describe('React generation devtools identification', () => {
26 beforeEach(() => {
27 captureMetadata.mockClear()
28 })
29​
30 it('keeps useGeneration identity while preserving caller metadata', () => {
31 const devtools = {
32 name: 'Custom generation',
33 framework: 'vue',
34 hookName: 'somethingElse',
35 outputKind: 'image' as const,
36 }
37​
38 renderHook(() =>
39 useGeneration({
40 fetcher: async () => ({ id: 'result-1' }),
41 devtools,
42 }),
43 )
44​
45 expect(captureMetadata).toHaveBeenLastCalledWith({
46 name: 'Custom generation',
47 outputKind: 'image',
48 framework: 'react',
49 hookName: 'useGeneration',
50 })
51 })
52​
53 it('keeps specialized hook identity', () => {
54 renderHook(() =>
55 useGenerateImage({
56 fetcher: async () => ({
57 id: 'image-1',
58 images: [],
59 model: 'test-model',
60 }),
61 devtools: { name: 'Image Studio' },
62 }),
63 )
64​
65 expect(captureMetadata).toHaveBeenLastCalledWith({
66 name: 'Image Studio',
67 framework: 'react',
68 hookName: 'useGenerateImage',
69 outputKind: 'image',
70 })
71 })
72})