openpencil/packages/fig/tests/instance-overrides/addressing.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

127 lines
4.4 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { expectPathError } from '#fig-tests/helpers/errors'
import { interpretInstance } from '#fig/instance-overrides/interpret'
import { symbolDataOf } from '#fig/instance-overrides/types'
import type { NodeChange } from '@open-pencil/kiwi/fig/codec'
const component = { sessionID: 1, localID: 1 }
const label = { sessionID: 1, localID: 2 }
const alias = { sessionID: 8, localID: 2 }
const first = { sessionID: 2, localID: 1 }
const second = { sessionID: 2, localID: 2 }
const outer = { sessionID: 3, localID: 1 }
const placed = { sessionID: 4, localID: 1 }
function fixture(): NodeChange[] {
return [
{ guid: component, type: 'SYMBOL' },
{
guid: label,
overrideKey: alias,
type: 'TEXT',
parentIndex: { guid: component, position: '!' },
textData: { characters: 'Default' }
},
{ guid: outer, type: 'SYMBOL' },
{
guid: first,
type: 'INSTANCE',
parentIndex: { guid: outer, position: '!' },
symbolData: { symbolID: component }
},
{
guid: second,
type: 'INSTANCE',
parentIndex: { guid: outer, position: '"' },
symbolData: { symbolID: component }
},
{
guid: placed,
type: 'INSTANCE',
symbolData: {
symbolID: outer,
symbolOverrides: [
{ guidPath: { guids: [second, alias] }, textData: { characters: 'Second' } },
{ guidPath: { guids: [outer] }, opacity: 0.5 }
]
}
} as NodeChange
]
}
describe('instance addressing contracts', () => {
test('targets the second occurrence through overrideKey without touching its sibling', () => {
const result = interpretInstance(fixture(), '4:1')
expect(result.properties.opacity).toBe(0.5)
expect(result.children[0].children[0].properties.textData?.characters).toBe('Default')
expect(result.children[1].children[0].properties.textData?.characters).toBe('Second')
})
test('retains explicit equal-to-default claims without treating inherited fields as overrides', () => {
const input = fixture()
const placedSource = input.find((node) => node.guid === placed)
const placedSymbolData = placedSource && symbolDataOf(placedSource)
if (!placedSymbolData) throw new Error('Missing placed source')
placedSymbolData.symbolOverrides = [
{ guidPath: { guids: [second, alias] }, textData: { characters: 'Default' } }
]
const result = interpretInstance(input, '4:1')
expect(result.children[0].propertyClaims).toEqual([])
expect(result.propertyClaims).toEqual([
{
declaredBy: '4:1',
path: [second, alias],
properties: { textData: { characters: 'Default' } }
}
])
expect(result.children[0].children[0].properties.textData?.characters).toBe(
result.children[1].children[0].properties.textData?.characters
)
})
test('occurrence payloads and subsequent interpretations share no mutable data', () => {
const input = fixture()
const result = interpretInstance(input, '4:1')
const text = result.children[0].children[0].properties.textData
if (!text) throw new Error('Expected text payload')
text.characters = 'Mutated'
expect(result.children[1].children[0].properties.textData?.characters).toBe('Second')
expect(
interpretInstance(input, '4:1').children[0].children[0].properties.textData?.characters
).toBe('Default')
})
test('does not search across nested instance boundaries for a missing path segment', () => {
const input = fixture()
const root = input.find((node) => node.guid === placed)
if (!root) throw new Error('Missing root')
root.symbolData = {
symbolID: outer,
symbolOverrides: [{ guidPath: { guids: [alias] }, opacity: 0 }]
} as NodeChange['symbolData']
expectPathError(() => interpretInstance(input, '4:1'), 'missing-target')
})
test('rejects ambiguous overrideKey matches rather than choosing the first', () => {
const input = fixture()
input.push({
guid: { sessionID: 1, localID: 3 },
type: 'TEXT',
overrideKey: alias,
parentIndex: { guid: component, position: '"' }
})
expectPathError(() => interpretInstance(input, '4:1'), 'ambiguous-target')
})
test('rejects missing component sources', () => {
expect(() =>
interpretInstance(
fixture().filter((node) => node.guid !== component),
'4:1'
)
).toThrow('Missing source node 1:1')
})
})