|
| 1 | +// Control UI tests cover browser credential submission and visible recovery. |
| 2 | +import { mkdir, writeFile } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { chromium, type Browser, type BrowserContext, type Page } from "playwright"; |
| 5 | +import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; |
| 6 | +import { ConnectErrorDetailCodes } from "../../../packages/gateway-protocol/src/connect-error-details.js"; |
| 7 | +import { |
| 8 | + canRunPlaywrightChromium, |
| 9 | + installMockGateway, |
| 10 | + resolvePlaywrightChromiumExecutablePath, |
| 11 | + startControlUiE2eServer, |
| 12 | + type ControlUiE2eServer, |
| 13 | + type MockGatewayControls, |
| 14 | + type MockGatewayRequest, |
| 15 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 16 | + |
| 17 | +const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath()); |
| 18 | +const chromiumAvailable = canRunPlaywrightChromium(chromiumExecutablePath); |
| 19 | +const allowMissingChromium = process.env.OPENCLAW_UI_E2E_ALLOW_MISSING_CHROMIUM === "1"; |
| 20 | +const describeControlUiE2e = chromiumAvailable || !allowMissingChromium ? describe : describe.skip; |
| 21 | +const artifactDir = path.resolve( |
| 22 | + process.cwd(), |
| 23 | + process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim() || |
| 24 | + ".artifacts/control-ui-e2e/control-ui-credentials", |
| 25 | +); |
| 26 | +const viewport = { height: 900, width: 1280 }; |
| 27 | + |
| 28 | +let browser: Browser; |
| 29 | +let server: ControlUiE2eServer; |
| 30 | +const openContexts = new Set<BrowserContext>(); |
| 31 | + |
| 32 | +function requireRecord(value: unknown): Record<string, unknown> { |
| 33 | + if (!value || typeof value !== "object" || Array.isArray(value)) { |
| 34 | + throw new Error("Expected object value"); |
| 35 | + } |
| 36 | + return value as Record<string, unknown>; |
| 37 | +} |
| 38 | + |
| 39 | +function readConnectAuth(request: MockGatewayRequest): Record<string, unknown> | undefined { |
| 40 | + const auth = requireRecord(request.params).auth; |
| 41 | + return auth == null ? undefined : requireRecord(auth); |
| 42 | +} |
| 43 | + |
| 44 | +async function createCredentialPage(): Promise<{ |
| 45 | + context: BrowserContext; |
| 46 | + gateway: MockGatewayControls; |
| 47 | + page: Page; |
| 48 | +}> { |
| 49 | + await mkdir(artifactDir, { recursive: true }); |
| 50 | + const context = await browser.newContext({ |
| 51 | + locale: "en-US", |
| 52 | + recordVideo: { dir: artifactDir, size: viewport }, |
| 53 | + serviceWorkers: "block", |
| 54 | + viewport, |
| 55 | + }); |
| 56 | + openContexts.add(context); |
| 57 | + const page = await context.newPage(); |
| 58 | + page.setDefaultTimeout(10_000); |
| 59 | + const gateway = await installMockGateway(page, { deferredMethods: ["connect"] }); |
| 60 | + const response = await page.goto(server.baseUrl); |
| 61 | + expect(response?.status()).toBe(200); |
| 62 | + return { context, gateway, page }; |
| 63 | +} |
| 64 | + |
| 65 | +async function waitForNextConnect( |
| 66 | + gateway: MockGatewayControls, |
| 67 | + previousCount: number, |
| 68 | +): Promise<MockGatewayRequest> { |
| 69 | + await expect |
| 70 | + .poll(async () => (await gateway.getRequests("connect")).length) |
| 71 | + .toBe(previousCount + 1); |
| 72 | + const request = (await gateway.getRequests("connect")).at(-1); |
| 73 | + if (!request) { |
| 74 | + throw new Error("Expected a new Control UI connect request"); |
| 75 | + } |
| 76 | + return request; |
| 77 | +} |
| 78 | + |
| 79 | +async function rejectInitialConnect( |
| 80 | + page: Page, |
| 81 | + gateway: MockGatewayControls, |
| 82 | + code: string, |
| 83 | + message: string, |
| 84 | +): Promise<void> { |
| 85 | + const initialConnect = await gateway.waitForRequest("connect"); |
| 86 | + expect(readConnectAuth(initialConnect)).toBeUndefined(); |
| 87 | + await gateway.rejectDeferred("connect", { |
| 88 | + code: "UNAUTHORIZED", |
| 89 | + details: { code }, |
| 90 | + message, |
| 91 | + }); |
| 92 | + await page.locator("openclaw-login-gate").waitFor(); |
| 93 | +} |
| 94 | + |
| 95 | +describeControlUiE2e("Control UI token and password credentials E2E", () => { |
| 96 | + beforeAll(async () => { |
| 97 | + if (!chromiumAvailable) { |
| 98 | + throw new Error( |
| 99 | + `Playwright Chromium is not installed or cannot start at ${chromiumExecutablePath}.`, |
| 100 | + ); |
| 101 | + } |
| 102 | + server = await startControlUiE2eServer(); |
| 103 | + browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 104 | + }); |
| 105 | + |
| 106 | + afterAll(async () => { |
| 107 | + await Promise.all([...openContexts].map((context) => context.close().catch(() => {}))); |
| 108 | + await browser?.close(); |
| 109 | + await server?.close(); |
| 110 | + }); |
| 111 | + |
| 112 | + afterEach(async () => { |
| 113 | + await Promise.all([...openContexts].map((context) => context.close().catch(() => {}))); |
| 114 | + openContexts.clear(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("submits token and password credentials with visible acceptance and rejection recovery", async () => { |
| 118 | + const tokenFlow = await createCredentialPage(); |
| 119 | + const tokenPageErrors: string[] = []; |
| 120 | + tokenFlow.page.on("pageerror", (error) => tokenPageErrors.push(String(error))); |
| 121 | + await rejectInitialConnect( |
| 122 | + tokenFlow.page, |
| 123 | + tokenFlow.gateway, |
| 124 | + ConnectErrorDetailCodes.AUTH_TOKEN_MISSING, |
| 125 | + "unauthorized: gateway token required", |
| 126 | + ); |
| 127 | + |
| 128 | + const tokenInput = tokenFlow.page.getByLabel(/Gateway token/i); |
| 129 | + const tokenConnectCount = (await tokenFlow.gateway.getRequests("connect")).length; |
| 130 | + await tokenFlow.gateway.deferNext("connect"); |
| 131 | + await tokenInput.fill("token-accepted-by-mock"); |
| 132 | + await tokenFlow.page.getByRole("button", { name: "Connect", exact: true }).click(); |
| 133 | + |
| 134 | + const tokenConnect = await waitForNextConnect(tokenFlow.gateway, tokenConnectCount); |
| 135 | + expect(readConnectAuth(tokenConnect)).toMatchObject({ |
| 136 | + token: "token-accepted-by-mock", |
| 137 | + }); |
| 138 | + expect(readConnectAuth(tokenConnect)?.password).toBeUndefined(); |
| 139 | + await tokenFlow.gateway.resolveDeferred("connect"); |
| 140 | + await tokenFlow.page.locator("openclaw-app-shell").waitFor(); |
| 141 | + expect(await tokenFlow.page.locator("openclaw-login-gate").count()).toBe(0); |
| 142 | + await tokenFlow.page.screenshot({ |
| 143 | + fullPage: true, |
| 144 | + path: path.join(artifactDir, "01-token-connected.png"), |
| 145 | + }); |
| 146 | + expect(tokenPageErrors).toEqual([]); |
| 147 | + await tokenFlow.context.close(); |
| 148 | + openContexts.delete(tokenFlow.context); |
| 149 | + |
| 150 | + const passwordFlow = await createCredentialPage(); |
| 151 | + const passwordPageErrors: string[] = []; |
| 152 | + passwordFlow.page.on("pageerror", (error) => passwordPageErrors.push(String(error))); |
| 153 | + await rejectInitialConnect( |
| 154 | + passwordFlow.page, |
| 155 | + passwordFlow.gateway, |
| 156 | + ConnectErrorDetailCodes.AUTH_PASSWORD_MISSING, |
| 157 | + "unauthorized: gateway password required", |
| 158 | + ); |
| 159 | + |
| 160 | + const passwordInput = passwordFlow.page.getByLabel(/Password \(not stored\)/i); |
| 161 | + const rejectedConnectCount = (await passwordFlow.gateway.getRequests("connect")).length; |
| 162 | + await passwordFlow.gateway.deferNext("connect"); |
| 163 | + await passwordInput.fill("password-rejected-by-mock"); |
| 164 | + await passwordFlow.page.getByRole("button", { name: "Connect", exact: true }).click(); |
| 165 | + |
| 166 | + const rejectedConnect = await waitForNextConnect(passwordFlow.gateway, rejectedConnectCount); |
| 167 | + expect(readConnectAuth(rejectedConnect)).toMatchObject({ |
| 168 | + password: "password-rejected-by-mock", |
| 169 | + }); |
| 170 | + expect(readConnectAuth(rejectedConnect)?.token).toBeUndefined(); |
| 171 | + await passwordFlow.gateway.rejectDeferred("connect", { |
| 172 | + code: "UNAUTHORIZED", |
| 173 | + details: { code: ConnectErrorDetailCodes.AUTH_PASSWORD_MISMATCH }, |
| 174 | + message: "unauthorized: gateway password mismatch", |
| 175 | + }); |
| 176 | + |
| 177 | + const failure = passwordFlow.page.locator(".login-gate__failure"); |
| 178 | + await failure.waitFor(); |
| 179 | + expect(await failure.getAttribute("role")).toBe("alert"); |
| 180 | + expect( |
| 181 | + (await failure.locator(".login-gate__failure-raw").textContent())?.toLowerCase(), |
| 182 | + ).toContain("gateway password mismatch"); |
| 183 | + expect(await failure.locator(".login-gate__failure-steps").isVisible()).toBe(true); |
| 184 | + expect(await passwordFlow.page.locator("openclaw-app-shell").count()).toBe(0); |
| 185 | + await passwordFlow.page.screenshot({ |
| 186 | + fullPage: true, |
| 187 | + path: path.join(artifactDir, "02-password-rejected.png"), |
| 188 | + }); |
| 189 | + |
| 190 | + const recoveredConnectCount = (await passwordFlow.gateway.getRequests("connect")).length; |
| 191 | + await passwordFlow.gateway.deferNext("connect"); |
| 192 | + await passwordInput.fill("password-accepted-by-mock"); |
| 193 | + await passwordFlow.page.getByRole("button", { name: "Connect", exact: true }).click(); |
| 194 | + |
| 195 | + const recoveredConnect = await waitForNextConnect(passwordFlow.gateway, recoveredConnectCount); |
| 196 | + expect(readConnectAuth(recoveredConnect)).toMatchObject({ |
| 197 | + password: "password-accepted-by-mock", |
| 198 | + }); |
| 199 | + expect(readConnectAuth(recoveredConnect)?.token).toBeUndefined(); |
| 200 | + await passwordFlow.gateway.resolveDeferred("connect"); |
| 201 | + await passwordFlow.page.locator("openclaw-app-shell").waitFor(); |
| 202 | + expect(await passwordFlow.page.locator("openclaw-login-gate").count()).toBe(0); |
| 203 | + await passwordFlow.page.screenshot({ |
| 204 | + fullPage: true, |
| 205 | + path: path.join(artifactDir, "03-password-recovered.png"), |
| 206 | + }); |
| 207 | + expect(passwordPageErrors).toEqual([]); |
| 208 | + |
| 209 | + await writeFile( |
| 210 | + path.join(artifactDir, "behavior-summary.json"), |
| 211 | + `${JSON.stringify( |
| 212 | + { |
| 213 | + password: { |
| 214 | + rejectedOutcome: "visible-error", |
| 215 | + recoveredOutcome: "connected", |
| 216 | + submittedInConnectFrame: true, |
| 217 | + }, |
| 218 | + token: { |
| 219 | + acceptedOutcome: "connected", |
| 220 | + submittedInConnectFrame: true, |
| 221 | + }, |
| 222 | + }, |
| 223 | + null, |
| 224 | + 2, |
| 225 | + )}\n`, |
| 226 | + "utf8", |
| 227 | + ); |
| 228 | + }); |
| 229 | +}); |
0 commit comments