Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions e2e/viewer-spike.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { resolve } from 'node:path'
import { expect, test, type Page } from '@playwright/test'

// Capture-review 3D view on the PuzlWerk viewer (spike); `?viewer=puzlwerk` selects it.
const fixtures = [
['4x4', 'test/fixtures/synthetic-4x4-high.zip', 4],
['5x5', 'test/fixtures/orientation-recovery-5x5.zip', 5],
['7x7', 'test/fixtures/orientation-recovery-7x7.zip', 7],
] as const

async function openRecovery(page: Page, url: string, archive: string) {
await page.goto(url)
await page
.locator('.capture-alternatives input[type="file"]')
.setInputFiles(resolve(archive))
await page
.getByRole('heading', { name: 'Does this match your cube?' })
.waitFor({ timeout: 30000 })
await page.getByRole('button', { name: /^Alternatives \([1-9]/ }).click()
}

for (const [size, archive, n] of fixtures) {
test(`recovery preview ${size} uses the PuzlWerk viewer with the flag`, async ({
page,
}) => {
const problems: string[] = []
page.on('pageerror', (error) => problems.push(String(error)))
page.on('console', (message) => {
if (message.type() === 'error') problems.push(message.text())
})
await openRecovery(page, '/?viewer=puzlwerk', archive)
const view = page.locator('.spatial-workspace .cube-3d-puzlwerk')
await expect(view.locator('canvas')).toBeVisible()
await expect(
page.locator('.spatial-workspace .cube-3d-canvas'),
).toHaveCount(0)
// The X/Y/Z axis gizmo of the old view is present, with the three axes.
const gizmo = view.locator(
'svg[aria-label="Cube orientation axes X, Y and Z"]',
)
await expect(gizmo).toBeVisible()
await expect(gizmo.locator('[data-axis]')).toHaveCount(3)
// The viewer's own floating panel stays hidden inside the dialog.
await expect(page.locator('.cube-appearance')).toBeHidden()
await expect(page.getByText('3D view unavailable')).toHaveCount(0)
// The inspected face is outlined: its n*n stickers, set after the projection loaded.
await expect(view).toHaveAttribute(
'data-highlight',
new RegExp(`^[urfdlb]:${n * n}$`),
)
// Inspecting a net face outlines exactly that face.
for (const [name, key] of [
['Up', 'u'],
['Down', 'd'],
['Left', 'l'],
] as const) {
await page
.getByRole('button', { name: `Inspect ${name} face`, exact: true })
.click()
await expect(view).toHaveAttribute('data-highlight', `${key}:${n * n}`)
}
expect(problems).toEqual([])
})
}

test('the search dialog ("Find a matching cube") also uses the PuzlWerk viewer', async ({
page,
}) => {
const problems: string[] = []
page.on('pageerror', (error) => problems.push(String(error)))
page.on('console', (message) => {
if (message.type() === 'error') problems.push(message.text())
})
await page.goto('/?viewer=puzlwerk')
await page
.locator('.capture-alternatives input[type="file"]')
.setInputFiles(
resolve('test/fixtures/photo-identity-4x4-2026-11-16T21-28-43.zip'),
)
await page
.getByRole('heading', { name: 'Does this match your cube?' })
.waitFor({ timeout: 30000 })
await page
.getByRole('button', { name: 'Find a matching cube', exact: true })
.click()
const view = page.locator('.spatial-workspace .cube-3d-puzlwerk')
await expect(view.locator('canvas')).toBeVisible()
await expect(page.locator('.spatial-workspace .cube-3d-canvas')).toHaveCount(
0,
)
await expect(view).toHaveAttribute('data-highlight', /^[urfdlb]:16$/)
await page.getByRole('button', { name: 'Cancel', exact: true }).click()
await expect(page.locator('.cube-3d-puzlwerk')).toHaveCount(0)
expect(problems).toEqual([])
})

test('closing the dialog releases the viewer WebGL context', async ({
page,
}) => {
await openRecovery(page, '/?viewer=puzlwerk', fixtures[0][1])
const canvas = page.locator('.spatial-workspace .cube-3d-puzlwerk canvas')
await expect(canvas).toBeVisible()
const handle = await canvas.elementHandle()
await page.getByRole('button', { name: 'Cancel', exact: true }).click()
await expect(page.locator('.cube-3d-puzlwerk')).toHaveCount(0)
expect(
await handle!.evaluate((element) => {
const canvas = element as HTMLCanvasElement
const gl = (canvas.getContext('webgl2') ??
canvas.getContext('webgl')) as WebGLRenderingContext | null
return gl ? gl.isContextLost() : 'no context'
}),
).toBe(true)
})

test('without WebGL the preview says so instead of staying empty', async ({
page,
}) => {
await page.addInitScript(() => {
// Called back with the canvas as `this` below.
// eslint-disable-next-line @typescript-eslint/unbound-method
const original = HTMLCanvasElement.prototype.getContext
HTMLCanvasElement.prototype.getContext = function (
this: HTMLCanvasElement,
kind: string,
...rest: unknown[]
) {
if (/webgl/.test(kind)) return null
return (original as (...args: unknown[]) => unknown).call(
this,
kind,
...rest,
)
} as typeof original
})
await openRecovery(page, '/?viewer=puzlwerk', fixtures[0][1])
await expect(page.getByRole('alert')).toContainText('3D view unavailable')
await expect(page.locator('.cube-3d-puzlwerk canvas')).toHaveCount(0)
})

test('recovery preview keeps the existing view without the flag', async ({
page,
}) => {
await openRecovery(page, '/', fixtures[0][1])
await expect(page.locator('.spatial-workspace .cube-3d-canvas')).toBeVisible()
await expect(page.locator('.cube-3d-puzlwerk')).toHaveCount(0)
})
62 changes: 62 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"rescript:build": "rescript build --warn-error +a",
"fixture:server": "node scripts/fixtureUploadServer.mjs",
"build": "npm run rescript:build && vite build",
"typecheck": "npm run verify:capture-core && npm run rescript:build && tsc --noEmit",
"typecheck": "npm run verify:capture-core && npm run verify:viewer-vendor && npm run rescript:build && tsc --noEmit",
"test": "npm run rescript:build && vitest run",
"test:e2e": "playwright test",
"test:watch": "npm run rescript:build && vitest",
Expand All @@ -19,13 +19,15 @@
"lint": "npm run lint:typescript && npm run lint:biome && npm run lint:rescript",
"format:check": "biome format . && rescript format --check",
"format": "biome format --write . && rescript format",
"verify:capture-core": "node scripts/verifyCaptureCore.mjs"
"verify:capture-core": "node scripts/verifyCaptureCore.mjs",
"verify:viewer-vendor": "node scripts/verifyViewerVendor.mjs"
},
"dependencies": {
"@fontsource/ibm-plex-mono": "^5.3.0",
"@fontsource/ibm-plex-sans": "^5.3.0",
"@fontsource/space-grotesk": "^5.3.0",
"@puzlwerk/capture-recovery": "file:vendor/puzlwerk-capture-recovery-0.3.0-f1fa30ac.tgz",
"@puzlwerk/viewer": "file:vendor/puzlwerk-viewer-0.1.0-7e14cbd8.tgz",
"fflate": "^0.8.3",
"preact": "^11.2.0"
},
Expand Down
31 changes: 31 additions & 0 deletions scripts/verifyViewerVendor.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createHash } from 'node:crypto'
import { existsSync, readFileSync } from 'node:fs'
import { resolve } from 'node:path'

