|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import type { Locator, Page } from "playwright"; |
| 4 | +import { expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + installMockGateway, |
| 7 | + navigateToControlUiSession, |
| 8 | + type MockGatewayControls, |
| 9 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 10 | +import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts"; |
| 11 | + |
| 12 | +const suite = createControlUiE2eSuite({ |
| 13 | + name: "session companion clear", |
| 14 | + startServerBeforeBrowser: true, |
| 15 | +}); |
| 16 | + |
| 17 | +const artifactDir = process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim(); |
| 18 | +const answer = "Keep this companion answer visible until the reset succeeds."; |
| 19 | +const initiatingSessionKey = "agent:main:companion-clear"; |
| 20 | +const nextSessionKey = "agent:main:companion-next"; |
| 21 | +const resetError = "Companion reset unavailable during reconnect"; |
| 22 | + |
| 23 | +type CompanionSurface = { |
| 24 | + companion: Locator; |
| 25 | + gateway: MockGatewayControls; |
| 26 | + menu: Locator; |
| 27 | + page: Page; |
| 28 | +}; |
| 29 | + |
| 30 | +async function withCompanion(run: (surface: CompanionSurface) => Promise<void>): Promise<void> { |
| 31 | + if (artifactDir) { |
| 32 | + await mkdir(artifactDir, { recursive: true }); |
| 33 | + } |
| 34 | + await suite.withPage( |
| 35 | + { |
| 36 | + locale: "en-US", |
| 37 | + serviceWorkers: "block", |
| 38 | + viewport: { height: 800, width: 1200 }, |
| 39 | + ...(artifactDir |
| 40 | + ? { recordVideo: { dir: artifactDir, size: { height: 800, width: 1200 } } } |
| 41 | + : {}), |
| 42 | + }, |
| 43 | + async ({ page }) => { |
| 44 | + const gateway = await installMockGateway(page, { |
| 45 | + methodResponses: { |
| 46 | + "sessions.companion.reset": { |
| 47 | + __mockError: { code: "UNAVAILABLE", message: resetError }, |
| 48 | + }, |
| 49 | + "sessions.companion.state": { |
| 50 | + cases: [ |
| 51 | + { |
| 52 | + match: { sessionKey: initiatingSessionKey }, |
| 53 | + response: { |
| 54 | + exchanges: [{ question: "What changed?", answer, ts: Date.now() - 1_000 }], |
| 55 | + }, |
| 56 | + }, |
| 57 | + { match: { sessionKey: nextSessionKey }, response: { exchanges: [] } }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + "sessions.list": { |
| 61 | + count: 2, |
| 62 | + defaults: { contextTokens: null, model: "gpt-5.5", modelProvider: "openai" }, |
| 63 | + path: "", |
| 64 | + sessions: [ |
| 65 | + { key: initiatingSessionKey, kind: "direct", label: "Original", updatedAt: 2 }, |
| 66 | + { key: nextSessionKey, kind: "direct", label: "Next", updatedAt: 1 }, |
| 67 | + ], |
| 68 | + ts: Date.now(), |
| 69 | + }, |
| 70 | + }, |
| 71 | + sessionKey: initiatingSessionKey, |
| 72 | + }); |
| 73 | + |
| 74 | + await page.goto(`${suite.server.baseUrl}chat`); |
| 75 | + const stateRequest = await gateway.waitForRequest("sessions.companion.state"); |
| 76 | + expect(stateRequest.params).toEqual({ |
| 77 | + agentId: "main", |
| 78 | + sessionKey: initiatingSessionKey, |
| 79 | + }); |
| 80 | + const companion = page.locator("openclaw-chat-session-rail"); |
| 81 | + await companion.locator(".chat-session-rail__expand").click(); |
| 82 | + await companion.getByText(answer, { exact: true }).waitFor(); |
| 83 | + const menu = companion.locator("wa-dropdown.chat-session-rail__menu"); |
| 84 | + await run({ companion, gateway, menu, page }); |
| 85 | + }, |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +async function clearCompanion(menu: Locator): Promise<void> { |
| 90 | + await menu.getByRole("button", { name: "More companion actions" }).click(); |
| 91 | + await menu.locator('wa-dropdown-item[value="clear"]').click(); |
| 92 | +} |
| 93 | + |
| 94 | +suite.define(() => { |
| 95 | + it("shows a reset failure without clearing the thread, then clears after a successful retry", async () => { |
| 96 | + await withCompanion(async ({ companion, gateway, menu, page }) => { |
| 97 | + await clearCompanion(menu); |
| 98 | + await gateway.waitForRequest("sessions.companion.reset"); |
| 99 | + |
| 100 | + const alert = page.getByRole("alert").filter({ hasText: resetError }); |
| 101 | + await alert.waitFor({ state: "visible" }); |
| 102 | + await companion.getByText(answer, { exact: true }).waitFor(); |
| 103 | + if (artifactDir) { |
| 104 | + await page.screenshot({ |
| 105 | + fullPage: true, |
| 106 | + path: path.join(artifactDir, "reset-failure.png"), |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + await alert.getByRole("button", { name: "Dismiss error" }).click(); |
| 111 | + await gateway.setMethodResponse("sessions.companion.reset", { ok: true }); |
| 112 | + await clearCompanion(menu); |
| 113 | + |
| 114 | + await expect |
| 115 | + .poll(async () => (await gateway.getRequests("sessions.companion.reset")).length) |
| 116 | + .toBe(2); |
| 117 | + await expect.poll(() => companion.getByText(answer, { exact: true }).count()).toBe(0); |
| 118 | + expect(await page.getByRole("alert").count()).toBe(0); |
| 119 | + if (artifactDir) { |
| 120 | + await page.screenshot({ |
| 121 | + fullPage: true, |
| 122 | + path: path.join(artifactDir, "reset-success.png"), |
| 123 | + }); |
| 124 | + } |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("does not publish a delayed reset rejection into a newly selected session", async () => { |
| 129 | + await withCompanion(async ({ gateway, menu, page }) => { |
| 130 | + await gateway.deferNext("sessions.companion.reset"); |
| 131 | + await clearCompanion(menu); |
| 132 | + await gateway.waitForRequest("sessions.companion.reset"); |
| 133 | + |
| 134 | + await navigateToControlUiSession(page, nextSessionKey); |
| 135 | + await gateway.rejectDeferred("sessions.companion.reset", { |
| 136 | + code: "UNAVAILABLE", |
| 137 | + message: resetError, |
| 138 | + }); |
| 139 | + |
| 140 | + const visiblePane = page.locator("openclaw-chat-pane.chat-pane-cache__pane--visible"); |
| 141 | + expect(await visiblePane.getByRole("alert").filter({ hasText: resetError }).count()).toBe(0); |
| 142 | + if (artifactDir) { |
| 143 | + await page.screenshot({ |
| 144 | + fullPage: true, |
| 145 | + path: path.join(artifactDir, "stale-reset-error-suppressed.png"), |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + await navigateToControlUiSession(page, initiatingSessionKey); |
| 150 | + const initiatingPane = page.locator("openclaw-chat-pane.chat-pane-cache__pane--visible"); |
| 151 | + expect(await initiatingPane.getByRole("alert").filter({ hasText: resetError }).count()).toBe( |
| 152 | + 0, |
| 153 | + ); |
| 154 | + await initiatingPane |
| 155 | + .locator("openclaw-chat-session-rail") |
| 156 | + .getByText(answer, { exact: true }) |
| 157 | + .waitFor(); |
| 158 | + if (artifactDir) { |
| 159 | + await page.screenshot({ |
| 160 | + fullPage: true, |
| 161 | + path: path.join(artifactDir, "initiating-thread-preserved.png"), |
| 162 | + }); |
| 163 | + } |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments