| 1 | import assert from "node:assert/strict"; |
| 2 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 3 | |
| 4 | const { browser, page, state, errors } = await createWorkbench(); |
| 5 | await page.addInitScript(() => { |
| 6 | window.EventSource = class extends EventTarget { |
| 7 | constructor() { |
| 8 | super(); |
| 9 | window.navigationEvents = this; |
| 10 | queueMicrotask(() => this.onopen?.()); |
| 11 | } |
| 12 | close() {} |
| 13 | }; |
| 14 | }); |
| 15 | const back = page.getByRole("button", { name: "后退", exact: true }); |
| 16 | const forward = page.getByRole("button", { name: "前进", exact: true }); |
| 17 | const send = (snapshot) => page.evaluate(snapshot => { |
| 18 | window.navigationEvents.onmessage({ data: JSON.stringify(snapshot) }); |
| 19 | }, { tabs: state.tabs, active: state.active, ...snapshot }); |
| 20 | async function navigate(button, path, id) { |
| 21 | const request = page.waitForRequest(`**/api/browser/${path}`); |
| 22 | await button.click(); |
| 23 | assert.deepEqual((await request).postDataJSON(), { id }); |
| 24 | } |
| 25 | try { |
| 26 | state.status.ready = state.status.vm_running = true; |
| 27 | state.tabs = [ |
| 28 | { id: "first", title: "First tab", kind: "web", url: "https://example.test/first" }, |
| 29 | { id: "second", title: "Second tab", kind: "web", url: "https://example.test/second" }, |
| 30 | ]; |
| 31 | state.active = "first"; |
| 32 | await page.reload(); |
| 33 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 34 | await send({ back: false, forward: false }); |
| 35 | assert.equal(await back.isDisabled(), true); |
| 36 | assert.equal(await forward.isDisabled(), true); |
| 37 | const reads = () => state.requests.filter(item => item.path === "/browser/tabs").length; |
| 38 | const before = reads(); |
| 39 | |
| 40 | await send({ back: true, forward: false }); |
| 41 | await navigate(back, "back", "first"); |
| 42 | await send({ back: false, forward: true }); |
| 43 | await navigate(forward, "forward", "first"); |
| 44 | |
| 45 | |
| 46 | await send({ back: true, forward: true }); |
| 47 | await page.route("**/api/browser/activate", async route => { |
| 48 | await new Promise(resolve => setTimeout(resolve, 150)); |
| 49 | state.active = route.request().postDataJSON().id; |
| 50 | await route.fulfill({ json: { ok: true } }); |
| 51 | }); |
| 52 | await page.getByRole("tab", { name: "Second tab", exact: true }).click(); |
| 53 | assert.equal(await back.isDisabled(), true); |
| 54 | assert.equal(await forward.isDisabled(), true); |
| 55 | await send({ active: "second", back: false, forward: false }); |
| 56 | assert.equal(await back.isDisabled(), true); |
| 57 | await send({ active: "second", back: true, forward: false }); |
| 58 | await Promise.all([ |
| 59 | page.waitForResponse("**/api/status"), |
| 60 | page.waitForResponse("**/api/terminals"), |
| 61 | ]); |
| 62 | await navigate(back, "back", "second"); |
| 63 | assert.equal(reads(), before, "live history updates must work without REST polling"); |
| 64 | assert.deepEqual(errors, []); |
| 65 | console.log("PASS: back/forward state via events, correct action targets, no stale history on tab switch and no REST polling"); |
| 66 | } finally { |
| 67 | await browser.close(); |
| 68 | } |