Open History to see the last commit
1import { expect, test } from '@playwright/test'
2import {
3 forkWithSourceMessageSnapshot,
4 sameSerializableValue,
5} from '../src/routes/api.sandbox-file-persistence'
6
7test.describe('sandbox portable file snapshots', () => {
8 test('captures source messages before the fork and compares dates', async () => {
9 const messages = [{ id: 'message-1', nested: { value: 'before' } }]
10 const result = await forkWithSourceMessageSnapshot({
11 loadSourceMessages: () => Promise.resolve(messages),
12 fork: () => {
13 const message = messages[0]
14 if (!message) throw new Error('Expected a message')
15 message.nested.value = 'after'
16 return Promise.resolve()
17 },
18 })
19
20 expect(result.sourceMessagesUnchanged).toBe(false)
21 expect(
22 sameSerializableValue(
23 { createdAt: new Date('2026-01-01T00:00:00.000Z') },
24 { createdAt: new Date('2026-01-02T00:00:00.000Z') },
25 ),
26 ).toBe(false)
27 })
28
29 test('saves, recovers, reads, and forks an immutable snapshot', async ({
30 request,
31 }) => {
32 const response = await request.post('/api/sandbox-file-persistence')
33
34 expect(response.ok()).toBe(true)
35 await expect(response.json()).resolves.toEqual({
36 namedSave: 'release-1',
37 recoveredFiles: ['notes.txt'],
38 recoveredFileBytes: [115, 97, 118, 101, 100, 32, 102, 105, 108, 101],
39 recoveredEmptyDirectories: ['empty'],
40 artifactText: 'artifact data',
41 conversation: [{ content: 'saved conversation', role: 'user' }],
42 automaticConversation: [
43 { content: 'saved conversation', role: 'user' },
44 { content: 'recover', role: 'user' },
45 {
46 content: 'automatic conversation',
47 createdAt: expect.any(String),
48 id: 'automatic-message',
49 // withPersistence records the run that produced the message, and
50 // chat() records the source and the finish reason of the answer.
51 metadata: {
52 tanstack: {
53 finishReason: 'stop',
54 run: { id: 'recover' },
55 source: { provider: 'fixed', api: 'text', model: 'test-model' },
56 },
57 },
58 role: 'assistant',
59 },
60 ],
61 excluded: ['.env', '.git'],
62 fork: {
63 selectedCheckpointIsHead: false,
64 files: ['notes.txt'],
65 sourceThreadUnchanged: true,
66 sourceMessagesUnchanged: true,
67 conversation: [{ content: 'saved conversation', role: 'user' }],
68 },
69 })
70 })
71})