openpencil/packages/scene-graph/tests/basic/id-generator.test.ts
Danila Poyarkov 510cdbc36f
feat(scene-graph)!: let a SceneGraph take its ID generator (#887)
* feat(scene-graph): let a SceneGraph take its ID generator

The constructor accepts an ID generator, used for the root node and for
nodes, variables, and collections created later; generated IDs skip any
node, variable, collection, or mode ID already in the graph.

Refs #770

Signed-off-by: Marc Went <marc@went.io>

* perf(scene-graph): check mode IDs without allocating per created entity

Generating an ID spread every collection into a new array and scanned it
for each candidate, which runs on every createNode. Check the node,
variable, and collection maps first and walk the modes with an early exit.
An index of mode IDs is not kept because history snapshots, transfer, and
undo replace collection maps and edit modes in place, which would leave it
stale.

* test(scene-graph): cover injected ID generators and collision skipping

* docs: note the SceneGraph ID generator in the changelog

* test(core): keep reopened .fig GUIDs when a sibling is inserted before them

Refs #770

* test(collab): cover concurrent additions, same-property writes, and delete versus edit

Move the synced-store harness to tests/helpers/collab so the new cases
live in tests/app/collab, and let it bind graph events as a collab session
does. The cases edit both peers while disconnected and check the
converged graphs.

Refs #770

* fix(scene-graph): keep a collection's IDs apart and stop on an exhausted generator

createCollection asks for the collection and default mode IDs before
registering either, so a generator repeating a candidate gave both the
same ID. A generator that only returned taken IDs looped forever, even in
the constructor; it now throws after a bounded number of attempts.

* test: require the inserted GUID and a real disconnect in new tests

The GUID test passed even if export dropped the inserted layer, and the
concurrent-edit helper would have tested sequential sync had its peers
not been disconnectable.

* feat(scene-graph): create variable modes through the graph's ID generator

The editor minted added and duplicated modes as mode:<random hex>, so an
injected generator governed every graph entity except modes added after
a collection's first. SceneGraph.createMode mints the ID and adds the
mode; undo and redo replay it with addMode.

* test(core): type the reopened .fig buffer in the GUID test

* refactor(scene-graph)!: share random helpers and one component property ID

randomHex, randomInt, and randomIndex move from @open-pencil/core/random
to @open-pencil/scene-graph/random, so format packages can use them:
design-jsx and the MCP test server dropped private copies. Component
property IDs, written as prop:<random hex> in six places across Core and
design-jsx, come from createComponentPropertyId.

* docs: point the randomness rule at the shared ID and random helpers

---------

Signed-off-by: Marc Went <marc@went.io>
Co-authored-by: Marc Went <marc@went.io>
2026-10-05 08:21:22 +00:00

90 lines
3.3 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { SceneGraph } from '@open-pencil/scene-graph'
import { pageId } from './helpers'
function sequence(prefix: string): () => string {
let next = 1
return () => `${prefix}:${next++}`
}
describe('SceneGraph ID generator', () => {
test('uses an injected generator for the root, pages, nodes, variables, and collections', () => {
const graph = new SceneGraph(sequence('7'))
expect(graph.rootId).toBe('7:1')
expect(pageId(graph)).toBe('7:2')
expect(graph.createNode('RECTANGLE', pageId(graph)).id).toBe('7:3')
const collection = graph.createCollection('Colors')
expect(collection.id).toBe('7:4')
expect(collection.defaultModeId).toBe('7:5')
expect(graph.createVariable('Primary', 'COLOR', collection.id).id).toBe('7:6')
})
test('skips generated IDs already used by nodes, variables, collections, or modes', () => {
const graph = new SceneGraph(sequence('7'))
const page = pageId(graph)
graph.createNode('RECTANGLE', page, { id: '7:3' })
graph.addCollection({
id: '7:4',
name: 'Imported',
modes: [{ modeId: '7:5', name: 'Light' }],
defaultModeId: '7:5',
variableIds: []
})
graph.addVariable({
id: '7:6',
name: 'Imported color',
type: 'COLOR',
collectionId: '7:4',
valuesByMode: { '7:5': { r: 0, g: 0, b: 0, a: 1 } },
description: '',
hiddenFromPublishing: false
})
expect(graph.createNode('RECTANGLE', page).id).toBe('7:7')
expect(graph.createCollection('Fresh').id).toBe('7:8')
})
test('skips IDs of modes added after their collection', () => {
const graph = new SceneGraph(sequence('7'))
const collection = graph.createCollection('Colors')
graph.addMode(collection.id, '7:5', 'Dark')
expect(graph.createNode('RECTANGLE', pageId(graph)).id).toBe('7:6')
})
test('the default generator keeps the shared session-zero scheme', () => {
const first = new SceneGraph()
const second = new SceneGraph()
const ids = [first.rootId, pageId(first), second.rootId, pageId(second)]
for (const id of ids) expect(id).toMatch(/^0:\d+$/)
expect(new Set(ids).size).toBe(ids.length)
})
test('gives a new collection and its default mode different IDs even if the generator repeats', () => {
const ids = ['7:1', '7:2', 'same', 'same', '7:3']
const graph = new SceneGraph(() => ids.shift() ?? 'exhausted')
const collection = graph.createCollection('Colors')
expect(collection.id).toBe('same')
expect(collection.defaultModeId).toBe('7:3')
})
test('throws instead of hanging when the generator only returns IDs in use', () => {
expect(() => new SceneGraph(() => 'same')).toThrow('IDs in a row that are in use')
})
test('gives modes added to a collection IDs from the injected generator', () => {
const graph = new SceneGraph(sequence('7'))
const collection = graph.createCollection('Colors')
const variable = graph.createVariable('Primary', 'COLOR', collection.id)
const dark = graph.createMode(collection.id, 'Dark')
expect(dark).toBe('7:6')
expect(collection.modes.map((mode) => mode.modeId)).toEqual(['7:4', '7:6'])
expect(Object.keys(variable.valuesByMode)).toContain('7:6')
expect(graph.createMode('missing', 'Dark')).toBeUndefined()
})
})