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

create-generation.test.ts

42 lines1.2 KB

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 id: 'handle-1',
10 update: vi.fn(async () => new AbortController().signal),
11 signal: controller.signal,
12 }
14​
15describe('createGenerateImage', () => {
16 it('sets result from a fetcher and updates the handle', async () => {
17 const handle = createMockHandle()
18 const mockResult: ImageGenerationResult = {
19 id: 'img-1',
20 images: [{ url: 'https://example.com/x.png' }],
21 model: 'dall-e-3',
22 }
23​
24 const gen = createGenerateImage(handle, {
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, {})).toThrow(
39 'createGeneration requires either a connection or fetcher option',
40 )
41 })
42})