Skip to content

AI-native design editor. Open-source Figma alternative.

chore(tests): guard engine node assertions

Danila Poyarkovac5bb4add363
diff --git a/tests/engine/editor/effects-and-resize.test.ts b/tests/engine/editor/effects-and-resize.test.ts
index 8fce42e4d..a5d6569f3 100644
--- a/tests/engine/editor/effects-and-resize.test.ts
+++ b/tests/engine/editor/effects-and-resize.test.ts
@@ -2,6 +2,8 @@ import { describe, test, expect } from 'bun:test'
import { SceneGraph, type Effect, type Rect } from '@open-pencil/core'
+import { getNodeOrThrow } from '#tests/helpers/assert'
+
function pageId(graph: SceneGraph) {
return graph.getPages()[0].id
}
@@ -29,7 +31,7 @@ describe('Effect types on scene graph', () => {
graph.updateNode(node.id, {
effects: [makeEffect({ spread: 10 })]
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.effects[0].spread).toBe(10)
})
@@ -43,7 +45,7 @@ describe('Effect types on scene graph', () => {
graph.updateNode(node.id, {
effects: [makeEffect({ type: 'INNER_SHADOW', spread: 5 })]
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.effects[0].type).toBe('INNER_SHADOW')
expect(updated.effects[0].spread).toBe(5)
})
@@ -58,7 +60,7 @@ describe('Effect types on scene graph', () => {
graph.updateNode(node.id, {
effects: [makeEffect({ type: 'BACKGROUND_BLUR', radius: 20 })]
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.effects[0].type).toBe('BACKGROUND_BLUR')
expect(updated.effects[0].radius).toBe(20)
})
@@ -73,7 +75,7 @@ describe('Effect types on scene graph', () => {
graph.updateNode(node.id, {
effects: [makeEffect({ type: 'FOREGROUND_BLUR', radius: 8 })]
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.effects[0].type).toBe('FOREGROUND_BLUR')
expect(updated.effects[0].radius).toBe(8)
})
@@ -88,7 +90,7 @@ describe('Effect types on scene graph', () => {
graph.updateNode(node.id, {
effects: [makeEffect({ type: 'LAYER_BLUR', radius: 12 })]
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.effects[0].type).toBe('LAYER_BLUR')
expect(updated.effects[0].radius).toBe(12)
})
@@ -104,7 +106,7 @@ describe('Effect types on scene graph', () => {
graph.updateNode(node.id, {
effects: [makeEffect({ type: 'DROP_SHADOW', radius: 4, offset: { x: 2, y: 2 } })]
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.type).toBe('TEXT')
expect(updated.effects[0].type).toBe('DROP_SHADOW')
})
@@ -120,7 +122,7 @@ describe('Effect types on scene graph', () => {
graph.updateNode(node.id, {
effects: [makeEffect({ type: 'INNER_SHADOW', radius: 2, offset: { x: 1, y: 1 } })]
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.type).toBe('TEXT')
expect(updated.effects[0].type).toBe('INNER_SHADOW')
})
@@ -140,7 +142,7 @@ describe('Effect types on scene graph', () => {
makeEffect({ type: 'LAYER_BLUR', radius: 4 })
]
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.effects).toHaveLength(4)
expect(updated.effects.map((e) => e.type)).toEqual([
'DROP_SHADOW',
@@ -160,7 +162,7 @@ describe('Effect types on scene graph', () => {
graph.updateNode(node.id, {
effects: [makeEffect({ visible: false })]
})
- expect(graph.getNode(node.id)!.effects[0].visible).toBe(false)
+ expect(getNodeOrThrow(graph, node.id).effects[0].visible).toBe(false)
})
test('drop shadow with negative spread (inset shrink)', () => {
@@ -173,7 +175,7 @@ describe('Effect types on scene graph', () => {
graph.updateNode(node.id, {
effects: [makeEffect({ spread: -5 })]
})
- expect(graph.getNode(node.id)!.effects[0].spread).toBe(-5)
+ expect(getNodeOrThrow(graph, node.id).effects[0].spread).toBe(-5)
})
})
diff --git a/tests/engine/figma/api.test.ts b/tests/engine/figma/api.test.ts
index 23cf993d1..67cb50547 100644
--- a/tests/engine/figma/api.test.ts
+++ b/tests/engine/figma/api.test.ts
@@ -2,6 +2,8 @@ import { describe, expect, test } from 'bun:test'
import { FigmaAPI, SceneGraph, type Fill } from '@open-pencil/core'
+import { expectDefined, getNodeOrThrow } from '#tests/helpers/assert'
+
function createAPI(): FigmaAPI {
return new FigmaAPI(new SceneGraph())
}
@@ -39,8 +41,7 @@ describe('FigmaAPI', () => {
const api = createAPI()
const frame = api.createFrame()
const found = api.getNodeById(frame.id)
- expect(found).not.toBeNull()
- expect(found!.id).toBe(frame.id)
+ expect(expectDefined(found, 'found frame').id).toBe(frame.id)
})
test('getNodeById returns null for unknown id', () => {
@@ -54,7 +55,7 @@ describe('FigmaAPI', () => {
const api = createAPI()
const frame = api.createFrame()
expect(frame.type).toBe('FRAME')
- expect(frame.parent!.id).toBe(api.currentPage.id)
+ expect(expectDefined(frame.parent, 'frame parent').id).toBe(api.currentPage.id)
})
test('createRectangle', () => {
@@ -323,7 +324,7 @@ describe('FigmaAPI', () => {
parent.appendChild(child)
expect(parent.children.length).toBe(1)
expect(parent.children[0].id).toBe(child.id)
- expect(child.parent!.id).toBe(parent.id)
+ expect(expectDefined(child.parent, 'child parent').id).toBe(parent.id)
})
test('insertChild at index', () => {
@@ -393,7 +394,7 @@ describe('FigmaAPI', () => {
parent.appendChild(a)
parent.appendChild(b)
const found = parent.findOne((n) => n.type === 'RECTANGLE')
- expect(found!.name).toBe('First')
+ expect(expectDefined(found, 'first rectangle').name).toBe('First')
})
test('findChild searches direct children only', () => {
@@ -441,7 +442,7 @@ describe('FigmaAPI', () => {
const groupId = group.id
api.ungroup(group)
expect(api.getNodeById(groupId)).toBeNull()
- expect(a.parent!.id).toBe(api.currentPage.id)
+ expect(expectDefined(a.parent, 'ungrouped parent').id).toBe(api.currentPage.id)
})
})
@@ -468,7 +469,7 @@ describe('FigmaAPI', () => {
comp.resize(200, 40)
const instance = comp.createInstance()
expect(instance.type).toBe('INSTANCE')
- expect(instance.mainComponent!.id).toBe(comp.id)
+ expect(expectDefined(instance.mainComponent, 'instance main component').id).toBe(comp.id)
})
})
@@ -494,7 +495,7 @@ describe('FigmaAPI', () => {
frame.appendChild(rect)
const json = frame.toJSON() as { children?: unknown[] }
expect(json.children).toBeDefined()
- expect(json.children!.length).toBe(1)
+ expect(json.children?.length).toBe(1)
})
test('toString returns readable string', () => {
@@ -554,7 +555,7 @@ describe('FigmaAPI', () => {
child.layoutSizingVertical = 'FIXED'
child.resize(375, 44)
expect(child.layoutSizingVertical).toBe('FIXED')
- const raw = api.graph.getNode(child.id)!
+ const raw = getNodeOrThrow(api.graph, child.id)
expect(raw.counterAxisSizing).toBe('FIXED')
})
@@ -569,7 +570,7 @@ describe('FigmaAPI', () => {
child.layoutSizingHorizontal = 'FIXED'
child.resize(200, 600)
expect(child.layoutSizingHorizontal).toBe('FIXED')
- const raw = api.graph.getNode(child.id)!
+ const raw = getNodeOrThrow(api.graph, child.id)
expect(raw.counterAxisSizing).toBe('FIXED')
})
})
@@ -956,8 +957,7 @@ describe('FigmaAPI', () => {
hiddenFromPublishing: false
})
const v = api.getVariableById('var1')
- expect(v).not.toBeNull()
- expect(v!.name).toBe('spacing-sm')
+ expect(expectDefined(v, 'spacing variable').name).toBe('spacing-sm')
expect(api.getVariableById('nonexistent')).toBeNull()
})
diff --git a/tests/engine/scene-graph/individual-strokes.test.ts b/tests/engine/scene-graph/individual-strokes.test.ts
index 7bfd18cc8..3fdf7a811 100644
--- a/tests/engine/scene-graph/individual-strokes.test.ts
+++ b/tests/engine/scene-graph/individual-strokes.test.ts
@@ -2,6 +2,8 @@ import { describe, test, expect } from 'bun:test'
import { SceneGraph, FigmaAPI, sceneNodeToKiwi, type Stroke } from '@open-pencil/core'
+import { expectDefined, getNodeOrThrow } from '#tests/helpers/assert'
+
function pageId(graph: SceneGraph) {
return graph.getPages()[0].id
}
@@ -51,7 +53,7 @@ describe('Stroke align', () => {
strokes[0] = { ...strokes[0], align: 'INSIDE' }
graph.updateNode(node.id, { strokes })
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.strokes[0].align).toBe('INSIDE')
expect(updated.strokes[0].weight).toBe(5)
expect(updated.strokes[0].color).toEqual({ r: 0, g: 0, b: 0, a: 1 })
@@ -80,7 +82,7 @@ describe('Individual stroke weights', () => {
borderLeftWeight: 0
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.independentStrokeWeights).toBe(true)
expect(updated.borderTopWeight).toBe(2)
expect(updated.borderRightWeight).toBe(0)
@@ -106,7 +108,7 @@ describe('Individual stroke weights', () => {
borderLeftWeight: 0
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.independentStrokeWeights).toBe(false)
})
@@ -121,7 +123,7 @@ describe('Individual stroke weights', () => {
borderLeftWeight: 0
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.borderTopWeight).toBe(3)
expect(updated.borderRightWeight).toBe(0)
expect(updated.borderBottomWeight).toBe(0)
@@ -139,7 +141,7 @@ describe('Individual stroke weights', () => {
borderLeftWeight: 4
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
expect(updated.strokes[0].weight).toBe(10)
expect(updated.borderTopWeight).toBe(1)
expect(updated.borderRightWeight).toBe(2)
@@ -169,7 +171,10 @@ describe('Instance sync with stroke weights', () => {
borderLeftWeight: 0
})
- const instance = graph.createInstance(comp.id, pageId(graph))!
+ const instance = expectDefined(
+ graph.createInstance(comp.id, pageId(graph)),
+ 'component instance'
+ )
expect(instance.independentStrokeWeights).toBe(true)
expect(instance.borderTopWeight).toBe(3)
expect(instance.borderBottomWeight).toBe(3)
@@ -177,7 +182,7 @@ describe('Instance sync with stroke weights', () => {
graph.updateNode(comp.id, { borderTopWeight: 5 })
graph.syncInstances(comp.id)
- const synced = graph.getNode(instance.id)!
+ const synced = getNodeOrThrow(graph, instance.id)
expect(synced.borderTopWeight).toBe(5)
})
})
@@ -228,7 +233,7 @@ describe('Kiwi serialization', () => {
borderLeftWeight: 4
})
- const updated = graph.getNode(node.id)!
+ const updated = getNodeOrThrow(graph, node.id)
const blobs: Uint8Array[] = []
const changes = sceneNodeToKiwi(
updated,
@@ -280,7 +285,7 @@ describe('FigmaAPI stroke properties', () => {
]
rect.strokeTopWeight = 5
- const raw = api.graph.getNode(rect.id)!
+ const raw = getNodeOrThrow(api.graph, rect.id)
expect(raw.independentStrokeWeights).toBe(true)
expect(raw.borderTopWeight).toBe(5)
})
@@ -321,7 +326,7 @@ describe('FigmaAPI stroke properties', () => {
]
rect.strokeAlign = 'OUTSIDE'
- const raw = api.graph.getNode(rect.id)!
+ const raw = getNodeOrThrow(api.graph, rect.id)
expect(raw.strokes[0].align).toBe('OUTSIDE')
})
@@ -338,7 +343,7 @@ describe('FigmaAPI stroke properties', () => {
expect(frame.strokeBottomWeight).toBe(3)
expect(frame.strokeLeftWeight).toBe(4)
- const raw = api.graph.getNode(frame.id)!
+ const raw = getNodeOrThrow(api.graph, frame.id)
expect(raw.independentStrokeWeights).toBe(true)
})
})