@@ -24,28 +24,61 @@ function waitForMockCycle(): Promise<void> {
2424}
2525
2626describe ( "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 ( ) => {
0 commit comments