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>
210 lines
8.1 KiB
TypeScript
210 lines
8.1 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { AddNodeCommand } from '../../src/core/commands/graphCommands';
|
|
import { PasteGraphCommand } from '../../src/core/commands/pasteCommands';
|
|
import { AddVariableCommand } from '../../src/core/commands/variableCommands';
|
|
import {
|
|
blockOf,
|
|
blockProblem,
|
|
FIND,
|
|
findNode,
|
|
type GraphNode,
|
|
GraphInterpreter,
|
|
MATCH,
|
|
matchOf,
|
|
} from '../../src/core/graph';
|
|
import type { PropertyValue } from '../../src/core/model/Properties';
|
|
import { blockFrames, BLOCK_PAD } from '../../src/editor/graph/blockFrames';
|
|
import { fakeContext, graphEditor, makeBehavior, makeProject, fire } from '../helpers';
|
|
|
|
/** A behavior with a list variable `names` and a Find over it, the Match it brought along beside it. */
|
|
function block() {
|
|
const fixture = makeProject();
|
|
const behavior = makeBehavior(fixture);
|
|
fixture.stack.execute(new AddVariableCommand(behavior.id, 'names', 'list'));
|
|
const edit = graphEditor(fixture, behavior);
|
|
const add = new AddNodeCommand(behavior.id, FIND, 0, 0);
|
|
fixture.stack.execute(add);
|
|
const find = add.node;
|
|
const match = add.closing!;
|
|
return { ...fixture, behavior, edit, find, match };
|
|
}
|
|
|
|
/**
|
|
* On start: Find over `names`, the test wired from `item` (or `index`) through an Equals into the
|
|
* Match. "found" logs item and index of the hit, "not found" logs "none" and the index it hands on.
|
|
*/
|
|
function search(names: readonly PropertyValue[], wanted: PropertyValue, by: 'item' | 'index' = 'item') {
|
|
const { behavior, edit, find, match } = block();
|
|
const start = edit.addNode('OnStart');
|
|
const get = edit.variables(['names']);
|
|
const equals = edit.addNode('Equals', { b: wanted });
|
|
const hit = edit.addNode('Log');
|
|
const miss = edit.addNode('Log');
|
|
const line = edit.addNode('Join');
|
|
const none = edit.addNode('Join', { a: 'none' });
|
|
edit.wire([
|
|
[start, 'exec', find, 'exec'],
|
|
[get, 'names', find, 'list'],
|
|
[find, by, equals, 'a'],
|
|
[equals, 'result', match, 'match'],
|
|
[match, 'found', hit, 'exec'],
|
|
[match, 'item', line, 'a'],
|
|
[match, 'index', line, 'b'],
|
|
[line, 'text', hit, 'message'],
|
|
[match, 'notFound', miss, 'exec'],
|
|
[match, 'index', none, 'b'],
|
|
[none, 'text', miss, 'message'],
|
|
]);
|
|
const ctx = fakeContext({ variables: { names: [...names] } });
|
|
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
|
return ctx.logs;
|
|
}
|
|
|
|
describe('Find … Match at run time', () => {
|
|
it('goes on at found with the first element the test is true for', () => {
|
|
expect(search(['a', 'b', 'c', 'b'], 'b')).toEqual(['b1']);
|
|
});
|
|
|
|
it('reads the test anew for every element, not only for the first', () => {
|
|
expect(search(['a', 'b', 'c'], 'c')).toEqual(['c2']);
|
|
});
|
|
|
|
it('takes a test built from index as well', () => {
|
|
expect(search(['a', 'b', 'c'], 1, 'index')).toEqual(['b1']);
|
|
});
|
|
|
|
it('goes on at not found with index -1 when nothing matches', () => {
|
|
expect(search(['a', 'b'], 'x')).toEqual(['none-1']);
|
|
});
|
|
|
|
it('goes on at not found for an empty list', () => {
|
|
expect(search([], 'a')).toEqual(['none-1']);
|
|
});
|
|
|
|
it('reports a Find no Match tests, and nothing runs after it', () => {
|
|
const { behavior, edit, find, match } = block();
|
|
const start = edit.addNode('OnStart');
|
|
const after = edit.addNode('Log', { message: 'after' });
|
|
edit.wire([
|
|
[start, 'exec', find, 'exec'],
|
|
[match, 'found', after, 'exec'],
|
|
[match, 'notFound', after, 'exec'],
|
|
]);
|
|
const ctx = fakeContext({ variables: { names: ['a'] } });
|
|
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
|
expect(ctx.logs).toEqual(['Find: No Match tests its item']);
|
|
});
|
|
});
|
|
|
|
describe('the block is the wiring', () => {
|
|
it('adding a Find brings its Match beside it, and one undo takes both', () => {
|
|
const { behavior, stack, find, match } = block();
|
|
expect(match.type).toBe(MATCH);
|
|
expect(match.x).toBeGreaterThan(find.x);
|
|
expect(match.y).toBe(find.y);
|
|
expect(behavior.graph.nodes.map((n) => n.type)).toEqual([FIND, MATCH]);
|
|
stack.undo();
|
|
expect(behavior.graph.nodes).toEqual([]);
|
|
});
|
|
|
|
it('pairs a Match with the Find its test is built from; the block is what lies between', () => {
|
|
const { behavior, edit, find, match } = block();
|
|
const expand = edit.addNode('Not');
|
|
const equals = edit.addNode('Equals');
|
|
const wanted = edit.addNode('String', { value: 'x' });
|
|
expect(matchOf(behavior.graph, find.id)).toBeNull();
|
|
expect(blockProblem(behavior.graph, find)).toBe('No Match tests its item');
|
|
expect(blockProblem(behavior.graph, match)).toBe('match is not built from a Find');
|
|
edit.wire([
|
|
[find, 'item', expand, 'value'],
|
|
[expand, 'result', equals, 'a'],
|
|
[wanted, 'value', equals, 'b'],
|
|
[equals, 'result', match, 'match'],
|
|
]);
|
|
expect(matchOf(behavior.graph, find.id)?.id).toBe(match.id);
|
|
// The literal feeds the test but does not come from the Find: it stays outside the frame.
|
|
const ids = blockOf(behavior.graph, find)!;
|
|
expect([ids[0], ids.at(-1)]).toEqual([find.id, match.id]);
|
|
expect(ids.slice(1, -1).sort()).toEqual([equals.id, expand.id].sort());
|
|
expect(blockProblem(behavior.graph, find)).toBeNull();
|
|
expect(blockProblem(behavior.graph, match)).toBeNull();
|
|
});
|
|
|
|
it('a second Match on the same Find closes nothing, and both say so', () => {
|
|
const { behavior, edit, find, match } = block();
|
|
const other = edit.addNode(MATCH);
|
|
const one = edit.addNode('Not');
|
|
const two = edit.addNode('Not');
|
|
edit.wire([
|
|
[find, 'item', one, 'value'],
|
|
[one, 'result', match, 'match'],
|
|
[find, 'item', two, 'value'],
|
|
[two, 'result', other, 'match'],
|
|
]);
|
|
expect(matchOf(behavior.graph, find.id)).toBeNull();
|
|
expect(blockProblem(behavior.graph, find)).toBe('2 Matches test its item');
|
|
expect(blockProblem(behavior.graph, match)).toBe('Another Match tests its Find');
|
|
});
|
|
|
|
it('a Match built from two Finds closes neither', () => {
|
|
const { behavior, edit, find, match } = block();
|
|
const second = edit.addNode(FIND);
|
|
const and = edit.addNode('And');
|
|
edit.wire([
|
|
[find, 'item', and, 'a'],
|
|
[second, 'item', and, 'b'],
|
|
[and, 'result', match, 'match'],
|
|
]);
|
|
expect(matchOf(behavior.graph, find.id)).toBeNull();
|
|
expect(blockProblem(behavior.graph, match)).toBe('match is built from 2 Finds');
|
|
expect(blockProblem(behavior.graph, find)).toBe('Its Match tests another Find');
|
|
});
|
|
|
|
it('a pasted copy forms a block of its own, paired with its own Find', () => {
|
|
const { behavior, stack, edit, find, match } = block();
|
|
const not = edit.addNode('Not');
|
|
edit.wire([
|
|
[find, 'item', not, 'value'],
|
|
[not, 'result', match, 'match'],
|
|
]);
|
|
const nodes = behavior.graph.nodes.map((n) => structuredClone(n));
|
|
const edges = behavior.graph.edges.map((e) => structuredClone(e));
|
|
const paste = new PasteGraphCommand(
|
|
behavior.id,
|
|
{ kind: 'graph', nodes, edges, annotations: [] },
|
|
{ x: 0, y: 400 },
|
|
);
|
|
stack.execute(paste);
|
|
const [copyFind, copyMatch] = paste.nodeIds.map((id) => findNode(behavior.graph, id)!);
|
|
expect(matchOf(behavior.graph, find.id)?.id).toBe(match.id);
|
|
expect(matchOf(behavior.graph, copyFind!.id)?.id).toBe(copyMatch!.id);
|
|
});
|
|
});
|
|
|
|
describe('block frames', () => {
|
|
it('frame the nodes of a closed block with room to spare, and nothing else', () => {
|
|
const { behavior, edit, find, match } = block();
|
|
const not = edit.addNode('Not');
|
|
const stray = edit.addNode('Not');
|
|
const box = (node: GraphNode, x: number, y: number) => ({ id: node.id, x, y, w: 100, h: 50 });
|
|
const boxes = new Map([
|
|
[find.id, box(find, 0, 0)],
|
|
[not.id, box(not, 200, 60)],
|
|
[match.id, box(match, 400, 30)],
|
|
[stray.id, box(stray, 900, 900)],
|
|
]);
|
|
const at = (id: string) => boxes.get(id) ?? null;
|
|
expect(blockFrames(behavior.graph, at)).toEqual([]);
|
|
edit.wire([
|
|
[find, 'item', not, 'value'],
|
|
[not, 'result', match, 'match'],
|
|
]);
|
|
expect(blockFrames(behavior.graph, at)).toEqual([
|
|
{
|
|
find: find.id,
|
|
bounds: { minX: -BLOCK_PAD, minY: -BLOCK_PAD, maxX: 500 + BLOCK_PAD, maxY: 110 + BLOCK_PAD },
|
|
},
|
|
]);
|
|
});
|
|
});
|