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

generation-persistence-resume.spec.ts

72 lines3.5 KB

1import { expect, test } from '@playwright/test'
2​
3/**
4 * Durable generation survives a MID-RUN reload (Fix A + Fix B).
5 *
6 * The plain `persistence: true` restore (`generation-persistence-server.spec.ts`)
7 * only proves a FINISHED run comes back. This proves the harder guarantee: a run
8 * still producing when the client disconnects keeps running to its terminal and
9 * is tailed to completion by a mount-time `joinRun`.
10 *
11 * The harness run holds its result until the client disconnects, so the result
12 * lands strictly AFTER the reload — the run cannot finish as a plain done-restore
13 * beforehand, making the mid-run rejoin the path under test. The run can only
14 * finish because the durability producer is decoupled from the delivery socket
15 * (a disconnect cancels the reader, not the run — Fix B) and `RUN_STARTED` is
16 * flushed to the log immediately so the mount `joinRun` finds a cursor to tail
17 * (Fix A). Without either, the reload would strand the run on `generating` or
18 * settle it to `error`.
19 *
20 * The rejoin is proven positively, not just by an end-state: the streamed run's
21 * result id is `image-1`, while the done-restore snapshot the GET would serve
22 * for an already-finished run carries a DISTINCT id (`image-restored`). So a
23 * green `result-id = image-1` can ONLY come from tailing the live run — if a
24 * reload ever degraded to a done-restore, the assertion would fail loudly.
25 *
26 * Provider-free: `/api/generation-persistence-resume` streams a fixed AG-UI
27 * sequence through a `memoryStream` sink (exempt from the aimock policy).
28 */
29​
30const DURABLE_IMAGE_URL = '/durable/generation-resume/image-1.png'
31​
32test.describe('generation persistence (durable mid-run reload)', () => {
33 test('a run interrupted by a reload is tailed to completion via joinRun', async ({
34 page,
35 }) => {
36 await page.goto('/generation-persistence-resume')
37 await expect(page.getByTestId('hydration-marker')).toBeAttached()
38 await expect(page.getByTestId('status')).toHaveText('idle')
39​
40 await page.getByTestId('generate-button').click()
41 // The run is in flight and its result is still pending (the harness holds it
42 // until this client disconnects).
43 await expect(page.getByTestId('status')).toHaveText('generating')
44 await expect(page.getByTestId('result-id')).toHaveText('none')
45​
46 // Reload MID-RUN: the live response is cancelled while the run is producing.
47 // Only the surviving durable producer + a mount `joinRun` can finish it.
48 await page.reload()
49 await expect(page.getByTestId('hydration-marker')).toBeAttached()
50​
51 // The rejoin tails the log to the terminal: success, not a stranded
52 // `generating` and not an `error`. `image-1` is the STREAMED run's id — the
53 // done-restore snapshot would read `image-restored`, so this proves the
54 // client tailed the live run rather than restoring a finished one.
55 await expect(page.getByTestId('status')).toHaveText('success', {
56 timeout: 15_000,
57 })
58 await expect(page.getByTestId('result-id')).toHaveText('image-1')
59 await expect(page.getByTestId('error')).toHaveText('none')
60 await expect(page.getByTestId('generated-image')).toHaveCount(1)
61 await expect(page.getByTestId('generated-image')).toHaveAttribute(
62 'src',
63 DURABLE_IMAGE_URL,
64 )
65​
66 // Server-driven mode keeps no local store — the rejoin came from the server.
67 const localKeys = await page.evaluate(() =>
68 Object.keys(window.localStorage),
69 )
70 expect(localKeys.some((k) => k.includes('generation-resume'))).toBe(false)
71 })
72})