|
| 1 | +// Browser proof that model setup reloads after a same-client Gateway reconnect. |
| 2 | +import { mkdir } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { chromium, type Browser } from "playwright"; |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 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 captureUiProofEnabled = process.env.OPENCLAW_CAPTURE_UI_PROOF === "1"; |
| 19 | +const artifactDir = path.join( |
| 20 | + process.cwd(), |
| 21 | + ".artifacts", |
| 22 | + "control-ui-e2e", |
| 23 | + "model-setup-reconnect", |
| 24 | +); |
| 25 | + |
| 26 | +function detection(modelRef: string) { |
| 27 | + return { |
| 28 | + candidates: [], |
| 29 | + manualProviders: [], |
| 30 | + configuredModel: modelRef, |
| 31 | + setupComplete: true, |
| 32 | + workspace: "/tmp/openclaw-e2e", |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +let browser: Browser; |
| 37 | +let server: ControlUiE2eServer; |
| 38 | + |
| 39 | +describeControlUiE2e("Control UI model setup same-client reconnect", () => { |
| 40 | + beforeAll(async () => { |
| 41 | + if (!chromiumAvailable) { |
| 42 | + throw new Error(`Playwright Chromium is unavailable at ${chromiumExecutablePath}`); |
| 43 | + } |
| 44 | + server = await startControlUiE2eServer(); |
| 45 | + browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 46 | + }); |
| 47 | + |
| 48 | + afterAll(async () => { |
| 49 | + await browser?.close(); |
| 50 | + await server?.close(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("re-detects once and replaces stale visible model state after reconnect", async () => { |
| 54 | + const context = await browser.newContext({ |
| 55 | + locale: "en-US", |
| 56 | + serviceWorkers: "block", |
| 57 | + viewport: { height: 900, width: 1280 }, |
| 58 | + }); |
| 59 | + const page = await context.newPage(); |
| 60 | + const pageErrors: string[] = []; |
| 61 | + page.on("pageerror", (error) => pageErrors.push(String(error))); |
| 62 | + const gateway = await installMockGateway(page, { |
| 63 | + featureMethods: ["openclaw.setup.detect"], |
| 64 | + methodResponses: { "openclaw.setup.detect": detection("provider/original-model") }, |
| 65 | + }); |
| 66 | + |
| 67 | + try { |
| 68 | + const response = await page.goto(`${server.baseUrl}settings/model-setup`); |
| 69 | + expect(response?.status()).toBe(200); |
| 70 | + await page.getByText("provider/original-model").waitFor(); |
| 71 | + const initialDetections = (await gateway.getRequests("openclaw.setup.detect")).length; |
| 72 | + const initialConnections = (await gateway.getRequests("connect")).length; |
| 73 | + await gateway.setMethodResponse( |
| 74 | + "openclaw.setup.detect", |
| 75 | + detection("provider/reconnected-model"), |
| 76 | + ); |
| 77 | + await gateway.deferNext("connect"); |
| 78 | + await gateway.closeLatest(1012, "model setup reconnect proof"); |
| 79 | + await expect.poll(async () => page.getByText("provider/original-model").count()).toBe(0); |
| 80 | + await expect |
| 81 | + .poll(async () => (await gateway.getRequests("connect")).length) |
| 82 | + .toBeGreaterThan(initialConnections); |
| 83 | + await gateway.resolveDeferred("connect"); |
| 84 | + await expect |
| 85 | + .poll(async () => (await gateway.getRequests("openclaw.setup.detect")).length) |
| 86 | + .toBe(initialDetections + 1); |
| 87 | + await page.getByText("provider/reconnected-model").waitFor(); |
| 88 | + expect(pageErrors).toEqual([]); |
| 89 | + |
| 90 | + if (captureUiProofEnabled) { |
| 91 | + await mkdir(artifactDir, { recursive: true }); |
| 92 | + await page.locator("openclaw-model-setup-page").screenshot({ |
| 93 | + animations: "disabled", |
| 94 | + path: path.join(artifactDir, "00-reconnected-model-visible.png"), |
| 95 | + }); |
| 96 | + } |
| 97 | + } finally { |
| 98 | + await context.close(); |
| 99 | + } |
| 100 | + }); |
| 101 | +}); |
0 commit comments