openpencil/packages/kiwi/tests/schema-runtime.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

176 lines
4.6 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import {
ByteBuffer,
compileSchema,
expectEnumValue,
expectFieldNumber,
parseSchema,
validateSchema
} from '../src/schema-runtime'
import type { RuntimeMessage } from '../src/schema-runtime/interpreter'
test('rejects malformed byte fields without corrupting buffer length', () => {
const buffer = new ByteBuffer()
expect(() => Reflect.apply(buffer.writeByteArray, buffer, [{ 0: 42 }])).toThrow(
'Expected byte array'
)
expect(buffer.length).toBe(0)
buffer.writeByteArray(new Uint8Array([42]))
expect(buffer.toUint8Array()).toEqual(new Uint8Array([1, 42]))
})
const schemaText = `
package Example;
enum Kind {
CARD = 1;
BADGE = 2;
}
message Item {
uint id = 1;
string name = 2;
Kind kind = 3;
string[] tags = 4;
}
`
describe('Kiwi schema runtime', () => {
test('parses and validates inline schemas', () => {
const schema = parseSchema(schemaText)
validateSchema(schema)
expectFieldNumber(schema, 'Item', 'name', 2)
expectFieldNumber(schema, 'Item', 'tags', 4)
expectEnumValue(schema, 'Kind', 'BADGE', 2)
})
test('compiles schemas and round-trips messages', () => {
const schema = parseSchema(schemaText)
const codec = compileSchema(schema)
const encodeItem = codec.encodeItem as (value: RuntimeMessage) => Uint8Array
const decodeItem = codec.decodeItem as (value: Uint8Array) => RuntimeMessage
const encoded = encodeItem({ id: 42, name: 'OpenPencil', kind: 'CARD', tags: ['kiwi'] })
expect(encoded.length).toBeGreaterThan(0)
expect(decodeItem(encoded)).toEqual({
id: 42,
name: 'OpenPencil',
kind: 'CARD',
tags: ['kiwi']
})
})
test('compiles and round-trips without calling the Function constructor', () => {
const OriginalFunction = globalThis.Function
globalThis.Function = new Proxy(OriginalFunction, {
construct() {
throw new EvalError(
"Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script"
)
}
})
try {
const codec = compileSchema(parseSchema(schemaText))
const encodeItem = codec.encodeItem as (value: RuntimeMessage) => Uint8Array
const decodeItem = codec.decodeItem as (value: Uint8Array) => RuntimeMessage
const encoded = encodeItem({ id: 7, name: 'CSP-safe', kind: 'BADGE', tags: [] })
expect(decodeItem(encoded)).toEqual({
id: 7,
name: 'CSP-safe',
kind: 'BADGE',
tags: []
})
} finally {
globalThis.Function = OriginalFunction
}
})
test('uses the codec ByteBuffer override', () => {
class CustomByteBuffer extends ByteBuffer {}
const codec = compileSchema(parseSchema(schemaText))
codec.ByteBuffer = CustomByteBuffer
const encoded = (codec.encodeItem as (value: RuntimeMessage) => Uint8Array)({
id: 1,
name: 'custom',
kind: 'CARD',
tags: []
})
const decoded = (codec.decodeItem as (value: Uint8Array) => RuntimeMessage)(encoded)
expect(decoded.name).toBe('custom')
})
test('reports unknown field types at their source location', () => {
const malformedSchema = {
package: null,
definitions: [
{
name: 'Item',
line: 4,
column: 1,
kind: 'MESSAGE' as const,
fields: [
{
name: 'value',
line: 5,
column: 3,
type: 'MissingType',
isArray: false,
isDeprecated: false,
value: 1
}
]
}
]
}
expect(() => compileSchema(malformedSchema)).toThrow(
'Invalid type "MissingType" for field "value"'
)
try {
compileSchema(malformedSchema)
} catch (error) {
expect(error).toMatchObject({ line: 5, column: 3 })
}
})
test('rejects inherited names as field types', () => {
const malformedSchema = {
package: null,
definitions: [
{
name: 'Item',
line: 4,
column: 1,
kind: 'MESSAGE' as const,
fields: [
{
name: 'value',
line: 5,
column: 3,
type: 'constructor',
isArray: false,
isDeprecated: false,
value: 1
}
]
}
]
}
expect(() => compileSchema(malformedSchema)).toThrow(
'Invalid type "constructor" for field "value"'
)
try {
compileSchema(malformedSchema)
} catch (error) {
expect(error).toMatchObject({ line: 5, column: 3 })
}
})
})