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

join-run-client-tool.spec.ts

96 lines3.8 KB

1import { expect, test } from '@playwright/test'
2import { sendMessage } from './helpers'
3​
4/**
5 * joinRun drain after a client-tool replay (issue #1058).
6 *
7 * The live `streamResponse` path drains queued post-stream actions in
8 * `finally`. Rejoin used a different teardown and skipped that drain, so a
9 * client-tool result from `joinRun` never went back to the server.
10 *
11 * This harness holds the first run open until disconnect, like
12 * `generation-persistence-resume.spec.ts`. The spec reloads while that run is
13 * still producing. On remount the client tails `activeRun` with `joinRun`.
14 * `JOIN_OK` is emitted only on the continuation POST that carries the tool
15 * result — so a green assertion cannot come from the first stream or from a
16 * done-restore.
17 *
18 * Provider-free: `/api/join-run-client-tool` streams a fixed AG-UI sequence
19 * through a `memoryStream` sink (exempt from the aimock policy).
20 */
21​
22const ALLOW_TOOL_KEY = 'e2e-join-run-client-tool-allow'
23​
24test.describe('joinRun client-tool continuation (issue #1058)', () => {
25 test('a reload mid client-tool run drains the tool result after joinRun', async ({
26 page,
27 }) => {
28 const threadId = `join-run-client-tool-${crypto.randomUUID()}`
29 await page.goto(
30 `/join-run-client-tool?threadId=${encodeURIComponent(threadId)}`,
31 )
32 await expect(page.getByTestId('hydration-marker')).toBeAttached()
33​
34 await sendMessage(page, 'look up')
35 // Wait until the first POST has a hanging client tool in the log. Reloading
36 // on `isLoading` alone can beat the producer, so remount would miss
37 // `activeRun` and skip `joinRun`.
38 await expect(page.getByTestId('client-tool-pending')).toHaveText('true')
39 await expect(page.getByTestId('client-tool-ran')).toHaveText('false')
40 await expect(page.getByTestId('assistant-text')).not.toContainText(
41 'JOIN_OK',
42 )
43​
44 await page.reload()
45 await expect(page.getByTestId('hydration-marker')).toBeAttached()
46 // Unlock the tool only after remount. The execute polls this flag, so a
47 // late first-page chunk cannot finish before unload and drain the live path.
48 await page.evaluate((key) => {
49 sessionStorage.setItem(key, '1')
50 }, ALLOW_TOOL_KEY)
51​
52 await expect(page.getByTestId('assistant-text')).toContainText('JOIN_OK', {
53 timeout: 15_000,
54 })
55 await expect(page.getByTestId('client-tool-ran')).toHaveText('true')
56 await expect(page.getByTestId('loading-indicator')).toHaveCount(0)
57​
58 await sendMessage(page, 'later')
59 await expect(page.getByTestId('assistant-text')).toContainText('LATER_OK', {
60 timeout: 15_000,
61 })
62 })
63​
64 test('a reload mid continuation run joins it, not the parent interrupt (issue #1429)', async ({
65 page,
66 }) => {
67 // The first run pauses on the client tool and the continuation is held.
68 // Hydrate then reports the continuation as active AND the parent interrupt
69 // as pending (the store commits it only when the continuation ends).
70 const threadId = `join-run-continuation-${crypto.randomUUID()}`
71 await page.goto(
72 `/join-run-client-tool?threadId=${encodeURIComponent(threadId)}`,
73 )
74 await expect(page.getByTestId('hydration-marker')).toBeAttached()
75 await page.evaluate((key) => {
76 sessionStorage.setItem(key, '1')
77 }, ALLOW_TOOL_KEY)
78​
79 await sendMessage(page, 'continue')
80 await expect(page.getByTestId('assistant-text')).toContainText('JOIN_OK', {
81 timeout: 15_000,
82 })
83​
84 // Block the tool so a restored parent interrupt cannot produce JOIN_OK
85 // again. Only a joinRun replay of the continuation can.
86 await page.evaluate((key) => {
87 sessionStorage.removeItem(key)
88 }, ALLOW_TOOL_KEY)
89 await page.reload()
90 await expect(page.getByTestId('hydration-marker')).toBeAttached()
91​
92 await expect(page.getByTestId('assistant-text')).toContainText('JOIN_OK', {
93 timeout: 15_000,
94 })
95 })
96})