| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdir, writeFile } from "node:fs/promises"; |
| 3 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | |
| 5 | const output = process.env.CAGE_ERROR_QA_OUTPUT || "/tmp/cage-readable-errors"; |
| 6 | await mkdir(output, { recursive: true }); |
| 7 | for (const platform of ["darwin", "windows", "linux"]) { |
| 8 | const fixture = await createWorkbench({ |
| 9 | clientContext: { mode: "local", connection_id: "local", client_platform: platform, server_platform: platform, connection: { state: "disconnected" } }, |
| 10 | }); |
| 11 | const { page, browser, state, errors } = fixture; |
| 12 | try { |
| 13 | state.status.job = { id: "readable-failure", kind: "start", running: false, message: "启动环境失败", error: "下载 Linux 镜像失败:等待下载服务器响应超时。请检查所选节点,再继续安装。", log: "fixture download timeout" }; |
| 14 | await page.reload(); |
| 15 | await page.getByText("启动环境失败", { exact: true }).first().waitFor(); |
| 16 | await page.getByRole("alert").filter({ hasText: "等待下载服务器响应超时" }).first().waitFor(); |
| 17 | await page.getByRole("button", { name: "查看操作详情", exact: true }).first().click(); |
| 18 | await page.getByRole("dialog", { name: "操作详情", exact: true }).getByText("fixture download timeout", { exact: true }).waitFor(); |
| 19 | await page.keyboard.press("Escape"); |
| 20 | await page.screenshot({ path: `${output}/${platform}-startup.png` }); |
| 21 | |
| 22 | await page.route("**/api/environment/resources", route => route.fulfill({ status: 503, contentType: "text/html", body: "<html>upstream failed token=private-test-secret</html>" })); |
| 23 | const failure = await page.evaluate(async () => { |
| 24 | const { api } = await import("/src/api.js"); |
| 25 | try { await api("/environment/resources"); return "unexpected success"; } |
| 26 | catch (error) { return { message: error.message, status: error.status }; } |
| 27 | }); |
| 28 | assert.match(failure.message, /资源配置.*HTTP 503.*尚未就绪/); |
| 29 | assert.equal(failure.status, 503); |
| 30 | assert.ok(!failure.message.includes("private-test-secret") && !failure.message.includes("<html>")); |
| 31 | await page.route("**/api/environment/resources", route => route.fulfill({ status: 200, contentType: "application/json", body: "not-json token=private-test-secret" })); |
| 32 | const malformed = await page.evaluate(async () => { |
| 33 | const { api } = await import("/src/api.js"); |
| 34 | try { await api("/environment/resources"); } catch (error) { return error.message; } |
| 35 | }); |
| 36 | assert.match(malformed, /资源配置.*格式无效/); |
| 37 | assert.ok(!malformed.includes("private-test-secret")); |
| 38 | assert.deepEqual(errors, []); |
| 39 | await writeFile(`${output}/${platform}.json`, JSON.stringify({ platform, failure, malformed }, null, 2)); |
| 40 | console.log(`PASS: ${platform} readable failures`); |
| 41 | } finally { await browser.close(); } |
| 42 | } |