@@ -8,6 +8,7 @@ import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
88import { WebSocket , WebSocketServer , type RawData } from "ws" ;
99import { ConnectErrorDetailCodes } from "../../../packages/gateway-protocol/src/connect-error-details.js" ;
1010import type { GatewayServer } from "../../../src/gateway/server.js" ;
11+ import { waitForActiveGatewayRootWork } from "../../../src/process/gateway-work-admission.js" ;
1112import {
1213 createOpenClawTestState ,
1314 type OpenClawTestState ,
@@ -32,6 +33,12 @@ const artifactDir = path.resolve(
3233const viewport = { height : 900 , width : 1280 } ;
3334const trustedProxyUser = "qa-operator" ;
3435const controlUiSettleTimeoutMs = 60_000 ;
36+ const originProxyHeaderBlocklist = new Set ( [
37+ "connection" ,
38+ "content-encoding" ,
39+ "content-length" ,
40+ "transfer-encoding" ,
41+ ] ) ;
3542
3643type ProxyRoute = "trusted" | "untrusted" ;
3744
@@ -310,6 +317,48 @@ async function startRealTransportProxy(gatewayUrl: string): Promise<RealTranspor
310317 } ;
311318}
312319
320+ async function startControlUiOriginProxy ( upstreamBaseUrl : string ) : Promise < ControlUiE2eServer > {
321+ const server = createServer ( ( request , response ) => {
322+ void ( async ( ) => {
323+ const upstream = await fetch ( new URL ( request . url ?? "/" , upstreamBaseUrl ) , {
324+ headers : { Accept : request . headers . accept ?? "*/*" } ,
325+ method : request . method ,
326+ redirect : "manual" ,
327+ } ) ;
328+ response . statusCode = upstream . status ;
329+ for ( const [ name , value ] of upstream . headers ) {
330+ if ( ! originProxyHeaderBlocklist . has ( name ) ) {
331+ response . setHeader ( name , value ) ;
332+ }
333+ }
334+ response . end (
335+ request . method === "HEAD" ? undefined : Buffer . from ( await upstream . arrayBuffer ( ) ) ,
336+ ) ;
337+ } ) ( ) . catch ( ( ) => {
338+ if ( ! response . headersSent ) {
339+ response . writeHead ( 502 , { "Content-Type" : "text/plain; charset=utf-8" } ) ;
340+ }
341+ response . end ( "Control UI origin proxy failed" ) ;
342+ } ) ;
343+ } ) ;
344+
345+ await new Promise < void > ( ( resolve , reject ) => {
346+ server . once ( "error" , reject ) ;
347+ server . listen ( 0 , "127.0.0.1" , resolve ) ;
348+ } ) ;
349+ const address = server . address ( ) ;
350+ if ( ! address || typeof address === "string" ) {
351+ throw new Error ( "Control UI origin proxy did not bind a TCP port" ) ;
352+ }
353+ return {
354+ baseUrl : `http://127.0.0.1:${ address . port } /` ,
355+ close : ( ) =>
356+ new Promise < void > ( ( resolve , reject ) => {
357+ server . close ( ( error ) => ( error ? reject ( error ) : resolve ( ) ) ) ;
358+ } ) ,
359+ } ;
360+ }
361+
313362async function getFreePort ( ) : Promise < number > {
314363 const server = net . createServer ( ) ;
315364 await new Promise < void > ( ( resolve , reject ) => {
@@ -445,28 +494,18 @@ async function createBrowserPage(
445494 return { context, errors, evidenceStartIndex, page } ;
446495}
447496
448- async function warmControlUiSource ( baseUrl : string ) : Promise < void > {
449- const context = await browser . newContext ( {
450- locale : "en-US" ,
451- serviceWorkers : "block" ,
452- viewport,
453- } ) ;
454- try {
455- const page = await context . newPage ( ) ;
456- page . setDefaultTimeout ( 60_000 ) ;
457- const response = await page . goto ( withGatewayUrl ( baseUrl , proxy . trustedUrl ) ) ;
458- expect ( response ?. status ( ) ) . toBe ( 200 ) ;
459- await page . locator ( "openclaw-gateway-url-confirmation" ) . waitFor ( ) ;
460- } finally {
461- await context . close ( ) ;
462- }
463- }
464-
465497async function closeContext ( context : BrowserContext ) : Promise < void > {
466498 openContexts . delete ( context ) ;
467499 await context . close ( ) ;
468500}
469501
502+ async function closeConnectedContext ( context : BrowserContext ) : Promise < void > {
503+ await closeContext ( context ) ;
504+ // UI requests intentionally outlive socket teardown. Drain their admitted work
505+ // before another browser interaction so lazy handler imports cannot starve it.
506+ await expect ( waitForActiveGatewayRootWork ( ) ) . resolves . toEqual ( { active : 0 , drained : true } ) ;
507+ }
508+
470509async function captureChromiumScreenshot ( page : Page , fileName : string ) : Promise < void > {
471510 if ( ! captureUiProofEnabled ) {
472511 return ;
@@ -532,12 +571,12 @@ describeControlUiE2e("Control UI real auth transports E2E", () => {
532571 }
533572 await mkdir ( artifactDir , { recursive : true } ) ;
534573 allowedUi = await startControlUiE2eServer ( ) ;
535- rejectedUi = await startControlUiE2eServer ( undefined , { source : true } ) ;
574+ // A lightweight proxy supplies the distinct rejected Origin without starting
575+ // a second Vite compiler in the already resource-intensive browser shard.
576+ rejectedUi = await startControlUiOriginProxy ( allowedUi . baseUrl ) ;
536577 gateway = await startRealGateway ( new URL ( allowedUi . baseUrl ) . origin ) ;
537578 proxy = await startRealTransportProxy ( gateway . url ) ;
538579 browser = await chromium . launch ( { executablePath : chromiumExecutablePath } ) ;
539- await warmControlUiSource ( allowedUi . baseUrl ) ;
540- await warmControlUiSource ( rejectedUi . baseUrl ) ;
541580 } , 120_000 ) ;
542581
543582 afterAll ( async ( ) => {
@@ -615,7 +654,7 @@ describeControlUiE2e("Control UI real auth transports E2E", () => {
615654 expect ( trustedEvidence . requiredHeaderInjected ) . toBe ( true ) ;
616655 await captureChromiumScreenshot ( connected . page , "01-trusted-proxy-connected.png" ) ;
617656 expect ( connected . errors ) . toEqual ( [ ] ) ;
618- await closeContext ( connected . context ) ;
657+ await closeConnectedContext ( connected . context ) ;
619658
620659 await writeFile (
621660 path . join ( artifactDir , "trusted-proxy-behavior.json" ) ,
@@ -663,7 +702,7 @@ describeControlUiE2e("Control UI real auth transports E2E", () => {
663702 ) ;
664703 await captureChromiumScreenshot ( allowed . page , "03-allowed-origin-connected.png" ) ;
665704 expect ( allowed . errors ) . toEqual ( [ ] ) ;
666- await closeContext ( allowed . context ) ;
705+ await closeConnectedContext ( allowed . context ) ;
667706
668707 await writeFile (
669708 path . join ( artifactDir , "allowed-origins-behavior.json" ) ,
0 commit comments