| 1 | |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | |
| 5 | let input = ""; |
| 6 | for await (const chunk of process.stdin) input += chunk; |
| 7 | const fixture = JSON.parse(input); |
| 8 | const forward = async route => { |
| 9 | const request = route.request(); |
| 10 | const url = new URL(request.url()); |
| 11 | const response = await fetch(fixture.origin + url.pathname + url.search, { |
| 12 | method: request.method(), |
| 13 | headers: { Origin: fixture.origin, Cookie: fixture.cookie, "Content-Type": "application/json" }, |
| 14 | body: request.postData() || undefined, |
| 15 | }); |
| 16 | return route.fulfill({ status: response.status, contentType: response.headers.get("Content-Type"), body: await response.text() }); |
| 17 | }; |
| 18 | const { page, browser, errors } = await createWorkbench({ |
| 19 | url: process.env.CAGE_UI_URL || fixture.origin, |
| 20 | cliRequest: forward, |
| 21 | beforeNavigate: async (page, state) => { |
| 22 | state.settings.workspace = fixture.workspace; |
| 23 | Object.assign(state.status, { ready: true, vm_running: true, projects: [fixture.workspace], environment_directory: fixture.workspace }); |
| 24 | await page.route("**/api/terminals", forward); |
| 25 | }, |
| 26 | }); |
| 27 | try { |
| 28 | const actions = page.getByRole("group", { name: `${fixture.title} 的操作`, exact: true, includeHidden: true }); |
| 29 | await page.locator(".sidebar-tab").filter({ has: actions }).hover(); |
| 30 | await actions.getByRole("button", { name: "归档", exact: true }).hover(); |
| 31 | await actions.locator(".terminal-action-label").filter({ hasText: /^归档$/ }).click(); |
| 32 | const dialog = page.getByRole("dialog", { name: "归档这段会话?", exact: true }); |
| 33 | await dialog.getByRole("button", { name: "归档会话", exact: true }).click(); |
| 34 | const pending = dialog.getByRole("button", { name: "正在归档…", exact: true }); |
| 35 | await pending.waitFor(); |
| 36 | assert.equal(await pending.isDisabled(), true); |
| 37 | await page.keyboard.press("Enter"); |
| 38 | await page.keyboard.press("Escape"); |
| 39 | assert.equal(await dialog.isVisible(), true); |
| 40 | await fetch(fixture.origin + "/fixture/release-operation", { method: "POST" }); |
| 41 | await dialog.waitFor({ state: "hidden" }); |
| 42 | await actions.waitFor({ state: "hidden" }); |
| 43 | |
| 44 | const reloadedSessions = page.waitForResponse(response => new URL(response.url()).pathname === "/api/cli/sessions"); |
| 45 | await page.reload(); |
| 46 | await page.getByRole("button", { name: "打开浏览器", exact: true }).waitFor(); |
| 47 | await reloadedSessions; |
| 48 | assert.equal(await actions.count(), 0); |
| 49 | assert.deepEqual(errors, []); |
| 50 | console.log(`${process.env.CAGE_TEST_BROWSER}: UI archive waited for the environment operation, submitted once, removed the row, retained history and survived reload`); |
| 51 | } finally { |
| 52 | await fetch(fixture.origin + "/fixture/release-operation", { method: "POST" }); |
| 53 | await browser.close(); |
| 54 | } |