Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import { createGenerateImage } from '../src/create-generate-image'
3import { createGeneration } from '../src/create-generation'
4import type { ImageGenerationResult } from '@tanstack/ai'
5
6function createMockHandle() {
7 const controller = new AbortController()
8 return {
9 update: vi.fn(async () => new AbortController().signal),
10 signal: controller.signal,
11 }
12}
13
14describe('createGenerateImage', () => {
15 it('sets result from a fetcher and updates the handle', async () => {
16 const handle = createMockHandle()
17 const mockResult: ImageGenerationResult = {
18 id: 'img-1',
19 images: [{ url: 'https://example.com/x.png' }],
20 model: 'dall-e-3',
21 }
22
23 const gen = createGenerateImage(handle, {
24 threadId: 'thread-1',
25 fetcher: async () => mockResult,
26 })
27
28 await gen.generate({ prompt: 'A sunset' })
29
30 expect(gen.result).toEqual(mockResult)
31 expect(handle.update).toHaveBeenCalled()
32 })
33})
34
35describe('createGeneration', () => {
36 it('throws when connection and fetcher are both missing', () => {
37 const handle = createMockHandle()
38 expect(() => createGeneration(handle, { threadId: 'thread-1' })).toThrow(
39 'createGeneration requires either a connection or fetcher option',
40 )
41 })
42})