openpencil/tests/engine/vector/validate.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

116 lines
3.6 KiB
TypeScript

import { describe, test, expect } from 'bun:test'
import {
normalizeVectorNetwork,
validateVectorNetwork,
type VectorNetwork,
type VectorSegment
} from '@open-pencil/core'
describe('validateVectorNetwork', () => {
test('valid network returns no errors', () => {
const network: VectorNetwork = {
vertices: [
{ x: 0, y: 0 },
{ x: 10, y: 10 }
],
segments: [{ start: 0, end: 1, tangentStart: { x: 0, y: 0 }, tangentEnd: { x: 0, y: 0 } }],
regions: []
}
expect(validateVectorNetwork(network)).toEqual([])
})
test('segments without tangents are valid (normalize handles them)', () => {
const network = {
vertices: [
{ x: 0, y: 0 },
{ x: 10, y: 10 }
],
segments: [{ start: 0, end: 1 }],
regions: []
}
expect(validateVectorNetwork(network)).toEqual([])
})
test('rejects segment with out-of-range start index', () => {
const network = {
vertices: [{ x: 0, y: 0 }],
segments: [{ start: 0, end: 5 }],
regions: []
}
const errors = validateVectorNetwork(network)
expect(errors.length).toBe(1)
expect(errors[0]).toContain('end index 5 out of range')
})
test('rejects missing vertices array', () => {
const network = { segments: [], regions: [] }
const errors = validateVectorNetwork(network)
expect(errors[0]).toContain('vertices must be an array')
})
test('rejects vertex with non-number coordinates', () => {
const network = {
vertices: [{ x: 'a', y: 0 }],
segments: [],
regions: []
}
const errors = validateVectorNetwork(network)
expect(errors[0]).toContain('x and y must be finite numbers')
})
test('rejects non-object input without throwing', () => {
expect(validateVectorNetwork(null)).toEqual(['network must be an object'])
expect(validateVectorNetwork('not a network')).toEqual(['network must be an object'])
})
test('accepts and normalizes omitted regions like the Figma Plugin API', () => {
const network = {
vertices: [
{ x: 0, y: 0 },
{ x: 10, y: 0 }
],
// `normalizeVectorNetwork` is what supplies the tangents this segment omits.
segments: [{ start: 0, end: 1 } as VectorSegment]
}
expect(validateVectorNetwork(network)).toEqual([])
expect(normalizeVectorNetwork(network).regions).toEqual([])
})
test('rejects a non-array regions value', () => {
const errors = validateVectorNetwork({ vertices: [], segments: [], regions: {} })
expect(errors).toContain('regions must be an array when provided')
})
test('rejects invalid region topology', () => {
const errors = validateVectorNetwork({
vertices: [
{ x: 0, y: 0 },
{ x: 10, y: 0 },
{ x: 20, y: 0 },
{ x: 30, y: 0 }
],
segments: [
{ start: 0, end: 1 },
{ start: 2, end: 3 },
{ start: 1, end: 2 }
],
regions: [
{ windingRule: 'INVALID', loops: [[3]] },
{ windingRule: 'NONZERO', loops: [] },
{ windingRule: 'NONZERO', loops: [[]] },
{ windingRule: 'NONZERO', loops: [[0, 0]] },
{ windingRule: 'NONZERO', loops: [[0, 1]] }
]
})
expect(errors).toContain('region[0]: windingRule must be NONZERO or EVENODD')
expect(errors).toContain('region[0].loop[0]: segment index 3 out of range')
expect(errors).toContain('region[1]: loops must contain at least one loop')
expect(errors).toContain('region[2].loop[0] must contain at least one segment')
expect(errors).toContain('region[3].loop[0] must not repeat segments')
expect(errors).toContain('region[4].loop[0] segments must form a continuous chain')
})
})