Open History to see the last commit
1import assert from 'node:assert/strict';
2import { mkdir, readFile, writeFile } from 'node:fs/promises';
3import { setTimeout as wait } from 'node:timers/promises';
4import type { AppSnapshot } from '@glow/contracts';
5import { config } from 'dotenv';
6
7config({ path: '.env.local', quiet: true });
8const base = process.env.GLOW_API_URL ?? 'http://localhost:3001';
9await mkdir('.local', { recursive: true });
10let state: Record<string, any> = {};
11try { state = JSON.parse(await readFile('.local/smoke.json', 'utf8')); } catch {}
12const save = () => writeFile('.local/smoke.json', JSON.stringify(state, null, 2), { mode: 0o600 });
13await save();
14async function request(path: string, body?: unknown, cookie = '', token = '', expected = 200) {
15 const response = await fetch(`${base}${path}`, { method: body === undefined ? 'GET' : 'POST', headers: { 'Content-Type': 'application/json', Origin: 'http://localhost:3000', ...(cookie ? { Cookie: cookie } : {}), ...(token ? { Authorization: `Bearer ${token}` } : {}) }, body: body === undefined ? undefined : JSON.stringify(body) });
16 const data = await response.json().catch(() => ({}));
17 assert.equal(response.status, expected, `${path}: ${JSON.stringify(data)}`);
18 return { data, cookie: response.headers.getSetCookie().map(value => value.split(';')[0]).join('; ') };
19}
20async function ready(appId: string, cookie: string, expectedRun?: string) {
21 let phase = '';
22 for (let attempt = 0; attempt < 360; attempt++) {
23 const snapshot = (await request(`/v1/apps/${appId}`, undefined, cookie)).data as AppSnapshot;
24 const run = snapshot.runs.at(0);
25 const next = `${snapshot.app.status}/${run?.status ?? 'no-run'}/${run?.phase ?? ''}`;
26 if (next !== phase) { console.log(next); phase = next; }
27 if (snapshot.app.status === 'failed') throw new Error('Provisioning did not complete; inspect its retained operation');
28 if (run?.status === 'failed') throw new Error(`Generation failed: ${run.error}`);
29 if (snapshot.app.status === 'ready' && snapshot.app.draftVersionId && (!run || run.status === 'ready') && (!expectedRun || run?.id === expectedRun)) return snapshot;
30 await wait(1000);
31 }
32 throw new Error('Timed out waiting for the working creation');
33}
34await request('/v1/apps', undefined, '', '', 401);
35const maker = process.env.GLOW_SMOKE_MAKER_COOKIE, friend = process.env.GLOW_SMOKE_FRIEND_COOKIE;
36if (!maker || !friend) throw new Error('Sign in to Neon Auth with two test accounts, then configure GLOW_SMOKE_MAKER_COOKIE and GLOW_SMOKE_FRIEND_COOKIE in the ignored environment file.');
37assert.ok(maker && friend, 'Both accounts received sessions');
38if (!state.appId) {
39 const result = await request('/v1/apps', { operationId: crypto.randomUUID(), prompt: 'Make a funny mobile tap toy: a tiny cloud that gets more dramatic each time I poke it. Use React and CSS with clear touch controls. Keep it small, expressive, and immediately playable.' }, maker, '', 201);
40 state.appId = result.data.data.id; await save();
41}
42const priorSnapshot = (await request(`/v1/apps/${state.appId}`, undefined, maker)).data as AppSnapshot;
43if (priorSnapshot.runs[0]?.status === 'failed') {
44 await request(`/v1/apps/${state.appId}/runs/${priorSnapshot.runs[0].id}/retry`, {}, maker);
45 console.log('Retrying the retained change after the implementation fix.');
46}
47const initial = await ready(state.appId, maker);
48await request(`/v1/apps/${state.appId}`, undefined, friend, '', 404);
49const preview = (await request(`/v1/apps/${state.appId}/preview`, undefined, maker)).data;
50assert.match(preview.html, /connect-src 'none'/);
51const visitor = (await request('/v1/visitor', {})).data.token;
52if (initial.app.visibility === 'private') {
53 await request(`/v1/play/${state.appId}`, undefined, '', visitor, 404);
54 await request(`/v1/apps/${state.appId}/share`, { operationId: crypto.randomUUID(), expectedVersion: initial.app.draftVersionId, visibility: 'unlisted' }, maker);
55}
56const play = (await request(`/v1/play/${state.appId}`, undefined, '', visitor)).data;
57const prior = (await request('/v1/state/read?key=pet&scope=player', undefined, '', play.token)).data;
58const operation = { operationId: crypto.randomUUID(), scope: 'player', key: 'pet', expectedRevision: prior.revision, operations: [{ type: 'set', field: 'name', value: 'Puddle' }] };
59const first = (await request('/v1/state/patch', operation, '', play.token)).data;
60const duplicate = (await request('/v1/state/patch', operation, '', play.token)).data;
61assert.deepEqual(first, duplicate);
62assert.equal((await request('/v1/state/read?key=pet&scope=player', undefined, '', preview.token)).data.revision, 0, 'Preview does not share live player state');
63if (!state.remixId) {
64 const result = await request(`/v1/apps/${state.appId}/remix`, { operationId: crypto.randomUUID(), versionId: play.versionId }, friend, '', 201);
65 state.remixId = result.data.data.id; await save();
66}
67const remix = await ready(state.remixId, friend);
68assert.equal(remix.app.parentAppId, state.appId);
69assert.ok(initial.app.projectId && initial.app.branchId && remix.app.projectId && remix.app.branchId, 'Both environments were provisioned');
70assert.equal(remix.app.projectId, initial.app.projectId, 'A remix uses the original Neon project');
71assert.notEqual(remix.app.branchId, initial.app.branchId, 'An explicit remix has its own branch');
72assert.equal(remix.messages.some(message => message.role === 'user'), false, 'A remix does not copy private prompts');
73async function edit(appId: string, cookie: string, key: string, prompt: string) {
74 state[key] ??= crypto.randomUUID(); await save();
75 const result = await request(`/v1/apps/${appId}/messages`, { operationId: state[key], prompt }, cookie, '', 202);
76 return ready(appId, cookie, result.data.data.id);
77}
78const updated = await edit(state.appId, maker, 'originalEdit', 'Keep this cloud toy and its interactions. Add one small playful button that shows an absurd weather forecast. Make only this small local change.');
79assert.equal(updated.app.projectId, initial.app.projectId);
80assert.equal(updated.app.branchId, initial.app.branchId, 'Editing does not branch');
81const updatedPlay = (await request(`/v1/play/${state.appId}`, undefined, '', visitor)).data;
82assert.equal(updatedPlay.versionId, updated.app.draftVersionId, 'The same share link opens the latest working version');
83assert.equal((await request(`/v1/apps/${state.remixId}`, undefined, friend)).data.app.draftVersionId, remix.app.draftVersionId, 'Parent edits do not change an existing remix');
84const changedRemix = await edit(state.remixId, friend, 'remixEdit', 'Keep the existing cloud toy and interactions. Make the cloud bright pink and add a funny BONK label when it is tapped. Make only this small local change.');
85assert.equal(changedRemix.app.branchId, remix.app.branchId, 'Remix edits stay on its existing branch');
86await request(`/v1/apps/${state.remixId}/share`, { operationId: crypto.randomUUID(), expectedVersion: changedRemix.app.draftVersionId, visibility: 'unlisted' }, friend);
87assert.ok((await request('/v1/responses', undefined, maker)).data.some((app: { id: string }) => app.id === state.remixId), 'A shared remix reaches the original creator as a response');
88const parentShared = (await request('/v1/state/read?key=weather&scope=shared', undefined, '', updatedPlay.token)).data;
89await request('/v1/state/patch', { operationId: crypto.randomUUID(), scope: 'shared', key: 'weather', expectedRevision: parentShared.revision, operations: [{ type: 'set', field: 'mood', value: 'dramatic' }] }, '', updatedPlay.token);
90const remixPlay = (await request(`/v1/play/${state.remixId}`, undefined, '', visitor)).data;
91assert.equal((await request('/v1/state/read?key=weather&scope=shared', undefined, '', remixPlay.token)).data.revision, 0, 'Remix live state starts independently');
92console.log(JSON.stringify({ appId: state.appId, remixId: state.remixId, compiled: true, crossAccountDenied: true, idempotentState: true, previewIsolated: true, privateChatExcluded: true, remixBranchReady: true, editRetainsBranch: true, latestShareVersion: true, remixResponse: true, branchStateIsolated: true }, null, 2));
93await save();