Skip to content

Commit ff4a6a9

Browse files
committed
test(ui): preserve offline socket cohorts
1 parent 42e08b4 commit ff4a6a9

4 files changed

Lines changed: 63 additions & 22 deletions

File tree

‎ui/src/e2e/locale-offline-retry.e2e.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { BrowserContext, Page, Route } from "playwright";
33
import { expect, it } from "vitest";
44
import { SUPPORTED_LOCALES } from "../i18n/lib/registry.ts";
55
import {
6+
controlUiBundledGatewayUrl,
67
installMockGateway,
78
startControlUiE2eServer,
89
type MockGatewayControls,
@@ -90,7 +91,9 @@ suite.define(() => {
9091
it("applies a locale whose first chunk request failed after the Gateway reconnects", async () => {
9192
const context = await createContext();
9293
const page = await context.newPage();
93-
const gateway = await installMockGateway(page);
94+
const gateway = await installMockGateway(page, {
95+
webSocketPassthroughPrefixes: [`${controlUiBundledGatewayUrl(suite.server.baseUrl)}/?token=`],
96+
});
9497
let abortedLocaleRequests = 0;
9598
const abortFrenchLocale = async (route: Route) => {
9699
abortedLocaleRequests += 1;

‎ui/src/pages/agents/memory/memory-panel.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ describe.runIf(process.env.OPENCLAW_UI_MEMORY_CHROMIUM_E2E === "1")(
622622
});
623623
const page = await context.newPage();
624624
const gateway = await e2e.installMockGateway(page, {
625+
webSocketPassthroughPrefixes: [`${e2e.controlUiBundledGatewayUrl(server.baseUrl)}/?token=`],
625626
featureMethods: [
626627
"chat.metadata",
627628
"chat.startup",

‎ui/src/test-helpers/control-ui-e2e.mock-gateway.test.ts‎

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,61 @@ function waitForMockCycle(): Promise<void> {
2424
}
2525

2626
describe("mock gateway stateful config", () => {
27-
it("takes every mock socket offline before a reconnect", async () => {
28-
const script = createControlUiMockGatewayInitScript({});
27+
it("takes existing mock sockets offline without closing their replacement", async () => {
28+
const passthroughPrefix = "ws://source-ui/?token=";
29+
const script = createControlUiMockGatewayInitScript({
30+
webSocketPassthroughPrefixes: [passthroughPrefix],
31+
});
2932
window.sessionStorage.clear();
30-
// oxlint-disable-next-line typescript/no-implied-eval -- Exercises the serialized browser fixture.
31-
new Function(script)();
32-
33-
const first = new WebSocket("ws://mock-gateway/first");
34-
const second = new WebSocket("ws://mock-gateway/second");
35-
await flushMockTimers();
36-
expect(first.readyState).toBe(WebSocket.OPEN);
37-
expect(second.readyState).toBe(WebSocket.OPEN);
38-
39-
const controls = (
40-
window as Window & {
41-
openclawControlUiE2eGateway?: { setOnline: (online: boolean) => void };
33+
const priorWebSocket = window.WebSocket;
34+
class PassthroughWebSocket extends EventTarget {
35+
static readonly CLOSED = 3;
36+
static readonly CLOSING = 2;
37+
static readonly CONNECTING = 0;
38+
static readonly OPEN = 1;
39+
readyState = PassthroughWebSocket.OPEN;
40+
41+
close(): void {
42+
this.readyState = PassthroughWebSocket.CLOSED;
4243
}
43-
).openclawControlUiE2eGateway;
44-
expect(controls).toBeDefined();
45-
controls?.setOnline(false);
44+
}
45+
window.WebSocket = PassthroughWebSocket as unknown as typeof WebSocket;
4646

47-
expect(first.readyState).toBe(WebSocket.CLOSED);
48-
expect(second.readyState).toBe(WebSocket.CLOSED);
47+
try {
48+
// oxlint-disable-next-line typescript/no-implied-eval -- Exercises the serialized browser fixture.
49+
new Function(script)();
50+
51+
const passthrough = new WebSocket(`${passthroughPrefix}vite`);
52+
const first = new WebSocket("ws://mock-gateway/first");
53+
const second = new WebSocket("ws://mock-gateway/second");
54+
await flushMockTimers();
55+
expect(passthrough.readyState).toBe(WebSocket.OPEN);
56+
expect(first.readyState).toBe(WebSocket.OPEN);
57+
expect(second.readyState).toBe(WebSocket.OPEN);
58+
59+
let replacement: WebSocket | undefined;
60+
first.addEventListener("close", () => {
61+
replacement = new WebSocket("ws://mock-gateway/replacement");
62+
});
63+
64+
const controls = (
65+
window as Window & {
66+
openclawControlUiE2eGateway?: { setOnline: (online: boolean) => void };
67+
}
68+
).openclawControlUiE2eGateway;
69+
expect(controls).toBeDefined();
70+
controls?.setOnline(false);
71+
72+
expect(passthrough.readyState).toBe(WebSocket.OPEN);
73+
expect(first.readyState).toBe(WebSocket.CLOSED);
74+
expect(second.readyState).toBe(WebSocket.CLOSED);
75+
expect(replacement?.readyState).toBe(WebSocket.CONNECTING);
76+
77+
controls?.setOnline(true);
78+
expect(replacement?.readyState).toBe(WebSocket.OPEN);
79+
} finally {
80+
window.WebSocket = priorWebSocket;
81+
}
4982
});
5083

5184
it("round-trips config.set through config.get with an advancing hash", async () => {

‎ui/src/test-helpers/control-ui-e2e.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,12 +2066,16 @@ function installControlUiMockGateway(
20662066
// The current document can still toggle the in-memory mock.
20672067
}
20682068
if (!online) {
2069-
for (const socket of sockets) {
2069+
// Close handlers can synchronously construct replacements. Snapshot the
2070+
// transition members so an offline replacement stays ready for recovery.
2071+
const transitionSockets = sockets.slice();
2072+
for (const socket of transitionSockets) {
20702073
socket.close(1006, "mock offline");
20712074
}
20722075
return;
20732076
}
2074-
for (const socket of sockets) {
2077+
const transitionSockets = sockets.slice();
2078+
for (const socket of transitionSockets) {
20752079
socket.openConnection();
20762080
}
20772081
},

0 commit comments

Comments
 (0)