Open History to see the last commit
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 threadId: 'thread-1',
41 fetcher: async () => ({ id: 'result-1' }),
42 devtools,
43 }),
44 )
45
46 expect(captureMetadata).toHaveBeenLastCalledWith({
47 name: 'Custom generation',
48 outputKind: 'image',
49 framework: 'react',
50 hookName: 'useGeneration',
51 })
52 })
53
54 it('keeps specialized hook identity', () => {
55 renderHook(() =>
56 useGenerateImage({
57 threadId: 'thread-1',
58 fetcher: async () => ({
59 id: 'image-1',
60 images: [],
61 model: 'test-model',
62 }),
63 devtools: { name: 'Image Studio' },
64 }),
65 )
66
67 expect(captureMetadata).toHaveBeenLastCalledWith({
68 name: 'Image Studio',
69 framework: 'react',
70 hookName: 'useGenerateImage',
71 outputKind: 'image',
72 })
73 })
74})