openpencil/tests/engine/cli/import.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

188 lines
5.2 KiB
TypeScript

import { expect, setDefaultTimeout, test } from 'bun:test'
import { mkdtemp } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { parseFigFile } from '@open-pencil/core/io'
import type { SceneNode } from '@open-pencil/scene-graph'
import { runOpenPencilCLI } from '#tests/helpers/cli'
import { uint8ArrayToArrayBuffer } from '#tests/helpers/fig/fixtures'
setDefaultTimeout(30_000)
async function createFixture() {
const dir = await mkdtemp(join(tmpdir(), 'open-pencil-import-cli-'))
const htmlPath = join(dir, 'card.html')
const cssPath = join(dir, 'card.css')
await Bun.write(
htmlPath,
`
<article class="card">
<h1>DOM/CSS card</h1>
<p>Imported from HTML and CSS.</p>
</article>
`
)
await Bun.write(
cssPath,
`
.card {
display: flex;
flex-direction: column;
gap: 8px;
width: 240px;
padding: 24px;
background: #ffffff;
border: 1px solid #dddddd;
border-radius: 16px;
}
h1 {
font-size: 24px;
}
`
)
return { dir, htmlPath, cssPath }
}
function findNode(nodes: Iterable<SceneNode>, name: string): SceneNode | undefined {
for (const node of nodes) {
if (node.name === name) return node
}
}
test.each(['card.json', 'missing/nested/card.json'])(
'import CLI writes DesignDOM JSON output to %s',
async (relativeOutput) => {
const { htmlPath, cssPath, dir } = await createFixture()
const output = join(dir, relativeOutput)
const { stdout, stderr, exitCode } = await runOpenPencilCLI([
'import',
htmlPath,
'--css',
cssPath,
'--format',
'json',
'--output',
output,
'--json'
])
expect(stderr).toBe('')
expect(exitCode).toBe(0)
const summary = JSON.parse(stdout)
expect(summary).toMatchObject({ format: 'json', output, pages: 1, rootElements: 1 })
const document = JSON.parse(await Bun.file(output).text())
expect(document.children[0].tagName).toBe('article')
expect(document.children[0].computedStyle.display).toBe('flex')
expect(document.children[0].computedStyle.width).toBe('240px')
}
)
test('import CLI reads embedded HTML styles without a sidecar CSS file', async () => {
const dir = await mkdtemp(join(tmpdir(), 'open-pencil-import-cli-embedded-'))
const htmlPath = join(dir, 'embedded.html')
const output = join(dir, 'embedded.json')
await Bun.write(
htmlPath,
`<!doctype html>
<html>
<head>
<style>
.card { display: flex; flex-direction: column; gap: 10px; width: 280px; height: 140px; padding: 20px; }
</style>
</head>
<body><article class="card"><h1>Embedded CSS</h1></article></body>
</html>`
)
const { stdout, stderr, exitCode } = await runOpenPencilCLI([
'import',
htmlPath,
'--format',
'json',
'--output',
output,
'--json'
])
expect(stderr).toBe('')
expect(exitCode).toBe(0)
expect(JSON.parse(stdout)).toMatchObject({ format: 'json', output, rootElements: 1 })
const document = JSON.parse(await Bun.file(output).text())
expect(document.children[0].tagName).toBe('article')
expect(document.children[0].computedStyle.width).toBe('280px')
expect(document.children[0].computedStyle.gap).toBe('10px')
})
test.each(['card.fig', 'missing/nested/card.fig'])(
'import CLI writes a .fig that core IO can import to %s',
async (relativeOutput) => {
const { htmlPath, cssPath, dir } = await createFixture()
const output = join(dir, relativeOutput)
const { stdout, stderr, exitCode } = await runOpenPencilCLI([
'import',
htmlPath,
'--css',
cssPath,
'--output',
output,
'--json'
])
expect(stderr).toBe('')
expect(exitCode).toBe(0)
expect(JSON.parse(stdout)).toMatchObject({ format: 'fig', output, pages: 1, rootElements: 1 })
const bytes = new Uint8Array(await Bun.file(output).arrayBuffer())
const graph = await parseFigFile(uint8ArrayToArrayBuffer(bytes))
const nodes = [...graph.nodes.values()]
const card = findNode(nodes, 'card')
const title = findNode(nodes, 'DOM/CSS card')
expect(graph.getPages()).toHaveLength(1)
expect(card?.type).toBe('FRAME')
expect(title?.type).toBe('TEXT')
}
)
test('import CLI compiles Tailwind candidates before import', async () => {
const dir = await mkdtemp(join(tmpdir(), 'open-pencil-import-cli-tailwind-'))
const htmlPath = join(dir, 'tailwind.html')
const output = join(dir, 'tailwind.json')
const classes = ['flex', 'flex-col', 'gap-2', 'w-60', 'p-6', 'rounded-xl', 'bg-white']
await Bun.write(
htmlPath,
`<article class="${classes.join(' ')}"><h1 class="text-2xl">Tailwind card</h1></article>`
)
const { stdout, stderr, exitCode } = await runOpenPencilCLI([
'import',
htmlPath,
'--tailwind',
classes.join(' '),
'--format',
'json',
'--output',
output,
'--json'
])
expect(stderr).toBe('')
expect(exitCode).toBe(0)
expect(JSON.parse(stdout)).toMatchObject({ format: 'json', output })
const document = JSON.parse(await Bun.file(output).text())
expect(document.children[0].computedStyle.display).toBe('flex')
expect(document.children[0].computedStyle.width).toBe('240px')
})