|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + installMockGateway, |
| 4 | + type MockGatewayRequest, |
| 5 | + waitForConfirmModal, |
| 6 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 7 | +import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts"; |
| 8 | + |
| 9 | +const suite = createControlUiE2eSuite({ |
| 10 | + name: "Control UI cloud workers settings mocked Gateway E2E", |
| 11 | + startServerBeforeBrowser: true, |
| 12 | + unavailableMessage: (executablePath) => `Playwright Chromium is unavailable at ${executablePath}`, |
| 13 | +}); |
| 14 | + |
| 15 | +function configResponse(config: Record<string, unknown>, hash: string) { |
| 16 | + return { |
| 17 | + appliedConfigHash: hash, |
| 18 | + config, |
| 19 | + sourceConfig: config, |
| 20 | + hash, |
| 21 | + issues: [], |
| 22 | + raw: JSON.stringify(config), |
| 23 | + valid: true, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function requestRaw(request: MockGatewayRequest): Record<string, unknown> { |
| 28 | + if (!request.params || typeof request.params !== "object" || Array.isArray(request.params)) { |
| 29 | + throw new Error("Expected config.patch params"); |
| 30 | + } |
| 31 | + return JSON.parse(String((request.params as Record<string, unknown>).raw)) as Record< |
| 32 | + string, |
| 33 | + unknown |
| 34 | + >; |
| 35 | +} |
| 36 | + |
| 37 | +suite.define(() => { |
| 38 | + it("adds and edits profiles while distinguishing advertised state", async () => { |
| 39 | + const context = await suite.browser.newContext({ |
| 40 | + locale: "en-US", |
| 41 | + serviceWorkers: "block", |
| 42 | + viewport: { height: 1_000, width: 1_440 }, |
| 43 | + }); |
| 44 | + const page = await context.newPage(); |
| 45 | + const gateway = await installMockGateway(page, { |
| 46 | + featureMethods: ["config.patch", "environments.list"], |
| 47 | + methodResponses: { |
| 48 | + "config.get": configResponse({}, "cloud-workers-1"), |
| 49 | + "environments.list": { |
| 50 | + environments: [], |
| 51 | + profiles: [{ id: "build-fleet", providerId: "crabbox" }], |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + try { |
| 57 | + expect((await page.goto(`${suite.server.baseUrl}settings/cloud-workers`))?.status()).toBe( |
| 58 | + 200, |
| 59 | + ); |
| 60 | + await gateway.waitForRequest("environments.list"); |
| 61 | + await page.getByText("No cloud worker profiles are configured.", { exact: true }).waitFor(); |
| 62 | + |
| 63 | + await page.getByRole("button", { name: "Add profile" }).click(); |
| 64 | + await page.getByLabel("Profile ID").fill("build-fleet"); |
| 65 | + await page.getByLabel("Crabbox backend").fill("hetzner"); |
| 66 | + await gateway.deferNext("config.patch"); |
| 67 | + await page.getByRole("button", { name: "Save" }).click(); |
| 68 | + const addPatch = requestRaw(await gateway.waitForRequest("config.patch")); |
| 69 | + expect(addPatch).toEqual({ |
| 70 | + cloudWorkers: { |
| 71 | + profiles: { |
| 72 | + "build-fleet": { |
| 73 | + provider: "crabbox", |
| 74 | + install: "bundle", |
| 75 | + settings: { |
| 76 | + provider: "hetzner", |
| 77 | + class: "standard", |
| 78 | + ttl: "8h", |
| 79 | + idleTimeout: "45m", |
| 80 | + setup: null, |
| 81 | + desktop: null, |
| 82 | + binary: null, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }); |
| 88 | + const buildFleet = { |
| 89 | + provider: "crabbox", |
| 90 | + install: "bundle", |
| 91 | + settings: { |
| 92 | + provider: "hetzner", |
| 93 | + class: "standard", |
| 94 | + ttl: "8h", |
| 95 | + idleTimeout: "45m", |
| 96 | + }, |
| 97 | + }; |
| 98 | + await gateway.resolveDeferred("config.patch", { |
| 99 | + ok: true, |
| 100 | + hash: "cloud-workers-2", |
| 101 | + config: { cloudWorkers: { profiles: { "build-fleet": buildFleet } } }, |
| 102 | + }); |
| 103 | + await page.getByText("Advertised", { exact: true }).waitFor(); |
| 104 | + await page.getByText("Gateway restart required.", { exact: true }).waitFor(); |
| 105 | + |
| 106 | + await page.getByRole("button", { name: "Edit" }).click(); |
| 107 | + await page.getByLabel("Machine class").selectOption("custom"); |
| 108 | + await page.getByLabel("Custom machine class").fill("ccx53"); |
| 109 | + await page.getByLabel("Max lifetime").fill("12h"); |
| 110 | + await page |
| 111 | + .locator(".settings-row") |
| 112 | + .filter({ hasText: "Desktop" }) |
| 113 | + .locator("wa-switch") |
| 114 | + .click(); |
| 115 | + await page.getByLabel("Crabbox binary").fill("/opt/bin/crabbox"); |
| 116 | + await gateway.deferNext("config.patch"); |
| 117 | + await page.getByRole("button", { name: "Save" }).click(); |
| 118 | + const editPatch = requestRaw(await gateway.waitForRequest("config.patch")); |
| 119 | + expect(editPatch).toMatchObject({ |
| 120 | + cloudWorkers: { |
| 121 | + profiles: { |
| 122 | + "build-fleet": { |
| 123 | + provider: "crabbox", |
| 124 | + install: "bundle", |
| 125 | + settings: { |
| 126 | + provider: "hetzner", |
| 127 | + class: "ccx53", |
| 128 | + ttl: "12h", |
| 129 | + idleTimeout: "45m", |
| 130 | + setup: null, |
| 131 | + desktop: true, |
| 132 | + binary: "/opt/bin/crabbox", |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }); |
| 138 | + const editedFleet = { |
| 139 | + provider: "crabbox", |
| 140 | + install: "bundle", |
| 141 | + settings: { |
| 142 | + provider: "hetzner", |
| 143 | + class: "ccx53", |
| 144 | + ttl: "12h", |
| 145 | + idleTimeout: "45m", |
| 146 | + desktop: true, |
| 147 | + binary: "/opt/bin/crabbox", |
| 148 | + }, |
| 149 | + }; |
| 150 | + await gateway.resolveDeferred("config.patch", { |
| 151 | + ok: true, |
| 152 | + hash: "cloud-workers-3", |
| 153 | + config: { cloudWorkers: { profiles: { "build-fleet": editedFleet } } }, |
| 154 | + }); |
| 155 | + await page.getByText(/Class: ccx53/).waitFor(); |
| 156 | + |
| 157 | + await page.getByRole("button", { name: "Add profile" }).click(); |
| 158 | + await page.getByLabel("Profile ID").fill("pending"); |
| 159 | + await page.getByLabel("Crabbox backend").fill("aws"); |
| 160 | + await gateway.deferNext("config.patch"); |
| 161 | + await page.getByRole("button", { name: "Save" }).click(); |
| 162 | + const pendingPatch = requestRaw(await gateway.waitForRequest("config.patch")); |
| 163 | + expect(pendingPatch).toMatchObject({ |
| 164 | + cloudWorkers: { |
| 165 | + profiles: { |
| 166 | + "build-fleet": editedFleet, |
| 167 | + pending: { |
| 168 | + provider: "crabbox", |
| 169 | + install: "bundle", |
| 170 | + settings: { provider: "aws", class: "standard" }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }); |
| 175 | + const pending = { |
| 176 | + provider: "crabbox", |
| 177 | + install: "bundle", |
| 178 | + settings: { |
| 179 | + provider: "aws", |
| 180 | + class: "standard", |
| 181 | + ttl: "8h", |
| 182 | + idleTimeout: "45m", |
| 183 | + }, |
| 184 | + }; |
| 185 | + await gateway.resolveDeferred("config.patch", { |
| 186 | + ok: true, |
| 187 | + hash: "cloud-workers-4", |
| 188 | + config: { |
| 189 | + cloudWorkers: { profiles: { "build-fleet": editedFleet, pending } }, |
| 190 | + }, |
| 191 | + }); |
| 192 | + await page.getByText("Restart required", { exact: true }).waitFor(); |
| 193 | + } finally { |
| 194 | + await context.close(); |
| 195 | + } |
| 196 | + }); |
| 197 | + |
| 198 | + it("deletes a profile only after confirmation", async () => { |
| 199 | + const context = await suite.browser.newContext({ locale: "en-US", serviceWorkers: "block" }); |
| 200 | + const page = await context.newPage(); |
| 201 | + const pending = { |
| 202 | + provider: "crabbox", |
| 203 | + install: "bundle", |
| 204 | + settings: { |
| 205 | + provider: "aws", |
| 206 | + class: "standard", |
| 207 | + ttl: "8h", |
| 208 | + idleTimeout: "45m", |
| 209 | + }, |
| 210 | + }; |
| 211 | + const initialConfig = { cloudWorkers: { profiles: { pending } } }; |
| 212 | + const gateway = await installMockGateway(page, { |
| 213 | + featureMethods: ["config.patch", "environments.list"], |
| 214 | + methodResponses: { |
| 215 | + "config.get": configResponse(initialConfig, "cloud-workers-delete-1"), |
| 216 | + "config.patch": { |
| 217 | + ok: true, |
| 218 | + hash: "cloud-workers-delete-2", |
| 219 | + config: { cloudWorkers: { profiles: {} } }, |
| 220 | + }, |
| 221 | + "environments.list": { environments: [], profiles: [] }, |
| 222 | + }, |
| 223 | + }); |
| 224 | + |
| 225 | + try { |
| 226 | + await page.goto(`${suite.server.baseUrl}settings/cloud-workers`); |
| 227 | + const pendingRow = page.locator(".settings-row").filter({ |
| 228 | + has: page.locator("code", { hasText: /^pending$/ }), |
| 229 | + }); |
| 230 | + await pendingRow.getByRole("button", { name: "Delete" }).click(); |
| 231 | + const confirmation = await waitForConfirmModal(page); |
| 232 | + await expect.poll(() => confirmation.textContent()).toContain("Delete profile pending?"); |
| 233 | + expect(await gateway.getRequests("config.patch")).toHaveLength(0); |
| 234 | + await confirmation.getByRole("button", { name: "Delete", exact: true }).click(); |
| 235 | + await expect.poll(async () => (await gateway.getRequests("config.patch")).length).toBe(1); |
| 236 | + const deleteRequest = (await gateway.getRequests("config.patch"))[0]; |
| 237 | + if (!deleteRequest) { |
| 238 | + throw new Error("Expected delete config.patch request"); |
| 239 | + } |
| 240 | + expect(requestRaw(deleteRequest)).toEqual({ |
| 241 | + cloudWorkers: { profiles: { pending: null } }, |
| 242 | + }); |
| 243 | + await expect.poll(() => pendingRow.count()).toBe(0); |
| 244 | + } finally { |
| 245 | + await context.close(); |
| 246 | + } |
| 247 | + }); |
| 248 | + |
| 249 | + it("keeps profile mutations admin-scoped", async () => { |
| 250 | + const context = await suite.browser.newContext({ locale: "en-US", serviceWorkers: "block" }); |
| 251 | + const page = await context.newPage(); |
| 252 | + await installMockGateway(page, { |
| 253 | + operatorScopes: ["operator.read"], |
| 254 | + featureMethods: ["config.patch", "environments.list"], |
| 255 | + methodResponses: { |
| 256 | + "config.get": configResponse({}, "cloud-workers-read-only"), |
| 257 | + "environments.list": { environments: [], profiles: [] }, |
| 258 | + }, |
| 259 | + }); |
| 260 | + |
| 261 | + try { |
| 262 | + await page.goto(`${suite.server.baseUrl}settings/cloud-workers`); |
| 263 | + await page |
| 264 | + .getByText("Administrator access is required to manage cloud worker profiles.", { |
| 265 | + exact: true, |
| 266 | + }) |
| 267 | + .waitFor(); |
| 268 | + expect(await page.getByRole("button", { name: "Add profile" }).count()).toBe(0); |
| 269 | + } finally { |
| 270 | + await context.close(); |
| 271 | + } |
| 272 | + }); |
| 273 | +}); |
0 commit comments