| 1 | |
| 2 | |
| 3 | import assert from "node:assert/strict"; |
| 4 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 5 | |
| 6 | const connection = "test-remote-connection"; |
| 7 | const instance = "a".repeat(64); |
| 8 | const nativeCalls = []; |
| 9 | const { browser, page, state, errors } = await createWorkbench({ |
| 10 | clientContext: { mode: "remote", client_only: true, connection_id: connection, instance_id: instance, host_id: "test-host", name: "云端测试环境", client_platform: "darwin", server_platform: "linux", connection: { state: "connected", connection_id: connection, instance_id: instance } }, |
| 11 | nativeRequest: async (body) => { nativeCalls.push(body); return body.action === "accountDevice" ? { supported: false } : true; }, |
| 12 | }); |
| 13 | const requests = []; |
| 14 | page.on("request", (request) => { if (request.url().includes("/api/")) requests.push(request); }); |
| 15 | let directoryRequests = 0; |
| 16 | await page.route("**/api/projects/directories?*", async (route) => { |
| 17 | const request = route.request(); |
| 18 | assert.equal(request.headers()["x-cage-connection"], connection); |
| 19 | directoryRequests++; |
| 20 | const path = new URL(request.url()).searchParams.get("path"); |
| 21 | await route.fulfill({ json: path === "/srv/projects/research" ? { path, parent: "/srv/projects", directories: [], selectable: true } : { path: "/srv/projects", parent: "/srv", directories: [{ name: "research", path: "/srv/projects/research" }], selectable: false } }); |
| 22 | }); |
| 23 | let resourceData = { |
| 24 | runtime: "container", automatic: false, running: true, |
| 25 | configured_memory_bytes: 8 * 1073741824, configured_cpus: 2, |
| 26 | recommended_memory_bytes: 8 * 1073741824, recommended_cpus: 2, |
| 27 | restart_required: false, observation: null, resources_verified: true, pids_limit: 512, resource_error: "", |
| 28 | }; |
| 29 | await page.route("**/api/environment/resources", (route) => route.fulfill({ json: resourceData })); |
| 30 | try { |
| 31 | state.settings.workspace = "/srv/cage/workspace"; |
| 32 | state.settings.keyboard = "native"; |
| 33 | Object.assign(state.status, { keyboard: "native", projects: [state.settings.workspace], environment_directory: state.settings.workspace }); |
| 34 | await page.reload(); |
| 35 | await page.getByRole("heading", { name: "先启动 Cage 环境" }).waitFor(); |
| 36 | await page.getByRole("button", { name: "启动 Cage 环境", exact: true }).click(); |
| 37 | await page.getByRole("heading", { name: "Cage 环境已就绪", exact: true }).waitFor(); |
| 38 | await page.getByRole("button", { name: "添加项目", exact: true }).click(); |
| 39 | await page.getByRole("button", { name: "添加云端项目…", exact: true }).click(); |
| 40 | const chooser = page.getByRole("region", { name: "云端文件夹" }); |
| 41 | await chooser.getByRole("button", { name: "research", exact: true }).click(); |
| 42 | await chooser.getByRole("button", { name: "选择此云端文件夹", exact: true }).click(); |
| 43 | assert.equal(await page.getByLabel("新项目文件夹", { exact: true }).inputValue(), "/srv/projects/research"); |
| 44 | await page.getByRole("dialog").getByRole("button", { name: "打开项目", exact: true }).click(); |
| 45 | await page.getByRole("dialog").waitFor({ state: "hidden" }); |
| 46 | assert.equal(state.settings.workspace, "/srv/projects/research"); |
| 47 | assert.equal(directoryRequests, 2); |
| 48 | await page.getByRole("button", { name: "设置", exact: true }).click(); |
| 49 | await page.getByRole("navigation", { name: "设置分组" }).getByRole("button", { name: "通用", exact: true }).click(); |
| 50 | const macKeyboard = page.getByRole("radio", { name: /Mac 快捷键/ }); |
| 51 | assert.equal(await macKeyboard.isChecked(), true, "Mac client defaults must not follow Linux server keyboard mode"); |
| 52 | const savesBefore = state.requests.filter((item) => item.path === "/settings" && item.method === "POST").length; |
| 53 | await page.getByRole("radio", { name: /原生 Linux 快捷键/ }).check(); |
| 54 | assert.equal(await page.evaluate((instance) => localStorage.getItem(`cage-keyboard:remote:${instance}`), instance), "native"); |
| 55 | assert.equal(state.requests.filter((item) => item.path === "/settings" && item.method === "POST").length, savesBefore, "client keyboard changes must not write cloud settings"); |
| 56 | await macKeyboard.check(); |
| 57 | await page.getByRole("navigation", { name: "设置分组" }).getByRole("button", { name: "环境与恢复", exact: true }).click(); |
| 58 | await page.getByRole("heading", { name: "服务器容器资源上限", exact: true }).waitFor(); |
| 59 | const resources = page.getByRole("region", { name: "计算资源" }); |
| 60 | await resources.getByText("8.0 GiB", { exact: true }).waitFor(); |
| 61 | await resources.getByText("已核验", { exact: true }).waitFor(); |
| 62 | await resources.getByText("512", { exact: true }).waitFor(); |
| 63 | assert.equal(await resources.getByText("等待采样", { exact: true }).count(), 0); |
| 64 | assert.equal(await resources.getByRole("button", { name: "重启并应用自动资源配置", exact: true }).count(), 0); |
| 65 | resourceData = { ...resourceData, configured_memory_bytes: 0, configured_cpus: 0, pids_limit: 0, resources_verified: false, resource_error: "测试:无法核验服务器资源组限制" }; |
| 66 | await resources.getByRole("alert").getByText("测试:无法核验服务器资源组限制", { exact: true }).waitFor(); |
| 67 | assert.equal(await resources.getByText("0.0 GiB", { exact: true }).count(), 0, "failed verification must not render zero as a real resource limit"); |
| 68 | assert.equal(await resources.getByText("8.0 GiB", { exact: true }).count(), 0, "failed verification must not retain a previously verified value"); |
| 69 | assert.equal(await resources.getByText("未核验", { exact: true }).count(), 3); |
| 70 | await page.locator("#settings-back").click(); |
| 71 | assert.ok(nativeCalls.some((item) => item.action === "accountDevice"), "remote account must query native device capability"); |
| 72 | assert.ok(nativeCalls.filter((item) => item.action === "accountDevice").every((item) => item.connection_id === connection), "device operations must bind to the remote generation"); |
| 73 | assert.ok(requests.some((request) => request.url().includes("/api/browser/events?connection=" + connection)), "SSE must bind to the remote generation"); |
| 74 | for (const request of requests.filter((request) => new URL(request.url()).pathname === "/api/projects/open")) assert.equal(request.headers()["x-cage-connection"], connection); |
| 75 | await page.evaluate(() => window.cageNativeAction("quit")); |
| 76 | assert.ok(nativeCalls.some((item) => item.action === "quitNow" && item.connection_id === connection)); |
| 77 | assert.equal(state.requests.some((item) => item.path === "/account/suspend"), false); |
| 78 | assert.equal(state.requests.some((item) => item.path === "/stop"), false); |
| 79 | const downloadID = "de30b5c6-5460-4ebe-bdc6-343cd850ce47"; |
| 80 | let saved = []; |
| 81 | await page.route("**/api/input/files/desktop/requests", (route) => route.fulfill({ json: { uploads: [], downloads: [{ id: downloadID, name: "report.pdf", path: "/srv/cage/Downloads/report.pdf", state: "saved", target: "desktop" }] } })); |
| 82 | await page.route("**/api/browser/files/saved", (route) => route.fulfill({ json: saved })); |
| 83 | await page.reload(); |
| 84 | await page.getByRole("button", { name: "下载文件", exact: true }).click(); |
| 85 | const downloads = page.getByRole("dialog", { name: "下载文件" }); |
| 86 | await downloads.getByText("已保存到云端", { exact: true }).waitFor(); |
| 87 | assert.equal(await downloads.getByRole("button", { name: "打开文件", exact: true }).count(), 0, "cloud path must not offer a local open action"); |
| 88 | saved = [{ id: downloadID, name: "report.pdf", path: "/tmp/cage-ui/report.pdf", instance_id: instance }]; |
| 89 | await page.evaluate(() => window.dispatchEvent(new Event("cage-download-saved"))); |
| 90 | await downloads.getByText("已保存到本机", { exact: true }).waitFor(); |
| 91 | await downloads.getByRole("button", { name: "打开文件", exact: true }).click(); |
| 92 | assert.ok(nativeCalls.some((item) => item.action === "browserDownloadAction" && item.connection_id === connection)); |
| 93 | state.account.unlocked = false; |
| 94 | await page.reload(); |
| 95 | await page.getByText("账户资料保存在当前云端主机", { exact: true }).waitFor(); |
| 96 | assert.ok(nativeCalls.filter((item) => item.action === "accountDevice").every((item) => item.connection_id === connection)); |
| 97 | assert.deepEqual(errors, []); |
| 98 | console.log("PASS: remote directories, client-scoped keyboard, container resource limits, HTTP/SSE/native-account generation binding, local/cloud download distinction, password fallback and quit preserving cloud tasks"); |
| 99 | } finally { await browser.close(); } |