openpencil/packages/scene-graph/tests/behaviours/layers.test.ts
Danila Poyarkov c47d1b78e6
refactor: share behaviour control roles through dom-css (#960)
* refactor: share behaviour control roles through dom-css

Code export needs the same control model and layer roles preview islands use. They move from packages/vue to packages/dom-css/src/behaviours as behaviourControls and controlRoles, and the layerPath, findLayerByPath and hasBehaviour helpers they need move from Core down to scene-graph, since dom-css cannot depend on Core. Preview behaves the same.

* fix(scene-graph): escape layer path names and stop on parent cycles

A layer named Wifi#1 or Row/1 did not round-trip through its path, so preview skipped that control or reveal target, and a parent cycle in imported data hung the walk. Names escape % / and #, and the walk goes through closest(), which stops on a cycle.
2026-10-07 20:06:16 +00:00

30 lines
1.3 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { findLayerByPath, layerPath, SceneGraph } from '@open-pencil/scene-graph'
describe('layer paths', () => {
test('round-trip names that contain the path separators', () => {
const graph = new SceneGraph()
const root = graph.createNode('FRAME', graph.getPages()[0].id, { name: 'Card' })
const group = graph.createNode('FRAME', root.id, { name: 'Row/1' })
for (const name of ['Wifi#1', '50%', 'Label', 'Label']) {
const node = graph.createNode('FRAME', group.id, { name })
const path = layerPath(graph, root.id, node.id)
expect(findLayerByPath(graph, root.id, path)?.id).toBe(node.id)
}
expect(layerPath(graph, root.id, group.id)).toBe('Row%2F1')
})
test('stops on a parent cycle in bad data', () => {
const graph = new SceneGraph()
const pageId = graph.getPages()[0].id
const root = graph.createNode('FRAME', pageId, { name: 'Card' })
const a = graph.createNode('FRAME', pageId, { name: 'A' })
const b = graph.createNode('FRAME', a.id, { name: 'B' })
// Imported data can point parents at each other; the walk must still end.
const loose = graph.getNode(a.id)
if (loose) loose.parentId = b.id
expect(typeof layerPath(graph, root.id, b.id)).toBe('string')
})
})