Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { reconstructSpeechResult } from '../src'
3import type { PersistedArtifactRef } from '@tanstack/ai/client'
4import type { GenerationRestoredResult } from '../src/generation-types'
5
6const audioArtifact: PersistedArtifactRef = {
7 role: 'output',
8 artifactId: 'artifact-speech-1',
9 threadId: 'thread-tts',
10 runId: 'run-tts',
11 name: 'speech.mp3',
12 mimeType: 'audio/mpeg',
13 size: 1024,
14 createdAt: '2026-07-06T00:00:00.000Z',
15 url: '/api/artifacts?id=artifact-speech-1',
16 source: {
17 activity: 'tts',
18 path: 'runs/run-tts/speech.mp3',
19 provider: 'test',
20 model: 'test-tts',
21 mediaType: 'audio',
22 },
23}
24
25const base: GenerationRestoredResult = {
26 id: 'gen-1',
27 model: 'test-tts',
28 activity: 'tts',
29 artifacts: [audioArtifact],
30}
31
32describe('reconstructSpeechResult', () => {
33 it('rebuilds a TTSResult that surfaces the durable url via artifacts', () => {
34 const result = reconstructSpeechResult(base)
35 expect(result).not.toBeNull()
36 expect(result!.audio).toBe('') // bytes are not persisted; url is in artifacts
37 expect(result!.contentType).toBe('audio/mpeg')
38 expect(result!.format).toBe('mpeg')
39 expect(result!.artifacts?.[0]?.url).toBe(
40 '/api/artifacts?id=artifact-speech-1',
41 )
42 })
43
44 it('returns null when no audio output artifact carries a url', () => {
45 expect(reconstructSpeechResult({ ...base, artifacts: [] })).toBeNull()
46 expect(
47 reconstructSpeechResult({
48 ...base,
49 artifacts: [{ ...audioArtifact, url: undefined }],
50 }),
51 ).toBeNull()
52 })
53})