| 1 | import assert from "node:assert/strict"; |
| 2 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 3 | import { startDisplay } from "./rfb-wire-fixture.mjs"; |
| 4 | |
| 5 | const display = await startDisplay(process.env.CAGE_UI_URL || "http://127.0.0.1:5198", { frameInterval: 200 }); |
| 6 | const { browser, page, state, errors } = await createWorkbench({ |
| 7 | url: display.url, mockWebSockets: false, viewport: { width: 1000, height: 740 }, |
| 8 | }); |
| 9 | page.setDefaultTimeout(10000); |
| 10 | await page.route("**/api/browser/resize", route => { |
| 11 | return route.fulfill({ json: { ok: true } }); |
| 12 | }); |
| 13 | try { |
| 14 | state.status.ready = state.status.vm_running = true; |
| 15 | state.tabs = [{ id: "page", kind: "web", title: "Resize", url: "https://example.test" }]; |
| 16 | state.active = "page"; |
| 17 | await page.reload(); |
| 18 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 19 | const canvas = page.locator(".browser-lane .remote-canvas canvas"); |
| 20 | const pixel = () => canvas.evaluate(c => [...c.getContext("2d").getImageData(c.width - 1, c.height - 1, 1, 1).data]); |
| 21 | await page.waitForFunction(() => { |
| 22 | const c = document.querySelector(".browser-lane canvas"); |
| 23 | return c?.width && c.getContext("2d").getImageData(0, 0, 1, 1).data[0] > 0; |
| 24 | }); |
| 25 | display.pauseFrames(true); |
| 26 | const oldPixel = await pixel(); |
| 27 | const connections = display.stats.connections; |
| 28 | const oldBox = await canvas.boundingBox(); |
| 29 | const resized = page.waitForRequest(req => req.url().endsWith("/api/browser/resize") && req.postDataJSON().width > oldBox.width * 2); |
| 30 | await page.setViewportSize({ width: 1240, height: 760 }); |
| 31 | const box = await page.locator(".browser-lane .remote-canvas").boundingBox(); |
| 32 | const shown = await canvas.boundingBox(); |
| 33 | assert.ok(Math.abs(shown.width - box.width) < 1 && Math.abs(shown.height - box.height) < 1, |
| 34 | "the previous frame must fill the browser pane before the guest responds"); |
| 35 | const x = Math.floor(box.width * .75), y = Math.floor(box.height * .25); |
| 36 | await canvas.click({ position: { x, y } }); |
| 37 | assert.ok(Math.abs(display.stats.pointer.x - x / box.width * 640) <= 1); |
| 38 | assert.ok(Math.abs(display.stats.pointer.y - y / box.height * 480) <= 1); |
| 39 | const desired = (await resized).postDataJSON(); |
| 40 | assert.equal(desired.width, Math.floor(box.width * 2)); |
| 41 | display.resize(desired); |
| 42 | await page.waitForFunction(w => document.querySelector(".browser-lane canvas")?.width === w, desired.width); |
| 43 | assert.deepEqual(await pixel(), oldPixel, "enlarging must preserve the visible corner until new pixels arrive"); |
| 44 | display.pauseFrames(false); |
| 45 | await page.waitForFunction(value => { |
| 46 | const c = document.querySelector(".browser-lane canvas"); |
| 47 | const p = c.getContext("2d").getImageData(c.width - 1, c.height - 1, 1, 1).data; |
| 48 | return p[3] === 255 && p[0] !== value; |
| 49 | }, oldPixel[0]); |
| 50 | assert.equal(display.stats.connections, connections); |
| 51 | assert.deepEqual(errors, []); |
| 52 | console.log("PASS: browser resize fills the pane, preserves the last frame and pointer mapping, then repaints over the same RFB connection"); |
| 53 | } catch (error) { |
| 54 | console.error({ errors, stats: display.stats, body: await page.locator("body").innerText() }); |
| 55 | throw error; |
| 56 | } finally { |
| 57 | await browser.close(); |
| 58 | await display.close(); |
| 59 | } |