openpencil/tests/e2e/stroke-picker/basic.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

215 lines
7.6 KiB
TypeScript

import { expect, test, type Page } from '@playwright/test'
import type { SceneNode, Stroke } from '@open-pencil/scene-graph'
import { CanvasHelper } from '#tests/helpers/canvas'
import { waitForDemo } from '#tests/helpers/demo'
import { propertyItems, propertySection } from '#tests/helpers/properties'
async function dragSlider(page: Page, canvas: CanvasHelper, testId: string, ratio: number) {
const slider = page.getByTestId(testId).locator(':scope > [data-orientation="horizontal"]')
const box = await slider.boundingBox()
if (!box) throw new Error(`Missing slider: ${testId}`)
await slider.click({
position: {
x: Math.max(2, Math.min(box.width - 2, box.width * ratio)),
y: box.height / 2
}
})
await canvas.waitForRender()
}
async function openStrokePicker(page: Page) {
await propertyItems(page, 'strokes')
.first()
.getByRole('button', { name: 'Stroke', exact: true })
.click()
await expect(page.locator('[data-picker-content]')).toBeVisible()
}
async function chooseFormat(page: Page, label: 'RGB' | 'HSL' | 'HSB' | 'OkHCL') {
await page.getByTestId('color-format-select').click()
await page.getByRole('option', { name: label, exact: true }).click()
}
async function getSelectedStroke(page: Page) {
return page.evaluate(() => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const id = [...store.state.selectedIds][0]
const node = store.graph.getNode(id)
return node?.strokes?.[0] ?? null
})
}
test('stroke picker updates stroke color on a rectangle', async ({ page }) => {
const canvas = new CanvasHelper(page)
await page.goto('/')
await canvas.waitForInit()
await canvas.drawRect(120, 120, 180, 120)
await propertySection(page, 'Stroke').getByRole('button', { name: 'Add stroke' }).click()
await canvas.waitForRender()
const before = await getSelectedStroke(page)
await openStrokePicker(page)
await dragSlider(page, canvas, 'color-slider-hue', 0.7)
const after = await getSelectedStroke(page)
expect(after).not.toBeNull()
expect(
before?.color.r !== after?.color.r ||
before?.color.g !== after?.color.g ||
before?.color.b !== after?.color.b
).toBe(true)
})
test('stroke picker alpha slider updates stroke opacity and alpha', async ({ page }) => {
const canvas = new CanvasHelper(page)
await page.goto('/')
await canvas.waitForInit()
await canvas.drawRect(120, 120, 180, 120)
await propertySection(page, 'Stroke').getByRole('button', { name: 'Add stroke' }).click()
await canvas.waitForRender()
await openStrokePicker(page)
await dragSlider(page, canvas, 'color-slider-alpha', 0.25)
const after = await getSelectedStroke(page)
expect(after).not.toBeNull()
expect(after?.opacity).toBeLessThan(1)
expect(after?.color.a).toBeCloseTo(after?.opacity ?? 0, 3)
})
test('stroke picker hsb saturation and brightness sliders update stroke color on demo card', async ({
page
}) => {
const canvas = new CanvasHelper(page)
await page.goto('/demo')
await canvas.waitForInit()
await waitForDemo(page)
await page.evaluate(async () => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const nodes = Array.from(store.graph.nodes.values())
const card =
nodes.find((node) => node.name === 'Card' && node.type === 'COMPONENT') ??
nodes.find((node) => node.name === 'Card')
if (!card) throw new Error('Card not found')
let owner: SceneNode | undefined = card
while (owner && owner.type !== 'CANVAS') {
owner = owner.parentId ? store.graph.getNode(owner.parentId) : undefined
}
if (owner && owner.id !== store.state.currentPageId) await store.switchPage(owner.id)
const stroke: Stroke = {
type: 'SOLID',
color: { r: 0.9, g: 0.9, b: 0.92, a: 1 },
weight: 1,
opacity: 1,
visible: true,
align: 'INSIDE'
}
store.updateNodeWithUndo(card.id, { strokes: [stroke] }, 'Add demo card stroke')
store.select([card.id])
})
await canvas.waitForRender()
await expect(propertyItems(page, 'strokes')).toBeVisible()
await openStrokePicker(page)
await chooseFormat(page, 'HSB')
const beforeS = await getSelectedStroke(page)
await dragSlider(page, canvas, 'color-slider-hsb-s', 0.6)
const afterS = await getSelectedStroke(page)
expect(afterS).not.toBeNull()
expect(
beforeS?.color.r !== afterS?.color.r ||
beforeS?.color.g !== afterS?.color.g ||
beforeS?.color.b !== afterS?.color.b
).toBe(true)
const beforeB = await getSelectedStroke(page)
await dragSlider(page, canvas, 'color-slider-hsb-b', 0.2)
const afterB = await getSelectedStroke(page)
expect(afterB).not.toBeNull()
expect(
beforeB?.color.r !== afterB?.color.r ||
beforeB?.color.g !== afterB?.color.g ||
beforeB?.color.b !== afterB?.color.b
).toBe(true)
})
test('bound stroke picker is non-destructive, rolls back Escape, and detaches in one undo step', async ({
page
}) => {
const canvas = new CanvasHelper(page)
await page.goto('/')
await canvas.waitForInit()
await canvas.drawRect(120, 120, 180, 120)
await propertySection(page, 'Stroke').getByRole('button', { name: 'Add stroke' }).click()
await canvas.waitForRender()
const before = await page.evaluate(() => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const collection = store.graph.createCollection('Colors')
const variable = store.graph.createVariable('stroke-brand', 'COLOR', collection.id, {
r: 0.9,
g: 0.1,
b: 0.2,
a: 1
})
const id = [...store.state.selectedIds][0]
if (!id) throw new Error('Expected selected node')
store.graph.bindVariable(id, 'strokes/0/color', variable.id)
store.state.sceneVersion++
const node = store.getNode(id)
return { color: node?.strokes[0]?.color, binding: node?.boundVariables['strokes/0/color'] }
})
await canvas.waitForRender()
await openStrokePicker(page)
expect(
await page.evaluate(() => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const id = [...store.state.selectedIds][0]
return id ? store.getNode(id)?.boundVariables['strokes/0/color'] : undefined
})
).toBe(before.binding)
await dragSlider(page, canvas, 'color-slider-hue', 0.55)
await page.keyboard.press('Escape')
await canvas.waitForRender()
const afterEscape = await page.evaluate(() => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const id = [...store.state.selectedIds][0]
const node = id ? store.getNode(id) : null
return { color: node?.strokes[0]?.color, binding: node?.boundVariables['strokes/0/color'] }
})
expect(afterEscape).toEqual(before)
await openStrokePicker(page)
await dragSlider(page, canvas, 'color-slider-hue', 0.75)
await propertyItems(page, 'strokes')
.first()
.getByRole('button', { name: 'Stroke', exact: true })
.click()
await canvas.waitForRender()
expect((await getSelectedStroke(page))?.color).not.toEqual(before.color)
await canvas.undo()
await canvas.waitForRender()
const restored = await page.evaluate(() => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const id = [...store.state.selectedIds][0]
const node = id ? store.getNode(id) : null
return { color: node?.strokes[0]?.color, binding: node?.boundVariables['strokes/0/color'] }
})
expect(restored).toEqual(before)
})