AI-native design editor. Open-source Figma alternative.
test(kiwi): split serialization regression coverage
Danila Poyarkov42d346ba4757
diff --git a/oxlint.json b/oxlint.jsonindex 712dfd89e..199a2b6eb 100644--- a/oxlint.json+++ b/oxlint.json@@ -217,7 +217,6 @@"tests/engine/editor/clipboard/import-nodes.test.ts","tests/engine/fig/import/legacy.test.ts","tests/engine/figma/api.test.ts",- "tests/engine/kiwi/serialize-fixes.test.ts","tests/engine/layout/auto-layout.test.ts","tests/engine/scene-graph/basic.test.ts"],diff --git a/tests/engine/kiwi/serialize-fixes.test.ts b/tests/engine/kiwi/serialize-fixes.test.tsdeleted file mode 100644index d0c5cbeb2..000000000--- a/tests/engine/kiwi/serialize-fixes.test.ts+++ /dev/null@@ -1,650 +0,0 @@-import { beforeAll, describe, expect, test } from 'bun:test'--import {- exportFigFile,- initCodec,- parseFigFile,- SceneGraph,- sceneNodeToKiwi-} from '@open-pencil/core'--import { expectDefined, getNodeOrThrow } from '#tests/helpers/assert'--beforeAll(async () => {- await initCodec()-})--function pageId(graph: SceneGraph) {- return graph.getPages()[0].id-}--const ROOT_GUID = { sessionID: 1, localID: 0 }--function toKiwi(node: ReturnType<SceneGraph['createNode']>, graph: SceneGraph) {- const blobs: Uint8Array[] = []- return sceneNodeToKiwi(node, ROOT_GUID, 0, { value: 100 }, graph, blobs)-}--// ----------------------------------------------------------------------------// Fix 1 — Auto-layout children get (0,0) transforms-// -----------------------------------------------------------------------------describe('Fix 1: auto-layout child transforms', () => {- test('auto-layout child gets zero transform regardless of its x/y', () => {- const graph = new SceneGraph()- const parent = graph.createNode('FRAME', pageId(graph), {- name: 'AutoLayout',- x: 0,- y: 0,- width: 300,- height: 400,- layoutMode: 'VERTICAL',- itemSpacing: 8,- paddingTop: 16,- paddingLeft: 16,- paddingBottom: 16,- paddingRight: 16- })-- graph.createNode('FRAME', parent.id, {- name: 'Child',- x: 50,- y: 100,- width: 200,- height: 60- })-- const blobs: Uint8Array[] = []- // Serialize the parent — children are serialized recursively- const changes = sceneNodeToKiwi(- getNodeOrThrow(graph, parent.id),- ROOT_GUID,- 0,- { value: 100 },- graph,- blobs- ) as Record<string, unknown>[]-- // changes[0] = parent, changes[1] = child- const childNc = expectDefined(- changes.find((nc) => nc.name === 'Child'),- 'child node change'- )- expect(childNc).toBeDefined()- expect(childNc.transform.m02).toBe(0)- expect(childNc.transform.m12).toBe(0)- })-- test('absolute-positioned child inside auto-layout keeps its real x/y', () => {- const graph = new SceneGraph()- const parent = graph.createNode('FRAME', pageId(graph), {- name: 'AutoLayout',- x: 0,- y: 0,- width: 300,- height: 400,- layoutMode: 'VERTICAL',- itemSpacing: 8- })-- graph.createNode('FRAME', parent.id, {- name: 'AbsChild',- x: 75,- y: 120,- width: 50,- height: 50,- layoutPositioning: 'ABSOLUTE'- })-- const blobs: Uint8Array[] = []- const changes = sceneNodeToKiwi(- getNodeOrThrow(graph, parent.id),- ROOT_GUID,- 0,- { value: 100 },- graph,- blobs- ) as Record<string, unknown>[]-- const absNc = expectDefined(- changes.find((nc) => nc.name === 'AbsChild'),- 'absolute child node change'- )- expect(absNc).toBeDefined()- expect(absNc.transform.m02).toBe(75)- expect(absNc.transform.m12).toBe(120)- })-- test('child in non-auto-layout parent keeps its real x/y', () => {- const graph = new SceneGraph()- const parent = graph.createNode('FRAME', pageId(graph), {- name: 'PlainFrame',- x: 0,- y: 0,- width: 300,- height: 400- // layoutMode defaults to 'NONE'- })-- graph.createNode('FRAME', parent.id, {- name: 'Child',- x: 30,- y: 45,- width: 100,- height: 80- })-- const blobs: Uint8Array[] = []- const changes = sceneNodeToKiwi(- getNodeOrThrow(graph, parent.id),- ROOT_GUID,- 0,- { value: 100 },- graph,- blobs- ) as Record<string, unknown>[]-- const childNc = expectDefined(- changes.find((nc) => nc.name === 'Child'),- 'child node change'- )- expect(childNc).toBeDefined()- expect(childNc.transform.m02).toBe(30)- expect(childNc.transform.m12).toBe(45)- })-- test('horizontal auto-layout child also gets zero transform', () => {- const graph = new SceneGraph()- const parent = graph.createNode('FRAME', pageId(graph), {- name: 'HorizontalLayout',- x: 0,- y: 0,- width: 600,- height: 100,- layoutMode: 'HORIZONTAL',- itemSpacing: 12- })-- graph.createNode('RECTANGLE', parent.id, {- name: 'Item',- x: 200,- y: 50,- width: 80,- height: 80- })-- const blobs: Uint8Array[] = []- const changes = sceneNodeToKiwi(- getNodeOrThrow(graph, parent.id),- ROOT_GUID,- 0,- { value: 100 },- graph,- blobs- ) as Record<string, unknown>[]-- const itemNc = expectDefined(- changes.find((nc) => nc.name === 'Item'),- 'item node change'- )- expect(itemNc.transform.m02).toBe(0)- expect(itemNc.transform.m12).toBe(0)- })-})--// ----------------------------------------------------------------------------// Fix 2 — frameMaskDisabled always true-// -----------------------------------------------------------------------------describe('Fix 2: frameMaskDisabled is inverse of clipsContent', () => {- test('FRAME without clipsContent gets frameMaskDisabled=true', () => {- const graph = new SceneGraph()- const node = graph.createNode('FRAME', pageId(graph), {- name: 'Frame',- x: 0,- y: 0,- width: 100,- height: 100- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- expect(changes[0].frameMaskDisabled).toBe(true)- })-- test('FRAME with clipsContent=true gets frameMaskDisabled=false', () => {- const graph = new SceneGraph()- const node = graph.createNode('FRAME', pageId(graph), {- name: 'Clipping',- x: 0,- y: 0,- width: 100,- height: 100,- clipsContent: true- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- expect(changes[0].frameMaskDisabled).toBe(false)- })-- test('FRAME with clipsContent=false gets frameMaskDisabled=true', () => {- const graph = new SceneGraph()- const node = graph.createNode('FRAME', pageId(graph), {- name: 'NoClip',- x: 0,- y: 0,- width: 100,- height: 100,- clipsContent: false- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- expect(changes[0].frameMaskDisabled).toBe(true)- })-- test('clipsContent roundtrips through export/parse', async () => {- const graph = new SceneGraph()- graph.createNode('FRAME', pageId(graph), {- name: 'ClipFrame',- x: 0,- y: 0,- width: 100,- height: 100,- clipsContent: true- })-- const exported = await exportFigFile(graph)- const reimported = await parseFigFile(exported.buffer as ArrayBuffer)-- const frame = expectDefined(- [...reimported.nodes.values()].find((n) => n.name === 'ClipFrame'),- 'ClipFrame'- )- expect(frame.clipsContent).toBe(true)- })-})--// ----------------------------------------------------------------------------// Fix 3 — bordersTakeSpace writes strokesIncludedInLayout-// -----------------------------------------------------------------------------describe('Fix 3: bordersTakeSpace serialization', () => {- test('auto-layout frame with strokesIncludedInLayout=true gets bordersTakeSpace=true', () => {- const graph = new SceneGraph()- const node = graph.createNode('FRAME', pageId(graph), {- name: 'LayoutWithBorders',- x: 0,- y: 0,- width: 300,- height: 200,- layoutMode: 'VERTICAL',- itemSpacing: 8,- strokesIncludedInLayout: true- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- expect(changes[0].bordersTakeSpace).toBe(true)- })-- test('auto-layout frame with strokesIncludedInLayout=false gets bordersTakeSpace=false', () => {- const graph = new SceneGraph()- const node = graph.createNode('FRAME', pageId(graph), {- name: 'LayoutNoBorders',- x: 0,- y: 0,- width: 300,- height: 200,- layoutMode: 'VERTICAL',- itemSpacing: 8,- strokesIncludedInLayout: false- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- expect(changes[0].bordersTakeSpace).toBe(false)- })-- test('non-auto-layout frame does not get bordersTakeSpace', () => {- const graph = new SceneGraph()- const node = graph.createNode('FRAME', pageId(graph), {- name: 'PlainFrame',- x: 0,- y: 0,- width: 300,- height: 200,- strokesIncludedInLayout: true- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- expect(changes[0].bordersTakeSpace).toBeUndefined()- })-})--// ----------------------------------------------------------------------------// Fix 4 — lineHeight always written on text nodes-// -----------------------------------------------------------------------------describe('Fix 4: text lineHeight serialization', () => {- test('text node with explicit lineHeight uses that value', () => {- const graph = new SceneGraph()- const node = graph.createNode('TEXT', pageId(graph), {- name: 'ExplicitLH',- x: 0,- y: 0,- width: 100,- height: 20,- text: 'Hello',- fontFamily: 'Inter',- fontWeight: 400,- fontSize: 16,- lineHeight: 24- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- expect(changes[0].lineHeight).toEqual({ value: 24, units: 'PIXELS' })- })-- test('text node without lineHeight defaults to ceil(fontSize * 1.2)', () => {- const graph = new SceneGraph()- const node = graph.createNode('TEXT', pageId(graph), {- name: 'DefaultLH',- x: 0,- y: 0,- width: 100,- height: 20,- text: 'Hello',- fontFamily: 'Inter',- fontWeight: 400,- fontSize: 16- // lineHeight not set — defaults to null- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- // ceil(16 * 1.2) = ceil(19.2) = 20- expect(changes[0].lineHeight).toEqual({ value: 20, units: 'PIXELS' })- })-- test('lineHeight default for odd fontSize', () => {- const graph = new SceneGraph()- const node = graph.createNode('TEXT', pageId(graph), {- name: 'OddSize',- x: 0,- y: 0,- width: 100,- height: 20,- text: 'Hello',- fontFamily: 'Inter',- fontWeight: 400,- fontSize: 14- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- // ceil(14 * 1.2) = ceil(16.8) = 17- expect(changes[0].lineHeight).toEqual({ value: 17, units: 'PIXELS' })- })-- test('lineHeight survives roundtrip through export/parse', async () => {- const graph = new SceneGraph()- graph.createNode('TEXT', pageId(graph), {- name: 'Roundtrip',- x: 0,- y: 0,- width: 100,- height: 20,- text: 'Hello',- fontFamily: 'Inter',- fontWeight: 400,- fontSize: 16,- lineHeight: 28- })-- const exported = await exportFigFile(graph)- const reimported = await parseFigFile(exported.buffer as ArrayBuffer)-- const textNode = expectDefined(- [...reimported.nodes.values()].find((n) => n.type === 'TEXT'),- 'text node'- )- expect(textNode.lineHeight).toBe(28)- })-})--// ----------------------------------------------------------------------------// Fix 5 — Font family normalization in derivedTextData-// -----------------------------------------------------------------------------describe('Fix 5: font family normalization in derivedTextData', () => {- test('optical size suffix stripped in roundtrip', async () => {- const graph = new SceneGraph()- graph.createNode('TEXT', pageId(graph), {- name: 'OpticalSize',- x: 0,- y: 0,- width: 100,- height: 20,- text: 'Hello',- fontFamily: 'DM Sans 9pt',- fontWeight: 400,- fontSize: 14- })-- const exported = await exportFigFile(graph)- const reimported = await parseFigFile(exported.buffer as ArrayBuffer)-- const textNode = expectDefined(- [...reimported.nodes.values()].find((n) => n.type === 'TEXT'),- 'text node'- )- expect(textNode.fontFamily).toBe('DM Sans')- })-- test('Variable suffix stripped in roundtrip', async () => {- const graph = new SceneGraph()- graph.createNode('TEXT', pageId(graph), {- name: 'Variable',- x: 0,- y: 0,- width: 100,- height: 20,- text: 'Hello',- fontFamily: 'Roboto Variable',- fontWeight: 700,- fontSize: 14- })-- const exported = await exportFigFile(graph)- const reimported = await parseFigFile(exported.buffer as ArrayBuffer)-- const textNode = expectDefined(- [...reimported.nodes.values()].find((n) => n.type === 'TEXT'),- 'text node'- )- expect(textNode.fontFamily).toBe('Roboto')- })-- test('normal font family preserved unchanged', async () => {- const graph = new SceneGraph()- graph.createNode('TEXT', pageId(graph), {- name: 'Normal',- x: 0,- y: 0,- width: 100,- height: 20,- text: 'Hello',- fontFamily: 'Inter',- fontWeight: 400,- fontSize: 14- })-- const exported = await exportFigFile(graph)- const reimported = await parseFigFile(exported.buffer as ArrayBuffer)-- const textNode = expectDefined(- [...reimported.nodes.values()].find((n) => n.type === 'TEXT'),- 'text node'- )- expect(textNode.fontFamily).toBe('Inter')- })-- test('fontName in kiwi output uses normalized family', () => {- const graph = new SceneGraph()- const node = graph.createNode('TEXT', pageId(graph), {- name: 'KiwiFontName',- x: 0,- y: 0,- width: 100,- height: 20,- text: 'Hello',- fontFamily: 'DM Sans 18px',- fontWeight: 400,- fontSize: 14- })-- const changes = toKiwi(node, graph) as Record<string, unknown>[]- expect(changes[0].fontName.family).toBe('DM Sans')- })-})--// ----------------------------------------------------------------------------// Integration: all fixes together in a realistic auto-layout component-// -----------------------------------------------------------------------------describe('Integration: auto-layout component with all fixes', () => {- test('full component roundtrip preserves layout structure', async () => {- const graph = new SceneGraph()-- // Parent auto-layout frame (like a card component)- const card = graph.createNode('FRAME', pageId(graph), {- name: 'StatCard',- x: 100,- y: 100,- width: 328,- height: 200,- layoutMode: 'VERTICAL',- itemSpacing: 8,- paddingTop: 16,- paddingLeft: 16,- paddingBottom: 16,- paddingRight: 16,- primaryAxisSizing: 'FIXED',- counterAxisSizing: 'FIXED',- clipsContent: true,- strokesIncludedInLayout: true,- fills: [{ type: 'SOLID', color: { r: 1, g: 1, b: 1, a: 1 }, opacity: 1, visible: true }]- })-- // Title text- graph.createNode('TEXT', card.id, {- name: 'Title',- x: 16,- y: 16,- width: 296,- height: 20,- text: 'Total Active Users',- fontFamily: 'DM Sans 9pt',- fontWeight: 600,- fontSize: 14,- lineHeight: 20- })-- // Value text- graph.createNode('TEXT', card.id, {- name: 'Value',- x: 16,- y: 44,- width: 296,- height: 40,- text: '12,345',- fontFamily: 'Inter',- fontWeight: 700,- fontSize: 32,- lineHeight: 40- })-- // Verify kiwi output directly- const blobs: Uint8Array[] = []- const changes = sceneNodeToKiwi(- getNodeOrThrow(graph, card.id),- ROOT_GUID,- 0,- { value: 100 },- graph,- blobs- ) as Record<string, unknown>[]-- const cardNc = changes[0]- const titleNc = expectDefined(- changes.find((nc) => nc.name === 'Title'),- 'Title node change'- )- const valueNc = expectDefined(- changes.find((nc) => nc.name === 'Value'),- 'Value node change'- )-- // Fix 1: children have zero transforms- expect(titleNc.transform.m02).toBe(0)- expect(titleNc.transform.m12).toBe(0)- expect(valueNc.transform.m02).toBe(0)- expect(valueNc.transform.m12).toBe(0)-- // Fix 2: frameMaskDisabled is inverse of clipsContent- expect(cardNc.frameMaskDisabled).toBe(false) // clipsContent=true → frameMaskDisabled=false- expect(titleNc.frameMaskDisabled).toBe(true) // text nodes don't clip-- // Fix 3: bordersTakeSpace- expect(cardNc.bordersTakeSpace).toBe(true)-- // Fix 4: lineHeight on text nodes- expect(titleNc.lineHeight).toEqual({ value: 20, units: 'PIXELS' })- expect(valueNc.lineHeight).toEqual({ value: 40, units: 'PIXELS' })-- // Fix 5: font family normalized- expect(titleNc.fontName.family).toBe('DM Sans')- expect(valueNc.fontName.family).toBe('Inter')-- // Roundtrip through export/parse- const exported = await exportFigFile(graph)- const reimported = await parseFigFile(exported.buffer as ArrayBuffer)-- const nodes = [...reimported.nodes.values()]- const cardNode = expectDefined(- nodes.find((n) => n.name === 'StatCard'),- 'StatCard node'- )- const titleNode = expectDefined(- nodes.find((n) => n.name === 'Title'),- 'Title node'- )-- expect(cardNode.layoutMode).toBe('VERTICAL')- expect(cardNode.clipsContent).toBe(true)- expect(titleNode.fontFamily).toBe('DM Sans')- expect(titleNode.lineHeight).toBe(20)- })-})--describe('Directionality plugin fallback', () => {- test('text and layout direction roundtrip through export/parse', async () => {- const graph = new SceneGraph()- const frame = graph.createNode('FRAME', pageId(graph), {- name: 'RTL Row',- layoutMode: 'HORIZONTAL',- layoutDirection: 'RTL',- width: 240,- height: 80- })- graph.createNode('TEXT', frame.id, {- text: 'مرحبا',- textDirection: 'RTL',- width: 120,- height: 24- })-- const bytes = await exportFigFile(graph)- const parsed = await parseFigFile(bytes.buffer as ArrayBuffer)-- const parsedFrame = [...parsed.getAllNodes()].find((node) => node.name === 'RTL Row')- const parsedText = [...parsed.getAllNodes()].find((node) => node.type === 'TEXT')- expect(parsedFrame?.layoutDirection).toBe('RTL')- expect(parsedText?.textDirection).toBe('RTL')- })-})diff --git a/tests/engine/kiwi/serialize-fixes/auto-layout-transforms.test.ts b/tests/engine/kiwi/serialize-fixes/auto-layout-transforms.test.tsnew file mode 100644index 000000000..0d6ef5695--- /dev/null+++ b/tests/engine/kiwi/serialize-fixes/auto-layout-transforms.test.ts@@ -0,0 +1,174 @@+import { describe, expect, test } from 'bun:test'++import { SceneGraph, sceneNodeToKiwi } from '@open-pencil/core'++import { expectDefined, getNodeOrThrow } from '#tests/helpers/assert'++import { ROOT_GUID, pageId } from './helpers'++describe('Fix 1: auto-layout child transforms', () => {+ test('auto-layout child gets zero transform regardless of its x/y', () => {+ const graph = new SceneGraph()+ const parent = graph.createNode('FRAME', pageId(graph), {+ name: 'AutoLayout',+ x: 0,+ y: 0,+ width: 300,+ height: 400,+ layoutMode: 'VERTICAL',+ itemSpacing: 8,+ paddingTop: 16,+ paddingLeft: 16,+ paddingBottom: 16,+ paddingRight: 16+ })++ graph.createNode('FRAME', parent.id, {+ name: 'Child',+ x: 50,+ y: 100,+ width: 200,+ height: 60+ })++ const blobs: Uint8Array[] = []+ // Serialize the parent — children are serialized recursively+ const changes = sceneNodeToKiwi(+ getNodeOrThrow(graph, parent.id),+ ROOT_GUID,+ 0,+ { value: 100 },+ graph,+ blobs+ ) as Record<string, unknown>[]++ // changes[0] = parent, changes[1] = child+ const childNc = expectDefined(+ changes.find((nc) => nc.name === 'Child'),+ 'child node change'+ )+ expect(childNc).toBeDefined()+ expect(childNc.transform.m02).toBe(0)+ expect(childNc.transform.m12).toBe(0)+ })++ test('absolute-positioned child inside auto-layout keeps its real x/y', () => {+ const graph = new SceneGraph()+ const parent = graph.createNode('FRAME', pageId(graph), {+ name: 'AutoLayout',+ x: 0,+ y: 0,+ width: 300,+ height: 400,+ layoutMode: 'VERTICAL',+ itemSpacing: 8+ })++ graph.createNode('FRAME', parent.id, {+ name: 'AbsChild',+ x: 75,+ y: 120,+ width: 50,+ height: 50,+ layoutPositioning: 'ABSOLUTE'+ })++ const blobs: Uint8Array[] = []+ const changes = sceneNodeToKiwi(+ getNodeOrThrow(graph, parent.id),+ ROOT_GUID,+ 0,+ { value: 100 },+ graph,+ blobs+ ) as Record<string, unknown>[]++ const absNc = expectDefined(+ changes.find((nc) => nc.name === 'AbsChild'),+ 'absolute child node change'+ )+ expect(absNc).toBeDefined()+ expect(absNc.transform.m02).toBe(75)+ expect(absNc.transform.m12).toBe(120)+ })++ test('child in non-auto-layout parent keeps its real x/y', () => {+ const graph = new SceneGraph()+ const parent = graph.createNode('FRAME', pageId(graph), {+ name: 'PlainFrame',+ x: 0,+ y: 0,+ width: 300,+ height: 400+ // layoutMode defaults to 'NONE'+ })++ graph.createNode('FRAME', parent.id, {+ name: 'Child',+ x: 30,+ y: 45,+ width: 100,+ height: 80+ })++ const blobs: Uint8Array[] = []+ const changes = sceneNodeToKiwi(+ getNodeOrThrow(graph, parent.id),+ ROOT_GUID,+ 0,+ { value: 100 },+ graph,+ blobs+ ) as Record<string, unknown>[]++ const childNc = expectDefined(+ changes.find((nc) => nc.name === 'Child'),+ 'child node change'+ )+ expect(childNc).toBeDefined()+ expect(childNc.transform.m02).toBe(30)+ expect(childNc.transform.m12).toBe(45)+ })++ test('horizontal auto-layout child also gets zero transform', () => {+ const graph = new SceneGraph()+ const parent = graph.createNode('FRAME', pageId(graph), {+ name: 'HorizontalLayout',+ x: 0,+ y: 0,+ width: 600,+ height: 100,+ layoutMode: 'HORIZONTAL',+ itemSpacing: 12+ })++ graph.createNode('RECTANGLE', parent.id, {+ name: 'Item',+ x: 200,+ y: 50,+ width: 80,+ height: 80+ })++ const blobs: Uint8Array[] = []+ const changes = sceneNodeToKiwi(+ getNodeOrThrow(graph, parent.id),+ ROOT_GUID,+ 0,+ { value: 100 },+ graph,+ blobs+ ) as Record<string, unknown>[]++ const itemNc = expectDefined(+ changes.find((nc) => nc.name === 'Item'),+ 'item node change'+ )+ expect(itemNc.transform.m02).toBe(0)+ expect(itemNc.transform.m12).toBe(0)+ })+})++// ---------------------------------------------------------------------------+// Fix 2 — frameMaskDisabled always true+// ---------------------------------------------------------------------------diff --git a/tests/engine/kiwi/serialize-fixes/borders-take-space.test.ts b/tests/engine/kiwi/serialize-fixes/borders-take-space.test.tsnew file mode 100644index 000000000..ff5a0717d--- /dev/null+++ b/tests/engine/kiwi/serialize-fixes/borders-take-space.test.ts@@ -0,0 +1,60 @@+import { describe, expect, test } from 'bun:test'++import { SceneGraph } from '@open-pencil/core'++import { pageId, toKiwi } from './helpers'++describe('Fix 3: bordersTakeSpace serialization', () => {+ test('auto-layout frame with strokesIncludedInLayout=true gets bordersTakeSpace=true', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('FRAME', pageId(graph), {+ name: 'LayoutWithBorders',+ x: 0,+ y: 0,+ width: 300,+ height: 200,+ layoutMode: 'VERTICAL',+ itemSpacing: 8,+ strokesIncludedInLayout: true+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ expect(changes[0].bordersTakeSpace).toBe(true)+ })++ test('auto-layout frame with strokesIncludedInLayout=false gets bordersTakeSpace=false', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('FRAME', pageId(graph), {+ name: 'LayoutNoBorders',+ x: 0,+ y: 0,+ width: 300,+ height: 200,+ layoutMode: 'VERTICAL',+ itemSpacing: 8,+ strokesIncludedInLayout: false+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ expect(changes[0].bordersTakeSpace).toBe(false)+ })++ test('non-auto-layout frame does not get bordersTakeSpace', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('FRAME', pageId(graph), {+ name: 'PlainFrame',+ x: 0,+ y: 0,+ width: 300,+ height: 200,+ strokesIncludedInLayout: true+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ expect(changes[0].bordersTakeSpace).toBeUndefined()+ })+})++// ---------------------------------------------------------------------------+// Fix 4 — lineHeight always written on text nodes+// ---------------------------------------------------------------------------diff --git a/tests/engine/kiwi/serialize-fixes/directionality.test.ts b/tests/engine/kiwi/serialize-fixes/directionality.test.tsnew file mode 100644index 000000000..2f394dad1--- /dev/null+++ b/tests/engine/kiwi/serialize-fixes/directionality.test.ts@@ -0,0 +1,32 @@+import { describe, expect, test } from 'bun:test'++import { exportFigFile, parseFigFile, SceneGraph } from '@open-pencil/core'++import { pageId } from './helpers'++describe('Directionality plugin fallback', () => {+ test('text and layout direction roundtrip through export/parse', async () => {+ const graph = new SceneGraph()+ const frame = graph.createNode('FRAME', pageId(graph), {+ name: 'RTL Row',+ layoutMode: 'HORIZONTAL',+ layoutDirection: 'RTL',+ width: 240,+ height: 80+ })+ graph.createNode('TEXT', frame.id, {+ text: 'مرحبا',+ textDirection: 'RTL',+ width: 120,+ height: 24+ })++ const bytes = await exportFigFile(graph)+ const parsed = await parseFigFile(bytes.buffer as ArrayBuffer)++ const parsedFrame = [...parsed.getAllNodes()].find((node) => node.name === 'RTL Row')+ const parsedText = [...parsed.getAllNodes()].find((node) => node.type === 'TEXT')+ expect(parsedFrame?.layoutDirection).toBe('RTL')+ expect(parsedText?.textDirection).toBe('RTL')+ })+})diff --git a/tests/engine/kiwi/serialize-fixes/font-family.test.ts b/tests/engine/kiwi/serialize-fixes/font-family.test.tsnew file mode 100644index 000000000..77c5d17b4--- /dev/null+++ b/tests/engine/kiwi/serialize-fixes/font-family.test.ts@@ -0,0 +1,103 @@+import { describe, expect, test } from 'bun:test'++import { exportFigFile, parseFigFile, SceneGraph } from '@open-pencil/core'++import { expectDefined } from '#tests/helpers/assert'++import { pageId, toKiwi } from './helpers'++describe('Fix 5: font family normalization in derivedTextData', () => {+ test('optical size suffix stripped in roundtrip', async () => {+ const graph = new SceneGraph()+ graph.createNode('TEXT', pageId(graph), {+ name: 'OpticalSize',+ x: 0,+ y: 0,+ width: 100,+ height: 20,+ text: 'Hello',+ fontFamily: 'DM Sans 9pt',+ fontWeight: 400,+ fontSize: 14+ })++ const exported = await exportFigFile(graph)+ const reimported = await parseFigFile(exported.buffer as ArrayBuffer)++ const textNode = expectDefined(+ [...reimported.nodes.values()].find((n) => n.type === 'TEXT'),+ 'text node'+ )+ expect(textNode.fontFamily).toBe('DM Sans')+ })++ test('Variable suffix stripped in roundtrip', async () => {+ const graph = new SceneGraph()+ graph.createNode('TEXT', pageId(graph), {+ name: 'Variable',+ x: 0,+ y: 0,+ width: 100,+ height: 20,+ text: 'Hello',+ fontFamily: 'Roboto Variable',+ fontWeight: 700,+ fontSize: 14+ })++ const exported = await exportFigFile(graph)+ const reimported = await parseFigFile(exported.buffer as ArrayBuffer)++ const textNode = expectDefined(+ [...reimported.nodes.values()].find((n) => n.type === 'TEXT'),+ 'text node'+ )+ expect(textNode.fontFamily).toBe('Roboto')+ })++ test('normal font family preserved unchanged', async () => {+ const graph = new SceneGraph()+ graph.createNode('TEXT', pageId(graph), {+ name: 'Normal',+ x: 0,+ y: 0,+ width: 100,+ height: 20,+ text: 'Hello',+ fontFamily: 'Inter',+ fontWeight: 400,+ fontSize: 14+ })++ const exported = await exportFigFile(graph)+ const reimported = await parseFigFile(exported.buffer as ArrayBuffer)++ const textNode = expectDefined(+ [...reimported.nodes.values()].find((n) => n.type === 'TEXT'),+ 'text node'+ )+ expect(textNode.fontFamily).toBe('Inter')+ })++ test('fontName in kiwi output uses normalized family', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('TEXT', pageId(graph), {+ name: 'KiwiFontName',+ x: 0,+ y: 0,+ width: 100,+ height: 20,+ text: 'Hello',+ fontFamily: 'DM Sans 18px',+ fontWeight: 400,+ fontSize: 14+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ expect(changes[0].fontName.family).toBe('DM Sans')+ })+})++// ---------------------------------------------------------------------------+// Integration: all fixes together in a realistic auto-layout component+// ---------------------------------------------------------------------------diff --git a/tests/engine/kiwi/serialize-fixes/frame-mask.test.ts b/tests/engine/kiwi/serialize-fixes/frame-mask.test.tsnew file mode 100644index 000000000..e7ca73f14--- /dev/null+++ b/tests/engine/kiwi/serialize-fixes/frame-mask.test.ts@@ -0,0 +1,78 @@+import { describe, expect, test } from 'bun:test'++import { exportFigFile, parseFigFile, SceneGraph } from '@open-pencil/core'++import { expectDefined } from '#tests/helpers/assert'++import { pageId, toKiwi } from './helpers'++describe('Fix 2: frameMaskDisabled is inverse of clipsContent', () => {+ test('FRAME without clipsContent gets frameMaskDisabled=true', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('FRAME', pageId(graph), {+ name: 'Frame',+ x: 0,+ y: 0,+ width: 100,+ height: 100+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ expect(changes[0].frameMaskDisabled).toBe(true)+ })++ test('FRAME with clipsContent=true gets frameMaskDisabled=false', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('FRAME', pageId(graph), {+ name: 'Clipping',+ x: 0,+ y: 0,+ width: 100,+ height: 100,+ clipsContent: true+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ expect(changes[0].frameMaskDisabled).toBe(false)+ })++ test('FRAME with clipsContent=false gets frameMaskDisabled=true', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('FRAME', pageId(graph), {+ name: 'NoClip',+ x: 0,+ y: 0,+ width: 100,+ height: 100,+ clipsContent: false+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ expect(changes[0].frameMaskDisabled).toBe(true)+ })++ test('clipsContent roundtrips through export/parse', async () => {+ const graph = new SceneGraph()+ graph.createNode('FRAME', pageId(graph), {+ name: 'ClipFrame',+ x: 0,+ y: 0,+ width: 100,+ height: 100,+ clipsContent: true+ })++ const exported = await exportFigFile(graph)+ const reimported = await parseFigFile(exported.buffer as ArrayBuffer)++ const frame = expectDefined(+ [...reimported.nodes.values()].find((n) => n.name === 'ClipFrame'),+ 'ClipFrame'+ )+ expect(frame.clipsContent).toBe(true)+ })+})++// ---------------------------------------------------------------------------+// Fix 3 — bordersTakeSpace writes strokesIncludedInLayout+// ---------------------------------------------------------------------------diff --git a/tests/engine/kiwi/serialize-fixes/helpers.ts b/tests/engine/kiwi/serialize-fixes/helpers.tsnew file mode 100644index 000000000..59e47fb97--- /dev/null+++ b/tests/engine/kiwi/serialize-fixes/helpers.ts@@ -0,0 +1,18 @@+import { beforeAll } from 'bun:test'++import { initCodec, sceneNodeToKiwi, type SceneGraph } from '@open-pencil/core'++beforeAll(async () => {+ await initCodec()+})++export function pageId(graph: SceneGraph) {+ return graph.getPages()[0].id+}++export const ROOT_GUID = { sessionID: 1, localID: 0 }++export function toKiwi(node: ReturnType<SceneGraph['createNode']>, graph: SceneGraph) {+ const blobs: Uint8Array[] = []+ return sceneNodeToKiwi(node, ROOT_GUID, 0, { value: 100 }, graph, blobs)+}diff --git a/tests/engine/kiwi/serialize-fixes/integration.test.ts b/tests/engine/kiwi/serialize-fixes/integration.test.tsnew file mode 100644index 000000000..8fe432e56--- /dev/null+++ b/tests/engine/kiwi/serialize-fixes/integration.test.ts@@ -0,0 +1,122 @@+import { describe, expect, test } from 'bun:test'++import { exportFigFile, parseFigFile, SceneGraph, sceneNodeToKiwi } from '@open-pencil/core'++import { expectDefined, getNodeOrThrow } from '#tests/helpers/assert'++import { ROOT_GUID, pageId } from './helpers'++describe('Integration: auto-layout component with all fixes', () => {+ test('full component roundtrip preserves layout structure', async () => {+ const graph = new SceneGraph()++ // Parent auto-layout frame (like a card component)+ const card = graph.createNode('FRAME', pageId(graph), {+ name: 'StatCard',+ x: 100,+ y: 100,+ width: 328,+ height: 200,+ layoutMode: 'VERTICAL',+ itemSpacing: 8,+ paddingTop: 16,+ paddingLeft: 16,+ paddingBottom: 16,+ paddingRight: 16,+ primaryAxisSizing: 'FIXED',+ counterAxisSizing: 'FIXED',+ clipsContent: true,+ strokesIncludedInLayout: true,+ fills: [{ type: 'SOLID', color: { r: 1, g: 1, b: 1, a: 1 }, opacity: 1, visible: true }]+ })++ // Title text+ graph.createNode('TEXT', card.id, {+ name: 'Title',+ x: 16,+ y: 16,+ width: 296,+ height: 20,+ text: 'Total Active Users',+ fontFamily: 'DM Sans 9pt',+ fontWeight: 600,+ fontSize: 14,+ lineHeight: 20+ })++ // Value text+ graph.createNode('TEXT', card.id, {+ name: 'Value',+ x: 16,+ y: 44,+ width: 296,+ height: 40,+ text: '12,345',+ fontFamily: 'Inter',+ fontWeight: 700,+ fontSize: 32,+ lineHeight: 40+ })++ // Verify kiwi output directly+ const blobs: Uint8Array[] = []+ const changes = sceneNodeToKiwi(+ getNodeOrThrow(graph, card.id),+ ROOT_GUID,+ 0,+ { value: 100 },+ graph,+ blobs+ ) as Record<string, unknown>[]++ const cardNc = changes[0]+ const titleNc = expectDefined(+ changes.find((nc) => nc.name === 'Title'),+ 'Title node change'+ )+ const valueNc = expectDefined(+ changes.find((nc) => nc.name === 'Value'),+ 'Value node change'+ )++ // Fix 1: children have zero transforms+ expect(titleNc.transform.m02).toBe(0)+ expect(titleNc.transform.m12).toBe(0)+ expect(valueNc.transform.m02).toBe(0)+ expect(valueNc.transform.m12).toBe(0)++ // Fix 2: frameMaskDisabled is inverse of clipsContent+ expect(cardNc.frameMaskDisabled).toBe(false) // clipsContent=true → frameMaskDisabled=false+ expect(titleNc.frameMaskDisabled).toBe(true) // text nodes don't clip++ // Fix 3: bordersTakeSpace+ expect(cardNc.bordersTakeSpace).toBe(true)++ // Fix 4: lineHeight on text nodes+ expect(titleNc.lineHeight).toEqual({ value: 20, units: 'PIXELS' })+ expect(valueNc.lineHeight).toEqual({ value: 40, units: 'PIXELS' })++ // Fix 5: font family normalized+ expect(titleNc.fontName.family).toBe('DM Sans')+ expect(valueNc.fontName.family).toBe('Inter')++ // Roundtrip through export/parse+ const exported = await exportFigFile(graph)+ const reimported = await parseFigFile(exported.buffer as ArrayBuffer)++ const nodes = [...reimported.nodes.values()]+ const cardNode = expectDefined(+ nodes.find((n) => n.name === 'StatCard'),+ 'StatCard node'+ )+ const titleNode = expectDefined(+ nodes.find((n) => n.name === 'Title'),+ 'Title node'+ )++ expect(cardNode.layoutMode).toBe('VERTICAL')+ expect(cardNode.clipsContent).toBe(true)+ expect(titleNode.fontFamily).toBe('DM Sans')+ expect(titleNode.lineHeight).toBe(20)+ })+})diff --git a/tests/engine/kiwi/serialize-fixes/line-height.test.ts b/tests/engine/kiwi/serialize-fixes/line-height.test.tsnew file mode 100644index 000000000..b378b9c96--- /dev/null+++ b/tests/engine/kiwi/serialize-fixes/line-height.test.ts@@ -0,0 +1,96 @@+import { describe, expect, test } from 'bun:test'++import { exportFigFile, parseFigFile, SceneGraph } from '@open-pencil/core'++import { expectDefined } from '#tests/helpers/assert'++import { pageId, toKiwi } from './helpers'++describe('Fix 4: text lineHeight serialization', () => {+ test('text node with explicit lineHeight uses that value', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('TEXT', pageId(graph), {+ name: 'ExplicitLH',+ x: 0,+ y: 0,+ width: 100,+ height: 20,+ text: 'Hello',+ fontFamily: 'Inter',+ fontWeight: 400,+ fontSize: 16,+ lineHeight: 24+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ expect(changes[0].lineHeight).toEqual({ value: 24, units: 'PIXELS' })+ })++ test('text node without lineHeight defaults to ceil(fontSize * 1.2)', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('TEXT', pageId(graph), {+ name: 'DefaultLH',+ x: 0,+ y: 0,+ width: 100,+ height: 20,+ text: 'Hello',+ fontFamily: 'Inter',+ fontWeight: 400,+ fontSize: 16+ // lineHeight not set — defaults to null+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ // ceil(16 * 1.2) = ceil(19.2) = 20+ expect(changes[0].lineHeight).toEqual({ value: 20, units: 'PIXELS' })+ })++ test('lineHeight default for odd fontSize', () => {+ const graph = new SceneGraph()+ const node = graph.createNode('TEXT', pageId(graph), {+ name: 'OddSize',+ x: 0,+ y: 0,+ width: 100,+ height: 20,+ text: 'Hello',+ fontFamily: 'Inter',+ fontWeight: 400,+ fontSize: 14+ })++ const changes = toKiwi(node, graph) as Record<string, unknown>[]+ // ceil(14 * 1.2) = ceil(16.8) = 17+ expect(changes[0].lineHeight).toEqual({ value: 17, units: 'PIXELS' })+ })++ test('lineHeight survives roundtrip through export/parse', async () => {+ const graph = new SceneGraph()+ graph.createNode('TEXT', pageId(graph), {+ name: 'Roundtrip',+ x: 0,+ y: 0,+ width: 100,+ height: 20,+ text: 'Hello',+ fontFamily: 'Inter',+ fontWeight: 400,+ fontSize: 16,+ lineHeight: 28+ })++ const exported = await exportFigFile(graph)+ const reimported = await parseFigFile(exported.buffer as ArrayBuffer)++ const textNode = expectDefined(+ [...reimported.nodes.values()].find((n) => n.type === 'TEXT'),+ 'text node'+ )+ expect(textNode.lineHeight).toBe(28)+ })+})++// ---------------------------------------------------------------------------+// Fix 5 — Font family normalization in derivedTextData+// ---------------------------------------------------------------------------