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>
95 lines
3.5 KiB
TypeScript
95 lines
3.5 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { type Graph, GraphInterpreter } from '../../src/core/graph';
|
||
import {
|
||
fakeContext,
|
||
fakeEntity,
|
||
graphEditor,
|
||
makeBehavior,
|
||
makeSpriteScene,
|
||
place,
|
||
startWorld,
|
||
fire,
|
||
} from '../helpers';
|
||
|
||
// Node-by-node behaviour lives in node-catalog.test.ts; what is left here needs a running
|
||
// world (On click's hit test) or a whole flow (Spawn feeding its object downstream).
|
||
describe('game-flow nodes', () => {
|
||
it('fires On click when the mouse is pressed on the owner', () => {
|
||
const f = makeSpriteScene('Button');
|
||
const behavior = makeBehavior(f, 'OnPress', ['clicks']);
|
||
// On click → clicks = clicks + 1
|
||
const edit = graphEditor(f, behavior);
|
||
const click = edit.addNode('OnClick');
|
||
const get = edit.variables(['clicks']);
|
||
const one = edit.addNode('Number', { value: 1 });
|
||
const add = edit.addNode('Add');
|
||
const set = edit.variables(['clicks'], { write: true });
|
||
edit.wire([
|
||
[click, 'exec', set, 'exec'],
|
||
[get, 'clicks', add, 'a'],
|
||
[one, 'value', add, 'b'],
|
||
[add, 'result', set, 'clicks'],
|
||
]);
|
||
const button = place(f, f.spriteId, { x: 0, y: 0 });
|
||
const logic = place(f, behavior.id, { x: 0, y: 0 }, button);
|
||
|
||
const { world } = startWorld(f, { behaviors: true });
|
||
const entity = world.findByObjectId(logic)!;
|
||
|
||
world.movePointer(500, 500, true); // beside the button
|
||
world.setPointerDown(true);
|
||
world.tick(0.1);
|
||
expect(entity.props.clicks).toBe(0);
|
||
|
||
world.setPointerDown(false);
|
||
world.movePointer(10, 10, true); // on the button
|
||
world.setPointerDown(true);
|
||
world.tick(0.1);
|
||
expect(entity.props.clicks).toBe(1);
|
||
world.tick(0.1); // still held – the press was one frame
|
||
expect(entity.props.clicks).toBe(1);
|
||
});
|
||
});
|
||
|
||
describe('Spawn node', () => {
|
||
// On start → Spawn(crate at (5,7)) → Log(the spawned object)
|
||
const graph: Graph = {
|
||
nodes: [
|
||
{ id: 'start', type: 'OnStart', x: 0, y: 0, params: {} },
|
||
{ id: 'spawn', type: 'Spawn', x: 0, y: 0, params: { asset: 'crate', x: 5, y: 7 } },
|
||
{ id: 'log', type: 'Log', x: 0, y: 0, params: {} },
|
||
],
|
||
edges: [
|
||
{ id: 'e1', from: { node: 'start', port: 'exec' }, to: { node: 'spawn', port: 'exec' } },
|
||
{ id: 'e2', from: { node: 'spawn', port: 'exec' }, to: { node: 'log', port: 'exec' } },
|
||
{ id: 'e3', from: { node: 'spawn', port: 'object' }, to: { node: 'log', port: 'message' } },
|
||
],
|
||
};
|
||
|
||
it('asks the context for a new instance at the inline position and outputs it', () => {
|
||
const calls: unknown[] = [];
|
||
const ctx = fakeContext({
|
||
spawn: (assetId, position, parent) => {
|
||
calls.push({ assetId, position, parent });
|
||
return fakeEntity('Crate', position.x);
|
||
},
|
||
});
|
||
fire(new GraphInterpreter(graph), 'OnStart', ctx);
|
||
expect(calls).toEqual([{ assetId: 'crate', position: { x: 5, y: 7, z: 0 }, parent: null }]);
|
||
expect(ctx.logs).toEqual(['Crate']); // the spawned object flowed into Log
|
||
});
|
||
|
||
it('logs a hint and continues when no asset is selected or the asset is unknown', () => {
|
||
const ctx = fakeContext({ spawn: () => null });
|
||
const noAsset: Graph = {
|
||
...graph,
|
||
nodes: graph.nodes.map((n) => (n.id === 'spawn' ? { ...n, params: {} } : n)),
|
||
};
|
||
fire(new GraphInterpreter(noAsset), 'OnStart', ctx);
|
||
expect(ctx.logs).toEqual(['Spawn: no asset selected', '']);
|
||
const ctx2 = fakeContext({ spawn: () => null });
|
||
fire(new GraphInterpreter(graph), 'OnStart', ctx2);
|
||
expect(ctx2.logs[0]).toBe('Spawn: asset crate not found');
|
||
});
|
||
});
|