| 1 | import assert from "node:assert/strict"; |
| 2 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 3 | |
| 4 | const { browser, page, errors } = await createWorkbench(); |
| 5 | const windowsRuntime = process.env.CAGE_RESOURCE_RUNTIME === "hyperv"; |
| 6 | let allocation = { automatic: true, memory_gib: 12, cpus: 5 }; |
| 7 | let rejectSave = false; |
| 8 | const saves = []; |
| 9 | await page.route("**/api/environment/resources", async (route) => { |
| 10 | if (route.request().method() === "POST") { |
| 11 | if (rejectSave) return route.fulfill({ status: 400, contentType: "text/plain; charset=utf-8", body: "资源配置保存失败" }); |
| 12 | const body = route.request().postDataJSON(); |
| 13 | saves.push(body); |
| 14 | if (body.allocation) allocation = body.allocation.automatic ? { automatic: true, memory_gib: 12, cpus: 5 } : body.allocation; |
| 15 | return route.fulfill({ json: { ok: true } }); |
| 16 | } |
| 17 | return route.fulfill({ json: { ...allocation, ...(windowsRuntime ? { runtime: "hyperv", min_memory_gib: 4, min_cpus: 2 } : {}), running: false, recommended_memory_bytes: 12 * 2 ** 30, recommended_cpus: 5, max_memory_gib: 12, max_cpus: 5 } }); |
| 18 | }); |
| 19 | async function open() { |
| 20 | await page.getByRole("button", { name: "设置", exact: true }).click(); |
| 21 | await page.getByRole("navigation", { name: "设置分组" }).getByRole("button", { name: "环境与恢复", exact: true }).click(); |
| 22 | await page.getByLabel("内存上限(GiB)", { exact: true }).waitFor(); |
| 23 | } |
| 24 | try { |
| 25 | await open(); |
| 26 | assert.equal(await page.getByRole("button", { name: "保存资源配置", exact: true }).isDisabled(), true); |
| 27 | assert.equal(await page.getByRole("button", { name: "恢复默认", exact: true }).isDisabled(), true); |
| 28 | if (windowsRuntime) { |
| 29 | assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).getAttribute("min"), "4"); |
| 30 | assert.equal(await page.getByLabel("CPU 逻辑核心数", { exact: true }).getAttribute("min"), "2"); |
| 31 | await page.getByLabel("内存上限(GiB)", { exact: true }).fill("3"); |
| 32 | await page.getByRole("button", { name: "保存资源配置", exact: true }).click(); |
| 33 | assert.equal(saves.length, 0); |
| 34 | |
| 35 | } |
| 36 | await page.getByLabel("内存上限(GiB)", { exact: true }).fill("4"); |
| 37 | await page.getByLabel("CPU 逻辑核心数", { exact: true }).fill("2"); |
| 38 | assert.equal(await page.getByText("没有未保存的修改", { exact: true }).count(), 0); |
| 39 | await page.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 40 | const discard = page.getByRole("dialog", { name: "放弃尚未保存的修改?", exact: true }); |
| 41 | await discard.waitFor(); |
| 42 | await discard.getByRole("button", { name: "取消", exact: true }).click(); |
| 43 | assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "4"); |
| 44 | await page.screenshot({ path: "/tmp/cage-resource-settings.png", fullPage: true }); |
| 45 | await page.getByRole("button", { name: "保存资源配置", exact: true }).click(); |
| 46 | await page.getByText("已保存,下次启动环境时生效。", { exact: true }).waitFor(); |
| 47 | assert.deepEqual(allocation, { automatic: false, memory_gib: 4, cpus: 2 }); |
| 48 | await page.reload(); |
| 49 | await open(); |
| 50 | assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "4"); |
| 51 | assert.equal(await page.getByLabel("CPU 逻辑核心数", { exact: true }).inputValue(), "2"); |
| 52 | const count = saves.length; |
| 53 | await page.getByLabel("内存上限(GiB)", { exact: true }).fill("13"); |
| 54 | await page.getByRole("button", { name: "保存资源配置", exact: true }).click(); |
| 55 | assert.equal(saves.length, count); |
| 56 | await page.getByLabel("内存上限(GiB)", { exact: true }).fill("6"); |
| 57 | rejectSave = true; |
| 58 | await page.getByRole("button", { name: "保存资源配置", exact: true }).click(); |
| 59 | await page.getByRole("alert").filter({ hasText: "资源配置保存失败" }).waitFor(); |
| 60 | await page.waitForResponse(response => response.url().endsWith("/api/environment/resources") && response.request().method() === "GET" && response.ok()); |
| 61 | |
| 62 | assert.equal(await page.getByRole("alert").filter({ hasText: "资源配置保存失败" }).isVisible(), true); |
| 63 | assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "6"); |
| 64 | rejectSave = false; |
| 65 | await page.getByRole("button", { name: "恢复默认", exact: true }).click(); |
| 66 | assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "12"); |
| 67 | assert.equal(await page.getByLabel("CPU 逻辑核心数", { exact: true }).inputValue(), "5"); |
| 68 | assert.equal(allocation.automatic, false, "reset stays a draft until saved"); |
| 69 | await page.getByRole("button", { name: "保存资源配置", exact: true }).click(); |
| 70 | await page.getByText("已保存,下次启动环境时生效。", { exact: true }).waitFor(); |
| 71 | assert.equal(allocation.automatic, true); |
| 72 | await page.reload(); |
| 73 | await open(); |
| 74 | assert.equal(await page.getByRole("button", { name: "恢复默认", exact: true }).isDisabled(), true); |
| 75 | assert.equal(await page.getByLabel("内存上限(GiB)", { exact: true }).inputValue(), "12"); |
| 76 | assert.equal(await page.getByRole("alert").filter({ hasText: "资源配置保存失败" }).count(), 0); |
| 77 | assert.deepEqual(errors, []); |
| 78 | console.log("Resource settings: manual save, reload, bounds, failure retention, defaults, editing, reset and persistence passed."); |
| 79 | } finally { await browser.close(); } |