Files
feuerwerk/tests/core/walk.test.ts
T
rootandClaude Opus 5 8f92a06c84 Consolidate duplicated editor code, trim comments, refresh README
Split GraphCanvas (1076 -> 831 lines) into EdgeLayer and graphMenus;
the port and marker menus were the same list and are now one function.
Value specs of the inspector and the list tab share valueSpecs.ts, and
the remaining copies (compound commands, type glyphs, typed asset
lookup, name rule, play/editor world setup, marquee) are merged.

Four copies had already drifted apart; merging them fixes:
- "Disconnect N links" in the port menu is one undo step, not N
- "+ Add collider" is hidden on a placed container instance, as in
  the hierarchy menu, instead of always failing
- a bool property coerced from the text "false" becomes false
- a port command on an unknown node type reports instead of crashing

Comment-only edits leave the bundle byte-identical. README corrected in
35 places (step limit, jump-run clock, suite duration, 409 body, ...).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-11 11:00:30 +02:00

91 lines
3.1 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { AddColliderCommand } from '../../src/core/commands/colliderCommands';
import { ENTITY_HANDLE, type EntityHandle, worldPositionOf } from '../../src/core/graph';
import { hasParentCycle, parentChain } from '../../src/core/model/walk';
import { makeSpriteScene, place, startWorld } from '../helpers';
interface Node {
id: string;
parent: Node | null;
}
const parentOf = (node: Node) => node.parent;
/** a → b → c, where c is parented back to a (only reachable through a broken document or a bug). */
function cycle(): { a: Node; b: Node; c: Node } {
const a: Node = { id: 'a', parent: null };
const b: Node = { id: 'b', parent: a };
const c: Node = { id: 'c', parent: b };
a.parent = c;
return { a, b, c };
}
describe('parentChain', () => {
it('yields the item and its ancestors, nearest first', () => {
const a: Node = { id: 'a', parent: null };
const b: Node = { id: 'b', parent: a };
expect([...parentChain(b, parentOf)].map((n) => n.id)).toEqual(['b', 'a']);
});
it('terminates on a cyclic parent chain, visiting every item once', () => {
const { c } = cycle();
expect([...parentChain(c, parentOf)].map((n) => n.id)).toEqual(['c', 'b', 'a']);
});
it('reports a cycle only when the parents never reach a root', () => {
const { c } = cycle();
expect(hasParentCycle(c, parentOf)).toBe(true);
expect(hasParentCycle({ id: 'lone', parent: null }, parentOf)).toBe(false);
});
});
describe('cycle-safe walks in the runtime', () => {
it('world position of a cyclic chain terminates and counts each ancestor once', () => {
const brand = { [ENTITY_HANDLE]: true } as const;
const a: EntityHandle = {
...brand,
id: 'a',
assetId: 'x',
name: 'a',
x: 1,
y: 10,
z: 0,
props: {},
parent: null,
};
const b: EntityHandle = {
...brand,
id: 'b',
assetId: 'x',
name: 'b',
x: 2,
y: 20,
z: 0,
props: {},
parent: a,
};
a.parent = b;
expect(worldPositionOf(b)).toEqual({ x: 3, y: 30, z: 0 });
});
// Guards `travelsWith`: a parent walk without a seen-set hangs here.
it('moveAndSlide terminates when a collider sits in a cyclic parent chain', () => {
const f = makeSpriteScene();
const moverId = place(f, f.spriteId, { x: 0, y: 0 });
const wallId = place(f, f.spriteId, { x: 200, y: 0 });
const moverCollider = new AddColliderCommand(f.sceneId, moverId);
const wallCollider = new AddColliderCommand(f.sceneId, wallId);
f.stack.execute(moverCollider);
f.stack.execute(wallCollider);
const { world, collisions } = startWorld(f, { collisions: true });
const mover = world.findByObjectId(moverId)!;
const wall = world.findByObjectId(wallId)!;
const wallColliderEntity = world.findByObjectId(wallCollider.objectId)!;
// Close the loop: the wall's collider is its child and the wall is parented to that collider.
wall.parent = wallColliderEntity;
expect(collisions.moveAndSlide(world, mover, 10, 0)).toEqual({ blockedX: false, blockedY: false });
expect(mover.x).toBe(10);
}, 2000);
});