diff options
| author | Oskar <oskar@mullvad.net> | 2025-08-28 16:53:01 +0200 |
|---|---|---|
| committer | Oskar <oskar@mullvad.net> | 2025-08-29 07:38:40 +0200 |
| commit | a2fd2e3fabac41053c08bdddb34b3beecf5d6a7d (patch) | |
| tree | d5f0782765e44cb6dfc0a944955f5a367815922b /desktop | |
| parent | c8e95ed6b3ecd3b0328dbe2bb934ecd0894f9e5f (diff) | |
| download | mullvadvpn-a2fd2e3fabac41053c08bdddb34b3beecf5d6a7d.tar.xz mullvadvpn-a2fd2e3fabac41053c08bdddb34b3beecf5d6a7d.zip | |
Add ipc structure containing keys for mocked tests
This commit adds a function along with types to enable type safety for
referencing ipc event keys.
Diffstat (limited to 'desktop')
3 files changed, 21 insertions, 4 deletions
diff --git a/desktop/packages/mullvad-vpn/src/shared/ipc-helpers.ts b/desktop/packages/mullvad-vpn/src/shared/ipc-helpers.ts index c49b7df9b8..db219f4535 100644 --- a/desktop/packages/mullvad-vpn/src/shared/ipc-helpers.ts +++ b/desktop/packages/mullvad-vpn/src/shared/ipc-helpers.ts @@ -61,6 +61,11 @@ export type IpcRenderer<S extends Schema> = { }; }; +// Transforms the provided schema into containing only the event keys. +export type IpcEvents<S> = { + [G in keyof S]: { [C in keyof S[G]]: string }; +}; + // Preforms the transformation of the main event channel in accordance with the above types. export function createIpcMain<S extends Schema>( schema: S, @@ -99,10 +104,15 @@ export function createIpcRenderer<S extends Schema>( }); } -function createIpc<S extends Schema, T, R extends IpcMain<S> | IpcRenderer<S>>( - ipc: S, - fn: (event: string, key: string, spec: AnyIpcCall) => [newKey: string, newValue: T], -): R { +export function createIpcEvents<S extends Schema>(schema: S): IpcEvents<S> { + return createIpc(schema, (event, key) => [key, event]); +} + +export function createIpc< + S extends Schema, + T, + R extends IpcMain<S> | IpcRenderer<S> | IpcEvents<S>, +>(ipc: S, fn: (event: string, key: string, spec: AnyIpcCall) => [newKey: string, newValue: T]): R { return Object.fromEntries( Object.entries(ipc).map(([groupKey, group]) => { const newGroup = Object.fromEntries( diff --git a/desktop/packages/mullvad-vpn/src/shared/ipc-schema.ts b/desktop/packages/mullvad-vpn/src/shared/ipc-schema.ts index 48148d9498..9a9580fc13 100644 --- a/desktop/packages/mullvad-vpn/src/shared/ipc-schema.ts +++ b/desktop/packages/mullvad-vpn/src/shared/ipc-schema.ts @@ -82,6 +82,8 @@ export interface IAppStateSnapshot { isMacOs13OrNewer: boolean; } +export type IpcSchema = typeof ipcSchema; + // The different types of requests are: // * send<ArgumentType>(), which is used for one-way communication from the renderer process to the // main process. The main channel will have a property named 'handle<PropertyName>' and the diff --git a/desktop/packages/mullvad-vpn/test/e2e/mocked/mocked-utils.ts b/desktop/packages/mullvad-vpn/test/e2e/mocked/mocked-utils.ts index 808321a254..0290536418 100644 --- a/desktop/packages/mullvad-vpn/test/e2e/mocked/mocked-utils.ts +++ b/desktop/packages/mullvad-vpn/test/e2e/mocked/mocked-utils.ts @@ -1,5 +1,7 @@ import { ElectronApplication } from 'playwright'; +import { createIpcEvents, IpcEvents } from '../../../src/shared/ipc-helpers'; +import { IpcSchema, ipcSchema } from '../../../src/shared/ipc-schema'; import { startApp, TestUtils } from '../utils'; // This option can be removed in the future when/if we're able to tun the tests with the sandbox @@ -14,6 +16,7 @@ export interface MockedTestUtils extends TestUtils { mockIpcHandle: MockIpcHandle; sendMockIpcResponse: SendMockIpcResponse; expectIpcCall: ExpectIpcCall; + ipcEvents: IpcEvents<IpcSchema>; } export const startMockedApp = async (): Promise<StartMockedAppResponse> => { @@ -37,6 +40,8 @@ export const startMockedApp = async (): Promise<StartMockedAppResponse> => { mockIpcHandle, sendMockIpcResponse, expectIpcCall, + + ipcEvents: createIpcEvents(ipcSchema), }, }; }; |
