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>
This commit is contained in:
1 parent
bc688f3efc
commit
14972d057f
81 files changed
+663
-577
No files matched your search
@@ -13,7 +13,7 @@ import {
|
||||
} from '../../src/core/graph';
|
||||
import { graphLookup } from '../../src/core/model/graphLookup';
|
||||
import type { BehaviorAsset } from '../../src/core/model/Asset';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, type ProjectFixture } from '../helpers';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, type ProjectFixture, fire } from '../helpers';
|
||||
|
||||
function sequenceFixture() {
|
||||
const fixture = makeProject();
|
||||
@@ -76,7 +76,7 @@ describe('a Sequence sized by what is plugged into it', () => {
|
||||
edit.connect(node, port, edit.addNode('Log', { message: port.toUpperCase() }), 'exec');
|
||||
|
||||
const ctx = fakeContext();
|
||||
new GraphInterpreter(behavior.graph, graphLookup(fixture.project)).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph, graphLookup(fixture.project)), 'OnStart', ctx);
|
||||
expect(ctx.logs).toEqual(['A', 'B']);
|
||||
});
|
||||
});
|
||||
@@ -122,7 +122,7 @@ describe('a struct travelling through a nested behavior', () => {
|
||||
// One edge into the call, where two numbers would need two.
|
||||
expect(caller.graph.edges.filter((e) => e.to.node === call.id && e.to.port !== 'exec')).toHaveLength(1);
|
||||
const ctx = fakeContext({ variables: { result: 0 } });
|
||||
new GraphInterpreter(caller.graph, graphLookup(fixture.project)).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(caller.graph, graphLookup(fixture.project)), 'OnStart', ctx);
|
||||
expect(ctx.variables.result).toBe(42);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
} 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 } from '../helpers';
|
||||
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() {
|
||||
@@ -56,7 +56,7 @@ function search(names: readonly PropertyValue[], wanted: PropertyValue, by: 'ite
|
||||
[none, 'text', miss, 'message'],
|
||||
]);
|
||||
const ctx = fakeContext({ variables: { names: [...names] } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
return ctx.logs;
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ describe('Find … Match at run time', () => {
|
||||
[match, 'notFound', after, 'exec'],
|
||||
]);
|
||||
const ctx = fakeContext({ variables: { names: ['a'] } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
expect(ctx.logs).toEqual(['Find: No Match tests its item']);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
import { graphLookup } from '../../src/core/model/graphLookup';
|
||||
import type { PropertyValue } from '../../src/core/model/Properties';
|
||||
import { paletteEntries, rankedEntries } from '../../src/editor/graph/paletteEntries';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject } from '../helpers';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, fire } from '../helpers';
|
||||
|
||||
/** A behavior with a list variable `names`, a Find over it testing `item == wanted`, and its Match. */
|
||||
function block(wanted: PropertyValue = 'b', mode?: FindMode) {
|
||||
@@ -54,7 +54,7 @@ function answer(mode: FindMode, names: readonly PropertyValue[], ports: string[]
|
||||
from = { node: log, port: 'exec' };
|
||||
}
|
||||
const ctx = fakeContext({ variables: { names: [...names] } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
return ctx.logs;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
foldMarkLabel,
|
||||
foldMarkRadius,
|
||||
} from '../../src/editor/graph/folding';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, roundTrip } from '../helpers';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, roundTrip, fire } from '../helpers';
|
||||
|
||||
/** "On start → Move(self, dx = 7)": two edges, so a fold can hide one and leave the other. */
|
||||
function mover() {
|
||||
@@ -38,7 +38,7 @@ describe('folding an edge', () => {
|
||||
expect(behavior.graph.edges.every((e) => e.folded === true)).toBe(true);
|
||||
|
||||
const ctx = fakeContext();
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
expect(ctx.self.x).toBe(7);
|
||||
// And the edges themselves are untouched apart from the flag.
|
||||
expect(findAsset(project, behavior.id)).toBe(behavior);
|
||||
|
||||
+18
-17
@@ -37,6 +37,7 @@ import {
|
||||
makeSpriteScene,
|
||||
place,
|
||||
roundTrip,
|
||||
fire,
|
||||
} from '../helpers';
|
||||
|
||||
/** Adds "On key held(ArrowRight) → Move(owner, dx = speed × dt)" to a project, built through commands like the editor would. */
|
||||
@@ -103,10 +104,10 @@ describe('GraphInterpreter', () => {
|
||||
const { behavior } = withMover(makeProject());
|
||||
const interpreter = new GraphInterpreter(behavior.graph);
|
||||
const ctx = fakeContext({ variables: { speed: 100 } });
|
||||
const fired = interpreter.fire('OnKeyHeld', ctx, { dt: 0.5 }, (n) => n.params.key === 'ArrowRight');
|
||||
const fired = fire(interpreter, 'OnKeyHeld', ctx, { dt: 0.5 }, (n) => n.params.key === 'ArrowRight');
|
||||
expect(fired).toBe(1);
|
||||
expect(ctx.owner!.x).toBe(50);
|
||||
expect(interpreter.fire('OnKeyHeld', ctx, { dt: 0.5 }, (n) => n.params.key === 'ArrowLeft')).toBe(0);
|
||||
expect(fire(interpreter, 'OnKeyHeld', ctx, { dt: 0.5 }, (n) => n.params.key === 'ArrowLeft')).toBe(0);
|
||||
expect(ctx.owner!.x).toBe(50);
|
||||
});
|
||||
|
||||
@@ -120,7 +121,7 @@ describe('GraphInterpreter', () => {
|
||||
[self, 'object', move, 'object'],
|
||||
]);
|
||||
const ctx = fakeContext();
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
expect(ctx.self.x).toBe(7);
|
||||
expect(ctx.self.y).toBe(-3);
|
||||
});
|
||||
@@ -137,7 +138,7 @@ describe('GraphInterpreter', () => {
|
||||
[clamp, 'result', move, 'dy'],
|
||||
]);
|
||||
const ctx = fakeContext();
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
expect(ctx.self.x).toBe(4); // scale defaulted to 1, not 0
|
||||
expect(ctx.self.y).toBe(1); // clamp(5, 0, 1)
|
||||
});
|
||||
@@ -162,7 +163,7 @@ describe('GraphInterpreter', () => {
|
||||
const interpreter = new GraphInterpreter(behavior.graph);
|
||||
const run = (keys: string[]) => {
|
||||
const ctx = fakeContext({ variables: { speed: 100 }, dt: 0.5, isKeyDown: (k) => keys.includes(k) });
|
||||
interpreter.fire('OnUpdate', ctx, { dt: 0.5 });
|
||||
fire(interpreter, 'OnUpdate', ctx, { dt: 0.5 });
|
||||
return { x: ctx.owner!.x, y: ctx.owner!.y };
|
||||
};
|
||||
expect(run([])).toEqual({ x: 0, y: 0 });
|
||||
@@ -199,7 +200,7 @@ describe('GraphInterpreter', () => {
|
||||
previous = log;
|
||||
});
|
||||
const ctx = fakeContext();
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
expect(ctx.logs).toEqual(['0.6', '5', '4', '2', '-1', '3']);
|
||||
});
|
||||
|
||||
@@ -226,7 +227,7 @@ describe('GraphInterpreter', () => {
|
||||
|
||||
const interpreter = new GraphInterpreter(behavior.graph);
|
||||
const ctx = fakeContext({ variables: { count: 0 } });
|
||||
for (let i = 0; i < 5; i++) interpreter.fire('OnStart', ctx);
|
||||
for (let i = 0; i < 5; i++) fire(interpreter, 'OnStart', ctx);
|
||||
expect(ctx.variables.count).toBe(3);
|
||||
expect(ctx.logs).toEqual(['then', 'then', 'then', 'else', 'else']);
|
||||
});
|
||||
@@ -237,7 +238,7 @@ describe('GraphInterpreter', () => {
|
||||
const edges: string[] = [];
|
||||
const visited: string[] = [];
|
||||
interpreter.trace = { onEdge: (id) => edges.push(id), onNode: (id) => visited.push(id) };
|
||||
interpreter.fire('OnKeyHeld', fakeContext(), { dt: 1 });
|
||||
fire(interpreter, 'OnKeyHeld', fakeContext(), { dt: 1 });
|
||||
expect(edges).toHaveLength(1);
|
||||
expect(visited).toContain(nodes.mul.id);
|
||||
expect(visited).toContain(nodes.move.id);
|
||||
@@ -253,7 +254,7 @@ describe('GraphInterpreter', () => {
|
||||
[a, 'a', b, 'exec'],
|
||||
[b, 'a', a, 'exec'],
|
||||
]);
|
||||
expect(() => new GraphInterpreter(behavior.graph).fire('OnStart', fakeContext())).toThrow(
|
||||
expect(() => fire(new GraphInterpreter(behavior.graph), 'OnStart', fakeContext())).toThrow(
|
||||
/nested too deeply/,
|
||||
);
|
||||
});
|
||||
@@ -267,7 +268,7 @@ describe('GraphInterpreter', () => {
|
||||
[start, 'exec', repeat, 'exec'],
|
||||
[repeat, 'body', log, 'exec'],
|
||||
]);
|
||||
expect(() => new GraphInterpreter(behavior.graph).fire('OnStart', fakeContext())).toThrow(/step limit/);
|
||||
expect(() => fire(new GraphInterpreter(behavior.graph), 'OnStart', fakeContext())).toThrow(/step limit/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -287,7 +288,7 @@ describe('Object by name', () => {
|
||||
edit.wire([[join, 'text', byName, 'name']]);
|
||||
}
|
||||
const ctx = fakeContext({ findByName: (name) => (name === 'Target' ? fakeEntity('Target') : null) });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
return ctx.logs;
|
||||
}
|
||||
|
||||
@@ -327,7 +328,7 @@ describe('Repeat', () => {
|
||||
it('runs the body once per round, each round seeing its own index', () => {
|
||||
const { behavior } = summingLoop(5);
|
||||
const ctx = fakeContext({ variables: { count: 0 } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
// 0+1+2+3+4. A stale data cache would add the first round's index five times (0) instead.
|
||||
expect(ctx.variables.count).toBe(10);
|
||||
});
|
||||
@@ -335,7 +336,7 @@ describe('Repeat', () => {
|
||||
it('runs nothing at all for a count of zero', () => {
|
||||
const { behavior } = summingLoop(0);
|
||||
const ctx = fakeContext({ variables: { count: 7 } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
expect(ctx.variables.count).toBe(7);
|
||||
});
|
||||
|
||||
@@ -344,7 +345,7 @@ describe('Repeat', () => {
|
||||
const done = edit.addNode('Log', { message: 'done' });
|
||||
edit.wire([[nodes.repeat, 'done', done, 'exec']]);
|
||||
const ctx = fakeContext({ variables: { count: 0 } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
expect({ count: ctx.variables.count, logs: ctx.logs }).toEqual({ count: 3, logs: ['done'] });
|
||||
});
|
||||
|
||||
@@ -364,7 +365,7 @@ describe('Repeat', () => {
|
||||
[add, 'result', set, 'count'],
|
||||
]);
|
||||
const ctx = fakeContext({ variables: { count: 0 } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
expect(ctx.variables.count).toBe(12);
|
||||
});
|
||||
|
||||
@@ -387,7 +388,7 @@ describe('Repeat', () => {
|
||||
[afterAdd, 'result', after, 'count'],
|
||||
]);
|
||||
const ctx = fakeContext({ variables: { count: 0 } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
// 3 rounds, then ×10 on the *current* count – a cache left over from the last round would give 20.
|
||||
expect(ctx.variables.count).toBe(30);
|
||||
});
|
||||
@@ -429,7 +430,7 @@ describe('For each', () => {
|
||||
]);
|
||||
}
|
||||
const ctx = fakeContext({ variables: { names: [...names] } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
return ctx.logs;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
place,
|
||||
roundTrip,
|
||||
startWorld,
|
||||
fire,
|
||||
} from '../helpers';
|
||||
|
||||
/** Three boxes, all top level: a at (100,100,z1), b at (10,0), c at (0,5). */
|
||||
@@ -264,7 +265,7 @@ describe('runtime hierarchy', () => {
|
||||
],
|
||||
};
|
||||
const ctx = fakeContext({ self: child, owner: parent, variables: {} });
|
||||
new GraphInterpreter(graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(graph), 'OnStart', ctx);
|
||||
expect(ctx.logs).toEqual(['105', '5', '100']);
|
||||
});
|
||||
});
|
||||
@@ -105,17 +105,12 @@ describe('HttpFileStore', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('lists and deletes', async () => {
|
||||
it('lists', async () => {
|
||||
stubFetch(JSON.stringify([{ file: FILE, width: 8, height: 4, bytes: 12, updatedAt: 'now' }]), {
|
||||
status: 200,
|
||||
});
|
||||
await expect(new HttpFileStore('/api', 'textures').list()).resolves.toEqual([
|
||||
{ file: FILE, width: 8, height: 4, bytes: 12, updatedAt: 'now' },
|
||||
]);
|
||||
|
||||
const mock = stubFetch(null, { status: 204 });
|
||||
await new HttpFileStore('/api', 'textures').delete(FILE);
|
||||
expect(mock.mock.calls[0]![0]).toBe(`/api?r=${encodeURIComponent(`textures/${FILE}`)}`);
|
||||
expect(mock.mock.calls[0]![1]?.method).toBe('DELETE');
|
||||
});
|
||||
});
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
} from '../../src/core/graph';
|
||||
import { BUILT_IN_NODES } from '../../src/core/graph/nodes';
|
||||
import { graphLookup } from '../../src/core/model/graphLookup';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject } from '../helpers';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, fire } from '../helpers';
|
||||
|
||||
const untouched = { params: {} };
|
||||
|
||||
@@ -31,7 +31,7 @@ function logged(
|
||||
...(source === text ? [] : ([[source, port, text, 'value']] as const)),
|
||||
]);
|
||||
const ctx = fakeContext();
|
||||
new GraphInterpreter(behavior.graph, graphLookup(fixture.project)).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph, graphLookup(fixture.project)), 'OnStart', ctx);
|
||||
return ctx.logs;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
type ProjectFixture,
|
||||
roundTrip,
|
||||
startWorld,
|
||||
fire,
|
||||
} from '../helpers';
|
||||
|
||||
/**
|
||||
@@ -98,7 +99,7 @@ function threeCalls(fixture: ProjectFixture, callee: BehaviorAsset): unknown[][]
|
||||
const ctx = fakeContext({ variables: { r0: 0, r1: 0 } });
|
||||
const seen: unknown[][] = [];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
seen.push([ctx.variables.r0, ctx.variables.r1]);
|
||||
}
|
||||
return seen;
|
||||
@@ -151,9 +152,9 @@ describe('Keep on a behavior variable (#229)', () => {
|
||||
const caller = callerOf(fixture, counter(fixture), 2);
|
||||
const interpreter = new GraphInterpreter(caller.graph, graphLookup(fixture.project));
|
||||
const ctx = fakeContext({ variables: { r0: 0, r1: 0 } });
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
expect([ctx.variables.r0, ctx.variables.r1]).toEqual([1, 1]);
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
expect([ctx.variables.r0, ctx.variables.r1]).toEqual([2, 2]);
|
||||
// The caller's own bag never learns of `count`: the kept values stay the callee's.
|
||||
expect('count' in ctx.variables).toBe(false);
|
||||
@@ -196,8 +197,8 @@ describe('Keep on a behavior variable (#229)', () => {
|
||||
const caller = callerOf(fixture, middle, 2);
|
||||
const interpreter = new GraphInterpreter(caller.graph, graphLookup(fixture.project));
|
||||
const ctx = fakeContext({ variables: { r0: 0, r1: 0 } });
|
||||
interpreter.fire('OnStart', ctx);
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
// One shared inner counter would have handed out 1, 2, 3, 4.
|
||||
expect([ctx.variables.r0, ctx.variables.r1]).toEqual([2, 2]);
|
||||
});
|
||||
@@ -234,8 +235,8 @@ describe('Keep on a behavior variable (#229)', () => {
|
||||
);
|
||||
const interpreter = new GraphInterpreter(caller.graph, graphLookup(fixture.project));
|
||||
const ctx = fakeContext();
|
||||
interpreter.fire('OnStart', ctx);
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
expect(ctx.logs).toEqual(['first', 'first']);
|
||||
// A local machine of the callee is none of the caller's business either.
|
||||
expect(ctx.states).toEqual({});
|
||||
|
||||
@@ -15,7 +15,15 @@ import { graphLookup } from '../../src/core/model/graphLookup';
|
||||
import { BehaviorSystem } from '../../src/runtime/systems/BehaviorSystem';
|
||||
import { Input } from '../../src/runtime/systems/Input';
|
||||
import { World } from '../../src/runtime/World';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, makeSpriteScene, place } from '../helpers';
|
||||
import {
|
||||
fakeContext,
|
||||
graphEditor,
|
||||
makeBehavior,
|
||||
makeProject,
|
||||
makeSpriteScene,
|
||||
place,
|
||||
fire,
|
||||
} from '../helpers';
|
||||
|
||||
function recorder() {
|
||||
const seen = new Map<Graph, Map<string, Record<string, Value>>>();
|
||||
@@ -47,7 +55,7 @@ describe('live values', () => {
|
||||
const interpreter = new GraphInterpreter(behavior.graph, graphLookup(fixture.project));
|
||||
interpreter.trace = listener;
|
||||
const ctx = fakeContext({ variables: { count: 5, other: 9 } });
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
ctx.variables.count = 99;
|
||||
|
||||
const values = seen.get(behavior.graph)!;
|
||||
@@ -77,7 +85,7 @@ describe('live values', () => {
|
||||
const { seen, listener } = recorder();
|
||||
const interpreter = new GraphInterpreter(caller.graph, graphLookup(fixture.project));
|
||||
interpreter.trace = listener;
|
||||
interpreter.fire('OnStart', fakeContext());
|
||||
fire(interpreter, 'OnStart', fakeContext());
|
||||
|
||||
expect(seen.get(callee.graph)?.get(start.id)).toEqual({ in1: 21 });
|
||||
expect(seen.get(callee.graph)?.get(times.id)).toEqual({ result: 42 });
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
import type { BehaviorAsset } from '../../src/core/model/Asset';
|
||||
import { graphLookup } from '../../src/core/model/graphLookup';
|
||||
import { deserializeProject, serializeProject } from '../../src/core/store/serialize';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, type ProjectFixture } from '../helpers';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, type ProjectFixture, fire } from '../helpers';
|
||||
|
||||
/**
|
||||
* A behavior that doubles what it is given: `in1` → Multiply by 2 → `out1`, reached through its
|
||||
@@ -57,7 +57,7 @@ function callerOf(fixture: ProjectFixture, callee: BehaviorAsset, input: number)
|
||||
function runOnStart(fixture: ProjectFixture, asset: BehaviorAsset, variables: Record<string, number> = {}) {
|
||||
const interpreter = new GraphInterpreter(asset.graph, graphLookup(fixture.project));
|
||||
const ctx = fakeContext({ variables });
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
return { ctx, interpreter };
|
||||
}
|
||||
|
||||
@@ -367,7 +367,7 @@ describe('declared ports', () => {
|
||||
const caller = loaded.assets.find((a) => a.name === 'Caller') as BehaviorAsset;
|
||||
const interpreter = new GraphInterpreter(caller.graph, graphLookup(loaded));
|
||||
const ctx = fakeContext({ variables: { result: 0 } });
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
expect(ctx.variables.result).toBe(12);
|
||||
});
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
makeSpriteScene,
|
||||
place,
|
||||
startWorld,
|
||||
fire,
|
||||
} from '../helpers';
|
||||
|
||||
// Node-by-node behaviour lives in node-catalog.test.ts; what is left here needs a running
|
||||
@@ -73,7 +74,7 @@ describe('Spawn node', () => {
|
||||
return fakeEntity('Crate', position.x);
|
||||
},
|
||||
});
|
||||
new GraphInterpreter(graph).fire('OnStart', ctx);
|
||||
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
|
||||
});
|
||||
@@ -84,10 +85,10 @@ describe('Spawn node', () => {
|
||||
...graph,
|
||||
nodes: graph.nodes.map((n) => (n.id === 'spawn' ? { ...n, params: {} } : n)),
|
||||
};
|
||||
new GraphInterpreter(noAsset).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(noAsset), 'OnStart', ctx);
|
||||
expect(ctx.logs).toEqual(['Spawn: no asset selected', '']);
|
||||
const ctx2 = fakeContext({ spawn: () => null });
|
||||
new GraphInterpreter(graph).fire('OnStart', ctx2);
|
||||
fire(new GraphInterpreter(graph), 'OnStart', ctx2);
|
||||
expect(ctx2.logs[0]).toBe('Spawn: asset crate not found');
|
||||
});
|
||||
});
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
} from '../../src/core/model/hierarchy';
|
||||
import { instanceChildId, resolvedHolder } from '../../src/core/model/instance';
|
||||
import { findAsset, findScene } from '../../src/core/model/Project';
|
||||
import { groupName, objectName } from '../../src/core/model/SceneObject';
|
||||
import { serializeProject } from '../../src/core/store/serialize';
|
||||
import { makeSpriteScene, place, roundTrip } from '../helpers';
|
||||
|
||||
@@ -211,3 +212,16 @@ describe('hierarchy groups (#241)', () => {
|
||||
expect(content).toEqual([x, z, y].map((id) => instanceChildId(instance, id!)));
|
||||
});
|
||||
});
|
||||
|
||||
describe('the names the lists show', () => {
|
||||
it('call an object by its own name, else by its asset, and "?" once the asset is gone', () => {
|
||||
expect(objectName({ name: 'Hero' }, { name: 'Sprite' })).toBe('Hero');
|
||||
expect(objectName({ name: '' }, { name: 'Sprite' })).toBe('Sprite');
|
||||
expect(objectName({ name: '' }, undefined)).toBe('?');
|
||||
});
|
||||
|
||||
it('call a group that was never named a "Group"', () => {
|
||||
expect(groupName({ id: 'g', name: '' })).toBe('Group');
|
||||
expect(groupName({ id: 'g', name: 'Crates' })).toBe('Crates');
|
||||
});
|
||||
});
|
||||
@@ -26,7 +26,7 @@ import type { BehaviorAsset, StructAsset } from '../../src/core/model/Asset';
|
||||
import { graphLookup } from '../../src/core/model/graphLookup';
|
||||
import { outputStructType, structTypeUsages } from '../../src/core/model/structTypes';
|
||||
import { deserializeProject, serializeProject } from '../../src/core/store/serialize';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, type ProjectFixture } from '../helpers';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, type ProjectFixture, fire } from '../helpers';
|
||||
|
||||
/** `Loot` = { Name: "Coin", Amount: 1 } */
|
||||
function lootType(f: ProjectFixture): StructAsset {
|
||||
@@ -126,7 +126,7 @@ describe('splitting an output', () => {
|
||||
{ Name: 'Gem', Amount: 7 },
|
||||
];
|
||||
const ctx = fakeContext({ variables: { bag } });
|
||||
new GraphInterpreter(behavior.graph).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph), 'OnStart', ctx);
|
||||
expect(ctx.logs).toEqual(['7']);
|
||||
});
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
import type { BehaviorAsset } from '../../src/core/model/Asset';
|
||||
import { graphLookup } from '../../src/core/model/graphLookup';
|
||||
import type { PropertyBag } from '../../src/core/model/Properties';
|
||||
import { fakeContext, fakeEntity, graphEditor, makeBehavior, makeProject, roundTrip } from '../helpers';
|
||||
import { fakeContext, fakeEntity, graphEditor, makeBehavior, makeProject, roundTrip, fire } from '../helpers';
|
||||
|
||||
const SPRITE = { [OBJECT_TYPE_PARAM]: 'sprite' };
|
||||
const set = (key: string): DeclaredPort => ({ key, kind: 'number', write: true });
|
||||
@@ -46,7 +46,7 @@ function onSelf(ports: DeclaredPort[], params: PropertyBag = SPRITE) {
|
||||
function run(behavior: BehaviorAsset, fixture: ReturnType<typeof makeProject>, props: PropertyBag) {
|
||||
const self = { ...fakeEntity('self'), props: { ...props } };
|
||||
const ctx = fakeContext({ self, variables: { seen: 0 } });
|
||||
new GraphInterpreter(behavior.graph, graphLookup(fixture.project)).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph, graphLookup(fixture.project)), 'OnStart', ctx);
|
||||
return { props: self.props, seen: ctx.variables.seen };
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ describe('a node that reads and writes properties of an object', () => {
|
||||
]);
|
||||
const self_ = { ...fakeEntity('self'), props: { coins: 5 } };
|
||||
const interpreter = new GraphInterpreter(behavior.graph, graphLookup(fixture.project));
|
||||
for (let i = 0; i < 2; i++) interpreter.fire('OnStart', fakeContext({ self: self_ }));
|
||||
for (let i = 0; i < 2; i++) fire(interpreter, 'OnStart', fakeContext({ self: self_ }));
|
||||
expect(self_.props.coins).toBe(7);
|
||||
});
|
||||
|
||||
|
||||
@@ -329,7 +329,7 @@ describe('compileShader', () => {
|
||||
wrong.link(doubled, 'result', out, 'alpha');
|
||||
const build = compileShader(wrong.asset().graph);
|
||||
expect(build.compiled).toBeNull();
|
||||
expect(build.problems).toEqual({ [out]: '"alpha" needs a number, not a colour' });
|
||||
expect(build.problems).toEqual({ [out]: '"alpha" needs a number, not a color' });
|
||||
});
|
||||
|
||||
it('names the problem on the node, and compiles nothing while there is one', () => {
|
||||
@@ -345,8 +345,8 @@ describe('compileShader', () => {
|
||||
s.link(mixed, 'result', output, 'color');
|
||||
let build = compileShader(s.asset().graph);
|
||||
expect(build.compiled).toBeNull();
|
||||
expect(build.problems).toEqual({ [mixed]: '"t" needs a number, not a colour' });
|
||||
expect(build.reason).toBe('"t" needs a number, not a colour');
|
||||
expect(build.problems).toEqual({ [mixed]: '"t" needs a number, not a color' });
|
||||
expect(build.reason).toBe('"t" needs a number, not a color');
|
||||
|
||||
const dup = shaderEditor(f, 'Dup');
|
||||
dup.add(SHADER_OUTPUT);
|
||||
|
||||
@@ -51,6 +51,7 @@ import {
|
||||
type ProjectFixture,
|
||||
roundTrip,
|
||||
startWorld,
|
||||
fire,
|
||||
} from '../helpers';
|
||||
|
||||
/** A machine "Doors" with the states Open and Closed, added the way the tab does it. */
|
||||
@@ -250,12 +251,12 @@ describe('a running state machine', () => {
|
||||
const behavior = build(f, machine.id, machine.ids);
|
||||
const ctx = fakeContext();
|
||||
const interpreter = new GraphInterpreter(behavior.graph, graphLookup(f.project));
|
||||
interpreter.fire('OnUpdate', ctx);
|
||||
fire(interpreter, 'OnUpdate', ctx);
|
||||
expect(ctx.logs).toEqual([]);
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
expect(ctx.states).toEqual({ [machine.id]: machine.ids[0] });
|
||||
interpreter.fire('OnUpdate', ctx);
|
||||
interpreter.fire('OnUpdate', ctx);
|
||||
fire(interpreter, 'OnUpdate', ctx);
|
||||
fire(interpreter, 'OnUpdate', ctx);
|
||||
expect(ctx.logs).toEqual(['open', 'closed']);
|
||||
expect(ctx.states).toEqual({ [machine.id]: machine.ids[1] });
|
||||
});
|
||||
@@ -296,7 +297,7 @@ describe('a running state machine', () => {
|
||||
[set, 'exec', call, 'exec'],
|
||||
]);
|
||||
const ctx = fakeContext();
|
||||
new GraphInterpreter(outer.graph, graphLookup(f.project)).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(outer.graph, graphLookup(f.project)), 'OnStart', ctx);
|
||||
expect(ctx.states).toEqual({ [machine.id]: machine.ids[0] });
|
||||
expect(ctx.logs).toEqual([]);
|
||||
});
|
||||
@@ -328,12 +329,12 @@ describe('a running state machine', () => {
|
||||
]);
|
||||
const ctx = fakeContext({ variables: { doors: machine.id } });
|
||||
const interpreter = new GraphInterpreter(behavior.graph, graphLookup(f.project));
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
// The name moved the machine; the one nothing answers to took "not found" instead of ending.
|
||||
expect(ctx.states).toEqual({ [machine.id]: machine.ids[1] });
|
||||
expect(ctx.logs).toEqual(['no such state']);
|
||||
// The branch has no state outputs at all here, so even a state that is set leaves through else.
|
||||
interpreter.fire('OnUpdate', ctx);
|
||||
fire(interpreter, 'OnUpdate', ctx);
|
||||
expect(ctx.logs).toEqual(['no such state', 'unknown to the graph']);
|
||||
});
|
||||
|
||||
@@ -566,7 +567,7 @@ describe('reading the current state', () => {
|
||||
[current, 'name', log, 'message'],
|
||||
]);
|
||||
const ctx = fakeContext();
|
||||
new GraphInterpreter(behavior.graph, graphLookup(f.project)).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph, graphLookup(f.project)), 'OnStart', ctx);
|
||||
expect(ctx.logs).toEqual(['Open']);
|
||||
});
|
||||
|
||||
@@ -657,7 +658,7 @@ describe('transitions between states', () => {
|
||||
[set, 'exec', next, 'exec'],
|
||||
]);
|
||||
const ctx = fakeContext();
|
||||
new GraphInterpreter(behavior.graph, graphLookup(f.project)).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph, graphLookup(f.project)), 'OnStart', ctx);
|
||||
expect(ctx.states).toEqual({ [machine.id]: closed });
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { AddVariableCommand, RenameVariableCommand } from '../../src/core/comman
|
||||
import { type GraphNode, GraphInterpreter, inlineValue, NO_LOOKUP, resolveDef } from '../../src/core/graph';
|
||||
import type { BehaviorAsset } from '../../src/core/model/Asset';
|
||||
import { graphLookup } from '../../src/core/model/graphLookup';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject } from '../helpers';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, fire } from '../helpers';
|
||||
|
||||
function setup(variables = ['count', 'copy']) {
|
||||
const fixture = makeProject();
|
||||
@@ -19,7 +19,7 @@ function run(
|
||||
variables: Record<string, number> = { count: 5, copy: 0 },
|
||||
) {
|
||||
const ctx = fakeContext({ variables: { ...variables } });
|
||||
new GraphInterpreter(behavior.graph, graphLookup(fixture.project)).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph, graphLookup(fixture.project)), 'OnStart', ctx);
|
||||
return ctx.variables;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import {
|
||||
} from '../../src/core/graph';
|
||||
import type { BehaviorAsset } from '../../src/core/model/Asset';
|
||||
import { graphLookup } from '../../src/core/model/graphLookup';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, roundTrip } from '../helpers';
|
||||
import { fakeContext, graphEditor, makeBehavior, makeProject, roundTrip, fire } from '../helpers';
|
||||
|
||||
/** `px` and `py` are set in one node and read back in another, their sum landing in `sum`. */
|
||||
function bothWays() {
|
||||
@@ -50,7 +50,7 @@ function run(
|
||||
) {
|
||||
const interpreter = new GraphInterpreter(behavior.graph, graphLookup(fixture.project));
|
||||
const ctx = fakeContext({ variables });
|
||||
interpreter.fire('OnStart', ctx);
|
||||
fire(interpreter, 'OnStart', ctx);
|
||||
return ctx.variables;
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ describe('a node that reads or writes several variables at once', () => {
|
||||
]);
|
||||
|
||||
const ctx = fakeContext({ variables: { piece: { shape: 3, rot: 0 } } });
|
||||
new GraphInterpreter(behavior.graph, graphLookup(fixture.project)).fire('OnStart', ctx);
|
||||
fire(new GraphInterpreter(behavior.graph, graphLookup(fixture.project)), 'OnStart', ctx);
|
||||
expect(ctx.variables.piece).toEqual({ shape: 3, rot: 2 });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user