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>
81 lines
3.3 KiB
PHP
81 lines
3.3 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* The snapshot store (#231) on a scratch directory. Plain PHP like accounts.test.php; CI runs it
|
||
* with `php tests/php/snapshots.test.php` and reads the last line.
|
||
*/
|
||
|
||
require __DIR__ . '/../../api/lib/AtomicFile.php';
|
||
require __DIR__ . '/../../api/lib/Store.php';
|
||
require __DIR__ . '/../../api/lib/SnapshotStore.php';
|
||
|
||
$failed = 0;
|
||
$passed = 0;
|
||
|
||
function check(string $name, bool $ok, mixed $got = null): void
|
||
{
|
||
global $failed, $passed;
|
||
if ($ok) {
|
||
$passed++;
|
||
echo "PASS {$name}\n";
|
||
return;
|
||
}
|
||
$failed++;
|
||
echo "FAIL {$name}", $got === null ? '' : ' — got ' . json_encode($got), "\n";
|
||
}
|
||
|
||
$dir = sys_get_temp_dir() . '/game-maker-php-snapshots-' . bin2hex(random_bytes(4));
|
||
mkdir($dir);
|
||
$snapshots = new SnapshotStore($dir);
|
||
|
||
check('a project without snapshots lists none', $snapshots->list('p1') === []);
|
||
|
||
$doc = ['revision' => 4, 'project' => ['id' => 'p1', 'name' => 'Game', 'assets' => [['id' => 'a']]]];
|
||
$first = $snapshots->create('p1', $doc, 'first');
|
||
$second = $snapshots->create('p1', ['revision' => 5] + $doc, 'second');
|
||
check('a snapshot keeps its name and the revision it copied', $first['name'] === 'first' && $first['revision'] === 4, $first);
|
||
check('two snapshots get two ids', $first['id'] !== $second['id']);
|
||
check('the summary carries no project', !isset($first['project']));
|
||
check('the snapshot file is not readable for the web server', (fileperms("{$dir}/p1/{$first['id']}.json") & 0o007) === 0);
|
||
|
||
$listed = $snapshots->list('p1');
|
||
check(
|
||
'the list is newest first, also within one second',
|
||
array_column($listed, 'name') === ['second', 'first'],
|
||
array_column($listed, 'name'),
|
||
);
|
||
// The ids are random, so two files come back from the folder in the right order half the time
|
||
// anyway. With eight, the folder's own order matching by chance is one in 40320.
|
||
$more = new SnapshotStore("{$dir}/order");
|
||
foreach (range(1, 8) as $n) {
|
||
$more->create('p', $doc, "n{$n}");
|
||
}
|
||
check(
|
||
'eight made in a row list in reverse order of making',
|
||
array_column($more->list('p'), 'name') === ['n8', 'n7', 'n6', 'n5', 'n4', 'n3', 'n2', 'n1'],
|
||
array_column($more->list('p'), 'name'),
|
||
);
|
||
|
||
$loaded = $snapshots->load('p1', $first['id']);
|
||
check('a snapshot loads the project as it was copied', $loaded !== null && $loaded['project'] === $doc['project'], $loaded);
|
||
check('a snapshot of another project is not found', $snapshots->load('p2', $first['id']) === null);
|
||
|
||
file_put_contents("{$dir}/p1/broken.json", '{"id":"broken"');
|
||
check('a broken file is left out of the list', count($snapshots->list('p1')) === 2, $snapshots->list('p1'));
|
||
unlink("{$dir}/p1/broken.json");
|
||
|
||
check('deleting a snapshot says so', $snapshots->delete('p1', $first['id']));
|
||
check('it is gone from the list', array_column($snapshots->list('p1'), 'name') === ['second']);
|
||
check('deleting it again says it was not there', !$snapshots->delete('p1', $first['id']));
|
||
|
||
$snapshots->create('p2', $doc, 'other');
|
||
$snapshots->deleteAll('p1');
|
||
check('deleting all removes the folder of that project', !is_dir("{$dir}/p1"));
|
||
check('and leaves the snapshots of other projects', count($snapshots->list('p2')) === 1);
|
||
|
||
exec('rm -rf ' . escapeshellarg($dir));
|
||
|
||
echo $failed === 0 ? "ALL PASSED – {$passed} checks\n" : "{$failed} FAILED, {$passed} passed\n";
|
||
exit($failed === 0 ? 0 : 1);
|