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

queue.spec.ts

94 lines4.2 KB

1import { test, expect } from './fixtures'
2import { sendMessage, waitForAssistantText, featureUrl } from './helpers'
3​
4// Fixture: fixtures/queue/basic.json
5// - "[queue] first message" streams slowly (tps: 6, chunkSize: 2) so the run
6// stays in flight long enough to send follow-up messages while it streams.
7// - "[queue] third message" resolves immediately.
8// "[queue] second message" has no fixture on purpose — it's cancelled before
9// it ever drains, so if a regression let it send anyway the request would
10// fail to match and the test would fail loudly instead of silently passing.
11​
12test.describe('client message queue', () => {
13 test('queues messages sent while streaming, supports cancel, and drains FIFO', async ({
14 page,
15 testId,
16 aimockPort,
17 }) => {
18 // A's fixture streams slowly on purpose (see fixtures/queue/basic.json)
19 // so there's a window to send/cancel queued messages; give the whole
20 // scenario more room than the default 30s test timeout.
21 test.setTimeout(60_000)
22​
23 await page.goto(featureUrl('openai', 'chat', testId, aimockPort))
24​
25 // Send A and wait for its stream to start.
26 await sendMessage(page, '[queue] first message')
27 await page
28 .getByTestId('loading-indicator')
29 .waitFor({ state: 'visible', timeout: 10_000 })
30​
31 // While A is still streaming, send B then C. Both should be queued
32 // rather than starting a second concurrent request.
33 await sendMessage(page, '[queue] second message')
34 await sendMessage(page, '[queue] third message')
35​
36 const queued = page.getByTestId('queued-message')
37 await expect(queued).toHaveCount(2)
38 await expect(queued.nth(0)).toContainText('second message')
39 await expect(queued.nth(1)).toContainText('third message')
40​
41 // The queue region is distinct from the delivered message list — only
42 // A's user message has landed there so far.
43 await expect(page.getByTestId('user-message')).toHaveCount(1)
44​
45 // Cancel B before it drains.
46 await queued
47 .filter({ hasText: 'second message' })
48 .getByTestId('cancel-queued-button')
49 .click()
50​
51 await expect(queued).toHaveCount(1)
52 await expect(queued.first()).toContainText('third message')
53​
54 // Let A's stream settle. The queue only auto-drains C once A's run fully
55 // completes, and draining adds C's user message immediately (before its
56 // own response streams back) — so waiting for a second user message is
57 // a reliable settle signal that doesn't fire early on A's own streamed
58 // text (which contains the substring "first response" from its very
59 // first chunk, long before the run actually finishes).
60 await expect(page.getByTestId('user-message')).toHaveCount(2, {
61 timeout: 40_000,
62 })
63 await waitForAssistantText(page, 'first response', 5_000)
64 await expect(queued).toHaveCount(0)
65 await waitForAssistantText(page, 'third response', 20_000)
66​
67 // Final conversation: exactly A and C, in order — B was never sent.
68 const userMessages = page.getByTestId('user-message')
69 await expect(userMessages).toHaveCount(2)
70 await expect(userMessages.nth(0)).toContainText('first message')
71 await expect(userMessages.nth(1)).toContainText('third message')
72 await expect(
73 page.getByTestId('user-message').filter({ hasText: 'second message' }),
74 ).toHaveCount(0)
75​
76 const assistantMessages = page.getByTestId('assistant-message')
77 await expect(assistantMessages).toHaveCount(2)
78 await expect(assistantMessages.nth(0)).toContainText('first response')
79 await expect(assistantMessages.nth(1)).toContainText('third response')
80​
81 // Cross-check ordering across the whole transcript, not just per-role
82 // counts — proves A's turn fully precedes C's turn in the DOM.
83 const transcript = await page.getByTestId('message-list').innerText()
84 const iFirstMsg = transcript.indexOf('first message')
85 const iFirstResp = transcript.indexOf('first response')
86 const iThirdMsg = transcript.indexOf('third message')
87 const iThirdResp = transcript.indexOf('third response')
88 expect(iFirstMsg).toBeGreaterThanOrEqual(0)
89 expect(iFirstResp).toBeGreaterThan(iFirstMsg)
90 expect(iThirdMsg).toBeGreaterThan(iFirstResp)
91 expect(iThirdResp).toBeGreaterThan(iThirdMsg)
92 expect(transcript).not.toContain('second message')
93 })
94})