openpencil/tests/e2e/perf/large-document.spec.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

150 lines
5.4 KiB
TypeScript

import { expect, test } from '@playwright/test'
import { CanvasHelper } from '#tests/helpers/canvas'
import { seedLargeDocument } from '#tests/helpers/large-document'
import { measureHitTesting } from '#tests/helpers/performance/hit-testing'
type TimingSummary = {
hitTestMissMs: number
hitTestHitMs: number
cachedFrameMs: number
overlayFrameMs: number
volatileFrameMs: number
volatileMode: string
}
type PerformanceProfile = {
timings: TimingSummary
pointer: { calls: number; totalMs: number }
}
const SCALES = [500, 2000]
const profiles = new Map<number, PerformanceProfile>()
test.describe.serial('large-document performance', () => {
for (const nodeCount of SCALES) {
test(`profiles representative ${nodeCount}-node interactions`, async ({ page }, testInfo) => {
test.setTimeout(120_000)
await page.goto('/?test&no-chrome&no-rulers')
const canvas = new CanvasHelper(page)
await canvas.waitForInit()
await canvas.clearCanvas()
const fixture = await seedLargeDocument(page, nodeCount)
await canvas.waitForRender()
const bounds = await canvas.canvas.boundingBox()
if (!bounds) throw new Error('Canvas bounds unavailable')
const pointerProfile = await measureHitTesting(page, async () => {
await page.mouse.move(bounds.x + 10, bounds.y + 10)
await page.mouse.move(bounds.x + bounds.width - 10, bounds.y + bounds.height - 10, {
steps: 40
})
})
const result = await page.evaluate((profile): TimingSummary => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const renderer = store.renderer
if (!renderer) throw new Error('OpenPencil renderer not initialized')
const graph = store.graph
const iterations = 50
function average(run: () => void) {
const startedAt = performance.now()
for (let index = 0; index < iterations; index++) run()
return (performance.now() - startedAt) / iterations
}
renderer.dpr = window.devicePixelRatio || 1
renderer.panX = store.state.panX
renderer.panY = store.state.panY
renderer.zoom = store.state.zoom
renderer.viewportWidth = 1280
renderer.viewportHeight = 800
renderer.showRulers = false
renderer.pageColor = store.state.pageColor
renderer.pageId = store.state.currentPageId
renderer.render(graph, store.state.selectedIds, {}, store.state.sceneVersion)
const lastId = profile.leafIds.at(-1)
const lastNode = lastId ? graph.getNode(lastId) : null
if (!lastNode) throw new Error('Large-document target not found')
const lastPosition = graph.getAbsolutePosition(lastNode.id)
const hitTestMissMs = average(() => {
graph.hitTestSelectable(
profile.worldWidth + 100,
profile.worldHeight + 100,
store.state.currentPageId,
store.state.selectedIds
)
})
const hitTestHitMs = average(() => {
graph.hitTestSelectable(
lastPosition.x + lastNode.width / 2,
lastPosition.y + lastNode.height / 2,
store.state.currentPageId,
store.state.selectedIds
)
})
const cachedFrameMs = average(() => {
renderer.render(graph, store.state.selectedIds, {}, store.state.sceneVersion)
})
const overlayFrameMs = average(() => {
renderer.render(
graph,
store.state.selectedIds,
{ hoveredNodeId: lastNode.id },
store.state.sceneVersion
)
})
const volatileFrameMs = average(() => {
renderer.render(
graph,
store.state.selectedIds,
{ rotationPreview: { nodeId: lastNode.id, angle: 1 } },
store.state.sceneVersion
)
})
return {
hitTestMissMs,
hitTestHitMs,
cachedFrameMs,
overlayFrameMs,
volatileFrameMs,
volatileMode: renderer.profiler.stats.scenePictureMode
}
}, fixture)
const profile = { timings: result, pointer: pointerProfile }
profiles.set(nodeCount, profile)
await testInfo.attach(`large-document-${nodeCount}.json`, {
body: JSON.stringify(profile, null, 2),
contentType: 'application/json'
})
expect(fixture.nodeCount).toBe(nodeCount)
expect(pointerProfile.calls).toBeGreaterThan(0)
expect(result.volatileMode).toBe('volatile')
expect(result.hitTestMissMs).toBeGreaterThanOrEqual(0)
expect(result.hitTestHitMs).toBeGreaterThanOrEqual(0)
expect(result.cachedFrameMs).toBeGreaterThanOrEqual(0)
expect(result.overlayFrameMs).toBeGreaterThanOrEqual(0)
expect(result.volatileFrameMs).toBeGreaterThanOrEqual(0)
const smallerProfile = profiles.get(SCALES[0])
if (nodeCount !== SCALES[0] && smallerProfile) {
const scaleRatio = nodeCount / SCALES[0]
expect(result.hitTestMissMs).toBeLessThanOrEqual(
Math.max(smallerProfile.timings.hitTestMissMs * scaleRatio * 2, 1)
)
expect(pointerProfile.totalMs).toBeLessThanOrEqual(
Math.max(smallerProfile.pointer.totalMs * scaleRatio * 2, 10)
)
}
expect(canvas.errors.filter((error) => !error.includes('127.0.0.1:7600'))).toEqual([])
})
}
})