| 1 | import assert from "node:assert/strict"; |
| 2 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 3 | |
| 4 | const calls = []; |
| 5 | const view = { |
| 6 | mode: "local", desktop_gateway: true, connection_id: "test-local-view", |
| 7 | connection: { state: "connected" }, |
| 8 | }; |
| 9 | const { page, browser, errors } = await createWorkbench({ |
| 10 | clientContext: view, |
| 11 | nativeRequest: async (request) => { calls.push(request); return null; }, |
| 12 | }); |
| 13 | const quit = () => page.evaluate(() => window.cageNativeAction("quit")); |
| 14 | const exits = () => calls.filter(({ action }) => action === "quitNow").length; |
| 15 | const refused = (route) => route.abort("connectionrefused"); |
| 16 | const stops = []; |
| 17 | page.on("request", (request) => { |
| 18 | if (new URL(request.url()).pathname === "/api/stop") stops.push(request); |
| 19 | }); |
| 20 | try { |
| 21 | await page.exposeFunction("testDesktopExits", exits); |
| 22 | |
| 23 | await page.route("**/api/**", refused); |
| 24 | await page.locator(".connection-banner").waitFor(); |
| 25 | await quit(); |
| 26 | const dialog = page.getByRole("dialog", { name: "退出 Cage" }); |
| 27 | await dialog.getByRole("button", { name: "取消" }).click(); |
| 28 | assert.equal(exits(), 0); |
| 29 | await quit(); |
| 30 | await dialog.getByRole("button", { name: "仅退出桌面应用" }).click(); |
| 31 | await page.waitForFunction(async () => (await window.testDesktopExits()) === 1, null, { timeout: 5000 }); |
| 32 | assert.equal(exits(), 1); |
| 33 | |
| 34 | |
| 35 | await page.reload(); |
| 36 | await page.getByRole("alert").getByRole("button", { name: "重新连接" }).waitFor(); |
| 37 | await quit(); |
| 38 | assert.equal(exits(), 2); |
| 39 | |
| 40 | |
| 41 | view.connection = { state: "failed", error_code: "connection_lost" }; |
| 42 | await page.unroute("**/api/**", refused); |
| 43 | await page.reload(); |
| 44 | await page.getByRole("heading", { name: "重新连接本机工作台" }).waitFor(); |
| 45 | await quit(); |
| 46 | assert.equal(exits(), 3); |
| 47 | assert.equal(stops.length, 0); |
| 48 | assert.deepEqual(errors, []); |
| 49 | } finally { |
| 50 | await browser.close(); |
| 51 | } |