* 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>
34 lines
1 KiB
TypeScript
34 lines
1 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
|
|
import { randomHex, randomIndex, randomInt } from '@open-pencil/scene-graph/random'
|
|
|
|
describe('random helpers', () => {
|
|
test('randomHex returns requested byte length as hex', () => {
|
|
expect(randomHex(0)).toBe('')
|
|
expect(randomHex(8)).toMatch(/^[0-9a-f]{16}$/)
|
|
})
|
|
|
|
test('randomHex validates byte count', () => {
|
|
expect(() => randomHex(-1)).toThrow(RangeError)
|
|
expect(() => randomHex(1.5)).toThrow(RangeError)
|
|
})
|
|
|
|
test('randomInt returns an integer', () => {
|
|
expect(Number.isInteger(randomInt())).toBe(true)
|
|
})
|
|
|
|
test('randomIndex returns a bounded index', () => {
|
|
for (let i = 0; i < 100; i++) {
|
|
const index = randomIndex(10)
|
|
expect(index).toBeGreaterThanOrEqual(0)
|
|
expect(index).toBeLessThan(10)
|
|
}
|
|
})
|
|
|
|
test('randomIndex validates length', () => {
|
|
expect(() => randomIndex(0)).toThrow(RangeError)
|
|
expect(() => randomIndex(-1)).toThrow(RangeError)
|
|
expect(() => randomIndex(1.5)).toThrow(RangeError)
|
|
})
|
|
})
|