Get/Set property, Get/Set properties, Set enabled and Set text are gone. A single Properties node takes a fixed "object" input; every chosen property is an output that reads the current value, and one switched to Set is also an input written when the flow arrives. Exec pins only exist while at least one entry writes. The node carries a type (Sprite, Text, Collider, Camera, Container, Behavior, Any) that decides what "+ Property" offers: the fields of that type, the variables of the chosen behavior, or for Any a kind to pick and a name to type. Each port takes the default and field of its type, so a Camera width starts at 640. Switching the type keeps the ports and marks those the new type lacks. At runtime the type is only an editor aid; the node writes to whatever object it gets. Old project files are converted on load. The type is inferred from the keys: the first object type that has them all, else the one behavior that declares them all, else Any. enabled belongs to every object, so a converted Set enabled lands on Any. The jump-run and tetris seeders use Properties throughout; draw-piece switches its cell on in the same node that sets the colour. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
223 lines
9.6 KiB
TypeScript
223 lines
9.6 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { GraphInterpreter, OBJECT_BEHAVIOR_PARAM, OBJECT_TYPE_PARAM } from '../../src/core/graph';
|
|
import type { BehaviorAsset } from '../../src/core/model/Asset';
|
|
import { behaviorLookup } from '../../src/core/model/behaviorPrograms';
|
|
import { findAsset } from '../../src/core/model/Project';
|
|
import type { PropertyDef } from '../../src/core/properties/PropertySchema';
|
|
import { pinPropertyTarget } from '../../src/core/store/legacyNodes';
|
|
import { deserializeProject, serializeProject } from '../../src/core/store/serialize';
|
|
import { fakeContext, fakeEntity, makeBehavior, makeProject } from '../helpers';
|
|
|
|
type RawNode = { id: string; type: string; params?: Record<string, unknown>; ports?: unknown[] };
|
|
type RawEdge = [from: string, fromPort: string, to: string, toPort: string];
|
|
|
|
/** A saved project whose behavior "B" holds `nodes` and `edges` exactly as an older editor wrote them. */
|
|
function loadOld(nodes: RawNode[], edges: RawEdge[]) {
|
|
const fixture = makeProject();
|
|
const behavior = makeBehavior(fixture, 'B', ['seen']);
|
|
const game = makeBehavior(fixture, 'Game', ['coins']);
|
|
const raw = serializeProject(fixture.project) as { assets: Record<string, unknown>[] };
|
|
raw.assets.find((a) => a.id === behavior.id)!.graph = {
|
|
nodes: nodes.map((n) => ({ x: 0, y: 0, params: {}, ...n })),
|
|
edges: edges.map(([from, fromPort, to, toPort], i) => ({
|
|
id: `e${i}`,
|
|
from: { node: from, port: fromPort },
|
|
to: { node: to, port: toPort },
|
|
})),
|
|
};
|
|
const project = deserializeProject(raw);
|
|
return { project, game, behavior: findAsset(project, behavior.id) as BehaviorAsset };
|
|
}
|
|
|
|
const node = (behavior: BehaviorAsset, id: string) => behavior.graph.nodes.find((n) => n.id === id)!;
|
|
const edges = (behavior: BehaviorAsset) =>
|
|
behavior.graph.edges.map((e) => `${e.from.node}.${e.from.port}>${e.to.node}.${e.to.port}`);
|
|
|
|
describe('property nodes of an older file', () => {
|
|
it('load as Properties nodes that compute what the old graph computed', () => {
|
|
const { project, behavior } = loadOld(
|
|
[
|
|
{ id: 'start', type: 'OnStart' },
|
|
{ id: 'self', type: 'Self' },
|
|
{ id: 'paint', type: 'SetProperty', params: { property: 'color', value: 0xff0000 } },
|
|
{
|
|
id: 'size',
|
|
type: 'SetProperties',
|
|
params: { width: 40 },
|
|
ports: [{ key: 'width', kind: 'number' }],
|
|
},
|
|
{ id: 'get', type: 'GetProperty', params: { property: 'width' } },
|
|
{ id: 'store', type: 'Variables', ports: [{ key: 'seen', kind: 'number', write: true }] },
|
|
],
|
|
[
|
|
['start', 'exec', 'paint', 'exec'],
|
|
['self', 'object', 'paint', 'object'],
|
|
['paint', 'exec', 'size', 'exec'],
|
|
['self', 'object', 'size', 'object'],
|
|
['size', 'exec', 'store', 'exec'],
|
|
['self', 'object', 'get', 'object'],
|
|
['get', 'value', 'store', 'seen'],
|
|
],
|
|
);
|
|
expect(['paint', 'size', 'get'].map((id) => node(behavior, id).type)).toEqual([
|
|
'Properties',
|
|
'Properties',
|
|
'Properties',
|
|
]);
|
|
expect(edges(behavior)).toContain('get.width>store.seen');
|
|
const self = { ...fakeEntity('self'), props: { width: 10, color: 0 } };
|
|
const ctx = fakeContext({ self, variables: { seen: 0 } });
|
|
new GraphInterpreter(behavior.graph, behaviorLookup(project)).fire('OnStart', ctx);
|
|
expect({ ...self.props, seen: ctx.variables.seen }).toEqual({ width: 40, color: 0xff0000, seen: 40 });
|
|
});
|
|
|
|
it('keep whether they wrote and their typed-in value, under the kind of the property', () => {
|
|
const { behavior, game } = loadOld(
|
|
[
|
|
{ id: 'paint', type: 'SetProperty', params: { property: 'color', value: 7 } },
|
|
{ id: 'coins', type: 'SetProperty', params: { property: 'coins', value: 0 } },
|
|
{ id: 'speed', type: 'GetProperty', params: { property: 'speed' } },
|
|
],
|
|
[],
|
|
);
|
|
expect(node(behavior, 'paint')).toMatchObject({
|
|
params: { [OBJECT_TYPE_PARAM]: 'sprite', color: 7 },
|
|
ports: [{ key: 'color', kind: 'number', write: true }],
|
|
});
|
|
expect(node(behavior, 'coins')).toMatchObject({
|
|
params: { [OBJECT_TYPE_PARAM]: 'behavior', [OBJECT_BEHAVIOR_PARAM]: game.id, coins: 0 },
|
|
ports: [{ key: 'coins', kind: 'number', write: true }],
|
|
});
|
|
// Nothing declares `speed`, so nothing says its kind.
|
|
expect(node(behavior, 'speed').ports).toEqual([{ key: 'speed', kind: 'text' }]);
|
|
expect(node(behavior, 'speed').params).toEqual({});
|
|
});
|
|
|
|
it('turn Set enabled into Properties writing enabled, on unless told otherwise', () => {
|
|
const { project, behavior } = loadOld(
|
|
[
|
|
{ id: 'start', type: 'OnStart' },
|
|
{ id: 'self', type: 'Self' },
|
|
{ id: 'screen', type: 'ObjectByName', params: { name: 'screen' } },
|
|
{ id: 'no', type: 'Boolean', params: { value: false } },
|
|
{ id: 'hide', type: 'SetEnabled' },
|
|
{ id: 'show', type: 'SetEnabled', params: { enabled: false } },
|
|
],
|
|
[
|
|
['start', 'exec', 'hide', 'exec'],
|
|
['self', 'object', 'hide', 'object'],
|
|
['no', 'value', 'hide', 'enabled'],
|
|
['hide', 'exec', 'show', 'exec'],
|
|
['screen', 'object', 'show', 'object'],
|
|
],
|
|
);
|
|
expect(node(behavior, 'show')).toMatchObject({
|
|
type: 'Properties',
|
|
params: { enabled: false },
|
|
ports: [{ key: 'enabled', kind: 'boolean', write: true }],
|
|
});
|
|
expect(node(behavior, 'hide').params).toEqual({ enabled: true });
|
|
expect(edges(behavior)).toEqual([
|
|
'start.exec>hide.exec',
|
|
'self.object>hide.object',
|
|
'no.value>hide.enabled',
|
|
'hide.exec>show.exec',
|
|
'screen.object>show.object',
|
|
]);
|
|
const self = { ...fakeEntity('self'), props: { enabled: true } };
|
|
const screen = { ...fakeEntity('screen'), props: { enabled: true } };
|
|
const ctx = fakeContext({ self, findByName: (name) => (name === 'screen' ? screen : null) });
|
|
new GraphInterpreter(behavior.graph, behaviorLookup(project)).fire('OnStart', ctx);
|
|
expect([self.props.enabled, screen.props.enabled]).toEqual([false, false]);
|
|
});
|
|
|
|
it('turn Set text into Text properties writing text, empty unless told otherwise', () => {
|
|
const { project, behavior } = loadOld(
|
|
[
|
|
{ id: 'start', type: 'OnStart' },
|
|
{ id: 'a', type: 'ObjectByName', params: { name: 'a' } },
|
|
{ id: 'b', type: 'ObjectByName', params: { name: 'b' } },
|
|
{ id: 'c', type: 'ObjectByName', params: { name: 'c' } },
|
|
{ id: 'num', type: 'Number', params: { value: 12 } },
|
|
{ id: 'blank', type: 'SetText' },
|
|
{ id: 'typed', type: 'SetText', params: { text: 'Go' } },
|
|
{ id: 'fed', type: 'SetText' },
|
|
],
|
|
[
|
|
['start', 'exec', 'blank', 'exec'],
|
|
['a', 'object', 'blank', 'object'],
|
|
['blank', 'exec', 'typed', 'exec'],
|
|
['b', 'object', 'typed', 'object'],
|
|
['typed', 'exec', 'fed', 'exec'],
|
|
['c', 'object', 'fed', 'object'],
|
|
['num', 'value', 'fed', 'text'],
|
|
],
|
|
);
|
|
expect(node(behavior, 'typed')).toMatchObject({
|
|
type: 'Properties',
|
|
params: { [OBJECT_TYPE_PARAM]: 'text', text: 'Go' },
|
|
ports: [{ key: 'text', kind: 'text', write: true }],
|
|
});
|
|
expect(node(behavior, 'blank').params).toEqual({ [OBJECT_TYPE_PARAM]: 'text', text: '' });
|
|
expect(edges(behavior)).toContain('num.value>fed.text');
|
|
const texts = Object.fromEntries(
|
|
['a', 'b', 'c'].map((name) => [name, { ...fakeEntity(name), props: { text: 'old' } }]),
|
|
);
|
|
const ctx = fakeContext({ findByName: (name) => texts[name] ?? null });
|
|
new GraphInterpreter(behavior.graph, behaviorLookup(project)).fire('OnStart', ctx);
|
|
expect(['a', 'b', 'c'].map((name) => texts[name]!.props.text)).toEqual(['', 'Go', '12']);
|
|
});
|
|
|
|
it('keep only the object input of one that named no property', () => {
|
|
const { behavior } = loadOld(
|
|
[
|
|
{ id: 'start', type: 'OnStart' },
|
|
{ id: 'self', type: 'Self' },
|
|
{ id: 'num', type: 'Number' },
|
|
{ id: 'blank', type: 'SetProperty', params: { value: 5 } },
|
|
],
|
|
[
|
|
['start', 'exec', 'blank', 'exec'],
|
|
['self', 'object', 'blank', 'object'],
|
|
['num', 'value', 'blank', 'value'],
|
|
],
|
|
);
|
|
expect(node(behavior, 'blank')).toMatchObject({ type: 'Properties', params: {} });
|
|
expect(node(behavior, 'blank').ports).toBeUndefined();
|
|
expect(edges(behavior)).toEqual(['self.object>blank.object']);
|
|
});
|
|
});
|
|
|
|
describe('the type an old node is pinned to', () => {
|
|
const variable = (key: string): PropertyDef => ({ key, label: key, kind: 'number', default: 0 });
|
|
const behaviors = new Map([
|
|
['game', [variable('coins'), variable('lives')]],
|
|
['hud', [variable('lives')]],
|
|
]);
|
|
const pinned = (keys: string[]) => pinPropertyTarget(keys, behaviors).params;
|
|
|
|
it('is the first object type holding every key', () => {
|
|
expect(pinned(['color'])).toEqual({ [OBJECT_TYPE_PARAM]: 'sprite' });
|
|
expect(pinned(['color', 'align'])).toEqual({ [OBJECT_TYPE_PARAM]: 'text' });
|
|
expect(pinned(['width', 'zoom'])).toEqual({ [OBJECT_TYPE_PARAM]: 'camera' });
|
|
});
|
|
|
|
it('else the one behavior declaring them all', () => {
|
|
expect(pinned(['coins', 'lives'])).toEqual({
|
|
[OBJECT_TYPE_PARAM]: 'behavior',
|
|
[OBJECT_BEHAVIOR_PARAM]: 'game',
|
|
});
|
|
});
|
|
|
|
it('else Any: every object has it, nothing declares it, two behaviors do, or nothing to go by', () => {
|
|
expect(pinPropertyTarget(['enabled'], behaviors)).toMatchObject({
|
|
params: {},
|
|
defs: [{ kind: 'boolean' }],
|
|
});
|
|
expect(pinned(['enabled', 'color'])).toEqual({ [OBJECT_TYPE_PARAM]: 'sprite' });
|
|
expect(pinned(['speed'])).toEqual({});
|
|
expect(pinned(['lives'])).toEqual({});
|
|
expect(pinned([])).toEqual({});
|
|
});
|
|
});
|