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

app-studio.test.ts

158 lines4.7 KB

1import { describe, expect, it } from 'vitest'
2import { memorySandboxSnapshots } from '@tanstack/ai-sandbox'
3import { forkStudioThreads } from './app-studio-fork'
4import {
5 comparePrompt,
6 errorMessageFromBody,
7 previewUrlFrom,
8 previewUrlFromText,
9 threadIdsFromForkBody,
10 variantPrompt,
11} from './app-studio-helpers'
12​
13async function seedHead(
14 snapshots: Awaited<ReturnType<typeof memorySandboxSnapshots>>,
15 threadId: string,
16 conversation: Array<{ role: 'user'; content: string }> = [],
17) {
18 const writer = await snapshots.checkpoints.acquireWriter(threadId)
19 await snapshots.checkpoints.append({
20 checkpoint: {
21 id: `${threadId}-head`,
22 threadId,
23 parentCheckpointId: null,
24 createdAt: 1,
25 reason: 'automatic',
26 files: [],
27 conversation,
28 artifacts: [],
29 },
30 expectedHeadId: null,
31 writer,
32 })
33 await writer.release()
35​
36describe('app studio helpers', () => {
37 it('reads a preview URL from an exposePreview result', () => {
38 expect(previewUrlFrom({ url: 'http://127.0.0.1:5173' })).toBe(
39 'http://127.0.0.1:5173',
40 )
41 expect(previewUrlFrom('{"url":"http://127.0.0.1:5173"}')).toBe(
42 'http://127.0.0.1:5173',
43 )
44 expect(previewUrlFrom('http://127.0.0.1:5173')).toBe(
45 'http://127.0.0.1:5173',
46 )
47 expect(previewUrlFrom('not-a-url')).toBeNull()
48 expect(
49 previewUrlFromText(
50 'exposePreview returned: http://localhost:62133 and that is the app.',
51 ),
52 ).toBe('http://localhost:62133')
53 })
54​
55 it('builds distinct compare prompts', () => {
56 expect(variantPrompt('Make it warmer', 'A')).toContain('variant A')
57 expect(variantPrompt('Make it warmer', 'B')).toContain('variant B')
58 expect(comparePrompt(' ')).toBe(
59 'Keep the same product. Change only the visual direction.',
60 )
61 expect(comparePrompt('Make it warmer')).toBe('Make it warmer')
62 })
63​
64 it('reads fork thread ids and error text from JSON bodies', () => {
65 expect(
66 threadIdsFromForkBody({
67 forks: [{ threadId: 'a' }, { threadId: 'b' }],
68 }),
69 ).toEqual(['a', 'b'])
70 expect(threadIdsFromForkBody({ forks: [{}] })).toEqual([])
71 expect(errorMessageFromBody({ error: 'no checkpoint' }, 'fallback')).toBe(
72 'no checkpoint',
73 )
74 expect(errorMessageFromBody({}, 'fallback')).toBe('fallback')
75 })
76})
77​
78describe('forkStudioThreads', () => {
79 it('throws when the thread has no checkpoint', async () => {
80 const snapshots = await memorySandboxSnapshots()
81 await expect(
82 forkStudioThreads({
83 snapshots,
84 threadId: 'empty',
85 runId: 'run-1',
86 count: 1,
87 }),
88 ).rejects.toThrow('Build the app first')
89 })
90​
91 it('forks one destination from the saved head', async () => {
92 const snapshots = await memorySandboxSnapshots()
93 await seedHead(snapshots, 'source')
94 const result = await forkStudioThreads({
95 snapshots,
96 threadId: 'source',
97 runId: 'run-1',
98 count: 1,
99 })
100 expect(result.sourceCheckpointId).toBe('source-head')
101 expect(result.forks).toHaveLength(1)
102 const destination = result.forks[0]
103 expect(destination?.threadId.startsWith('studio-')).toBe(true)
104 expect(
105 await snapshots.checkpoints.getHead(destination?.threadId ?? ''),
106 ).toBe(destination?.checkpointId)
107 })
108​
109 it('forks two destinations for a compare', async () => {
110 const snapshots = await memorySandboxSnapshots()
111 await seedHead(snapshots, 'source')
112 const result = await forkStudioThreads({
113 snapshots,
114 threadId: 'source',
115 runId: 'run-2',
116 count: 2,
117 })
118 expect(result.forks).toHaveLength(2)
119 expect(result.forks[0]?.threadId).not.toBe(result.forks[1]?.threadId)
120 })
121​
122 it('copies the source conversation onto each fork', async () => {
123 const snapshots = await memorySandboxSnapshots()
124 await seedHead(snapshots, 'source', [{ role: 'user', content: 'build it' }])
125 const result = await forkStudioThreads({
126 snapshots,
127 threadId: 'source',
128 runId: 'run-3',
129 count: 1,
130 })
131 const destination = result.forks[0]
132 const checkpoint = await snapshots.checkpoints.get(
133 destination?.checkpointId ?? '',
134 )
135 expect(checkpoint?.conversation).toEqual([
136 { role: 'user', content: 'build it' },
137 ])
138 })
139​
140 it('does not hide a save error that is not a missing-sandbox miss', async () => {
141 const snapshots = await memorySandboxSnapshots()
142 await seedHead(snapshots, 'source')
143 const failing = {
144 ...snapshots,
145 save: async () => {
146 throw new Error('disk full')
147 },
148 }
149 await expect(
150 forkStudioThreads({
151 snapshots: failing,
152 threadId: 'source',
153 runId: 'run-4',
154 count: 1,
155 }),
156 ).rejects.toThrow('disk full')
157 })
158})