openpencil/tests/engine/io/fig/export/path-text-reflow-export.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

100 lines
4.1 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { parseFigBuffer } from '@open-pencil/fig'
import { exportFigFile } from '#core/io/formats/fig/export'
import { calibratePathTextLayout, getTextPathData, reflowPathTextGlyphs } from '#core/text/path'
import { expectDefined } from '#tests/helpers/assert'
import { loadFigFixture, uint8ArrayToArrayBuffer } from '#tests/helpers/fig/fixtures'
const LOCAL_CIRCLE_TEXT = 'tests/fixtures/circle-text.fig'
/** exportFigFile returns a Uint8Array; parseFigBuffer takes an ArrayBuffer. */
function reparse(out: Uint8Array) {
return parseFigBuffer(uint8ArrayToArrayBuffer(out))
}
describe('TEXT_PATH reflow export hygiene (optional local)', () => {
test('reflowed node exports no strokeGeometry and reflowed glyph positions', async () => {
const graph = await loadFigFixture(LOCAL_CIRCLE_TEXT)
if (!graph) return
const node = expectDefined(
[...graph.getAllNodes()].find((n) => n.name === 'ArnoCoenen.art'),
'path text node'
)
const data = expectDefined(getTextPathData(node), 'text path data')
const box = expectDefined(node.textPathBox, 'textPathBox')
const origGlyphs = expectDefined(node.derivedTextGlyphs, 'imported glyphs')
const layout = expectDefined(calibratePathTextLayout(origGlyphs, data, box), 'layout')
const halfBox = {
x: box.x * 0.5,
y: box.y * 0.5,
width: box.width * 0.5,
height: box.height * 0.5
}
const reflowed = expectDefined(
reflowPathTextGlyphs(origGlyphs, data, layout, halfBox),
'reflowed glyphs'
)
// Sanity: reflow at half size must actually move the glyphs.
expect(reflowed.some((g, i) => Math.abs(g.x - origGlyphs[i].x) > 1)).toBe(true)
// Simulate the state resize.ts's reflow leaves behind: scene strokeGeometry
// emptied while rawNodeFields still holds the ORIGINAL baked silhouettes
// plus vectorData/textPathStart. preserveSourceMetadataDuring keeps the raw
// fields — a bare updateNode would wipe them (clearEditedSourceMetadata)
// and hide exactly the stale-silhouette fallback this test guards against.
graph.preserveSourceMetadataDuring(() => {
graph.updateNode(node.id, {
strokeGeometry: [],
textPathBox: halfBox,
derivedTextGlyphs: reflowed
})
})
const updated = expectDefined(graph.getNode(node.id), 'updated node')
expect(updated.strokeGeometry.length).toBe(0)
expect('strokeGeometry' in updated.source.fig.rawNodeFields).toBe(true)
const out = await exportFigFile(graph)
const reparsed = reparse(out)
const exported = expectDefined(
reparsed.nodeChanges.find((nc) => nc.name === 'ArnoCoenen.art'),
'exported node change'
)
expect(exported.type).toBe('TEXT_PATH')
// No strokeGeometry at all: neither from the (empty) scene field nor from
// stale rawNodeFields — silhouettes are re-derived from the glyphs.
expect(exported.strokeGeometry).toBeUndefined()
// derivedTextData carries the reflowed positions, not raw pre-resize ones.
const glyphs = expectDefined(exported.derivedTextData?.glyphs, 'exported glyphs')
expect(glyphs.length).toBe(reflowed.length)
for (let i = 0; i < reflowed.length; i++) {
expect(expectDefined(glyphs[i]?.position?.x)).toBeCloseTo(reflowed[i].x, 2)
expect(expectDefined(glyphs[i]?.position?.y)).toBeCloseTo(reflowed[i].y, 2)
}
// The layout path payload survives so reimport can reflow again.
const rawExported = exported as typeof exported & Record<string, unknown>
expect(rawExported.vectorData).toBeDefined()
expect(rawExported.textPathStart).toBeDefined()
})
test('unmodified fixture still round-trips WITH strokeGeometry', async () => {
const graph = await loadFigFixture(LOCAL_CIRCLE_TEXT)
if (!graph) return
const out = await exportFigFile(graph)
const reparsed = reparse(out)
const exported = expectDefined(
reparsed.nodeChanges.find((nc) => nc.name === 'ArnoCoenen.art'),
'exported node change'
)
expect(exported.type).toBe('TEXT_PATH')
expect(exported.strokeGeometry?.length ?? 0).toBeGreaterThan(0)
})
})