openpencil/tests/engine/tools/create.test.ts
Danila Poyarkov aa86873dd7
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

240 lines
7.9 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
// eslint-disable-next-line open-pencil/no-mixed-case-acronym-identifiers -- Upstream export spelling.
import { toJsonSchema as toJSONSchema } from '@valibot/to-json-schema'
import * as v from 'valibot'
import { expectDefined } from '#tests/helpers/assert'
import { getTool, setupToolTest, type ToolResult } from '#tests/helpers/tools'
/** The `render` tool answers with the rendered root and its child IDs. */
interface RenderToolResult extends ToolResult {
children: string[]
}
describe('create_shape', () => {
test('creates a frame', () => {
const { figma } = setupToolTest()
const tool = getTool('create_shape')
const result = tool.execute(figma, {
type: 'FRAME',
x: 100,
y: 200,
width: 300,
height: 400,
name: 'Test Frame'
}) as ToolResult
expect(result.name).toBe('Test Frame')
expect(result.type).toBe('FRAME')
const node = expectDefined(
figma.getNodeById(expectDefined(result.id, 'created node id')),
'created node'
)
expect(node.x).toBe(100)
expect(node.y).toBe(200)
expect(node.width).toBe(300)
expect(node.height).toBe(400)
})
test('names every supported node type in the tool description', () => {
const tool = getTool('create_shape')
const property = toJSONSchema(tool.input, { typeMode: 'input' }).properties?.type
const supportedTypes = v.parse(
v.array(v.string()),
typeof property === 'object' ? property.enum : undefined
)
expect(supportedTypes.length).toBeGreaterThan(0)
expect(supportedTypes.filter((type) => !tool.description.includes(type))).toEqual([])
})
test('creates nested inside parent', () => {
const { figma } = setupToolTest()
const tool = getTool('create_shape')
const parent = tool.execute(figma, {
type: 'FRAME',
x: 0,
y: 0,
width: 500,
height: 500,
name: 'Parent'
}) as ToolResult
const child = tool.execute(figma, {
type: 'RECTANGLE',
x: 10,
y: 10,
width: 50,
height: 50,
parent_id: parent.id
}) as ToolResult
const parentNode = expectDefined(
figma.getNodeById(expectDefined(parent.id, 'created parent id')),
'created parent node'
)
expect(parentNode.children.some((c) => c.id === child.id)).toBe(true)
})
})
describe('combine_as_variants', () => {
test('rejects components from different parents', () => {
const { figma } = setupToolTest()
const firstParent = figma.createFrame()
const secondParent = figma.createFrame()
const first = figma.createComponent()
const second = figma.createComponent()
firstParent.appendChild(first)
secondParent.appendChild(second)
const result = getTool('combine_as_variants').execute(figma, {
ids: [first.id, second.id]
}) as ToolResult
expect(result.error).toBe('combineAsVariants requires components to share a parent')
expect(first.parent?.id).toBe(firstParent.id)
expect(second.parent?.id).toBe(secondParent.id)
})
})
describe('render', () => {
test('renders JSX string', async () => {
const { figma } = setupToolTest()
const tool = getTool('render')
const result = (await tool.execute(figma, {
jsx: '<Frame name="Card" w={200} h={100} bg="#FFF"><Text>Hello</Text></Frame>'
})) as RenderToolResult
expect(result.name).toBe('Card')
expect(result.type).toBe('FRAME')
expect(result.children.length).toBeGreaterThan(0)
})
test('returns JSX warnings', async () => {
const { figma } = setupToolTest()
const tool = getTool('render')
const result = (await tool.execute(figma, {
jsx: '<Frame name="Card" w={200} h={100} mt={8} />'
})) as ToolResult
expect(result.warnings).toEqual(['Unsupported prop "mt" on <frame> is ignored.'])
})
test('accepts SVG markup attributes without hiding invalid root props', async () => {
const { figma } = setupToolTest()
const render = getTool('render')
const valid = (await render.execute(figma, {
jsx: '<svg name="Boat" viewBox="0 0 640 700" size={600}><path d="M380 40 L380 560" stroke="#021A3B" stroke-width="6" fill="none" /></svg>'
})) as ToolResult
const invalid = (await render.execute(figma, {
jsx: '<svg viewBox="0 0 1 1" mt={8}><path d="M0 0 L1 1" /></svg>'
})) as ToolResult
expect(valid.warnings).toBeUndefined()
expect(invalid.warnings).toEqual(['Unsupported prop "mt" on <svg> is ignored.'])
})
test('get_node exposes text style fields', async () => {
const { figma } = setupToolTest()
const render = getTool('render')
const card = (await render.execute(figma, {
jsx: '<Frame name="Card" w={200} h={100}><Text name="Title" size={24} weight={700} font="Inter" color="#111" textAlign="center">Hello</Text></Frame>'
})) as ToolResult
const textId = (card.children as string[])[0]
const getNode = getTool('get_node')
const result = getNode.execute(figma, { id: textId, depth: 0 }) as ToolResult
expect(result.characters).toBe('Hello')
expect(result.fontFamily).toBe('Inter')
expect(result.fontSize).toBe(24)
expect(result.fontWeight).toBe(700)
expect(result.textAlignHorizontal).toBe('CENTER')
})
})
describe('create_vector', () => {
test('creates tightly bounded curved geometry from SVG path data', () => {
const { figma, graph } = setupToolTest()
const result = getTool('create_vector').execute(figma, {
x: 100,
y: 200,
name: 'Curved outline',
path: 'M10 20 C20 20 30 40 40 40 L10 40 Z'
}) as ToolResult
const node = expectDefined(
graph.getNode(expectDefined(result.id, 'created vector id')),
'created vector'
)
const network = expectDefined(node.vectorNetwork, 'SVG vector network')
expect({ x: node.x, y: node.y, width: node.width, height: node.height }).toEqual({
x: 100,
y: 200,
width: 30,
height: 20
})
expect(network.vertices).toEqual([
{ x: 0, y: 0 },
{ x: 30, y: 20 },
{ x: 0, y: 20 }
])
expect(network.segments[0]?.tangentStart).toEqual({ x: 10, y: 0 })
expect(network.segments[0]?.tangentEnd).toEqual({ x: -10, y: 0 })
expect(network.regions).toEqual([{ windingRule: 'NONZERO', loops: [[0, 1, 2]] }])
})
test('preserves complete VectorNetwork JSON topology', () => {
const { figma, graph } = setupToolTest()
const result = getTool('create_vector').execute(figma, {
x: 0,
y: 0,
path: JSON.stringify({
vertices: [
{ x: 0, y: 0 },
{ x: 10, y: 0 },
{ x: 0, y: 10 }
],
segments: [
{ start: 0, end: 1, tangentStart: { x: 1, y: 0 }, tangentEnd: { x: -1, y: 0 } },
{ start: 1, end: 2 },
{ start: 2, end: 0 }
],
regions: [{ windingRule: 'EVENODD', loops: [[0, 1, 2]] }]
})
}) as ToolResult
const node = expectDefined(
graph.getNode(expectDefined(result.id, 'created vector id')),
'created vector'
)
expect(node.vectorNetwork).toEqual({
vertices: [
{ x: 0, y: 0 },
{ x: 10, y: 0 },
{ x: 0, y: 10 }
],
segments: [
{
start: 0,
end: 1,
tangentStart: { x: 1, y: 0 },
tangentEnd: { x: -1, y: 0 }
},
{ start: 1, end: 2, tangentStart: { x: 0, y: 0 }, tangentEnd: { x: 0, y: 0 } },
{ start: 2, end: 0, tangentStart: { x: 0, y: 0 }, tangentEnd: { x: 0, y: 0 } }
],
regions: [{ windingRule: 'EVENODD', loops: [[0, 1, 2]] }]
})
})
test('rejects invalid supplied paths before creating a node', () => {
const { figma, graph } = setupToolTest()
const tool = getTool('create_vector')
const before = graph.nodes.size
for (const path of ['', 'not a path', 'null', 'M0 0 L']) {
const result = tool.execute(figma, { x: 0, y: 0, name: 'Ghost', path }) as ToolResult
expect(result.error).toBeDefined()
expect(graph.nodes.size).toBe(before)
}
})
})