|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { chromium, type Browser, type BrowserContext, type Page } from "playwright"; |
| 4 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 5 | +import type { AuditRunInspectResult } from "../../../packages/gateway-protocol/src/schema/audit-run.js"; |
| 6 | +import { |
| 7 | + canRunPlaywrightChromium, |
| 8 | + installMockGateway, |
| 9 | + resolvePlaywrightChromiumExecutablePath, |
| 10 | + startControlUiE2eServer, |
| 11 | + type ControlUiE2eServer, |
| 12 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 13 | + |
| 14 | +const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath()); |
| 15 | +const chromiumAvailable = canRunPlaywrightChromium(chromiumExecutablePath); |
| 16 | +const allowMissingChromium = process.env.OPENCLAW_UI_E2E_ALLOW_MISSING_CHROMIUM === "1"; |
| 17 | +const describeControlUiE2e = chromiumAvailable || !allowMissingChromium ? describe : describe.skip; |
| 18 | +const captureUiProof = process.env.OPENCLAW_CAPTURE_UI_PROOF === "1"; |
| 19 | +const proofDir = path.resolve(".artifacts/control-ui-e2e/activity-run-inspector"); |
| 20 | +const hmacRef = `hmac-sha256:v1:${"a".repeat(32)}:${"b".repeat(64)}`; |
| 21 | + |
| 22 | +let browser: Browser; |
| 23 | +let server: ControlUiE2eServer; |
| 24 | + |
| 25 | +function presentResult(runId: string): AuditRunInspectResult { |
| 26 | + return { |
| 27 | + schemaVersion: 1, |
| 28 | + run: { runId, executionId: "execution-safe-ref", status: "known" }, |
| 29 | + identity: { |
| 30 | + state: "present", |
| 31 | + context: { |
| 32 | + schemaVersion: 1, |
| 33 | + contextId: "context-safe-ref", |
| 34 | + executionId: "execution-safe-ref", |
| 35 | + runId, |
| 36 | + createdAt: 1_786_000_000_000, |
| 37 | + trustDomain: { kind: "gateway-cell", domainRef: hmacRef, state: "present" }, |
| 38 | + invoker: { state: "absent" }, |
| 39 | + ingress: { |
| 40 | + kind: "gateway-client", |
| 41 | + boundary: "agent-command.gateway", |
| 42 | + sourceRef: hmacRef, |
| 43 | + state: "present", |
| 44 | + }, |
| 45 | + agentPrincipal: { |
| 46 | + kind: "agent", |
| 47 | + domainRef: hmacRef, |
| 48 | + principalRef: "main", |
| 49 | + displayLabel: "Primary agent", |
| 50 | + }, |
| 51 | + agentDefinition: { definitionRef: "main", state: "unknown" }, |
| 52 | + runtimeInstance: { runtimeRef: hmacRef, kind: "gateway", state: "unsupported" }, |
| 53 | + representedSubject: { |
| 54 | + principal: { kind: "person", domainRef: hmacRef, principalRef: hmacRef }, |
| 55 | + state: "unknown", |
| 56 | + }, |
| 57 | + sponsor: { |
| 58 | + principal: { kind: "service", domainRef: hmacRef, principalRef: hmacRef }, |
| 59 | + state: "unsupported", |
| 60 | + }, |
| 61 | + applicableGrants: [{ grantRef: hmacRef, state: "absent" }], |
| 62 | + assurance: [ |
| 63 | + { kind: "runtime-binding", evidenceRef: hmacRef, strength: "boundary-verified" }, |
| 64 | + ], |
| 65 | + lineage: { parentRunId: "parent-safe-ref", depth: 1 }, |
| 66 | + coverageState: "unattributed", |
| 67 | + missingEvidence: ["invoker.principal"], |
| 68 | + }, |
| 69 | + }, |
| 70 | + decisions: [ |
| 71 | + { |
| 72 | + schemaVersion: 1, |
| 73 | + receiptId: "receipt-safe-ref", |
| 74 | + contextId: "context-safe-ref", |
| 75 | + executionId: "execution-safe-ref", |
| 76 | + runId, |
| 77 | + occurredAt: 1_786_000_000_000, |
| 78 | + action: { |
| 79 | + family: "run", |
| 80 | + operation: "admission", |
| 81 | + summary: "Run admission was recorded without identity-aware evaluation.", |
| 82 | + }, |
| 83 | + decision: { |
| 84 | + outcome: "not-applicable", |
| 85 | + reasonCode: "run_admission_identity_not_evaluated", |
| 86 | + }, |
| 87 | + enforcement: { |
| 88 | + coverageState: "unattributed", |
| 89 | + policyRefs: [], |
| 90 | + grantRefs: [], |
| 91 | + contextFieldsUsed: [], |
| 92 | + }, |
| 93 | + source: { |
| 94 | + owner: "agent-command", |
| 95 | + recordRef: "context-safe-ref", |
| 96 | + decisionBoundary: "agent-command.run-admission", |
| 97 | + }, |
| 98 | + missingEvidence: ["invoker.principal"], |
| 99 | + remediation: [ |
| 100 | + { |
| 101 | + code: "no_identity_enforcement_claimed", |
| 102 | + text: "Treat this receipt as attribution only; it does not prove authorization.", |
| 103 | + }, |
| 104 | + ], |
| 105 | + }, |
| 106 | + ], |
| 107 | + coverage: { state: "unattributed", missingEvidence: ["invoker.principal"] }, |
| 108 | + nextDecisionCursor: "1", |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +function unavailableResult(params: { |
| 113 | + state: "unknown" | "unsupported"; |
| 114 | + reasonCode: string; |
| 115 | + remediation?: Array<{ code: string; text: string }>; |
| 116 | +}): AuditRunInspectResult { |
| 117 | + return { |
| 118 | + schemaVersion: 1, |
| 119 | + run: { runId: "typed-state", status: params.state === "unknown" ? "unknown" : "known" }, |
| 120 | + identity: { |
| 121 | + state: params.state, |
| 122 | + reasonCode: params.reasonCode, |
| 123 | + missingEvidence: ["identity.context"], |
| 124 | + remediation: params.remediation ?? [], |
| 125 | + }, |
| 126 | + decisions: [], |
| 127 | + coverage: { state: params.state, missingEvidence: ["identity.context"] }, |
| 128 | + }; |
| 129 | +} |
| 130 | + |
| 131 | +async function newContext(options: { video?: boolean } = {}): Promise<BrowserContext> { |
| 132 | + if (captureUiProof) { |
| 133 | + await mkdir(path.join(proofDir, "video"), { recursive: true }); |
| 134 | + } |
| 135 | + return browser.newContext({ |
| 136 | + locale: "en-US", |
| 137 | + serviceWorkers: "block", |
| 138 | + viewport: { height: 1000, width: 1440 }, |
| 139 | + ...(captureUiProof && options.video |
| 140 | + ? { recordVideo: { dir: path.join(proofDir, "video"), size: { height: 1000, width: 1440 } } } |
| 141 | + : {}), |
| 142 | + }); |
| 143 | +} |
| 144 | + |
| 145 | +async function screenshot(page: Page, name: string) { |
| 146 | + if (!captureUiProof) { |
| 147 | + return; |
| 148 | + } |
| 149 | + await mkdir(proofDir, { recursive: true }); |
| 150 | + await page.screenshot({ |
| 151 | + animations: "disabled", |
| 152 | + fullPage: true, |
| 153 | + path: path.join(proofDir, name), |
| 154 | + }); |
| 155 | +} |
| 156 | + |
| 157 | +describeControlUiE2e("Control UI durable Activity run inspector", () => { |
| 158 | + beforeAll(async () => { |
| 159 | + if (!chromiumAvailable) { |
| 160 | + throw new Error(`Playwright Chromium is unavailable at ${chromiumExecutablePath}`); |
| 161 | + } |
| 162 | + server = await startControlUiE2eServer(); |
| 163 | + browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 164 | + }); |
| 165 | + |
| 166 | + afterAll(async () => { |
| 167 | + await browser?.close(); |
| 168 | + await server?.close(); |
| 169 | + }); |
| 170 | + |
| 171 | + it("deep-links, reloads durable evidence, and exposes text-first accessible states", async () => { |
| 172 | + const context = await newContext({ video: true }); |
| 173 | + const page = await context.newPage(); |
| 174 | + const runId = "run:durable/one"; |
| 175 | + const gateway = await installMockGateway(page, { |
| 176 | + featureMethods: ["audit.run.inspect"], |
| 177 | + methodResponses: { "audit.run.inspect": presentResult(runId) }, |
| 178 | + }); |
| 179 | + |
| 180 | + try { |
| 181 | + await page.goto(`${server.baseUrl}activity?view=run&run=${encodeURIComponent(runId)}`); |
| 182 | + await page.getByRole("heading", { name: "Identity and authority" }).waitFor(); |
| 183 | + expect(await gateway.waitForRequest("audit.run.inspect")).toMatchObject({ |
| 184 | + params: { runId, decisionLimit: 50, executionLimit: 50 }, |
| 185 | + }); |
| 186 | + expect((await gateway.getRequests("audit.run.inspect")).length).toBe(1); |
| 187 | + |
| 188 | + const runTab = page.getByRole("tab", { name: "Run inspector" }); |
| 189 | + await expect.poll(() => runTab.getAttribute("aria-selected")).toBe("true"); |
| 190 | + await page.getByRole("status", { name: "Inspection coverage: Unattributed" }).waitFor(); |
| 191 | + for (const state of ["Present", "Absent", "Unknown", "Unsupported"]) { |
| 192 | + await page.locator(`[aria-label="Evidence state: ${state}"]`).first().waitFor(); |
| 193 | + } |
| 194 | + for (const dimension of [ |
| 195 | + "Trust domain", |
| 196 | + "Ingress", |
| 197 | + "Invoker", |
| 198 | + "Represented subject", |
| 199 | + "Sponsor", |
| 200 | + "Agent principal", |
| 201 | + "Runtime instance", |
| 202 | + "Applicable grant 1", |
| 203 | + "Assurance evidence 1", |
| 204 | + "Lineage", |
| 205 | + ]) { |
| 206 | + await page.getByText(dimension, { exact: true }).waitFor(); |
| 207 | + } |
| 208 | + await page.getByText("Best-effort audit warning", { exact: false }).waitFor(); |
| 209 | + await page |
| 210 | + .getByText("Additional decision receipts are available", { exact: false }) |
| 211 | + .waitFor(); |
| 212 | + expect(await page.getByText("receipt-safe-ref", { exact: false }).count()).toBe(0); |
| 213 | + expect(await page.getByText("context-safe-ref", { exact: false }).count()).toBe(0); |
| 214 | + expect(await page.getByText("execution-safe-ref", { exact: false }).count()).toBe(0); |
| 215 | + expect(await page.getByText("raw-sender-id-42", { exact: false }).count()).toBe(0); |
| 216 | + await screenshot(page, "01-present-unattributed.png"); |
| 217 | + |
| 218 | + await page.reload(); |
| 219 | + await page.getByRole("heading", { name: "Identity and authority" }).waitFor(); |
| 220 | + expect(await gateway.waitForRequest("audit.run.inspect")).toMatchObject({ |
| 221 | + params: { runId, decisionLimit: 50, executionLimit: 50 }, |
| 222 | + }); |
| 223 | + expect((await gateway.getRequests("audit.run.inspect")).length).toBe(1); |
| 224 | + |
| 225 | + const liveTab = page.getByRole("tab", { name: "Live activity" }); |
| 226 | + await liveTab.focus(); |
| 227 | + await expect |
| 228 | + .poll(() => page.evaluate(() => document.activeElement?.textContent?.trim())) |
| 229 | + .toBe("Live activity"); |
| 230 | + await page.keyboard.press("Enter"); |
| 231 | + await page.getByText("No activity yet.", { exact: true }).waitFor(); |
| 232 | + expect(new URL(page.url()).search).toBe(""); |
| 233 | + await page.goBack(); |
| 234 | + await page.getByRole("heading", { name: "Identity and authority" }).waitFor(); |
| 235 | + } finally { |
| 236 | + await context.close(); |
| 237 | + } |
| 238 | + }); |
| 239 | + |
| 240 | + it("renders empty, typed unavailable, corrupt, and expired results without guessing", async () => { |
| 241 | + const context = await newContext(); |
| 242 | + const page = await context.newPage(); |
| 243 | + const gateway = await installMockGateway(page, { |
| 244 | + featureMethods: ["audit.run.inspect"], |
| 245 | + methodResponses: { |
| 246 | + "audit.run.inspect": { |
| 247 | + cases: [ |
| 248 | + { |
| 249 | + match: { runId: "missing" }, |
| 250 | + response: unavailableResult({ state: "unknown", reasonCode: "run_not_found" }), |
| 251 | + }, |
| 252 | + { |
| 253 | + match: { runId: "expired" }, |
| 254 | + response: unavailableResult({ |
| 255 | + state: "unsupported", |
| 256 | + reasonCode: "identity_context_unavailable", |
| 257 | + remediation: [ |
| 258 | + { |
| 259 | + code: "run_again_after_expiry", |
| 260 | + text: "This run is outside the 30-day retention window.", |
| 261 | + }, |
| 262 | + ], |
| 263 | + }), |
| 264 | + }, |
| 265 | + { |
| 266 | + match: { runId: "corrupt" }, |
| 267 | + response: unavailableResult({ |
| 268 | + state: "unknown", |
| 269 | + reasonCode: "identity_context_corrupt", |
| 270 | + }), |
| 271 | + }, |
| 272 | + ], |
| 273 | + }, |
| 274 | + }, |
| 275 | + }); |
| 276 | + |
| 277 | + try { |
| 278 | + await page.goto(`${server.baseUrl}activity?view=run`); |
| 279 | + await page.getByRole("heading", { name: "No run selected" }).waitFor(); |
| 280 | + expect((await gateway.getRequests("audit.run.inspect")).length).toBe(0); |
| 281 | + await screenshot(page, "02-empty.png"); |
| 282 | + |
| 283 | + for (const [runId, heading, screenshotName] of [ |
| 284 | + ["missing", "Run not found", "03-not-found.png"], |
| 285 | + ["expired", "Identity evidence expired", "04-expired.png"], |
| 286 | + ["corrupt", "Identity evidence is corrupt", "05-corrupt.png"], |
| 287 | + ] as const) { |
| 288 | + await page.goto(`${server.baseUrl}activity?view=run&run=${runId}`); |
| 289 | + await page.getByRole("heading", { name: heading }).waitFor(); |
| 290 | + await screenshot(page, screenshotName); |
| 291 | + } |
| 292 | + } finally { |
| 293 | + await context.close(); |
| 294 | + } |
| 295 | + }); |
| 296 | + |
| 297 | + it("shows loading, request failure, retry, and disconnected states", async () => { |
| 298 | + const context = await newContext(); |
| 299 | + const page = await context.newPage(); |
| 300 | + const runId = "retry-run"; |
| 301 | + const gateway = await installMockGateway(page, { |
| 302 | + featureMethods: ["audit.run.inspect"], |
| 303 | + deferredMethods: ["audit.run.inspect"], |
| 304 | + methodResponses: { "audit.run.inspect": presentResult(runId) }, |
| 305 | + }); |
| 306 | + |
| 307 | + try { |
| 308 | + await page.goto(`${server.baseUrl}activity?view=run&run=${runId}`); |
| 309 | + await page.getByRole("heading", { name: "Loading run inspection" }).waitFor(); |
| 310 | + await screenshot(page, "06-loading.png"); |
| 311 | + await gateway.rejectDeferred("audit.run.inspect", { |
| 312 | + code: "UNAVAILABLE", |
| 313 | + message: "temporarily unavailable", |
| 314 | + retryable: true, |
| 315 | + }); |
| 316 | + await page.getByRole("heading", { name: "Run inspection failed" }).waitFor(); |
| 317 | + await screenshot(page, "07-error.png"); |
| 318 | + await page.getByRole("button", { name: "Retry inspection" }).click(); |
| 319 | + await page.getByRole("heading", { name: "Identity and authority" }).waitFor(); |
| 320 | + |
| 321 | + await gateway.closeLatest(1006, "network unavailable"); |
| 322 | + await page.getByRole("heading", { name: "Gateway disconnected" }).waitFor(); |
| 323 | + await screenshot(page, "08-disconnected.png"); |
| 324 | + } finally { |
| 325 | + await context.close(); |
| 326 | + } |
| 327 | + }); |
| 328 | + |
| 329 | + it("distinguishes missing access from an older unsupported Gateway", async () => { |
| 330 | + for (const scenario of [ |
| 331 | + { |
| 332 | + name: "unauthorized", |
| 333 | + featureMethods: ["audit.run.inspect"], |
| 334 | + operatorScopes: ["operator.approvals"], |
| 335 | + heading: "Operator read access required", |
| 336 | + screenshotName: "09-unauthorized.png", |
| 337 | + }, |
| 338 | + { |
| 339 | + name: "unsupported", |
| 340 | + featureMethods: ["chat.startup"], |
| 341 | + operatorScopes: ["operator.read"], |
| 342 | + heading: "Run inspection unsupported", |
| 343 | + screenshotName: "10-unsupported-gateway.png", |
| 344 | + }, |
| 345 | + ] as const) { |
| 346 | + const context = await newContext(); |
| 347 | + const page = await context.newPage(); |
| 348 | + const gateway = await installMockGateway(page, { |
| 349 | + featureMethods: [...scenario.featureMethods], |
| 350 | + operatorScopes: [...scenario.operatorScopes], |
| 351 | + }); |
| 352 | + try { |
| 353 | + await page.goto(`${server.baseUrl}activity?view=run&run=${scenario.name}`); |
| 354 | + await page.getByRole("heading", { name: scenario.heading }).waitFor(); |
| 355 | + expect((await gateway.getRequests("audit.run.inspect")).length).toBe(0); |
| 356 | + await screenshot(page, scenario.screenshotName); |
| 357 | + } finally { |
| 358 | + await context.close(); |
| 359 | + } |
| 360 | + } |
| 361 | + }); |
| 362 | +}); |
0 commit comments