openpencil/tests/engine/render/canvas/effects/edge-cases.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

434 lines
11 KiB
TypeScript

import { describe, expect, mock, test } from 'bun:test'
import type { Path } from 'canvaskit-wasm'
import type { SceneNode } from '@open-pencil/scene-graph'
import { renderEffects } from '#core/canvas/shadows'
import { asCanvas } from '../helpers'
import { createMockCanvas, createMockRenderer, mockCalls } from './helpers'
describe('Edge cases and bug fixes', () => {
test('drop shadow cast from stroke when node has no fill', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
const node: Partial<SceneNode> = {
type: 'RECTANGLE',
width: 100,
height: 100,
fills: [{ visible: false, type: 'SOLID', color: { r: 1, g: 1, b: 1, a: 1 }, opacity: 1 }],
effects: [
{
type: 'DROP_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 0, y: 0 },
radius: 10,
spread: 0
}
],
strokes: [
{
type: 'SOLID',
visible: true,
weight: 2,
opacity: 1,
color: { r: 0, g: 0, b: 0, a: 1 },
align: 'CENTER'
}
],
strokeGeometry: [{ windingRule: 'NONZERO', commandsBlob: new Uint8Array() }],
childIds: []
}
r.getStrokeGeometry = mock(() => [{} as Path])
renderEffects(r, asCanvas(canvas), node as SceneNode, new Float32Array(4), false, 'behind')
expect(r.getStrokeGeometry).toHaveBeenCalledWith(node)
expect(canvas.drawPath).toHaveBeenCalled()
})
test('drop shadow on stroked containers uses the container bounds', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
const node: Partial<SceneNode> = {
type: 'FRAME',
width: 224,
height: 424,
fills: [],
effects: [
{
type: 'DROP_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 0, y: 2 },
radius: 4,
spread: 0,
showShadowBehindNode: false
}
],
strokes: [
{
type: 'SOLID',
visible: true,
weight: 1,
opacity: 1,
color: { r: 0, g: 0, b: 0, a: 1 },
align: 'CENTER'
}
],
strokeGeometry: [{ windingRule: 'NONZERO', commandsBlob: new Uint8Array() }],
childIds: ['header']
}
r.getStrokeGeometry = mock(() => [{} as Path])
renderEffects(r, asCanvas(canvas), node as SceneNode, new Float32Array(4), true, 'behind')
expect(r.getStrokeGeometry).not.toHaveBeenCalled()
expect(canvas.drawRRect).toHaveBeenCalled()
})
test('drop shadow from child applies child transforms (geometric parity)', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
const child: Partial<SceneNode> = {
type: 'RECTANGLE',
x: 10,
y: 20,
width: 50,
height: 60,
rotation: 45,
flipY: true,
effects: []
}
const node: Partial<SceneNode> = {
type: 'FRAME',
width: 100,
height: 100,
fills: [],
effects: [
{
type: 'DROP_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 5, y: 5 },
radius: 0,
spread: 0
}
],
childIds: ['child1']
}
renderEffects(
r,
asCanvas(canvas),
node as SceneNode,
new Float32Array(4),
false,
'behind',
child as SceneNode
)
// Verify order: translate (offset) -> rotate -> translate (flip) -> scale
const translateCalls = mockCalls(canvas.translate)
const rotateCalls = mockCalls(canvas.rotate)
const scaleCalls = mockCalls(canvas.scale)
expect(translateCalls[0]).toEqual([5 + 10, 5 + 20])
expect(rotateCalls[0]).toEqual([45, 25, 30])
expect(translateCalls[1]).toEqual([0, 60])
expect(scaleCalls[0]).toEqual([1, -1])
expect(canvas.drawRect).toHaveBeenCalled()
})
test('drop shadow from TEXT child renders glyphs (fidelity parity)', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
const child: Partial<SceneNode> = {
type: 'TEXT',
x: 10,
y: 20,
width: 50,
height: 60,
text: 'Hello',
effects: []
}
const node: Partial<SceneNode> = {
type: 'FRAME',
width: 100,
height: 100,
fills: [],
effects: [
{
type: 'DROP_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 5, y: 5 },
radius: 10,
spread: 0
}
],
childIds: ['child1']
}
renderEffects(
r,
asCanvas(canvas),
node as SceneNode,
new Float32Array(4),
false,
'behind',
child as SceneNode
)
expect(r.getCachedDropShadow).toHaveBeenCalled()
expect(canvas.saveLayer).toHaveBeenCalled()
expect(canvas.translate).toHaveBeenCalledWith(15, 25)
expect(r.renderText).toHaveBeenCalledWith(expect.anything(), child)
})
test('inner shadow from shape child applies child transforms and rects (geometric parity)', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
const child: Partial<SceneNode> = {
type: 'RECTANGLE',
x: 10,
y: 20,
width: 50,
height: 60,
rotation: 45,
flipY: true,
effects: []
}
const node: Partial<SceneNode> = {
type: 'FRAME',
width: 100,
height: 100,
fills: [],
effects: [
{
type: 'INNER_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 5, y: 5 },
radius: 0,
spread: 0
}
],
childIds: ['child1']
}
renderEffects(
r,
asCanvas(canvas),
node as SceneNode,
new Float32Array(4),
false,
'front',
child as SceneNode
)
const translateCalls = mockCalls(canvas.translate)
const rotateCalls = mockCalls(canvas.rotate)
const scaleCalls = mockCalls(canvas.scale)
expect(translateCalls[0]).toEqual([10, 20])
expect(rotateCalls[0]).toEqual([45, 25, 30])
expect(translateCalls[1]).toEqual([0, 60])
expect(scaleCalls[0]).toEqual([1, -1])
expect(r.ck.LTRBRect).toHaveBeenCalledWith(0, 0, 50, 60)
expect(canvas.clipRect).toHaveBeenCalled()
})
test('drop shadow from TEXT child applies offset in parent space', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
const child: Partial<SceneNode> = {
type: 'TEXT',
x: 10,
y: 20,
width: 50,
height: 60,
text: 'Hello',
rotation: 45,
effects: []
}
const node: Partial<SceneNode> = {
type: 'FRAME',
width: 100,
height: 100,
fills: [],
effects: [
{
type: 'DROP_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 5, y: 5 },
radius: 10,
spread: 0
}
],
childIds: ['child1']
}
renderEffects(
r,
asCanvas(canvas),
node as SceneNode,
new Float32Array(4),
false,
'behind',
child as SceneNode
)
const translateCalls = mockCalls(canvas.translate)
expect(translateCalls[0]).toEqual([15, 25]) // offset + child position
// The filter should NOT have the offset (neutralized to 0,0)
expect(r.getCachedDropShadow).toHaveBeenCalledWith(0, 0, 5, expect.anything())
})
test('INNER_SHADOW with large offset does not vanish (bounding box union)', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
const node: Partial<SceneNode> = {
type: 'RECTANGLE',
width: 100,
height: 100,
fills: [],
effects: [
{
type: 'INNER_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 200, y: 0 },
radius: 10,
spread: 0
}
]
}
renderEffects(
r,
asCanvas(canvas),
node as SceneNode,
new Float32Array([0, 0, 100, 100]),
false,
'front'
)
// Expect LTRBRect for 'big' to encompass both the shape (0 to 100) and the offset hole (200 to 300)
// with expand (20) padding: min(-20, -20+200) = -20, max(100+20, 100+20+200) = 320
expect(r.ck.LTRBRect).toHaveBeenCalledWith(
-20, // min(-expand, -expand + offsetX)
-20, // min(-expand, -expand + offsetY)
320, // max(width + expand, width + expand + offsetX)
120 // max(height + expand, height + expand + offsetY)
)
expect(canvas.drawPath).toHaveBeenCalled()
})
test('INNER_SHADOW with spread on rounded rect shrinks hole concentrically', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
const node: Partial<SceneNode> = {
type: 'RECTANGLE',
width: 100,
height: 100,
cornerRadius: 10,
fills: [],
effects: [
{
type: 'INNER_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 5, y: 5 },
radius: 10,
spread: 4
}
]
}
renderEffects(
r,
asCanvas(canvas),
node as SceneNode,
new Float32Array([0, 0, 100, 100]),
true,
'front'
)
// makeRRectWithOffset should receive (node, localOffsetX, localOffsetY, spread)
// localOffsetX = 5, localOffsetY = 5, spread = 4
expect(r.makeRRectWithOffset).toHaveBeenCalledWith(node, 5, 5, 4)
})
test('drop shadow restores canvas and paint state when drawing throws', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
canvas.drawRect = mock(() => {
throw new Error('draw failed')
})
const node: Partial<SceneNode> = {
type: 'RECTANGLE',
width: 100,
height: 100,
fills: [],
effects: [
{
type: 'DROP_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 0, y: 0 },
radius: 10,
spread: 0
}
],
strokeGeometry: [],
childIds: []
}
expect(() =>
renderEffects(r, asCanvas(canvas), node as SceneNode, new Float32Array(4), false, 'behind')
).toThrow('draw failed')
expect(canvas.restore).toHaveBeenCalled()
expect(r.auxFill.setMaskFilter).toHaveBeenCalledWith(null)
expect(r.auxFill.setBlendMode).toHaveBeenCalledWith(r.ck.BlendMode.SrcOver)
expect(r.effectLayerPaint.setImageFilter).toHaveBeenCalledWith(null)
})
test('text inner shadow restores canvas when glyph rendering throws', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
r.renderText = mock(() => {
throw new Error('text failed')
})
const node: Partial<SceneNode> = {
type: 'TEXT',
width: 100,
height: 100,
fills: [],
effects: [
{
type: 'INNER_SHADOW',
visible: true,
color: { r: 0, g: 0, b: 0, a: 1 },
offset: { x: 0, y: 0 },
radius: 10,
spread: 0
}
]
}
expect(() =>
renderEffects(r, asCanvas(canvas), node as SceneNode, new Float32Array(4), false, 'front')
).toThrow('text failed')
expect(mockCalls(canvas.restore).length).toBe(2)
expect(r.effectLayerPaint.setColorFilter).toHaveBeenCalledWith(null)
expect(r.effectLayerPaint.setImageFilter).toHaveBeenCalledWith(null)
})
})