openpencil/tests/e2e/collab/session.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

178 lines
5.8 KiB
TypeScript

import { expect, test } from '@playwright/test'
import {
collaborationErrors,
connect,
createPeer,
startRelay,
type Peer
} from '#tests/helpers/collab/room'
test('mobile presence popover returns focus and disconnects the peer', async ({ browser }) => {
const relay = await startRelay()
let peer: Peer | null = null
try {
peer = await createPeer(browser, 'Mobile', relay.url)
await peer.page.setViewportSize({ width: 390, height: 844 })
await connect(peer)
const trigger = peer.page.getByRole('button', { name: 'Online: 1', exact: true })
await expect(trigger).toBeVisible()
await trigger.focus()
await trigger.press('Enter')
await expect(peer.page.getByRole('button', { name: 'Disconnect', exact: true })).toBeVisible()
await peer.page.keyboard.press('Escape')
await expect(trigger).toBeFocused()
await trigger.press('Space')
await peer.page.getByRole('button', { name: 'Disconnect', exact: true }).click()
await expect(trigger).toHaveCount(0)
expect(collaborationErrors(peer)).toEqual([])
} finally {
await peer?.context.close()
await relay.close()
}
})
test('two browser peers synchronize editing, awareness, departure, and reconnect', async ({
browser
}) => {
test.setTimeout(120_000)
const relay = await startRelay()
let hostToClose: Peer | null = null
let guestToClose: Peer | null = null
try {
const host = await createPeer(browser, 'Host', relay.url)
hostToClose = host
const guest = await createPeer(browser, 'Guest', relay.url)
guestToClose = guest
await connect(host)
await connect(guest)
await expect
.poll(() => host.page.evaluate(() => window.openPencil?.test?.collab?.peerCount()))
.toBe(1)
await expect
.poll(() => guest.page.evaluate(() => window.openPencil?.test?.collab?.peerCount()))
.toBe(1)
const nodeId = await host.page.evaluate(() => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const node = store.graph.createNode('RECTANGLE', store.state.currentPageId, {
name: 'Shared rectangle',
x: 160,
y: 140,
width: 120,
height: 80
})
store.requestRender()
return node.id
})
await expect
.poll(() =>
guest.page.evaluate((id) => window.openPencil?.getStore?.().graph.getNode(id)?.name, nodeId)
)
.toBe('Shared rectangle')
await guest.page.evaluate((id) => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
store.updateNode(id, { name: 'Edited by Guest', x: 320 })
store.select([id])
window.openPencil?.test?.collab?.updateSelection([id])
}, nodeId)
await expect
.poll(() =>
host.page.evaluate((id) => window.openPencil?.getStore?.().graph.getNode(id)?.name, nodeId)
)
.toBe('Edited by Guest')
await expect
.poll(() => host.page.evaluate(() => window.openPencil?.test?.collab?.peerSelections()[0]))
.toEqual([nodeId])
relay.pause()
await guest.page.evaluate((id) => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
store.updateNode(id, { y: 280 })
}, nodeId)
await host.page.evaluate((id) => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
store.updateNode(id, { name: 'Host partition edit' })
}, nodeId)
await expect.poll(() => relay.queuedCount()).toBeGreaterThan(0)
relay.resume()
for (const peer of [host, guest]) {
await expect
.poll(() =>
peer.page.evaluate((id) => {
const node = window.openPencil?.getStore?.().graph.getNode(id)
return node ? { name: node.name, y: node.y } : null
}, nodeId)
)
.toEqual({ name: 'Host partition edit', y: 280 })
}
await guest.page.evaluate(() => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
window.openPencil?.test?.collab?.updateCursor(420, 260, store.state.currentPageId)
})
await expect
.poll(() =>
host.page.evaluate(() => window.openPencil?.getStore?.().state.presenceCursors.length)
)
.toBe(1)
expect(collaborationErrors(guest)).toEqual([])
await guest.context.close()
guestToClose = null
await expect
.poll(() => host.page.evaluate(() => window.openPencil?.test?.collab?.peerCount()))
.toBe(0)
await expect
.poll(() =>
host.page.evaluate(() => window.openPencil?.getStore?.().state.presenceCursors.length)
)
.toBe(0)
const reconnectingGuest = await createPeer(browser, 'Guest', relay.url)
try {
await host.page.evaluate((id) => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
store.updateNode(id, { name: 'Edited while offline', y: 260 })
}, nodeId)
await connect(reconnectingGuest)
await expect
.poll(() =>
reconnectingGuest.page.evaluate(
(id) => window.openPencil?.getStore?.().graph.getNode(id)?.name,
nodeId
)
)
.toBe('Edited while offline')
await expect
.poll(() => host.page.evaluate(() => window.openPencil?.test?.collab?.peerCount()))
.toBe(1)
expect(collaborationErrors(reconnectingGuest)).toEqual([])
} finally {
await reconnectingGuest.context.close()
}
expect(collaborationErrors(host)).toEqual([])
} finally {
try {
await guestToClose?.context.close()
} finally {
try {
await hostToClose?.context.close()
} finally {
await relay.close()
}
}
}
})