2026-09-13 16:27:06 +00:00
|
|
|
|
import { expect, expectTypeOf, test } from 'bun:test'
|
|
|
|
|
|
|
refactor(design-jsx): extract design JSX into its own package (#793)
* refactor(design-jsx): extract design JSX into its own package
Design JSX elements, helpers, schema, reference, and JSX export only need
the scene graph, yet lived in Core, so every consumer of the authoring API
pulled in the renderer, layout, and file formats.
@open-pencil/design-jsx now owns them and depends only on scene-graph. The
renderer takes icon lookup, SVG conversion, vector creation, and layout as
DesignJSXServices; Core binds its own and exports the bound renderJSX and
renderTree from @open-pencil/core/design-jsx.
* feat(design-jsx): export the JSX runtime for TSX authoring
The package already had a JSX runtime, but nothing exported it, so design
trees could only be written as function calls or JSX strings. Export
`./jsx-runtime` and `./jsx-dev-runtime` so `jsxImportSource` works, and make
`Fragment` produce the same empty-type node as `<>` in `renderJSX` strings.
* fix(design-jsx): render fragments nested in other elements
A fragment builds a node with an empty type, which only renderJSX expanded, and only at the root. Nested fragments and fragments passed to renderTree failed with 'Unknown element: <>'. Inline fragment children when trees are built, and share root expansion between renderTree and renderJSX.
2026-09-30 16:55:27 +00:00
|
|
|
|
import { renderTree } from '@open-pencil/core/design-jsx'
|
|
|
|
|
|
import { Component, ComponentSet, type Frame, Instance, Text, node } from '@open-pencil/design-jsx'
|
2026-09-13 16:27:06 +00:00
|
|
|
|
import type { ComponentPropertyDefinition, SceneNode } from '@open-pencil/scene-graph'
|
|
|
|
|
|
|
|
|
|
|
|
import { getNodeOrThrow } from '#tests/helpers/assert'
|
|
|
|
|
|
import { makeSceneGraph } from '#tests/helpers/scene'
|
|
|
|
|
|
|
|
|
|
|
|
test('property props are scoped to their authoring entities', () => {
|
|
|
|
|
|
expectTypeOf<Parameters<typeof Frame>[0]['properties']>().toEqualTypeOf<undefined>()
|
|
|
|
|
|
expectTypeOf<Parameters<typeof Text>[0]['properties']>().toEqualTypeOf<undefined>()
|
|
|
|
|
|
expectTypeOf<Parameters<typeof Component>[0]['properties']>().toEqualTypeOf<
|
|
|
|
|
|
SceneNode['componentPropertyDefinitions'] | undefined
|
|
|
|
|
|
>()
|
|
|
|
|
|
expectTypeOf<Parameters<typeof Instance>[0]['properties']>().toEqualTypeOf<
|
|
|
|
|
|
SceneNode['componentPropertyAssignments'] | undefined
|
|
|
|
|
|
>()
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const MESSAGE: ComponentPropertyDefinition = {
|
|
|
|
|
|
id: 'message',
|
|
|
|
|
|
name: 'Message',
|
|
|
|
|
|
type: 'TEXT',
|
|
|
|
|
|
defaultValue: 'Default'
|
|
|
|
|
|
}
|
|
|
|
|
|
const VISIBLE: ComponentPropertyDefinition = {
|
|
|
|
|
|
id: 'visible',
|
|
|
|
|
|
name: 'Visible',
|
|
|
|
|
|
type: 'BOOLEAN',
|
|
|
|
|
|
defaultValue: 'true'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function setup() {
|
|
|
|
|
|
const graph = makeSceneGraph()
|
|
|
|
|
|
const component = await renderTree(
|
|
|
|
|
|
graph,
|
|
|
|
|
|
Component({
|
|
|
|
|
|
name: 'Note',
|
|
|
|
|
|
flex: 'col',
|
|
|
|
|
|
w: 280,
|
|
|
|
|
|
h: 'hug',
|
|
|
|
|
|
properties: [MESSAGE, VISIBLE],
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Text({
|
|
|
|
|
|
name: 'Duplicate name',
|
|
|
|
|
|
children: 'Default',
|
|
|
|
|
|
w: 'fill',
|
|
|
|
|
|
color: '#111111',
|
|
|
|
|
|
propertyRefs: [
|
|
|
|
|
|
{ propertyId: MESSAGE.id, field: 'TEXT' },
|
|
|
|
|
|
{ propertyId: VISIBLE.id, field: 'VISIBLE' }
|
|
|
|
|
|
]
|
|
|
|
|
|
}),
|
|
|
|
|
|
Text({ name: 'Duplicate name', children: 'Unrelated', color: '#111111' })
|
|
|
|
|
|
]
|
|
|
|
|
|
})
|
|
|
|
|
|
)
|
|
|
|
|
|
return { graph, component }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test('assigns real text and visibility properties without resolving child names', async () => {
|
|
|
|
|
|
const { graph, component } = await setup()
|
|
|
|
|
|
const result = await renderTree(
|
|
|
|
|
|
graph,
|
|
|
|
|
|
Instance({ of: component.id, properties: { message: 'June’s review', visible: 'false' } })
|
|
|
|
|
|
)
|
|
|
|
|
|
const instance = getNodeOrThrow(graph, result.id)
|
|
|
|
|
|
const children = graph.getChildren(instance.id)
|
|
|
|
|
|
expect(children.map((child) => child.text)).toEqual(['June’s review', 'Unrelated'])
|
|
|
|
|
|
expect(children[0].visible).toBe(false)
|
|
|
|
|
|
expect(instance.componentPropertyAssignments).toEqual({
|
|
|
|
|
|
message: 'June’s review',
|
|
|
|
|
|
visible: 'false'
|
|
|
|
|
|
})
|
|
|
|
|
|
graph.syncInstances(component.id)
|
|
|
|
|
|
expect(graph.getChildren(instance.id)[0].text).toBe('June’s review')
|
|
|
|
|
|
expect(graph.getChildren(instance.id)[0].visible).toBe(false)
|
|
|
|
|
|
expect(graph.getChildren(component.id)[0].text).toBe('Default')
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('variant selection ignores non-variant instance props', async () => {
|
|
|
|
|
|
const graph = makeSceneGraph()
|
|
|
|
|
|
const set = await renderTree(
|
|
|
|
|
|
graph,
|
|
|
|
|
|
ComponentSet({
|
|
|
|
|
|
name: 'Button',
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Component({ name: 'variant=Primary', w: 100, h: 40 }),
|
|
|
|
|
|
Component({
|
|
|
|
|
|
name: 'variant=Secondary',
|
|
|
|
|
|
w: 100,
|
|
|
|
|
|
h: 40,
|
|
|
|
|
|
properties: [MESSAGE],
|
|
|
|
|
|
children: Text({
|
|
|
|
|
|
children: 'Default',
|
|
|
|
|
|
propertyRefs: [{ propertyId: MESSAGE.id, field: 'TEXT' }]
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
]
|
|
|
|
|
|
})
|
|
|
|
|
|
)
|
|
|
|
|
|
const result = await renderTree(
|
|
|
|
|
|
graph,
|
|
|
|
|
|
Instance({ of: set.id, variant: 'Secondary', w: 120, properties: { message: 'Changed' } })
|
|
|
|
|
|
)
|
|
|
|
|
|
const instance = getNodeOrThrow(graph, result.id)
|
|
|
|
|
|
expect(instance.componentId).toBe(graph.getChildren(set.id)[1].id)
|
|
|
|
|
|
expect(graph.getChildren(instance.id)[0].text).toBe('Changed')
|
|
|
|
|
|
})
|
|
|
|
|
|
|
feat: name the call and list every problem in validation errors (#911)
* feat: name the call and list every problem in validation errors
Tool arguments were checked with v.parse, whose error names only the first problem and neither the tool nor the argument, so a model that sent a wrong create_shape call read 'Expected string but received 42'. Tool arguments in Core, which AI chat, MCP, the CLI, and WebMCP all reach, the automation bridge's file commands, design JSX component properties, and gradient stops now throw a heading that names the call followed by v.summarize, which lists each issue with its path. parseToolArgs is exported for the app's own parses of tool arguments.
* test(design-jsx): pass a deliberately wrong property value through the types
The test checks what a script sees for a non-string instance property, which the Instance types reject at compile time.
* docs: say that MCP clients get the MCP SDK's validation report
The MCP SDK validates tool arguments against the registered schema before OpenPencil's handler runs, so an MCP client already received every problem with its path and does not see parseToolArgs' message.
2026-10-05 14:06:26 +00:00
|
|
|
|
test('an assignment of the wrong type names the prop and the property', async () => {
|
|
|
|
|
|
const { graph, component } = await setup()
|
|
|
|
|
|
await expect(
|
|
|
|
|
|
// A script can pass any value; the types only describe valid calls.
|
|
|
|
|
|
renderTree(graph, Instance({ of: component.id, properties: { message: 42 } as never }))
|
|
|
|
|
|
).rejects.toThrow(
|
|
|
|
|
|
'Invalid properties on <Instance>:\n× Invalid type: Expected string but received 42\n → at message'
|
|
|
|
|
|
)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-09-13 16:27:06 +00:00
|
|
|
|
test('invalid assignments remove the new instance and do not edit its source', async () => {
|
|
|
|
|
|
const { graph, component } = await setup()
|
|
|
|
|
|
const count = graph.nodes.size
|
test: typecheck the test suites and fix what that found (#896)
* build: typecheck the test suites
Tests were in no TypeScript program: no tsconfig included tests/** or
packages/*/tests/**, and bun strips types without checking them, so a
fixture could drop a required field and keep passing until something
read it.
@types/bun moves to the root because it was installed per package only,
and #cli-tests/* joins the paths the root config already carries.
* test: fix the type errors the test suites were hiding
Typechecking the tests turned up 1123 errors. Most were ordinary
strictness, but some were real: `NodeChange` bound to Figma's plugin
typings rather than the Kiwi codec in thirteen .fig tests,
materializeInstance was called with six arguments against five so the
blobs and source children were dropped, CanvasKit pixels were written
to a plain object that never reached WASM, and assertions were made
through accessors that do not exist, so they asserted nothing.
Fixtures that had quietly lost a required field now carry it, nullable
results are narrowed through the existing expectDefined helper rather
than assumed, and stand-ins for CanvasKit and the editor go through one
named helper instead of an unexplained cast at each site.
No test was deleted, skipped, or weakened, and no `any`, non-null
assertion, or ts-expect-error was introduced.
* docs: record what typechecking the tests established
Pins the app program's global types with an assertion rather than a
note, since an unpinned types list lets any root @types package decide
which platform src/** is judged against.
The two environment faults that look like code regressions — Vite's
dependency pre-bundle outliving a package rebuild, and heavy .fig
suites failing under load — go to the development docs, where an
explanation belongs.
* fix: align @types/bun and keep node types resolvable when extended
The root manifest declared a newer @types/bun than every package, which
check:monorepo rejects, and pinning the app program's types left them
unresolvable from a config that extends this one out of tree.
* fix: fail the test typecheck when the compiler itself fails
The gate matched diagnostics by substring, so a compiler or config
failure that named no test file printed a pass while having checked
nothing. Diagnostics are now split by whether they name a file: an
unscoped one is the run failing and stops the gate, a test file's is a
finding, and a source file's stays out by design.
Also drops the parameter planComponentConstruction never read, and
makes the inner-shadow verification script exit non-zero when it
renders no image instead of logging and succeeding.
* chore: merge master into tests-typecheck
2026-10-05 12:42:38 +00:00
|
|
|
|
const invalidAssignments: Record<string, string>[] = [
|
2026-09-13 16:27:06 +00:00
|
|
|
|
{ missing: 'value' },
|
|
|
|
|
|
{ visible: 'yes' },
|
|
|
|
|
|
{ message: 'Changed', missing: 'value' }
|
test: typecheck the test suites and fix what that found (#896)
* build: typecheck the test suites
Tests were in no TypeScript program: no tsconfig included tests/** or
packages/*/tests/**, and bun strips types without checking them, so a
fixture could drop a required field and keep passing until something
read it.
@types/bun moves to the root because it was installed per package only,
and #cli-tests/* joins the paths the root config already carries.
* test: fix the type errors the test suites were hiding
Typechecking the tests turned up 1123 errors. Most were ordinary
strictness, but some were real: `NodeChange` bound to Figma's plugin
typings rather than the Kiwi codec in thirteen .fig tests,
materializeInstance was called with six arguments against five so the
blobs and source children were dropped, CanvasKit pixels were written
to a plain object that never reached WASM, and assertions were made
through accessors that do not exist, so they asserted nothing.
Fixtures that had quietly lost a required field now carry it, nullable
results are narrowed through the existing expectDefined helper rather
than assumed, and stand-ins for CanvasKit and the editor go through one
named helper instead of an unexplained cast at each site.
No test was deleted, skipped, or weakened, and no `any`, non-null
assertion, or ts-expect-error was introduced.
* docs: record what typechecking the tests established
Pins the app program's global types with an assertion rather than a
note, since an unpinned types list lets any root @types package decide
which platform src/** is judged against.
The two environment faults that look like code regressions — Vite's
dependency pre-bundle outliving a package rebuild, and heavy .fig
suites failing under load — go to the development docs, where an
explanation belongs.
* fix: align @types/bun and keep node types resolvable when extended
The root manifest declared a newer @types/bun than every package, which
check:monorepo rejects, and pinning the app program's types left them
unresolvable from a config that extends this one out of tree.
* fix: fail the test typecheck when the compiler itself fails
The gate matched diagnostics by substring, so a compiler or config
failure that named no test file printed a pass while having checked
nothing. Diagnostics are now split by whether they name a file: an
unscoped one is the run failing and stops the gate, a test file's is a
finding, and a source file's stays out by design.
Also drops the parameter planComponentConstruction never read, and
makes the inner-shadow verification script exit non-zero when it
renders no image instead of logging and succeeding.
* chore: merge master into tests-typecheck
2026-10-05 12:42:38 +00:00
|
|
|
|
]
|
|
|
|
|
|
for (const assignments of invalidAssignments) {
|
2026-09-13 16:27:06 +00:00
|
|
|
|
await expect(
|
|
|
|
|
|
renderTree(graph, Instance({ of: component.id, properties: assignments }))
|
|
|
|
|
|
).rejects.toThrow()
|
|
|
|
|
|
expect(graph.nodes.size).toBe(count)
|
|
|
|
|
|
expect(graph.getChildren(component.id)[0].text).toBe('Default')
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('assigns nested instance swaps through stable property references', async () => {
|
|
|
|
|
|
const graph = makeSceneGraph()
|
|
|
|
|
|
const first = await renderTree(graph, Component({ name: 'First', w: 20, h: 20 }))
|
|
|
|
|
|
const second = await renderTree(graph, Component({ name: 'Second', w: 30, h: 30 }))
|
|
|
|
|
|
const component = await renderTree(
|
|
|
|
|
|
graph,
|
|
|
|
|
|
Component({
|
|
|
|
|
|
name: 'With slot',
|
|
|
|
|
|
properties: [{ id: 'slot', name: 'Slot', type: 'INSTANCE_SWAP', defaultValue: first.id }],
|
|
|
|
|
|
children: Instance({
|
|
|
|
|
|
of: first.id,
|
|
|
|
|
|
propertyRefs: [{ propertyId: 'slot', field: 'INSTANCE_SWAP' }]
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
)
|
|
|
|
|
|
const result = await renderTree(
|
|
|
|
|
|
graph,
|
|
|
|
|
|
Instance({ of: component.id, properties: { slot: second.id } })
|
|
|
|
|
|
)
|
|
|
|
|
|
expect(graph.getChildren(result.id)[0].componentId).toBe(second.id)
|
|
|
|
|
|
graph.syncInstances(component.id)
|
|
|
|
|
|
expect(graph.getChildren(result.id)[0].componentId).toBe(second.id)
|
|
|
|
|
|
expect(graph.getChildren(component.id)[0].componentId).toBe(first.id)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('ordinary elements reject properties before rendering', async () => {
|
|
|
|
|
|
const graph = makeSceneGraph()
|
|
|
|
|
|
for (const type of ['frame', 'text', 'rectangle', 'icon', 'svg']) {
|
|
|
|
|
|
await expect(renderTree(graph, node(type, { properties: [MESSAGE] }))).rejects.toThrow(
|
|
|
|
|
|
'Only components'
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('validates metadata rather than accepting silent malformed property definitions', async () => {
|
|
|
|
|
|
const graph = makeSceneGraph()
|
|
|
|
|
|
await expect(renderTree(graph, node('text', { properties: [MESSAGE] }))).rejects.toThrow(
|
|
|
|
|
|
'Only components'
|
|
|
|
|
|
)
|
|
|
|
|
|
await expect(renderTree(graph, Component({ properties: [MESSAGE, MESSAGE] }))).rejects.toThrow(
|
|
|
|
|
|
'Duplicate'
|
|
|
|
|
|
)
|
2026-09-13 21:14:09 +00:00
|
|
|
|
for (const element of [Component, ComponentSet]) {
|
|
|
|
|
|
await expect(
|
|
|
|
|
|
renderTree(
|
|
|
|
|
|
graph,
|
|
|
|
|
|
element({
|
|
|
|
|
|
properties: [
|
|
|
|
|
|
{ id: 'kind-a', name: 'Kind', type: 'VARIANT', defaultValue: 'A' },
|
|
|
|
|
|
{ id: 'kind-b', name: 'Kind', type: 'VARIANT', defaultValue: 'B' }
|
|
|
|
|
|
]
|
|
|
|
|
|
})
|
|
|
|
|
|
)
|
|
|
|
|
|
).rejects.toThrow('Duplicate variant property names')
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('preserves duplicate display names for non-variant properties with distinct IDs', async () => {
|
|
|
|
|
|
const graph = makeSceneGraph()
|
|
|
|
|
|
const result = await renderTree(
|
|
|
|
|
|
graph,
|
|
|
|
|
|
Component({ properties: [MESSAGE, { ...MESSAGE, id: 'other-message' }] })
|
|
|
|
|
|
)
|
|
|
|
|
|
expect(
|
|
|
|
|
|
getNodeOrThrow(graph, result.id).componentPropertyDefinitions.map((item) => item.id)
|
|
|
|
|
|
).toEqual(['message', 'other-message'])
|
2026-09-13 16:27:06 +00:00
|
|
|
|
})
|