/** * The tables a running game looks its graphs up in: they answer as searching the graph does, and an * edit to the project reaches the run once the run is told about it. */ import { describe, expect, it } from 'vitest'; import { AddPortsCommand } from '../../src/core/commands/portCommands'; import { searchingIndex, tabledIndex } from '../../src/core/graph/graphIndex'; import { graphLookup } from '../../src/core/model/graphLookup'; import { graphEditor, makeBehavior, makeSpriteScene, place, startWorld } from '../helpers'; describe('the tabled index', () => { it('answers as searching the graph does, edge order included', () => { const f = makeSpriteScene(); const behavior = makeBehavior(f, 'B', ['n']); const edit = graphEditor(f, behavior); const update = edit.addNode('OnUpdate'); const first = edit.addNode('Log', { message: 'first' }); const second = edit.addNode('Log', { message: 'second' }); const read = edit.variables(['n']); edit.wire([ [update, 'exec', first, 'exec'], [update, 'exec', second, 'exec'], [first, 'exec', second, 'exec'], [read, 'n', second, 'message'], ]); const lookup = graphLookup(f.project); const graph = behavior.graph; const searched = searchingIndex(graph, lookup); const tabled = tabledIndex(graph, lookup); for (const node of graph.nodes) { expect(tabled.node(node.id)).toBe(node); expect(tabled.def(node)).toEqual(searched.def(node)); for (const port of ['exec', 'message', 'n', 'nothing']) { expect(tabled.into(node.id, port)).toEqual(searched.into(node.id, port)); expect(tabled.outOf(node.id, port)).toEqual(searched.outOf(node.id, port)); } } expect(tabled.outOf(update.id, 'exec').map((e) => e.to.node)).toEqual([first.id, second.id]); expect(tabled.into(second.id, 'exec')).toHaveLength(2); expect(tabled.ofType('Log')).toEqual([first, second]); expect(tabled.ofType('OnStart')).toEqual([]); expect(tabled.node('gone')).toBeUndefined(); }); }); describe('a running game', () => { it('follows an edit to a graph from the frame after the run is told', () => { const f = makeSpriteScene(); const behavior = makeBehavior(f, 'B', ['n']); place(f, behavior.id, { x: 0, y: 0 }); const edit = graphEditor(f, behavior); const update = edit.addNode('OnUpdate'); const log = edit.addNode('Log', { message: 'before' }); edit.connect(update, 'exec', log, 'exec'); const read = edit.variables([]); const { world, frame, behaviors } = startWorld(f, { behaviors: true }); frame(); expect(world.log).toEqual(['before']); // A new edge, and a port the Variables node did not have when the run first looked at it. f.stack.execute(new AddPortsCommand(behavior.id, read.id, [{ key: 'n', kind: 'number' }])); edit.connect(read, 'n', log, 'message'); edit.connect(update, 'exec', edit.addNode('Log', { message: 'added' }), 'exec'); behaviors!.syncFromModel(); world.log.length = 0; frame(); expect(world.log).toEqual(['0', 'added']); }); });