pcbjam/tests/web/comments-viewport-resize.spec.ts
Gergő Törcsvári f4d3c3f146
collab-presence 0010: unified draggable overlay menu
One circular FAB (peer-count badge) replaces the top-right overlay row:
draggable anywhere (pointer capture, 4px click-vs-drag threshold — the
comment-pin pattern), position persisted in localStorage, panel opens
toward screen center and stacks composable sections — roster, source chip,
view-only pill, follow row, comments, chrome toggle. Renders at z-50 above
everything (decided: covers wx dialogs; trivially dismissed via click-away/
Esc/drag). CommentLayer portals its bar + list panel into the menu's
comments slot; pins/popovers/composer/click-catcher stay canvas-anchored.
The FAB is the chrome-hidden survivor. e2e specs updated with an
openOverlayMenu helper (panel is closed by default, click-away closes it).

NOTE: Playwright suites updated but not executed here (no local WASM
artifacts) — verify on CI / next session.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A42xfPFNdfsUt9eowkC9eM
2026-07-16 17:25:56 +02:00

113 lines
5 KiB
TypeScript

import { test, expect, type Page } from '@playwright/test';
import { openOverlayMenu } from './overlay-menu';
/**
* Comment pins vs. canvas resize (collab-presence 0005 regression): the DOM
* hit targets map world→CSS through the viewport transform pushed from C++
* ({cx,cy,scale,w,h} → worldToScreen maps through w/2,h/2). The push used to
* dedupe on scale+center only, so a canvas RESIZE (window resize, boot layout
* settling after the bind-time seed) left a stale w/h in React and every pin
* target sat vertically offset from its GAL dot — by exactly Δh/2 css-px —
* until the next pan/zoom. Now wxEVT_SIZE re-pushes and size participates in
* the dedupe, so pins must re-align after a resize with no pan/zoom at all.
*
* "GAL truth" is computed from a FRESH kicadCollabGetViewport() read + the
* live glcanvas rect — the same mapping the GAL dot itself renders through.
*/
const SCOPE = 'default';
const ROUTE = 'demo.kicad_sch';
const TITLE = /demo — Schematic Editor/i;
async function bootAs(page: Page, user: string): Promise<void> {
await page.goto(`/${SCOPE}/projects/demo/${ROUTE}?user=${user}`);
await expect(page.locator('#canvas')).toBeVisible({ timeout: 120000 });
await expect
.poll(() => page.title(), {
message: `${user}: editor never reached the expected title`,
timeout: 120000,
intervals: [1000],
})
.toMatch(TITLE);
await openOverlayMenu(page); // the comment bar lives in the overlay menu (0010)
await expect(page.getByTestId('comment-bar-toggle')).toBeVisible({ timeout: 30000 });
await page.getByTestId('comment-bar-toggle').click();
await expect(page.getByTestId('comment-mode-toggle')).toBeVisible();
}
/** Comments persist in the room's ydoc — start each run from a clean slate. */
async function deleteAllThreads(page: Page): Promise<void> {
await page.evaluate(() => {
const ctl = (window as unknown as {
__pcbjamComments?: {
threads(): Array<{ id: string }>;
deleteThread(id: string): void;
};
}).__pcbjamComments;
ctl?.threads().forEach((t) => ctl.deleteThread(t.id));
});
await expect(page.getByTestId('comment-pin')).toHaveCount(0);
}
/** First pin's DOM center minus its GAL-truth CSS position (fresh transform). */
async function pinDelta(page: Page): Promise<{ dx: number; dy: number }> {
return page.evaluate(() => {
const win = window as unknown as {
Module: { kicadCollabGetViewport(): string };
__pcbjamComments: { threads(): Array<{ world: { x: number; y: number } }> };
};
const vp = JSON.parse(win.Module.kicadCollabGetViewport()) as {
cx: number; cy: number; scale: number; w: number; h: number;
};
const gl = Array.from(document.querySelectorAll('[id^="glcanvas-"]')).find((c) => {
const r = (c as HTMLElement).getBoundingClientRect();
return getComputedStyle(c as HTMLElement).display !== 'none' && r.width > 0;
}) as HTMLElement;
const r = gl.getBoundingClientRect();
const ratio = r.width / vp.w;
const world = win.__pcbjamComments.threads()[0].world;
const truthX = r.x + ((world.x - vp.cx) * vp.scale + vp.w / 2) * ratio;
const truthY = r.y + ((world.y - vp.cy) * vp.scale + vp.h / 2) * ratio;
const pin = document.querySelector('[data-testid="comment-pin"]') as HTMLElement;
const pr = pin.getBoundingClientRect();
return { dx: pr.x + pr.width / 2 - truthX, dy: pr.y + pr.height / 2 - truthY };
});
}
test('comment pin targets track the GAL dots across a canvas resize', async ({ page }) => {
test.setTimeout(300000); // one full tool boot
await bootAs(page, 'alice');
await deleteAllThreads(page);
// Place a comment mid-canvas via comment mode.
await page.getByTestId('comment-mode-toggle').click();
const catcher = page.getByTestId('comment-click-catcher');
await expect(catcher).toBeVisible();
const box = (await catcher.boundingBox())!;
await catcher.click({ position: { x: box.width / 2, y: box.height / 2 } });
await page.getByTestId('comment-composer').locator('textarea').fill('resize pin');
await page.getByTestId('comment-submit').click();
await expect(page.getByTestId('comment-pin')).toHaveCount(1);
// Aligned at the boot size (the composer click and the pin share the same
// transform, so this mostly guards the harness itself).
await expect
.poll(async () => Math.abs((await pinDelta(page)).dy), {
message: 'pin never aligned with GAL truth at the boot size',
timeout: 15000,
})
.toBeLessThan(3);
// Shrink the window: the canvas height changes while scale/center stay put.
// The wxEVT_SIZE re-push must realign the DOM target with the GAL dot —
// before the fix it stayed exactly Δh/2 css-px below until a pan/zoom.
await page.setViewportSize({ width: 1280, height: 620 });
await expect
.poll(async () => Math.abs((await pinDelta(page)).dy), {
message: 'pin target did not re-align after the canvas resize (stale viewport w/h?)',
timeout: 15000,
})
.toBeLessThan(3);
expect(Math.abs((await pinDelta(page)).dx)).toBeLessThan(3);
});