const readJSON = (path) => JSON.parse(readFileSync(path, 'utf8'))
const pin = readJSON('vendor/viewer.json')
const sha = createHash('sha256')
.update(readFileSync(resolve('vendor', pin.artifact)))
.digest('hex')
const readme = readFileSync('vendor/README.md', 'utf8')
if (
!readme.includes(pin.artifact) ||
!readme.includes(pin.sha256) ||
!readme.includes(pin.sourceRevision)
)
throw new Error(
'vendor/README.md does not match the pin in vendor/viewer.json',
)
const installed = resolve('node_modules', pin.package)
const manifest = readJSON(resolve(installed, 'package.json'))
if (
sha !== pin.sha256 ||
manifest.name !== pin.package ||
manifest.version !== pin.version ||
!existsSync(resolve(installed, 'NOTICE')) ||
!existsSync(resolve(installed, 'dist/bridge/index.mjs'))
)
throw new Error('Viewer package does not match vendor/viewer.json')
console.log(
`Viewer ${pin.version}: source ${pin.sourceRevision}, artifact sha256 matches, NOTICE and bridge present`,
)
4 changes: 3 additions & 1 deletion src/client/capture/explicitCaptureRecoveryDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {
import { STICKER_COLORS } from '../vision/stickerColorGeometry'

const CubeView3D = lazy(() =>
import('../view3d/cubeView3D').then((m) => ({ default: m.CubeView3D })),
import('../view3d/cubeView3DChoice').then((m) => ({
default: m.CubeView3DChoice,
})),
)

export function ExplicitCaptureRecoveryDialog({
Expand Down
4 changes: 3 additions & 1 deletion src/client/capture/faceRecoveryDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import { STICKER_COLORS } from '../vision/stickerColorGeometry'
import { previewFaces } from './spatialOrientationWizard'

const CubeView3D = lazy(() =>
import('../view3d/cubeView3D').then((m) => ({ default: m.CubeView3D })),
import('../view3d/cubeView3DChoice').then((m) => ({
default: m.CubeView3DChoice,
})),
)
export function FaceRecoveryDialog({
recovery,
Expand Down
32 changes: 32 additions & 0 deletions src/client/view3d/cubeView3DChoice.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Spike switch between the existing 3D view and the PuzlWerk viewer. The URL parameter
// `?viewer=puzlwerk` selects the new one; anything else keeps the existing view.
import { lazy } from 'preact/compat'
import type { CubeView3DProps } from './cubeView3D'

const CubeView3D = lazy(() =>
import('./cubeView3D').then((m) => ({ default: m.CubeView3D })),
)
const PuzlwerkCubeView = lazy(() =>
import('./puzlwerkCubeView').then((m) => ({ default: m.PuzlwerkCubeView })),
)

export const puzlwerkViewerRequested = (search: string): boolean =>
new URLSearchParams(search).get('viewer') === 'puzlwerk'

type ChoiceProps = Pick<
CubeView3DProps,
'cube' | 'puzzleSize' | 'palette' | 'highlightedFace' | 'allowLayerMoves'
>

export function CubeView3DChoice(props: ChoiceProps) {
if (puzlwerkViewerRequested(window.location.search))
return (
<PuzlwerkCubeView
cube={props.cube}
puzzleSize={props.puzzleSize}
palette={props.palette}
highlightedFace={props.highlightedFace}
/>
)
return <CubeView3D {...props} />
}
Loading
Loading