From a359fe836743019211ce817d8e32eca09d236e41 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Sun, 29 Mar 2026 20:17:21 -0400 Subject: [PATCH] test: isolate gateway node browser globals --- ui/src/ui/gateway.node.test.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/ui/src/ui/gateway.node.test.ts b/ui/src/ui/gateway.node.test.ts index 02606ce501e..5eafa8314f5 100644 --- a/ui/src/ui/gateway.node.test.ts +++ b/ui/src/ui/gateway.node.test.ts @@ -92,6 +92,16 @@ type ConnectFrame = { }; }; +function stubWindowGlobals(storage?: ReturnType) { + vi.stubGlobal("window", { + location: { href: "http://127.0.0.1:18789/" }, + localStorage: storage, + setTimeout: (handler: (...args: unknown[]) => void, timeout?: number, ...args: unknown[]) => + globalThis.setTimeout(() => handler(...args), timeout), + clearTimeout: (timeoutId: number | undefined) => globalThis.clearTimeout(timeoutId), + }); +} + function getLatestWebSocket(): MockWebSocket { const ws = wsInstances.at(-1); if (!ws) { @@ -182,15 +192,7 @@ describe("GatewayBrowserClient", () => { }); vi.stubGlobal("localStorage", storage); - const windowLike = Object.assign(globalThis, { - location: { href: "http://127.0.0.1:18789/" }, - localStorage: storage, - }); - vi.stubGlobal("window", windowLike); - Object.defineProperty(window, "localStorage", { - configurable: true, - value: storage, - }); + stubWindowGlobals(storage); localStorage.clear(); vi.stubGlobal("WebSocket", MockWebSocket); @@ -433,6 +435,14 @@ describe("GatewayBrowserClient", () => { }); describe("shouldRetryWithDeviceToken", () => { + beforeEach(() => { + stubWindowGlobals(); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + }); + it("allows a bounded retry for trusted loopback endpoints", () => { expect( shouldRetryWithDeviceToken({