diff --git a/client/e2e/drag.test.mjs b/client/e2e/drag.test.mjs index 51be68e..55f861f 100644 --- a/client/e2e/drag.test.mjs +++ b/client/e2e/drag.test.mjs @@ -55,31 +55,60 @@ async function waitForRender(page, predicate, timeout = 20000) { throw new Error(`drag renderer never reached the expected state: ${JSON.stringify(last)}`) } -async function canvasOrigin(page) { +/** The world is 4096 px; the camera starts centred, so this is the middle of the screen at zoom 1. */ +const CENTRE = [2048, 2048] + +/** Canvas rect plus the world container's transform (#69): `screen = world * zoom + camera.xy`. */ +async function viewOf(page) { return page.evaluate(() => { const canvas = document.querySelector('#stage canvas') if (!canvas) throw new Error('no canvas') const rect = canvas.getBoundingClientRect() - return { x: rect.left, y: rect.top } + const [x, y] = (canvas.dataset.camera ?? '').split(',').map(Number) + return { left: rect.left, top: rect.top, x, y, zoom: Number(canvas.dataset.zoom ?? '1') } }) } +/** Page coordinates of a world point, through the camera at any zoom (no 1:1 assumption). */ +async function screenOf(page, world) { + const view = await viewOf(page) + return { + x: view.left + world[0] * view.zoom + view.x, + y: view.top + world[1] * view.zoom + view.y, + } +} + +/** Wheel-zoom around `anchorWorld`, keeping that point under the pointer; read the clamped zoom. */ +async function zoomTo(page, zoom, anchorWorld) { + for (let attempt = 0; attempt < 30; attempt += 1) { + const view = await viewOf(page) + if (Math.abs(view.zoom - zoom) < 1e-9) return view.zoom + const point = await screenOf(page, anchorWorld) + await page.mouse.move(point.x, point.y) + await page.mouse.wheel(0, view.zoom < zoom ? -250 : 250) + await sleep(40) + } + const view = await viewOf(page) + throw new Error(`zoom never reached ${zoom}, stuck at ${view.zoom}`) +} + /** Press on a world point, move in real steps (so the 20 Hz sender emits several), release. */ async function drag(page, from, to, steps = 8) { - const origin = await canvasOrigin(page) - await page.mouse.move(origin.x + from[0], origin.y + from[1]) + const start = await screenOf(page, from) + await page.mouse.move(start.x, start.y) await page.mouse.down() for (let step = 1; step <= steps; step += 1) { const x = from[0] + ((to[0] - from[0]) * step) / steps const y = from[1] + ((to[1] - from[1]) * step) / steps - await page.mouse.move(origin.x + x, origin.y + y) + const point = await screenOf(page, [x, y]) + await page.mouse.move(point.x, point.y) await sleep(60) } await page.mouse.up() } /** One GM-owned scene with a player and one controlled token. */ -async function sceneWithPlayer(origin, gmKey, pos = [400, 400]) { +async function sceneWithPlayer(origin, gmKey, pos = CENTRE) { const gm = await openAs(origin, gmKey) await gm.ws.waitText('hello') const scene = (await gm.ws.waitText('walls')).scene @@ -112,22 +141,22 @@ test('drag persists after reload (stories 38, 39)', { timeout: 180000 }, async ( await waitForBadge(page, 'connected') await waitForRender(page, view => view.ids.includes(token)) - await drag(page, [400, 400], [500, 460]) + await drag(page, CENTRE, [2148, 2108]) // The server remains the only judge: its frame carries the final position (story 39). const frame = await gm.ws.waitFor( message => message.t === 'token' && message.id === token && - Math.abs(message.pos[0] - 500) <= 2 && - Math.abs(message.pos[1] - 460) <= 2, + Math.abs(message.pos[0] - 2148) <= 2 && + Math.abs(message.pos[1] - 2108) <= 2, 10000, ) - assert.deepEqual(frame.pos, [500, 460]) + assert.deepEqual(frame.pos, [2148, 2108]) const settled = await waitForRender( page, view => - Math.abs(view.positions[token]?.[0] - 500) <= 2 && - Math.abs(view.positions[token]?.[1] - 460) <= 2 && + Math.abs(view.positions[token]?.[0] - 2148) <= 2 && + Math.abs(view.positions[token]?.[1] - 2108) <= 2 && view.dragId === '' && view.pending === 0, ) @@ -139,11 +168,13 @@ test('drag persists after reload (stories 38, 39)', { timeout: 180000 }, async ( await waitForRender(page, view => view.ids.includes(token)) const reloaded = await waitForRender( page, - view => Math.abs(view.positions[token]?.[0] - 500) <= 2 && Math.abs(view.positions[token]?.[1] - 460) <= 2, + view => + Math.abs(view.positions[token]?.[0] - 2148) <= 2 && + Math.abs(view.positions[token]?.[1] - 2108) <= 2, ) assert.equal(reloaded.dragPhase, '') // The player's own frame after the resync is the server's position, not a replay. - assert.deepEqual(gm.ws.messages.filter(m => m.t === 'token' && m.id === token).at(-1).pos, [500, 460]) + assert.deepEqual(gm.ws.messages.filter(m => m.t === 'token' && m.id === token).at(-1).pos, [2148, 2108]) } finally { gm.ws.close() await browser.close() @@ -157,7 +188,7 @@ test('a wall refuses the move and the sprite slides back (story 40)', { timeout: // A blocking wall between the token and the drop point. A player cannot see it: prediction is // optimistic and the server judges (DESIGN 7.5). const wall = await gm.ws.request({ - t: 'wall.add', scene, a: [600, 300], b: [600, 500], + t: 'wall.add', scene, a: [2248, 1948], b: [2248, 2148], }) assert.equal(wall.ok, true) @@ -170,7 +201,7 @@ test('a wall refuses the move and the sprite slides back (story 40)', { timeout: await waitForBadge(page, 'connected') await waitForRender(page, view => view.ids.includes(token)) - await drag(page, [400, 400], [680, 400], 10) + await drag(page, CENTRE, [2328, 2048], 10) // The refusal increments the counter and starts the cosmetic tween (436/440: a slide back). await page.waitForFunction( @@ -198,8 +229,8 @@ test('a wall refuses the move and the sprite slides back (story 40)', { timeout: ) assert.equal(settled.refused >= 1, true) const finalX = settled.positions[token]?.[0] ?? 0 - assert.ok(finalX <= 582, `rolled back to the clear side of the wall, got ${finalX}`) - assert.ok(finalX >= 400, 'the rollback is left of the wall but not past the start') + assert.ok(finalX <= 2230, `rolled back to the clear side of the wall, got ${finalX}`) + assert.ok(finalX >= 2048, 'the rollback is left of the wall but not past the start') assert.ok(midX > finalX, `the tween is between the drop and the target: mid=${midX} final=${finalX}`) // The authoritative frame says the same thing the sprite shows. await gm.ws.waitFor( @@ -225,37 +256,38 @@ test('authoritative frames are ignored under the finger; the GM can move the tok await waitForBadge(page, 'connected') await waitForRender(page, view => view.ids.includes(token)) - const base = await canvasOrigin(page) - await page.mouse.move(base.x + 400, base.y + 400) + const start = await screenOf(page, CENTRE) + const held = await screenOf(page, [2128, 2048]) + await page.mouse.move(start.x, start.y) await page.mouse.down() - await page.mouse.move(base.x + 480, base.y + 400) + await page.mouse.move(held.x, held.y) await sleep(80) let view = await waitForRender(page, state => state.dragId === token) - assert.deepEqual(view.dragPos, '480,400') + assert.deepEqual(view.dragPos, '2128,2048') // Story 41/43: the GM moves the same token while the player still holds it. The player's // sprite must not jump (the authoritative frame is ignored for drawing while dragging). - const gmMove = await gm.ws.request({ t: 'token.move', id: token, pos: [450, 400] }) + const gmMove = await gm.ws.request({ t: 'token.move', id: token, pos: [2098, 2048] }) assert.equal(gmMove.ok, true) await gm.ws.waitFor( - message => message.t === 'token' && message.id === token && message.pos[0] === 450, + message => message.t === 'token' && message.id === token && message.pos[0] === 2098, 5000, ) await sleep(150) view = await renderState(page) assert.equal(view.dragId, token) - assert.deepEqual(view.positions[token], [480, 400], 'the sprite stayed under the pointer') + assert.deepEqual(view.positions[token], [2128, 2048], 'the sprite stayed under the pointer') await page.mouse.up() - // The release send starts from the GM's position (450) to 480: inside the step cap, accepted. + // The release send starts from the GM's position (2098) to 2128: inside the step cap, accepted. await gm.ws.waitFor( message => message.t === 'token' && message.id === token && - Math.abs(message.pos[0] - 480) <= 2 && Math.abs(message.pos[1] - 400) <= 2, + Math.abs(message.pos[0] - 2128) <= 2 && Math.abs(message.pos[1] - 2048) <= 2, 10000, ) const settled = await waitForRender(page, state => state.dragId === '' && state.pending === 0) - assert.deepEqual(settled.positions[token], [480, 400]) + assert.deepEqual(settled.positions[token], [2128, 2048]) assert.equal(settled.rollbacks, 0, 'an accepted release needs no rollback') } finally { gm.ws.close() @@ -277,14 +309,15 @@ test('a reconnect mid-drag drops the pending intent and resumes from the server' await waitForBadge(page, 'connected') await waitForRender(page, view => view.ids.includes(token)) - const base = await canvasOrigin(page) - await page.mouse.move(base.x + 400, base.y + 400) + const start = await screenOf(page, CENTRE) + const held = await screenOf(page, [2118, 2048]) + await page.mouse.move(start.x, start.y) await page.mouse.down() - await page.mouse.move(base.x + 470, base.y + 400) + await page.mouse.move(held.x, held.y) await sleep(80) await waitForRender(page, state => state.dragId === token) const before = await gm.ws.waitFor( - message => message.t === 'token' && message.id === token && Math.abs(message.pos[0] - 470) <= 2, + message => message.t === 'token' && message.id === token && Math.abs(message.pos[0] - 2118) <= 2, 5000, ) assert.ok(before) @@ -305,14 +338,14 @@ test('a reconnect mid-drag drops the pending intent and resumes from the server' await waitForRender(page, state => state.ids.includes(token)) const resumed = await waitForRender( page, - state => Math.abs(state.positions[token]?.[0] - 470) <= 2 && state.dragPhase === '', + state => Math.abs(state.positions[token]?.[0] - 2118) <= 2 && state.dragPhase === '', ) - assert.deepEqual(resumed.positions[token], [470, 400], 'the resync is the server position') + assert.deepEqual(resumed.positions[token], [2118, 2048], 'the resync is the server position') assert.equal(resumed.pending, 0) await page.mouse.up() // the stale release must not send a move onto the new connection await sleep(150) const after = await renderState(page) - assert.deepEqual(after.positions[token], [470, 400]) + assert.deepEqual(after.positions[token], [2118, 2048]) } finally { if (restarted) await stopServer(restarted) gm.ws.close() @@ -320,3 +353,76 @@ test('a reconnect mid-drag drops the pending intent and resumes from the server' } }) }) + +test('drag follows the camera transform at zoom 0.5 and 4, and a token drag never pans', { timeout: 180000 }, async () => { + await withStack(async ({ origin, gmKey }) => { + const { gm, player, token } = await sceneWithPlayer(origin, gmKey) + const browser = await launchBrowser() + try { + const { page } = await newPage(browser) + await page.goto(`${origin}/`) + await waitForVisible(page, '#signin') + await signIn(page, player.key) + await waitForBadge(page, 'connected') + await waitForRender(page, view => view.ids.includes(token)) + + // Player maximum (4.0): 80 screen px is 20 world px. + assert.equal(await zoomTo(page, 4, CENTRE), 4) + const cameraAt4 = await viewOf(page) + await drag(page, CENTRE, [2068, 2048], 6) + await gm.ws.waitFor( + message => + message.t === 'token' && message.id === token && + Math.abs(message.pos[0] - 2068) <= 2 && Math.abs(message.pos[1] - 2048) <= 2, + 10000, + ) + await waitForRender( + page, + view => + Math.abs(view.positions[token]?.[0] - 2068) <= 2 && + Math.abs(view.positions[token]?.[1] - 2048) <= 2 && + view.dragId === '', + ) + // The press landed on a controlled token, so the camera's pan never saw the gesture. + const afterTokenDrag = await viewOf(page) + assert.equal(afterTokenDrag.zoom, 4) + assert.equal(afterTokenDrag.x, cameraAt4.x, 'a token drag must not pan the map') + assert.equal(afterTokenDrag.y, cameraAt4.y, 'a token drag must not pan the map') + + // Player minimum (0.5): 80 world px is only 40 screen px, still a real pointer path. + const moved = [2068, 2048] + assert.equal(await zoomTo(page, 0.5, moved), 0.5) + await drag(page, moved, [2148, 2048], 6) + await gm.ws.waitFor( + message => + message.t === 'token' && message.id === token && + Math.abs(message.pos[0] - 2148) <= 2 && Math.abs(message.pos[1] - 2048) <= 2, + 10000, + ) + await waitForRender( + page, + view => + Math.abs(view.positions[token]?.[0] - 2148) <= 2 && + Math.abs(view.positions[token]?.[1] - 2048) <= 2 && + view.dragId === '', + ) + + // Elsewhere the same gesture is a pan (#69): the empty canvas still moves the camera. + const empty = await screenOf(page, [2280, 2048]) + const cameraBeforePan = await viewOf(page) + await page.mouse.move(empty.x, empty.y) + await page.mouse.down() + await page.mouse.move(empty.x + 40, empty.y + 20) + await page.mouse.up() + await sleep(80) + const afterPan = await viewOf(page) + assert.equal(afterPan.zoom, 0.5) + assert.ok(afterPan.x > cameraBeforePan.x + 20, `the empty canvas panned: ${cameraBeforePan.x} -> ${afterPan.x}`) + const panned = await renderState(page) + assert.deepEqual(panned.positions[token], [2148, 2048], 'panning never moves the token') + } finally { + gm.ws.close() + await browser.close() + } + }) +}) diff --git a/client/src/render/modules/camera.ts b/client/src/render/modules/camera.ts index 37e2b47..f00c195 100644 --- a/client/src/render/modules/camera.ts +++ b/client/src/render/modules/camera.ts @@ -3,7 +3,8 @@ // so a pan is two numbers and never a redraw of the map. // // The module also owns the grid toggle button and the small `data-*` view contract the browser e2e -// reads: `data-zoom` (the exact clamped number), `data-grid` (on/off) and `data-tiles` (the tile +// reads: `data-zoom` (the exact clamped number), `data-camera` (the world container's `x,y`, so a +// test can go screen <- world at any zoom), `data-grid` (on/off) and `data-tiles` (the tile // module marks `ready` when every wanted tile of the current viewport has settled). Those are UI // facts, not renderer internals. @@ -51,6 +52,8 @@ export const renderModule: RenderModule = { world.scale.set(camera.zoom) world.position.set(camera.x, camera.y) canvas.dataset['zoom'] = String(camera.zoom) + // Screen <- world for the browser e2e and drag tests: `screen = world * zoom + x,y`. + canvas.dataset['camera'] = `${camera.x},${camera.y}` canvas.dataset['grid'] = grid ? 'on' : 'off' canvas.dataset['view'] = `${viewport.state.size.width}x${viewport.state.size.height}` } diff --git a/client/src/render/modules/tiles.ts b/client/src/render/modules/tiles.ts index 7660e28..e12afce 100644 --- a/client/src/render/modules/tiles.ts +++ b/client/src/render/modules/tiles.ts @@ -12,8 +12,7 @@ import { Sprite, Texture } from 'pixi.js' import type { RenderModule } from '../stage.ts' -import { viewport } from '../../viewport.ts' -import { visibleTiles } from '../../viewport.ts' +import { viewport, visibleTiles } from '../../viewport.ts' import { TileScheduler } from '../../tiles.ts' import type { TileLoadResult } from '../../tiles.ts'