Files
feuerwerk/tools/e2e/50-snapshots.mjs
T
adminandClaude Opus 5.5 14972d057f
game-maker / gate (push) Successful in 3m53s
game-maker / suite (push) Successful in 11m55s
Refactoring round 8: hide data files, safe saves, Esc as Cancel (#85)
Recurring cleanup round over the code since round 7 (bc688f3). The
issue now also asks to fix bugs and small inconsistencies found on the
way, so unlike the rounds before, this one changes behaviour in a few
places. Duplicated code in api went from 23 clones with 228 lines to
none (jscpd, at least 50 tokens). In src it stays at 22 clones with
271 lines, the same before and after.

Bugs:
- nginx served project and snapshot files as they were, so anyone who
  knew a project id could read it without signing in. Every file the
  API writes now goes through AtomicFile (temp file, mode, rename).
  Projects, snapshots, releases, accounts and sessions are 0640, and
  nginx answers 403 for them.
- Any signed-in account could delete a shared image, font or sound,
  which projects of other accounts may wear. Deleting one is now for
  administrators only. The editor never deleted a file, so the
  client's delete is gone.
- Two saves of one project could run at once. The lock file was
  removed after each save, so a waiting save and the next one locked
  different files. The lock file now stays, and deleting a project
  takes the lock as well.
- Bytes that are no picture got a 500 instead of a 422, because the
  error handler also threw for warnings silenced with @.
- A session file was rewritten in place. A request reading it at that
  moment found it empty and was signed out. It is replaced atomically.
- Delete did nothing for a shape picked in the vector art list. Delete
  now goes through removeSelection, one case per kind of selection,
  and a kind without a case fails the type check.

Inconsistencies:
- Escape on a question does what its Cancel does. Cancel on "Delete
  project?" and "Restore snapshot?" leads back to the picker or the
  list like the other questions did, and so does deleting a project.
- Editor texts use American spelling throughout, like the port names:
  48 British spellings in 45 lines (colour, centre, initialised).
- objectName and groupName replace eleven copies of "own name, else
  the asset's" and three of "Group". One copy fell back to the object
  id instead of "?".

Duplicates, dead code, comments:
- routeFiles serves images, fonts and sounds instead of three copies.
  HashedFileStore holds what the three stores shared: keeping a file
  under its hash with its name, listing and deleting. The stores keep
  only which bytes they take and what a record says.
- AtomicFile replaces nine copies of temp file plus rename.
- removeShapes is the one way the canvas, the inspector button and
  Delete take shapes out.
- GraphInterpreter.fire was called by tests only. The 54 calls use a
  helper in tests/helpers.ts now.
- FileStore.delete, HttpFileStore.delete and the PluginApi type were
  unused. A doc comment in actions.ts sat above the wrong function.

Checked and left as is:
- solidCells is used by tests only. It is the per-cell view before the
  merge into rectangles, the one place the largest-scale rule shows.
- An undo checker ran all 124 commands of the unit tests and compared
  the project before and after undo. The only differences are empty
  ports, groups and notes lists where there were none, which the file
  format drops anyway.

Docs:
- README: the check sum.
- Wiki, own commit: API (admin-only deletes, 422, file modes, lock
  file), Home (the "Not yet" list named three things that exist),
  Concepts, Keyboard, Vector art, Architecture and Development, and
  the 38 characters Gitea flagged as ambiguous on eight pages.
- .gitignore: the lock files, and pasted-*.png in any folder, which
  keeps the screenshot archive out.

Tests:
- ./check.sh: 195 files, 2817 tests, exit 0 (+2 for the name helpers).
- PHP: accounts 52 in the web container and 53 in the CI image with
  the race test (+2), snapshots 15 (+1), releases 20.
- Full e2e run: ALL PASSED, 1586 checks (+6 in 12, 47, 49 and 50).
- Smokes jumprun, tetris, actor: 24, 19 and 21 checks. The example
  project files were restored byte for byte afterwards.
- The file stores of HEAD and of this commit give byte-identical
  output for the live folders and for store, list and delete in a
  throwaway folder.
- Counter-checks, all fail as expected: a 500 for bad bytes fails 12,
  no admin check fails 49, removing the lock file again loses saves
  in 3 of 3 runs (151 to 170 of 321), Delete ignoring shapes fails 47,
  Escape that only closes fails 50, and dropping the shape case fails
  the type check (TS2366).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 18:12:55 +02:00

218 lines
9.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { authHeaders, forgetSession } from '../lib/auth.mjs';
/** The project this step makes and deletes again; a run that died halfway leaves it for the next. */
const TEMP_NAME = 'Snapshots 50';
const RENAMED = 'Renamed by 50';
/**
* Snapshots of a project (#231): made from the menu, restored over a later stand, the replaced
* stand kept as a snapshot of its own, and deleted with the project.
*
* Works on a throwaway project of its own, because a restore reloads the project it is done on.
* The last thing it does is delete that project and open the fixture again.
*/
export async function snapshots(t) {
const { check, cdp, json, click, press, typeText, locate, sleep, waitUntil, settle } = t;
const { clickToolbar, clickText, shot, saved, baseUrl, navigate } = t;
const send = async (method, route, body) =>
fetch(`${baseUrl}api/index.php?r=${route}`, {
method,
headers: { ...(await authHeaders(baseUrl)), 'Content-Type': 'application/json' },
body: body === undefined ? undefined : JSON.stringify(body),
signal: AbortSignal.timeout(20_000),
});
// Step 45 changes the password of `e2e`, which ends the session the tools had cached.
const api = async (method, route, body) => {
const first = await send(method, route, body);
if (first.status !== 401) return first;
forgetSession(baseUrl);
return send(method, route, body);
};
const sweep = async () => {
for (const project of await (await api('GET', 'projects')).json()) {
if (project.name === TEMP_NAME || project.name === RENAMED)
await api('DELETE', `projects/${project.id}`);
}
};
/** The last-drawn `text` – the dialog's, over a row or a menu that says the same. */
const clickLast = async (text) => {
await settle();
const at = await locate(text, true);
await click(at.x, at.y);
};
const openSnapshots = async () => {
await clickToolbar('menu');
await clickText('Snapshots…');
await waitUntil('__gm.snapshots() !== null', { label: 'the snapshots dialog opened' });
};
const notice = () => json('__gm.state.notice');
await sweep();
const made = await (await api('POST', 'projects', { name: TEMP_NAME })).json();
const stored = async () => (await (await api('GET', `projects/${made.id}`)).json()).project;
try {
await cdp.send('Page.navigate', { url: `${baseUrl}?project=${made.id}` });
await sleep(4000);
await waitUntil(`window.__gm?.state?.project?.id === ${JSON.stringify(made.id)}`, {
timeout: 15000,
label: 'the throwaway project is open',
});
// 50a. A new project has no snapshots; a named one is made from what is saved and listed.
await openSnapshots();
const empty = await json('__gm.snapshots()');
const field = empty?.nameAt;
await click(field.x, field.y);
await typeText('Clean start');
await press('Enter');
await waitUntil('__gm.snapshots()?.rows.length === 1', { label: 'the dialog shows the new snapshot' });
const one = await json('__gm.snapshots()');
const listed = await (await api('GET', `projects/${made.id}/snapshots`)).json();
check(
'Create snapshot copies the saved project under the name typed, and the list shows it',
empty.rows.length === 0 &&
one.rows[0]?.name === 'Clean start' &&
/^\d{4}-\d\d-\d\d \d\d:\d\d$/.test(one.rows[0]?.time ?? '') &&
listed.length === 1 &&
listed[0].revision === (await json('__gm.state.revision')) &&
(await notice()) === 'Snapshot "Clean start" created',
JSON.stringify({ empty, one, listed, notice: await notice() }),
);
await shot('50-snapshots');
await clickLast('Cancel');
// 50b. A later stand: the project renamed through the settings, and saved.
await clickToolbar('menu');
await clickText('Project settings…');
const nameField = await locate(TEMP_NAME, true);
await t.doubleClick(nameField.x, nameField.y);
await press('a', { ctrl: true });
await typeText(RENAMED);
await press('Enter');
await clickText('Apply');
await saved();
const later = {
name: await json('__gm.state.project.name'),
revision: await json('__gm.state.revision'),
};
// 50c. Escape on the question is its Cancel: the list comes back and nothing is restored.
const cameBack = (expression, label) =>
waitUntil(expression, { label }).then(
() => true,
() => false,
);
await openSnapshots();
await clickLast('Clean start');
await clickLast('Restore');
await press('Escape');
const listBack = await cameBack('__gm.snapshots() !== null', 'the list came back');
check(
'Escape on "Restore snapshot?" leads back to the list and restores nothing',
listBack &&
(await json('__gm.state.project.name')) === RENAMED &&
(await json('__gm.state.revision')) === later.revision,
JSON.stringify({ listBack, name: await json('__gm.state.project.name') }),
);
if (listBack) await clickLast('Cancel');
// Restore puts the snapshot back, keeps the renamed stand as "Before restoring …", reloads
// the project with an empty undo history, and tells so.
await openSnapshots();
await clickLast('Clean start');
await clickLast('Restore');
await clickLast('Restore');
await waitUntil('__gm.state.notice.startsWith("Restored")', { label: 'the restore finished' });
const restored = {
name: await json('__gm.state.project.name'),
revision: await json('__gm.state.revision'),
canUndo: await json('__gm.state.commands.canUndo'),
notice: await notice(),
stored: (await stored()).name,
list: (await (await api('GET', `projects/${made.id}/snapshots`)).json()).map((s) => s.name),
};
check(
'Restore brings the snapshot back as a new revision and keeps the replaced stand as a snapshot',
later.name === RENAMED &&
restored.name === TEMP_NAME &&
restored.stored === TEMP_NAME &&
restored.revision === later.revision + 1 &&
restored.canUndo === false &&
JSON.stringify(restored.list) === JSON.stringify(['Before restoring Clean start', 'Clean start']) &&
restored.notice ===
'Restored "Clean start" – the previous stand is kept as "Before restoring Clean start"',
JSON.stringify({ later, restored }),
);
// 50d. That kept stand restores like any other: the restore is taken back.
await openSnapshots();
await clickLast('Before restoring Clean start');
await clickLast('Restore');
await clickLast('Restore');
await waitUntil('__gm.state.notice.startsWith("Restored \\"Before")', {
label: 'the second restore finished',
});
check(
'restoring the kept stand takes the restore back',
(await json('__gm.state.project.name')) === RENAMED && (await stored()).name === RENAMED,
JSON.stringify({ name: await json('__gm.state.project.name'), stored: (await stored()).name }),
);
// 50e. Delete asks, then takes the one picked out of the list.
await openSnapshots();
const before = (await json('__gm.snapshots()')).rows.map((r) => r.name);
await clickLast('Clean start');
await clickLast('Delete');
await clickLast('Delete');
await waitUntil(`__gm.snapshots()?.rows.length === ${before.length - 1}`, {
label: 'the dialog shows one snapshot fewer',
});
const after = (await json('__gm.snapshots()')).rows.map((r) => r.name);
check(
'Delete removes the snapshot picked and nothing else',
before.includes('Clean start') &&
!after.includes('Clean start') &&
after.length === before.length - 1 &&
after.every((name) => before.includes(name)),
JSON.stringify({ before, after }),
);
await clickLast('Cancel');
// 50f. The project goes by the picker. Its question stands in for the picker: Cancel leads back to
// it, and so does the deletion, with the project gone from the list.
await clickToolbar('menu');
await clickText('Open project…');
await waitUntil('__gm.projectPicker() !== null', { label: 'the project picker opened' });
await clickLast('Delete');
await clickLast('Cancel');
const pickerBack = await cameBack('__gm.projectPicker() !== null', 'the picker came back');
check(
'Cancel on "Delete project?" leads back to the picker and keeps the project',
pickerBack && (await stored()).name === RENAMED,
JSON.stringify({ pickerBack, picker: await json('__gm.projectPicker()') }),
);
if (pickerBack) {
await clickLast('Delete');
await clickLast('Delete');
} else await api('DELETE', `projects/${made.id}`);
const pickerAgain = await cameBack(
`__gm.projectPicker() !== null && !__gm.projectPicker().names.includes(${JSON.stringify(RENAMED)})`,
'the picker came back without the project',
);
check(
'deleting from the picker leads back to it, without the project',
pickerAgain,
JSON.stringify(await json('__gm.projectPicker()')),
);
if (pickerAgain) await clickLast('Cancel');
// Deleting the project deletes its snapshots: the route answers 404 afterwards.
const gone = await api('GET', `projects/${made.id}/snapshots`);
check('deleting the project takes its snapshots along', gone.status === 404, String(gone.status));
} finally {
await sweep();
await navigate();
}
}