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

hook-dashboard-model.test.ts

150 lines4.1 KB

1import { describe, expect, it } from 'vitest'
2import {
3 createHookDashboardSummary,
4 getHookDisplayName,
5 getHookIdentityLabel,
6 getHookIdentitySubtitle,
7 groupHooksByCategory,
8 isHookRunning,
9 visibleHooks,
10} from '../src/components/hooks/hook-dashboard-model'
11import type {
12 HookOutputKind,
13 HookRecord,
14 RunRecord,
15} from '../src/store/hook-registry'
16​
17describe('hook dashboard model', () => {
18 it('groups hooks by output category in dashboard order', () => {
19 const groups = groupHooksByCategory([
20 createHook('image-1', 'useGenerateImage', 'image', 10),
21 createHook('chat-1', 'useChat', 'chat', 20),
22 createHook('audio-1', 'useGenerateAudio', 'audio', 30),
23 createHook('speech-1', 'useGenerateSpeech', 'audio', 50),
24 createHook('transcription-1', 'useTranscription', 'text', 60),
25 createHook('summarize-1', 'useSummarize', 'text', 70),
26 createHook('unknown-1', 'useUnknown', undefined, 40),
27 ])
28​
29 expect(groups.map((group) => group.id)).toEqual([
30 'chat',
31 'image',
32 'audio',
33 'speech',
34 'transcription',
35 'summarize',
36 'other',
37 ])
38 expect(groups.map((group) => group.label)).toEqual([
39 'Chat',
40 'Image',
41 'Audio',
42 'Speech',
43 'Transcription',
44 'Summarize',
45 'Other',
46 ])
47 expect(groups[0]?.hooks.map((hook) => hook.id)).toEqual(['chat-1'])
48 })
49​
50 it('detects hooks with active run updates', () => {
51 const hook = createHook('chat-1', 'useChat', 'chat', 20, ['run-1'])
52 const runningRun = createRun('run-1', 'updated')
53 const completedRun = createRun('run-1', 'completed')
54​
55 expect(isHookRunning(hook, { 'run-1': runningRun })).toBe(true)
56 expect(isHookRunning(hook, { 'run-1': completedRun })).toBe(false)
57 })
58​
59 it('summarizes hook dashboard metrics without needing a selected hook', () => {
60 const hooks = [
61 createHook('chat-1', 'useChat', 'chat', 20, ['run-1']),
62 createHook('image-1', 'useGenerateImage', 'image', 10),
63 createHook('audio-1', 'useGenerateAudio', 'audio', 8),
64 ]
65 const summary = createHookDashboardSummary(hooks, {
66 'run-1': createRun('run-1', 'started'),
67 })
68​
69 expect(summary).toEqual({
70 total: 3,
71 active: 3,
72 running: 1,
73 categories: 3,
74 tools: 0,
75 runs: 1,
76 })
77 })
78​
79 it('uses a custom display name when provided', () => {
80 const hook = {
81 ...createHook('chat-1', 'useChat', 'chat', 20),
82 displayName: 'Recipe Assistant',
83 }
84​
85 expect(getHookDisplayName(hook)).toBe('Recipe Assistant')
86 expect(
87 getHookDisplayName(createHook('chat-2', 'useChat', 'chat', 21)),
88 ).toBe('useChat')
89 })
90​
91 it('prefers threadId as the identity label over the registry id', () => {
92 const withThread = {
93 ...createHook('image-1', 'useGenerateImage', 'image', 10),
94 threadId: 'product-7-hero',
95 }
96 const ephemeral = createHook('gen-random', 'useGenerateImage', 'image', 11)
97​
98 expect(getHookIdentityLabel(withThread)).toBe('product-7-hero')
99 expect(getHookIdentityLabel(ephemeral)).toBe('gen-random')
100 expect(getHookIdentitySubtitle(withThread)).toBe('product-7-hero')
101 expect(
102 getHookIdentitySubtitle({
103 ...withThread,
104 displayName: 'Hero image',
105 }),
106 ).toBe('useGenerateImage - product-7-hero')
107 })
108​
109 it('returns all hooks from visible dashboard lists', () => {
110 const a = createHook('chat-1', 'useChat', 'chat', 20)
111 const b = createHook('image-1', 'useGenerateImage', 'image', 10)
112​
113 expect(visibleHooks([a, b]).map((hook) => hook.id)).toEqual([
114 'chat-1',
115 'image-1',
116 ])
117 })
118})
119​
120function createHook(
121 id: string,
122 hookName: string,
123 outputKind: HookOutputKind | undefined,
124 updatedAt: number,
125 runIds: Array<string> = [],
126): HookRecord {
127 return {
128 id,
129 hookName,
130 ...(outputKind ? { outputKind } : {}),
131 lifecycle: 'active',
132 registeredAt: updatedAt,
133 updatedAt,
134 state: {},
135 tools: [],
136 runIds,
137 eventIds: [],
138 activityRunIds: [],
139 }
141​
142function createRun(id: string, status: RunRecord['status']): RunRecord {
143 return {
144 id,
145 status,
146 startedAt: 1,
147 updatedAt: 2,
148 eventIds: [],
149 }