Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { sandboxWaitKind } from './sandboxes-durable'
3import type { UIMessage } from '@tanstack/ai'
4
5/**
6 * The waiting panel on a durable sandboxed run.
7 *
8 * Verified against the live route: for the whole of `withSandbox`'s `ensure` the
9 * hydration response is `{ messages: [], activeRun: { runId } }` — the run record
10 * exists (so the server knows a run is in flight) but the transcript does not,
11 * because chat persistence saves the pending turn from `onStart`, after every
12 * middleware `setup`. A returning tab therefore has an active run and ZERO
13 * messages, which is the state that must still render "Starting sandbox…".
14 */
15const user: UIMessage = {
16 id: 'u1',
17 role: 'user',
18 parts: [{ type: 'text', content: 'go' }],
19}
20const assistant: UIMessage = {
21 id: 'a1',
22 role: 'assistant',
23 parts: [{ type: 'text', content: 'hi' }],
24}
25
26describe('sandboxWaitKind', () => {
27 it('shows boot for an active run whose transcript has not hydrated yet', () => {
28 // THE REGRESSION. Requiring a message here rendered an empty pane for the
29 // entire sandbox-setup window — the exact moment the user switches away.
30 expect(sandboxWaitKind(false, true, [])).toBe('boot')
31 })
32
33 it('shows boot for the tab that started the run, before anything streams', () => {
34 expect(sandboxWaitKind(true, false, [user])).toBe('boot')
35 })
36
37 it('shows continue when a prior assistant turn exists', () => {
38 expect(sandboxWaitKind(false, true, [user, assistant, user])).toBe(
39 'continue',
40 )
41 })
42
43 it('hides once the assistant is the latest message', () => {
44 // Streaming has started, so the panel must give way to the real content even
45 // though the server still reports the run active.
46 expect(sandboxWaitKind(true, true, [user, assistant])).toBe(false)
47 })
48
49 it('hides when no run is in flight', () => {
50 expect(sandboxWaitKind(false, false, [])).toBe(false)
51 expect(sandboxWaitKind(false, false, [user])).toBe(false)
52 })
53})