|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import type { QuestionRecord } from "@openclaw/gateway-protocol"; |
| 4 | +import { expect, it } from "vitest"; |
| 5 | +import type { GatewaySessionRow, SessionsListResult } from "../api/types.ts"; |
| 6 | +import { |
| 7 | + controlUiSessionUrl, |
| 8 | + installMockGateway, |
| 9 | + type MockGatewayControls, |
| 10 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 11 | +import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts"; |
| 12 | + |
| 13 | +const suite = createControlUiE2eSuite({ |
| 14 | + name: "Control UI Home attention", |
| 15 | + startServerBeforeBrowser: true, |
| 16 | + trackBrowserContexts: true, |
| 17 | + unavailableMessage: (executablePath) => |
| 18 | + `Playwright Chromium is not available at ${executablePath}`, |
| 19 | +}); |
| 20 | + |
| 21 | +const mainSessionKey = "agent:main:main"; |
| 22 | +const captureUiProof = process.env.OPENCLAW_CAPTURE_UI_PROOF === "1"; |
| 23 | +const proofVariant = process.env.OPENCLAW_HOME_ATTENTION_PROOF_VARIANT || "candidate"; |
| 24 | +const proofDir = path.join( |
| 25 | + process.cwd(), |
| 26 | + ".artifacts", |
| 27 | + "control-ui-e2e", |
| 28 | + "home-attention", |
| 29 | + proofVariant, |
| 30 | +); |
| 31 | + |
| 32 | +function sessionsList(row: GatewaySessionRow): SessionsListResult { |
| 33 | + return { |
| 34 | + ts: Date.now(), |
| 35 | + path: "", |
| 36 | + count: 1, |
| 37 | + defaults: { modelProvider: "openai", model: "gpt-5.6-luna", contextTokens: null }, |
| 38 | + sessions: [row], |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +function sessionRow(overrides: Partial<GatewaySessionRow> = {}): GatewaySessionRow { |
| 43 | + return { |
| 44 | + key: mainSessionKey, |
| 45 | + kind: "direct", |
| 46 | + label: "Home", |
| 47 | + displayName: "Quiet control state", |
| 48 | + updatedAt: Date.now(), |
| 49 | + status: "done", |
| 50 | + ...overrides, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +async function publishSessionRow( |
| 55 | + gateway: MockGatewayControls, |
| 56 | + row: GatewaySessionRow, |
| 57 | +): Promise<void> { |
| 58 | + await gateway.setMethodResponse("sessions.list", sessionsList(row)); |
| 59 | + const requestCount = (await gateway.getRequests("sessions.list")).length; |
| 60 | + await gateway.emitGatewayEvent("sessions.changed", { |
| 61 | + key: row.key, |
| 62 | + reason: "home-attention-proof", |
| 63 | + updatedAt: row.updatedAt, |
| 64 | + }); |
| 65 | + await expect |
| 66 | + .poll(async () => (await gateway.getRequests("sessions.list")).length) |
| 67 | + .toBeGreaterThan(requestCount); |
| 68 | +} |
| 69 | + |
| 70 | +async function captureState( |
| 71 | + page: import("playwright").Page, |
| 72 | + home: import("playwright").Locator, |
| 73 | + name: string, |
| 74 | +): Promise<number> { |
| 75 | + await page.evaluate( |
| 76 | + () => |
| 77 | + new Promise<void>((resolve) => { |
| 78 | + requestAnimationFrame(() => requestAnimationFrame(() => resolve())); |
| 79 | + }), |
| 80 | + ); |
| 81 | + if (captureUiProof) { |
| 82 | + await page.screenshot({ |
| 83 | + animations: "disabled", |
| 84 | + fullPage: true, |
| 85 | + path: path.join(proofDir, `${name}.png`), |
| 86 | + }); |
| 87 | + } |
| 88 | + return home.locator("[data-session-attention]").count(); |
| 89 | +} |
| 90 | + |
| 91 | +suite.define(() => { |
| 92 | + it("projects question, failure, and agent-declared attention onto Home", async () => { |
| 93 | + if (captureUiProof) { |
| 94 | + await mkdir(proofDir, { recursive: true }); |
| 95 | + } |
| 96 | + const context = await suite.newBrowserContext({ |
| 97 | + locale: "en-US", |
| 98 | + serviceWorkers: "block", |
| 99 | + viewport: { height: 900, width: 1280 }, |
| 100 | + ...(captureUiProof |
| 101 | + ? { recordVideo: { dir: proofDir, size: { height: 900, width: 1280 } } } |
| 102 | + : {}), |
| 103 | + }); |
| 104 | + const page = await context.newPage(); |
| 105 | + const gateway = await installMockGateway(page, { |
| 106 | + featureMethods: ["question.get", "question.list", "question.resolve"], |
| 107 | + methodResponses: { |
| 108 | + "question.list": { questions: [] }, |
| 109 | + "sessions.list": sessionsList(sessionRow()), |
| 110 | + }, |
| 111 | + sessionKey: mainSessionKey, |
| 112 | + }); |
| 113 | + |
| 114 | + await page.goto(controlUiSessionUrl(suite.server.baseUrl, mainSessionKey)); |
| 115 | + await gateway.waitForRequest("sessions.list"); |
| 116 | + const home = page.locator(".nav-item--home"); |
| 117 | + await home.waitFor(); |
| 118 | + const observed = { |
| 119 | + quiet: await captureState(page, home, "00-non-attention"), |
| 120 | + agent: 0, |
| 121 | + question: 0, |
| 122 | + error: 0, |
| 123 | + }; |
| 124 | + |
| 125 | + await publishSessionRow( |
| 126 | + gateway, |
| 127 | + sessionRow({ |
| 128 | + displayName: "Declared attention state", |
| 129 | + status: "running", |
| 130 | + hasActiveRun: true, |
| 131 | + agentStatus: { |
| 132 | + note: "Blocked: operator input required", |
| 133 | + attention: "flag", |
| 134 | + expiresAt: Date.now() + 60_000, |
| 135 | + }, |
| 136 | + }), |
| 137 | + ); |
| 138 | + observed.agent = await captureState(page, home, "01-agent-declared-attention"); |
| 139 | + |
| 140 | + const now = Date.now(); |
| 141 | + const question = { |
| 142 | + id: "home-attention-question", |
| 143 | + agentId: "main", |
| 144 | + sessionKey: mainSessionKey, |
| 145 | + questions: [ |
| 146 | + { |
| 147 | + questionId: "continue_run", |
| 148 | + header: "Continue", |
| 149 | + question: "Should the run continue?", |
| 150 | + options: [{ label: "Continue", description: "Resume the waiting run." }], |
| 151 | + }, |
| 152 | + ], |
| 153 | + createdAtMs: now, |
| 154 | + expiresAtMs: now + 60_000, |
| 155 | + status: "pending", |
| 156 | + } satisfies QuestionRecord; |
| 157 | + await gateway.emitGatewayEvent("question.requested", question); |
| 158 | + await page.getByText("Should the run continue?", { exact: true }).waitFor(); |
| 159 | + observed.question = await captureState(page, home, "02-question-attention"); |
| 160 | + |
| 161 | + await gateway.emitGatewayEvent("question.resolved", { |
| 162 | + id: question.id, |
| 163 | + status: "cancelled", |
| 164 | + }); |
| 165 | + await expect |
| 166 | + .poll(() => page.getByText("Should the run continue?", { exact: true }).count()) |
| 167 | + .toBe(0); |
| 168 | + await publishSessionRow( |
| 169 | + gateway, |
| 170 | + sessionRow({ |
| 171 | + displayName: "Failed run state", |
| 172 | + endedAt: Date.now(), |
| 173 | + lastRunError: "Release validation failed", |
| 174 | + status: "failed", |
| 175 | + }), |
| 176 | + ); |
| 177 | + observed.error = await captureState(page, home, "03-failed-run-attention"); |
| 178 | + |
| 179 | + expect(observed).toEqual({ quiet: 0, agent: 1, question: 1, error: 1 }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments