Skip to content

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

test: fix heavy unit-test measurement and round-trip checks (#655)

Danila Poyarkov9dac52da1371
diff --git a/tests/engine/io/fig/roundtrip/helpers.ts b/tests/engine/io/fig/roundtrip/helpers.ts
index e3e60921c..2e729cf76 100644
--- a/tests/engine/io/fig/roundtrip/helpers.ts
+++ b/tests/engine/io/fig/roundtrip/helpers.ts
@@ -97,7 +97,11 @@ function sameNodeReferences(
a.length === b.length &&
a.every((value, index) => {
const other = b[index]
- return other !== undefined && sameNodeReference(ctx, value, other)
+ return (
+ other !== undefined &&
+ (sameNodeReference(ctx, value, other) ||
+ (!ctx.aGraph.getNode(value) && !ctx.bGraph.getNode(other) && value === other))
+ )
})
)
}
@@ -400,6 +404,7 @@ export const RAW_VERIFIERS = new Map<string, Verifier>([
return true
}
],
+ ['textAlignHorizontal', defaultEqual('LEFT')],
['borderRightWeight', defaultEqual(0)],
['borderLeftWeight', defaultEqual(0)],
['borderTopWeight', defaultEqual(0)],
diff --git a/tests/engine/io/fig/roundtrip/verifiers.test.ts b/tests/engine/io/fig/roundtrip/verifiers.test.ts
new file mode 100644
index 000000000..d7654e37a
--- /dev/null
+++ b/tests/engine/io/fig/roundtrip/verifiers.test.ts
@@ -0,0 +1,72 @@
+import { expect, test } from 'bun:test'
+
+import { SceneGraph } from '@open-pencil/scene-graph'
+
+import { RAW_VERIFIERS, SCENE_VERIFIERS, type VerifierContext } from './helpers'
+
+function context(a: unknown, b: unknown, generation = 0): VerifierContext {
+ return {
+ a,
+ b,
+ generation,
+ key: '',
+ path: '',
+ label: 'verifier regression',
+ aGraph: new SceneGraph(),
+ bGraph: new SceneGraph(),
+ aNodes: new Map(),
+ bNodes: new Map(),
+ aNodePaths: new Map(),
+ bNodePaths: new Map(),
+ aComponentPropertyDefinitions: new Map(),
+ bComponentPropertyDefinitions: new Map(),
+ errors: [],
+ fixture: {
+ file: '',
+ fileSize: 0,
+ nodeCount: 0,
+ nodeTypes: {},
+ schemaSize: 0,
+ thumbnailSize: 0,
+ thumbnailWidth: 0,
+ thumbnailHeight: 0,
+ imageCount: 0,
+ figKiwiVersion: 0,
+ g1ExportSize: 0,
+ g2ExportSize: 0
+ }
+ }
+}
+
+test('alignment normalization accepts only default LEFT on the first round trip', () => {
+ const verify = RAW_VERIFIERS.get('textAlignHorizontal')
+ expect(verify).toBeDefined()
+ expect(verify?.(context(undefined, 'LEFT'))).toBe(true)
+ expect(verify?.(context(undefined, 'RIGHT'))).toBe(false)
+ expect(verify?.(context(undefined, 'LEFT', 1))).toBe(false)
+ expect(verify?.(context('LEFT', 'LEFT', 1))).toBe(true)
+})
+
+test('preferred component keys must remain identical while local IDs follow node paths', () => {
+ const definition = {
+ id: 'property',
+ name: 'Avatar',
+ type: 'INSTANCE_SWAP',
+ defaultValue: 'old',
+ preferredValues: ['external-key']
+ }
+ const other = { ...definition, defaultValue: 'new', preferredValues: ['external-key'] }
+ const ctx = context([definition], [other])
+ ctx.aNodePaths = new Map([['old', '0/0']])
+ ctx.bNodePaths = new Map([['new', '0/0']])
+ const verify = SCENE_VERIFIERS.get('componentPropertyDefinitions')
+ expect(verify).toBeDefined()
+ expect(verify?.(ctx)).toBe(true)
+ other.preferredValues = ['different-key']
+ expect(verify?.(ctx)).toBe(false)
+ definition.preferredValues = ['old']
+ other.preferredValues = ['new']
+ expect(verify?.(ctx)).toBe(true)
+ ctx.bNodePaths = new Map([['new', '0/1']])
+ expect(verify?.(ctx)).toBe(false)
+})
diff --git a/tests/engine/layout/auto-layout/text/measurement.test.ts b/tests/engine/layout/auto-layout/text/measurement.test.ts
index 3307b0d47..a27239337 100644
--- a/tests/engine/layout/auto-layout/text/measurement.test.ts
+++ b/tests/engine/layout/auto-layout/text/measurement.test.ts
@@ -1,8 +1,7 @@
import { describe, expect, test } from 'bun:test'
import { computeAllLayouts, SceneGraph, setTextMeasurer } from '@open-pencil/core'
-
-import { createEditorStore } from '@/app/editor/session'
+import { createEditor } from '@open-pencil/core/editor'
import { getNodeOrThrow } from '#tests/helpers/assert'
import { autoFrame, loadFixtureGraph, pageId, rect } from '#tests/helpers/layout'
@@ -132,7 +131,7 @@ describe('text measurement', () => {
{ timeout: HEAVY_TEST_TIMEOUT_MS },
async () => {
const graph = await loadFixtureGraph('gold-preview.fig')
- const store = createEditorStore(graph)
+ const store = createEditor({ graph, skipInitialGraphSetup: true })
const title = [...store.graph.getAllNodes()].find(
(node) => node.type === 'TEXT' && node.text === "World's largest"
)