Files
feuerwerk/tests/core/scope.test.ts
T
rootandClaude Opus 5 8143130ed2
game-maker / suite (push) Failing after 8m59s
A shared value belongs to no behavior, so it becomes an asset of its own (#91)
This reverses the design of the previous commit. `PropertyDef.scope` is gone
without replacement: a behavior variable is an object property again, without
exception, and sharing is something you point at rather than something you
declare twice.

A **Variables asset** is declarations and nothing else -- no graph, no position,
never placed, no tab of its own (`TAB_OF_ASSET` is already `Partial<>`, so that
costs no line). A `Variables` node names one through its new `variablesAsset`
setting, empty meaning the behavior's own variables as before, and then reads
and writes one value per key for the whole run. Its property list follows the
chosen source, which was the actual request: a value another graph declared is
now offerable, where before every participant had to declare the name itself.

That deletes more than it adds (-72 lines net): `globalConflicts` and its
run-stopping kind clash, `SetVariableScopeCommand`, the scope picker, both
object-inspector filters and the "shared by the whole run" note on the placed
object. A typo is no longer a silent second variable -- naming an asset is
picking from a list. And two Variables assets may carry the same key without
meeting, because the asset is the key, not the name. The one shape it copies is
the one that already existed: `Set state` picks its machine the same way.

Lifetime is unchanged. The bag is built at `BehaviorSystem.start()` from the
declarations and never written back, so playing in the editor cannot change the
project -- which is the whole reason a counter must not be an object property
(`World.syncToModel` would `commit()` it, and the coin counter would stand at 7
for good).

Full symmetry across that gap, without mirroring anything. Two named wires:
`Inspector.liveVariables` shows the values as they stand while a run is on and
falls back to the declarations when it ends, and `adoptDeclarations()` takes a
key into the run *only where the document changed it since the last sync*.
Without that condition any unrelated project edit would snap a running counter
back to its starting value; a counter-test bends exactly that and falls over.
Nothing was ever cached, incidentally: `GraphInterpreter` only pulse-caches
nodes without a `read`, and the variable node has one. The wire was missing.

The state machine `Scope` row is now an ordinary `enum` row through
`createFieldRow`. It was hand-built from an `HStack` with a ghost dropdown and
began ~34px left of every value above it; the explanations beside `Local` and
`Global` are gone from the menu, the sentence below the row keeps saying it.
Machine scope is otherwise untouched -- `StateMachineAsset.scope`,
`SetMachineScopeCommand`, `stateBag()` and its persistence all stand, so #59
stays closed as built and `Scope` as a type now carries machines alone.

Six counter-proofs, each biting at its own guard. One of them did not bite at
first: the suggestion list was computed twice, in `nodeSection` and
`portSection`, so only the browser test covered the request itself. It is one
named function now, `variableChoices`, with a unit test of its own.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 18:05:36 +02:00

256 lines
11 KiB
TypeScript
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.
/**
* A Variables asset holds one value per key for the whole run, whatever points at it; a state
* machine declared `global` does the same for its current state. A behavior's own variables stay
* what they were: properties of the object it is placed on.
*/
import { describe, expect, it } from 'vitest';
import {
CreateAssetCommand,
SetAssetPropertyCommand,
SetMachineScopeCommand,
} from '../../src/core/commands/assetCommands';
import { AddNodeCommand, ConnectCommand } from '../../src/core/commands/graphCommands';
import { AddVariableCommand, RenameVariableCommand } from '../../src/core/commands/variableCommands';
import { STATE, STATE_NAME_PARAM, VARIABLES_PARAM } from '../../src/core/graph';
import type { BehaviorAsset } from '../../src/core/model/Asset';
import type { PropertyBag } from '../../src/core/model/Properties';
import {
graphEditor,
makeBehavior,
makeSpriteScene,
makeVariables,
place,
type SceneFixture,
startWorld,
} from '../helpers';
/**
* A behavior that adds one to `key` whenever it starts – the coin counter of the example game.
* With `source` it counts on that Variables asset, without it on its own declaration.
*/
function makeCounter(fixture: SceneFixture, name: string, key: string, source = '') {
const behavior = makeBehavior(fixture, name, source ? [] : [key]);
const editor = graphEditor(fixture, behavior);
const params: PropertyBag = source ? { [VARIABLES_PARAM]: source } : {};
const start = editor.addNode('OnStart');
const read = editor.variables([key], { params });
const add = editor.addNode('Add', { b: 1 });
const write = editor.variables([key], { write: true, params });
editor.wire([
[start, 'exec', write, 'exec'],
[read, key, add, 'a'],
[add, 'result', write, key],
]);
return behavior;
}
/** Places one object per behavior and runs a frame, so every `On start` has fired. */
function runWith(fixture: SceneFixture, behaviors: readonly BehaviorAsset[]) {
for (const behavior of behaviors) place(fixture, behavior.id, { x: 0, y: 0 });
const started = startWorld(fixture, { behaviors: true });
started.frame();
return started;
}
describe('variables of a behavior', () => {
it('counts once per instance', () => {
const f = makeSpriteScene();
const counter = makeCounter(f, 'Counter', 'score');
const started = runWith(f, [counter, counter]);
const values = started.world.entities.filter((e) => e.type === 'behavior').map((e) => e.props.score);
expect(values).toEqual([1, 1]);
});
});
describe('variables of a Variables asset', () => {
it('counts once for the whole run', () => {
const f = makeSpriteScene();
const shared = makeVariables(f, 'Score', ['score']);
const counter = makeCounter(f, 'Counter', 'score', shared.id);
const started = runWith(f, [counter, counter]);
expect(started.behaviors!.shared.variables[shared.id]!.score).toBe(2);
// The value belongs to the run, so no placed object carries one of its own.
for (const entity of started.world.entities) expect(entity.props.score).toBeUndefined();
});
it('shares one value between two different behaviors', () => {
const f = makeSpriteScene();
const shared = makeVariables(f, 'Score', ['score']);
const one = makeCounter(f, 'One', 'score', shared.id);
const other = makeCounter(f, 'Other', 'score', shared.id);
const started = runWith(f, [one, other]);
expect(started.behaviors!.shared.variables[shared.id]!.score).toBe(2);
});
/** The asset is the key, not the name – which is what two declaration places are worth. */
it('keeps two assets apart although they declare the same name', () => {
const f = makeSpriteScene();
const players = makeVariables(f, 'Players', ['score']);
const enemies = makeVariables(f, 'Enemies', ['score']);
const player = makeCounter(f, 'Player', 'score', players.id);
const enemy = makeCounter(f, 'Enemy', 'score', enemies.id);
const started = runWith(f, [player, player, enemy]);
expect(started.behaviors!.shared.variables[players.id]!.score).toBe(2);
expect(started.behaviors!.shared.variables[enemies.id]!.score).toBe(1);
});
it('starts at the declared value again on the next run', () => {
const f = makeSpriteScene();
const shared = makeVariables(f, 'Score', ['score']);
f.stack.execute(new SetAssetPropertyCommand(shared.id, 'score', 7));
const counter = makeCounter(f, 'Counter', 'score', shared.id);
place(f, counter.id, { x: 0, y: 0 });
const first = startWorld(f, { behaviors: true });
first.frame();
expect(first.behaviors!.shared.variables[shared.id]!.score).toBe(8);
const second = startWorld(f, { behaviors: true });
second.frame();
expect(second.behaviors!.shared.variables[shared.id]!.score).toBe(8);
});
/** The declaration is in another asset, so the rename has to reach graphs it knows nothing about. */
it('carries a rename into every graph pointing at it', () => {
const f = makeSpriteScene();
const shared = makeVariables(f, 'Score', ['score']);
const counter = makeCounter(f, 'Counter', 'score', shared.id);
const keys = () => counter.graph.nodes.filter((n) => n.ports).flatMap((n) => n.ports!.map((p) => p.key));
expect(keys()).toEqual(['score', 'score']);
f.stack.execute(new RenameVariableCommand(shared.id, 'score', 'total'));
expect(keys()).toEqual(['total', 'total']);
f.stack.undo();
expect(keys()).toEqual(['score', 'score']);
});
/** A node left pointing at a deleted asset must not fall back to the behavior it sits in. */
it('writes nowhere when the asset it names is gone', () => {
const f = makeSpriteScene();
const counter = makeCounter(f, 'Counter', 'score', 'no-such-asset');
const started = runWith(f, [counter]);
expect(started.world.stopRequested).toBe(false);
for (const entity of started.world.entities) expect(entity.props.score).toBeUndefined();
});
});
/**
* Editing a declaration while the run is on. The run holds its values apart from the document, so
* the two have to meet on purpose: an edit reaches the run, and the run reaches nothing else.
*/
describe('a declaration edited during the run', () => {
/** Counts `score` up by one, with `lives` declared alongside it and left to the document. */
function counting() {
const f = makeSpriteScene();
const shared = makeVariables(f, 'Score', ['score', 'lives']);
const started = runWith(f, [makeCounter(f, 'Counter', 'score', shared.id)]);
const bag = () => started.behaviors!.shared.variables[shared.id]!;
expect(bag().score).toBe(1);
return { f, shared, started, bag };
}
it('reaches the running value', () => {
const { f, shared, started, bag } = counting();
f.stack.execute(new SetAssetPropertyCommand(shared.id, 'lives', 3));
started.behaviors!.syncDeclarations();
expect(bag().lives).toBe(3);
});
it('leaves a value the run has moved on', () => {
const { f, shared, started, bag } = counting();
f.stack.execute(new SetAssetPropertyCommand(shared.id, 'lives', 3));
started.behaviors!.syncDeclarations();
// The document still declares score as 0, but the run counted it to 1 – and keeps counting.
expect(bag().score).toBe(1);
});
it('wins over the run for the very key it changes', () => {
const { f, shared, started, bag } = counting();
f.stack.execute(new SetAssetPropertyCommand(shared.id, 'score', 10));
started.behaviors!.syncDeclarations();
expect(bag().score).toBe(10);
});
it('brings a variable added during the run along', () => {
const { f, shared, started, bag } = counting();
f.stack.execute(new AddVariableCommand(shared.id, 'coins', 'number'));
started.behaviors!.syncDeclarations();
expect(bag().coins).toBe(0);
expect(bag().score).toBe(1);
});
it('writes nothing back into the document', () => {
const { f, shared, started, bag } = counting();
f.stack.execute(new SetAssetPropertyCommand(shared.id, 'lives', 3));
started.behaviors!.syncDeclarations();
// What the run counted stays in the run: the document still declares the value it started from.
const asset = f.project.assets.find((a) => a.id === shared.id)!;
expect(asset.props.score).toBe(0);
expect(bag().score).toBe(1);
});
});
describe('state machine scope', () => {
/** A machine with one state, plus a behavior that enters it on start. */
function makeMachineAndSetter(fixture: SceneFixture, scope: 'local' | 'global') {
const machine = new CreateAssetCommand('statemachine', 'Doors');
fixture.stack.execute(machine);
const state = new AddNodeCommand(machine.asset.id, STATE, 0, 0, { [STATE_NAME_PARAM]: 'Open' });
fixture.stack.execute(state);
if (scope === 'global') fixture.stack.execute(new SetMachineScopeCommand(machine.asset.id, scope));
const behavior = makeBehavior(fixture, 'Opener');
const editor = graphEditor(fixture, behavior);
const start = editor.addNode('OnStart');
const set = editor.addNode('SetState', { machine: machine.asset.id });
fixture.stack.execute(
new ConnectCommand(
behavior.id,
{ node: start.id, port: 'exec' },
{ node: set.id, port: state.node.id },
),
);
return { machineId: machine.asset.id, stateId: state.node.id, behavior };
}
/**
* A second behavior that never sets the state, only branches on it: it writes `sawOpen = 1` when
* the machine is already in Open. That is the whole difference a scope makes.
*/
function makeReader(fixture: SceneFixture, machineId: string, stateId: string) {
const behavior = makeBehavior(fixture, 'Reader', ['sawOpen']);
const editor = graphEditor(fixture, behavior);
const start = editor.addNode('OnStart');
const branch = editor.addNode('StateBranch', { machine: machineId });
const one = editor.addNode('Add', { a: 1, b: 0 });
const write = editor.variables(['sawOpen'], { write: true });
editor.wire([
[start, 'exec', branch, 'exec'],
[branch, stateId, write, 'exec'],
[one, 'result', write, 'sawOpen'],
]);
return behavior;
}
it('hides the state from another instance when local', () => {
const f = makeSpriteScene();
const { machineId, stateId, behavior } = makeMachineAndSetter(f, 'local');
const reader = makeReader(f, machineId, stateId);
const started = runWith(f, [behavior, reader]);
expect(started.behaviors!.shared.states[machineId]).toBeUndefined();
const seen = started.world.entities.find((e) => e.assetId === reader.id)!;
expect(seen.props.sawOpen).toBe(0);
});
it('shows the state to another instance when global', () => {
const f = makeSpriteScene();
const { machineId, stateId, behavior } = makeMachineAndSetter(f, 'global');
const reader = makeReader(f, machineId, stateId);
const started = runWith(f, [behavior, reader]);
expect(started.behaviors!.shared.states[machineId]).toBe(stateId);
const seen = started.world.entities.find((e) => e.assetId === reader.id)!;
expect(seen.props.sawOpen).toBe(1);
});
});