|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import type { |
| 5 | + GatewayBrowserClient, |
| 6 | + GatewayBrowserClientOptions, |
| 7 | + GatewayHelloOk, |
| 8 | +} from "../api/gateway.ts"; |
| 9 | +import { createStorageMock } from "../test-helpers/storage.ts"; |
| 10 | +import "./app-host.ts"; |
| 11 | +import type { ApplicationContext, ApplicationGateway } from "./context.ts"; |
| 12 | +import { createApplicationGateway } from "./gateway-store.ts"; |
| 13 | +import { loadSettings } from "./settings.ts"; |
| 14 | + |
| 15 | +const HELLO: GatewayHelloOk = { |
| 16 | + type: "hello-ok", |
| 17 | + protocol: 1, |
| 18 | + auth: { role: "operator", scopes: [] }, |
| 19 | +}; |
| 20 | + |
| 21 | +function createGatewayHarness() { |
| 22 | + vi.stubGlobal("localStorage", createStorageMock()); |
| 23 | + vi.stubGlobal("sessionStorage", createStorageMock()); |
| 24 | + const clients: Array<{ opts: GatewayBrowserClientOptions }> = []; |
| 25 | + const gateway = createApplicationGateway(loadSettings(), "", "", (opts) => { |
| 26 | + const client = { |
| 27 | + opts, |
| 28 | + instanceId: opts.instanceId, |
| 29 | + start: vi.fn(), |
| 30 | + stop: vi.fn(), |
| 31 | + request: vi.fn(), |
| 32 | + }; |
| 33 | + clients.push(client); |
| 34 | + return client as unknown as GatewayBrowserClient; |
| 35 | + }); |
| 36 | + return { gateway, clients }; |
| 37 | +} |
| 38 | + |
| 39 | +function renderGatewaySurface(gateway: ApplicationGateway): string { |
| 40 | + const app = document.createElement("openclaw-app") as unknown as { |
| 41 | + runtime: { context: ApplicationContext }; |
| 42 | + render: () => { strings: readonly string[] }; |
| 43 | + synchronizeGateway: (gateway: ApplicationGateway) => void; |
| 44 | + }; |
| 45 | + app.runtime = { |
| 46 | + context: { |
| 47 | + gateway, |
| 48 | + basePath: "", |
| 49 | + config: { current: { terminalEnabled: false } }, |
| 50 | + } as unknown as ApplicationContext, |
| 51 | + }; |
| 52 | + app.synchronizeGateway(gateway); |
| 53 | + return app.render().strings.join(""); |
| 54 | +} |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + vi.unstubAllGlobals(); |
| 58 | + vi.restoreAllMocks(); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("Control UI Gateway target lineage", () => { |
| 62 | + it("returns to the login gate when a newly selected Gateway's first attempt fails", () => { |
| 63 | + const { gateway, clients } = createGatewayHarness(); |
| 64 | + gateway.start(); |
| 65 | + clients[0]?.opts.onHello?.(HELLO); |
| 66 | + gateway.connect({ gatewayUrl: "wss://other-gateway.example.test" }); |
| 67 | + clients[1]?.opts.onClose?.({ code: 1006, reason: "remote refused", willRetry: true }); |
| 68 | + |
| 69 | + const surface = renderGatewaySurface(gateway); |
| 70 | + |
| 71 | + expect(surface).toContain("<openclaw-login-gate"); |
| 72 | + expect(surface).not.toContain("<openclaw-app-shell"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("keeps an established Gateway's dashboard mounted during its own retry", () => { |
| 76 | + const { gateway, clients } = createGatewayHarness(); |
| 77 | + gateway.start(); |
| 78 | + clients[0]?.opts.onHello?.(HELLO); |
| 79 | + clients[0]?.opts.onClose?.({ code: 1006, reason: "same gateway blip", willRetry: true }); |
| 80 | + |
| 81 | + const surface = renderGatewaySurface(gateway); |
| 82 | + |
| 83 | + expect(surface).toContain("<openclaw-app-shell"); |
| 84 | + expect(surface).not.toContain("<openclaw-login-gate"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("retains a replacement Gateway's dashboard after its own successful hello", () => { |
| 88 | + const { gateway, clients } = createGatewayHarness(); |
| 89 | + gateway.start(); |
| 90 | + clients[0]?.opts.onHello?.(HELLO); |
| 91 | + gateway.connect({ gatewayUrl: "wss://other-gateway.example.test" }); |
| 92 | + clients[1]?.opts.onHello?.(HELLO); |
| 93 | + clients[1]?.opts.onClose?.({ code: 1006, reason: "replacement blip", willRetry: true }); |
| 94 | + |
| 95 | + const surface = renderGatewaySurface(gateway); |
| 96 | + |
| 97 | + expect(surface).toContain("<openclaw-app-shell"); |
| 98 | + expect(surface).not.toContain("<openclaw-login-gate"); |
| 99 | + }); |
| 100 | +}); |
0 commit comments