diff --git a/package.json b/package.json index 9a0c91b8d..e827ece0d 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "test": "playwright test --project=openpencil", "test:update": "playwright test --project=openpencil --update-snapshots", "test:figma": "playwright test --project=figma", - "figma:debug": "open -a Figma --args --remote-debugging-port=9222" + "figma:debug": "open -a Figma --args --remote-debugging-port=9222", + "test:unit": "bun test ./tests/engine" }, "dependencies": { "@tailwindcss/vite": "^4.2.1", diff --git a/tests/engine/scene-graph.test.ts b/tests/engine/scene-graph.test.ts new file mode 100644 index 000000000..f6596a8cf --- /dev/null +++ b/tests/engine/scene-graph.test.ts @@ -0,0 +1,53 @@ +import { describe, test, expect } from 'bun:test' + +import { SceneGraph } from '../../src/engine/scene-graph' + +function rect(graph: SceneGraph, name: string, x = 0, y = 0, w = 50, h = 50) { + return graph.createNode('RECTANGLE', graph.rootId, { name, x, y, width: w, height: h }).id +} + +describe('SceneGraph', () => { + test('create rectangle', () => { + const graph = new SceneGraph() + const id = rect(graph, 'Rect', 100, 100, 200, 150) + const node = graph.getNode(id)! + expect(node.type).toBe('RECTANGLE') + expect(node.x).toBe(100) + expect(node.width).toBe(200) + }) + + test('create and delete', () => { + const graph = new SceneGraph() + const id = rect(graph, 'R') + expect(graph.getNode(id)).toBeTruthy() + graph.deleteNode(id) + expect(graph.getNode(id)).toBeFalsy() + }) + + test('reparent into frame', () => { + const graph = new SceneGraph() + const frame = graph.createNode('FRAME', graph.rootId, { name: 'F', x: 50, y: 50, width: 400, height: 400 }).id + const r = rect(graph, 'R', 100, 100) + graph.reparentNode(r, frame) + const children = graph.getChildren(frame) + expect(children.map(c => c.id)).toContain(r) + }) + + test('children order', () => { + const graph = new SceneGraph() + rect(graph, 'A') + rect(graph, 'B') + rect(graph, 'C') + const names = graph.getChildren(graph.rootId).map(n => n.name) + expect(names).toEqual(['A', 'B', 'C']) + }) + + test('update node', () => { + const graph = new SceneGraph() + const id = rect(graph, 'R') + graph.updateNode(id, { x: 200, name: 'Updated' }) + const node = graph.getNode(id)! + expect(node.x).toBe(200) + expect(node.name).toBe('Updated') + }) +})