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

reconstruct-generation.test.ts

222 lines6.6 KB

1import { describe, expect, it } from 'vitest'
2import { memoryPersistence } from '../src/memory'
3import {
4 getGenerationHydration,
5 reconstructGeneration,
6} from '../src/reconstruct-generation'
7import type { ReconstructedGeneration } from '../src/reconstruct-generation'
8​
9async function body(response: Response): Promise<ReconstructedGeneration> {
10 return (await response.json()) as ReconstructedGeneration
12​
13describe('reconstructGeneration', () => {
14 it('maps a completed job to a resume snapshot', async () => {
15 const persistence = memoryPersistence()
16 await persistence.stores.generationRuns.createOrResume({
17 runId: 'job-done',
18 threadId: 'thread-1',
19 activity: 'image',
20 provider: 'test-image-provider',
21 model: 'test-image-model',
22 startedAt: 1000,
23 })
24 await persistence.stores.generationRuns.update('job-done', {
25 status: 'completed',
26 finishedAt: 2000,
27 result: { id: 'image-result' },
28 })
29​
30 const response = await reconstructGeneration(
31 persistence,
32 new Request('http://example.test/api/generation?threadId=thread-1'),
33 )
34 expect(response.status).toBe(200)
35 expect(response.headers.get('cache-control')).toBe('no-store')
36​
37 const parsed = await body(response)
38 expect(parsed).toEqual({
39 resumeSnapshot: {
40 schemaVersion: 1,
41 resumeState: null,
42 status: 'complete',
43 result: { id: 'image-result' },
44 activity: 'image',
45 },
46 activeRun: null,
47 })
48 })
49​
50 it('reports an active run and resume state for a running job', async () => {
51 const persistence = memoryPersistence()
52 await persistence.stores.generationRuns.createOrResume({
53 runId: 'job-live',
54 threadId: 'thread-live',
55 activity: 'video',
56 provider: 'test-video-provider',
57 model: 'test-video-model',
58 startedAt: 5000,
59 })
60​
61 // Resolve by runId directly.
62 const parsed = await body(
63 await reconstructGeneration(
64 persistence,
65 new Request('http://example.test/api/generation?runId=job-live'),
66 ),
67 )
68 expect(parsed.activeRun).toEqual({ runId: 'job-live' })
69 expect(parsed.resumeSnapshot).toMatchObject({
70 status: 'running',
71 resumeState: { runId: 'job-live', threadId: 'thread-live' },
72 activity: 'video',
73 })
74 })
75​
76 it('surfaces an interrupted job as error status', async () => {
77 const persistence = memoryPersistence()
78 await persistence.stores.generationRuns.createOrResume({
79 runId: 'job-int',
80 threadId: 'thread-int',
81 activity: 'audio',
82 provider: 'p',
83 model: 'm',
84 startedAt: 1,
85 })
86 await persistence.stores.generationRuns.update('job-int', {
87 status: 'interrupted',
88 finishedAt: 2,
89 })
90​
91 const parsed = await body(
92 await reconstructGeneration(
93 persistence,
94 new Request('http://example.test/api/generation?runId=job-int'),
95 ),
96 )
97 expect(parsed.resumeSnapshot?.status).toBe('error')
98 expect(parsed.resumeSnapshot?.resumeState).toBeNull()
99 expect(parsed.activeRun).toBeNull()
100 })
101​
102 it('returns nulls when the id is missing or the thread is unknown', async () => {
103 const persistence = memoryPersistence()
104​
105 const missing = await body(
106 await reconstructGeneration(
107 persistence,
108 new Request('http://example.test/api/generation'),
109 ),
110 )
111 expect(missing).toEqual({ resumeSnapshot: null, activeRun: null })
112​
113 const unknown = await body(
114 await reconstructGeneration(
115 persistence,
116 new Request('http://example.test/api/generation?threadId=nope'),
117 ),
118 )
119 expect(unknown).toEqual({ resumeSnapshot: null, activeRun: null })
120 })
121​
122 it('returns 403 when authorize returns false', async () => {
123 const persistence = memoryPersistence()
124 await persistence.stores.generationRuns.createOrResume({
125 runId: 'job-secret',
126 threadId: 'thread-secret',
127 activity: 'image',
128 provider: 'p',
129 model: 'm',
130 startedAt: 1,
131 })
132​
133 const response = await reconstructGeneration(
134 persistence,
135 new Request('http://example.test/api/generation?runId=job-secret'),
136 { authorize: () => false },
137 )
138 expect(response.status).toBe(403)
139 expect(await response.json()).toEqual({ error: 'Forbidden' })
140 })
141})
142​
143describe('getGenerationHydration', () => {
144 it('resolves the latest run for a thread id', async () => {
145 const persistence = memoryPersistence()
146 await persistence.stores.generationRuns.createOrResume({
147 runId: 'job-old',
148 threadId: 'thread-1',
149 activity: 'image',
150 provider: 'p',
151 model: 'm',
152 startedAt: 1000,
153 })
154 await persistence.stores.generationRuns.update('job-old', {
155 status: 'completed',
156 finishedAt: 1500,
157 result: { id: 'old-result' },
158 })
159 await persistence.stores.generationRuns.createOrResume({
160 runId: 'job-new',
161 threadId: 'thread-1',
162 activity: 'image',
163 provider: 'p',
164 model: 'm',
165 startedAt: 2000,
166 })
167 await persistence.stores.generationRuns.update('job-new', {
168 status: 'completed',
169 finishedAt: 2500,
170 result: { id: 'new-result' },
171 })
172​
173 const hydration = await getGenerationHydration(persistence, 'thread-1')
174 expect(hydration).toEqual({
175 resumeSnapshot: {
176 schemaVersion: 1,
177 resumeState: null,
178 status: 'complete',
179 result: { id: 'new-result' },
180 activity: 'image',
181 },
182 activeRun: null,
183 })
184 })
185​
186 it('resolves a specific run by run id, with an active-run cursor while running', async () => {
187 const persistence = memoryPersistence()
188 await persistence.stores.generationRuns.createOrResume({
189 runId: 'job-live',
190 threadId: 'thread-live',
191 activity: 'video',
192 provider: 'p',
193 model: 'm',
194 startedAt: 5000,
195 })
196​
197 const hydration = await getGenerationHydration(persistence, 'job-live', {
198 by: 'runId',
199 })
200 expect(hydration.activeRun).toEqual({ runId: 'job-live' })
201 expect(hydration.resumeSnapshot).toMatchObject({
202 status: 'running',
203 resumeState: { runId: 'job-live', threadId: 'thread-live' },
204 })
205 })
206​
207 it('returns nulls for an empty id or no matching run', async () => {
208 const persistence = memoryPersistence()
209​
210 await expect(getGenerationHydration(persistence, '')).resolves.toEqual({
211 resumeSnapshot: null,
212 activeRun: null,
213 })
214 await expect(getGenerationHydration(persistence, 'nope')).resolves.toEqual({
215 resumeSnapshot: null,
216 activeRun: null,
217 })
218 await expect(
219 getGenerationHydration(persistence, 'nope', { by: 'runId' }),
220 ).resolves.toEqual({ resumeSnapshot: null, activeRun: null })
221 })
222})