diff --git a/packages/core/src/main/getDiForUnitTesting.ts b/packages/core/src/main/getDiForUnitTesting.ts index b71ab53239f8..0712f1e128f9 100644 --- a/packages/core/src/main/getDiForUnitTesting.ts +++ b/packages/core/src/main/getDiForUnitTesting.ts @@ -22,7 +22,6 @@ import electronQuitAndInstallUpdateInjectable from "./electron-app/features/elec import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable"; import setUpdateOnQuitInjectable from "./electron-app/features/set-update-on-quit.injectable"; import waitUntilBundledExtensionsAreLoadedInjectable from "./start-main-application/lens-window/application-window/wait-until-bundled-extensions-are-loaded.injectable"; -import electronInjectable from "./utils/resolve-system-proxy/electron.injectable"; import initializeClusterManagerInjectable from "./cluster/initialize-manager.injectable"; import type { GlobalOverride } from "@k8slens/test-utils"; import { getOverrideFsWithFakes } from "../test-utils/override-fs-with-fakes"; @@ -56,7 +55,6 @@ export function getDiForUnitTesting() { di.override(globalOverride.injectable, globalOverride.overridingInstantiate); } - di.override(electronInjectable, () => ({})); di.override(waitUntilBundledExtensionsAreLoadedInjectable, () => async () => {}); overrideRunnablesHavingSideEffects(di); diff --git a/packages/core/src/main/utils/resolve-system-proxy/electron-browser-window.global-override-for-injectable.ts b/packages/core/src/main/utils/resolve-system-proxy/electron-browser-window.global-override-for-injectable.ts new file mode 100644 index 000000000000..c6535e9ef274 --- /dev/null +++ b/packages/core/src/main/utils/resolve-system-proxy/electron-browser-window.global-override-for-injectable.ts @@ -0,0 +1,19 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverride } from "@k8slens/test-utils"; +import type { BrowserWindow, Session, WebContents } from "electron"; +import electronBrowserWindowInjectable from "./electron-browser-window.injectable"; + +export default getGlobalOverride( + electronBrowserWindowInjectable, + () => () => ({ + webContents: { + session: { + resolveProxy: () => "DIRECT", + } as unknown as Session, + } as unknown as WebContents, + } as unknown as BrowserWindow), +); diff --git a/packages/core/src/main/utils/resolve-system-proxy/electron-browser-window.injectable.ts b/packages/core/src/main/utils/resolve-system-proxy/electron-browser-window.injectable.ts new file mode 100644 index 000000000000..93f2066f93e6 --- /dev/null +++ b/packages/core/src/main/utils/resolve-system-proxy/electron-browser-window.injectable.ts @@ -0,0 +1,19 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import type { BrowserWindowConstructorOptions } from "electron"; +import { BrowserWindow } from "electron"; + +const electronBrowserWindowInjectable = getInjectable({ + id: "electron-browser-window", + instantiate: () => { + return (opts: BrowserWindowConstructorOptions) => { + return new BrowserWindow(opts); + }; + }, + causesSideEffects: true, +}); + +export default electronBrowserWindowInjectable; diff --git a/packages/core/src/main/utils/resolve-system-proxy/electron.injectable.ts b/packages/core/src/main/utils/resolve-system-proxy/electron.injectable.ts deleted file mode 100644 index a5999c9e5971..000000000000 --- a/packages/core/src/main/utils/resolve-system-proxy/electron.injectable.ts +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { getInjectable } from "@ogre-tools/injectable"; -import electron from "electron"; - -const electronInjectable = getInjectable({ - id: "electron", - instantiate: () => electron, - causesSideEffects: true, -}); - -export default electronInjectable; diff --git a/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-from-electron.injectable.ts b/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-from-electron.injectable.ts index c663d59fde8a..caa13c89ded2 100644 --- a/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-from-electron.injectable.ts +++ b/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-from-electron.injectable.ts @@ -3,28 +3,22 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import electronInjectable from "./electron.injectable"; +import electronBrowserWindowInjectable from "./electron-browser-window.injectable"; import withErrorLoggingInjectable from "../../../common/utils/with-error-logging/with-error-logging.injectable"; const resolveSystemProxyFromElectronInjectable = getInjectable({ id: "resolve-system-proxy-from-electron", instantiate: (di) => { - const electron = di.inject(electronInjectable); + const browserWindow = di.inject(electronBrowserWindowInjectable); const withErrorLoggingFor = di.inject(withErrorLoggingInjectable); - const withErrorLogging = withErrorLoggingFor(() => "Error resolving proxy"); + const hiddenWindow = browserWindow({ + show: false, + }); return withErrorLogging(async (url: string) => { - const webContent = electron.webContents - .getAllWebContents() - .find((x) => !x.isDestroyed()); - - if (!webContent) { - throw new Error(`Tried to resolve proxy for "${url}", but no browser window was available`); - } - - return await webContent.session.resolveProxy(url); + return await hiddenWindow.webContents.session.resolveProxy(url); }); }, }); diff --git a/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-from-electron.test.ts b/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-from-electron.test.ts index c197566a10cb..39b1904004df 100644 --- a/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-from-electron.test.ts +++ b/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-from-electron.test.ts @@ -5,13 +5,13 @@ import { getDiForUnitTesting } from "../../getDiForUnitTesting"; import resolveSystemProxyFromElectronInjectable from "./resolve-system-proxy-from-electron.injectable"; -import electronInjectable from "./electron.injectable"; +import electronBrowserWindowInjectable from "./electron-browser-window.injectable"; import type { AsyncFnMock } from "@async-fn/jest"; import asyncFn from "@async-fn/jest"; -import type electron from "electron"; import { getPromiseStatus } from "@k8slens/test-utils"; import logErrorInjectable from "../../../common/log-error.injectable"; import type { DiContainer } from "@ogre-tools/injectable"; +import type { BrowserWindow, Session, WebContents } from "electron"; describe("technical: resolve-system-proxy-from-electron", () => { let resolveSystemProxyMock: AsyncFnMock<(url: string) => Promise>; @@ -26,44 +26,19 @@ describe("technical: resolve-system-proxy-from-electron", () => { di.override(logErrorInjectable, () => logErrorMock); }); - describe("given there are non-destroyed Lens windows, when called with URL", () => { + describe("given there are no unexpected issues, when called with URL", () => { beforeEach(() => { resolveSystemProxyMock = asyncFn(); di.override( - electronInjectable, - - () => - ({ - webContents: { - getAllWebContents: () => [ - { - isDestroyed: () => true, - - session: { - resolveProxy: () => { - throw new Error("should never come here"); - }, - }, - }, - - { - isDestroyed: () => false, - session: { resolveProxy: resolveSystemProxyMock }, - }, - - { - isDestroyed: () => false, - - session: { - resolveProxy: () => { - throw new Error("should never come here"); - }, - }, - }, - ], - }, - } as unknown as typeof electron), + electronBrowserWindowInjectable, + () => () => ({ + webContents: { + session: { + resolveProxy: resolveSystemProxyMock, + } as unknown as Session, + } as unknown as WebContents, + } as unknown as BrowserWindow), ); const resolveSystemProxyFromElectron = di.inject( @@ -73,7 +48,7 @@ describe("technical: resolve-system-proxy-from-electron", () => { actualPromise = resolveSystemProxyFromElectron("some-url"); }); - it("calls to resolve proxy from the first window", () => { + it("calls to resolve proxy from the browser window", () => { expect(resolveSystemProxyMock).toHaveBeenCalledWith("some-url"); }); @@ -90,28 +65,23 @@ describe("technical: resolve-system-proxy-from-electron", () => { }); }); - describe("given there are only destroyed Lens windows, when called with URL", () => { + describe("given there are unexpected issues, when called with URL", () => { let error: any; beforeEach(async () => { + resolveSystemProxyMock = asyncFn(); + di.override( - electronInjectable, - () => - ({ - webContents: { - getAllWebContents: () => [ - { - isDestroyed: () => true, - - session: { - resolveProxy: () => { - throw new Error("should never come here"); - }, - }, - }, - ], - }, - } as unknown as typeof electron), + electronBrowserWindowInjectable, + () => () => ({ + webContents: { + session: { + resolveProxy: () => { + throw new Error("unexpected error"); + }, + } as unknown as Session, + } as unknown as WebContents, + } as unknown as BrowserWindow), ); resolveSystemProxyMock = asyncFn(); @@ -128,7 +98,7 @@ describe("technical: resolve-system-proxy-from-electron", () => { }); it("throws error", () => { - expect(error.message).toBe('Tried to resolve proxy for "some-url", but no browser window was available'); + expect(error.message).toBe("unexpected error"); }); it("logs error", () => {