|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import type { Page } from "playwright"; |
| 4 | +import { expect as expectBrowser } from "playwright/test"; |
| 5 | +import { expect, it } from "vitest"; |
| 6 | +import { controlUiSessionUrl, installMockGateway } 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 session owner assignment mocked Gateway E2E", |
| 11 | + startServerBeforeBrowser: true, |
| 12 | +}); |
| 13 | +const sessionKey = "agent:main:owner-outcome"; |
| 14 | +const proofPhase = process.env.OPENCLAW_OWNER_ASSIGNMENT_PROOF_PHASE; |
| 15 | +const proofDir = path.resolve(".artifacts/control-ui-e2e/session-owner-assignment"); |
| 16 | + |
| 17 | +function sessionsListResponse() { |
| 18 | + return { |
| 19 | + count: 2, |
| 20 | + owners: [ |
| 21 | + { type: "human" as const, id: "profile-ada", label: "Ada" }, |
| 22 | + { type: "human" as const, id: "profile-bob", label: "Bob" }, |
| 23 | + ], |
| 24 | + defaults: { contextTokens: null, model: null, modelProvider: null }, |
| 25 | + path: "", |
| 26 | + sessions: [ |
| 27 | + { |
| 28 | + key: "agent:main:ada-research", |
| 29 | + kind: "direct", |
| 30 | + label: "Ada research", |
| 31 | + createdActor: { type: "human", id: "profile-ada", label: "Ada" }, |
| 32 | + owner: { actor: { type: "human", id: "profile-ada", label: "Ada" } }, |
| 33 | + updatedAt: 2, |
| 34 | + }, |
| 35 | + { |
| 36 | + key: sessionKey, |
| 37 | + kind: "direct", |
| 38 | + label: "Owner outcome", |
| 39 | + createdActor: { type: "human", id: "profile-bob", label: "Bob" }, |
| 40 | + owner: { actor: { type: "human", id: "profile-bob", label: "Bob" } }, |
| 41 | + updatedAt: 1, |
| 42 | + }, |
| 43 | + ], |
| 44 | + ts: 1, |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +async function installOwnerGateway(page: Page) { |
| 49 | + const gateway = await installMockGateway(page, { |
| 50 | + featureMethods: ["chat.startup", "sessions.assignOwner"], |
| 51 | + historyMessages: [{ role: "assistant", content: "Owner assignment outcome proof." }], |
| 52 | + methodResponses: { "sessions.list": sessionsListResponse() }, |
| 53 | + operatorScopes: ["operator.read", "operator.write"], |
| 54 | + presenceUsers: [{ self: true, id: "profile-ada", name: "Ada" }], |
| 55 | + sessionKey, |
| 56 | + }); |
| 57 | + await page.goto(controlUiSessionUrl(suite.server.baseUrl, sessionKey)); |
| 58 | + await page.getByText("Owner assignment outcome proof.", { exact: true }).waitFor(); |
| 59 | + await gateway.deferNext("sessions.assignOwner"); |
| 60 | + return gateway; |
| 61 | +} |
| 62 | + |
| 63 | +async function expectAssignmentRequest( |
| 64 | + gateway: Awaited<ReturnType<typeof installOwnerGateway>>, |
| 65 | +): Promise<void> { |
| 66 | + const request = await gateway.waitForRequest("sessions.assignOwner"); |
| 67 | + expect(request.params).toEqual({ |
| 68 | + agentId: "main", |
| 69 | + key: sessionKey, |
| 70 | + owner: { type: "human", id: "profile-ada" }, |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +async function captureProof(page: Page, surface: string): Promise<void> { |
| 75 | + if (!proofPhase) { |
| 76 | + return; |
| 77 | + } |
| 78 | + await mkdir(proofDir, { recursive: true }); |
| 79 | + await page.screenshot({ |
| 80 | + animations: "disabled", |
| 81 | + fullPage: true, |
| 82 | + path: path.join(proofDir, `${surface}-${proofPhase}.png`), |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +async function chooseAssignToMe(page: Page): Promise<void> { |
| 87 | + const action = page.locator('wa-dropdown-item[value="assign-owner:human:profile-ada"]:visible'); |
| 88 | + await action.waitFor({ state: "visible" }); |
| 89 | + await action.click(); |
| 90 | +} |
| 91 | + |
| 92 | +suite.define(() => { |
| 93 | + it("keeps a rejected header owner assignment visible", async () => { |
| 94 | + await suite.withPage( |
| 95 | + { |
| 96 | + colorScheme: "dark", |
| 97 | + locale: "en-US", |
| 98 | + serviceWorkers: "block", |
| 99 | + viewport: { height: 900, width: 1280 }, |
| 100 | + }, |
| 101 | + async ({ page }) => { |
| 102 | + const gateway = await installOwnerGateway(page); |
| 103 | + const activePane = page.locator("openclaw-chat-pane.chat-pane-cache__pane--active"); |
| 104 | + const menuTrigger = activePane.getByRole("button", { name: "Actions for Owner outcome" }); |
| 105 | + await menuTrigger.press("Enter"); |
| 106 | + await chooseAssignToMe(page); |
| 107 | + await expectAssignmentRequest(gateway); |
| 108 | + |
| 109 | + const message = "Owner assignment rejected for visible outcome proof."; |
| 110 | + await gateway.rejectDeferred("sessions.assignOwner", { |
| 111 | + code: "INVALID_REQUEST", |
| 112 | + message, |
| 113 | + }); |
| 114 | + await captureProof(page, "header"); |
| 115 | + |
| 116 | + await expectBrowser( |
| 117 | + activePane.getByRole("alert").filter({ hasText: message }), |
| 118 | + ).toBeVisible(); |
| 119 | + await expectBrowser( |
| 120 | + activePane.getByRole("img", { name: "Created by Bob", exact: true }), |
| 121 | + ).toHaveCount(1); |
| 122 | + }, |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it("keeps a rejected sidebar owner assignment visible", async () => { |
| 127 | + await suite.withPage( |
| 128 | + { |
| 129 | + locale: "en-US", |
| 130 | + serviceWorkers: "block", |
| 131 | + viewport: { height: 900, width: 1280 }, |
| 132 | + }, |
| 133 | + async ({ page }) => { |
| 134 | + const gateway = await installOwnerGateway(page); |
| 135 | + const row = page.locator(`[data-session-key="${sessionKey}"]`); |
| 136 | + await row.hover(); |
| 137 | + await row |
| 138 | + .getByRole("button", { name: "Open session menu: Owner outcome", exact: true }) |
| 139 | + .click(); |
| 140 | + await chooseAssignToMe(page); |
| 141 | + await expectAssignmentRequest(gateway); |
| 142 | + |
| 143 | + const message = "Sidebar owner assignment rejected for visible outcome proof."; |
| 144 | + await gateway.rejectDeferred("sessions.assignOwner", { |
| 145 | + code: "INVALID_REQUEST", |
| 146 | + message, |
| 147 | + }); |
| 148 | + |
| 149 | + await expectBrowser(page.getByRole("alert").filter({ hasText: message })).toBeVisible(); |
| 150 | + await expectBrowser( |
| 151 | + row.getByRole("img", { name: "Created by Bob", exact: true }), |
| 152 | + ).toHaveCount(1); |
| 153 | + }, |
| 154 | + ); |
| 155 | + }); |
| 156 | +}); |
0 commit comments