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

image-to-image.spec.ts

105 lines3.8 KB

1import path from 'path'
2import { fileURLToPath } from 'url'
3import { test, expect } from './fixtures'
4import {
5 fillPrompt,
6 clickGenerate,
7 waitForGenerationComplete,
8 featureUrl,
9} from './helpers'
10import { providersFor } from './test-matrix'
11​
12const __filename = fileURLToPath(import.meta.url)
13const __dirname = path.dirname(__filename)
14const testImagePath = path.resolve(__dirname, '../test-assets/guitar-shop.png')
15​
16// Image-conditioned generation: the prompt is sent as multimodal parts
17// (text + attached image). For OpenAI this routes generateImage() to the
18// multipart /v1/images/edits endpoint instead of /v1/images/generations,
19// exercising the imagePartToFile upload path end-to-end.
20for (const provider of providersFor('image-to-image')) {
21 test.describe(`${provider} -- image-to-image`, () => {
22 test('sse -- edits an image via SSE connection', async ({
23 page,
24 request,
25 testId,
26 aimockPort,
27 }) => {
28 await page.goto(
29 featureUrl(provider, 'image-to-image', testId, aimockPort, 'sse'),
30 )
31 // Cold vite compiles of this route can delay hydration past fillPrompt's
32 // fallback; wait for the page to settle before interacting.
33 await page.waitForLoadState('networkidle')
34 await fillPrompt(page, 'add a tree to this product photo')
35 await page.getByTestId('image-input').setInputFiles(testImagePath)
36 await clickGenerate(page)
37 await waitForGenerationComplete(page)
38 const images = page.getByTestId('generated-image')
39 await expect(images).toHaveCount(1)
40​
41 // The fixture matches on prompt text regardless of endpoint, so also
42 // prove the adapter routed to the multipart edits endpoint (and didn't
43 // silently drop the image part and call /v1/images/generations).
44 const journalRes = await request.get(
45 `http://127.0.0.1:${aimockPort}/v1/_requests`,
46 )
47 const entries = (await journalRes.json()) as Array<{
48 path?: string
49 body?: unknown
50 }>
51 const editEntry = entries.find(
52 (e) =>
53 e.path === '/v1/images/edits' &&
54 JSON.stringify(e.body ?? '').includes(
55 'add a tree to this product photo',
56 ),
57 )
58 expect(editEntry).toBeTruthy()
59 })
60​
61 test('http-stream -- edits an image via HTTP stream', async ({
62 page,
63 testId,
64 aimockPort,
65 }) => {
66 await page.goto(
67 featureUrl(
68 provider,
69 'image-to-image',
70 testId,
71 aimockPort,
72 'http-stream',
73 ),
74 )
75 // Cold vite compiles of this route can delay hydration past fillPrompt's
76 // fallback; wait for the page to settle before interacting.
77 await page.waitForLoadState('networkidle')
78 await fillPrompt(page, 'add a tree to this product photo')
79 await page.getByTestId('image-input').setInputFiles(testImagePath)
80 await clickGenerate(page)
81 await waitForGenerationComplete(page)
82 const images = page.getByTestId('generated-image')
83 await expect(images).toHaveCount(1)
84 })
85​
86 test('fetcher -- edits an image via server function', async ({
87 page,
88 testId,
89 aimockPort,
90 }) => {
91 await page.goto(
92 featureUrl(provider, 'image-to-image', testId, aimockPort, 'fetcher'),
93 )
94 // Cold vite compiles of this route can delay hydration past fillPrompt's
95 // fallback; wait for the page to settle before interacting.
96 await page.waitForLoadState('networkidle')
97 await fillPrompt(page, 'add a tree to this product photo')
98 await page.getByTestId('image-input').setInputFiles(testImagePath)
99 await clickGenerate(page)
100 await waitForGenerationComplete(page)
101 const images = page.getByTestId('generated-image')
102 await expect(images).toHaveCount(1)
103 })
104 })