From 7782fd5171889edfaf520d1e39f51be62e412d10 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Mon, 26 Oct 2026 16:48:52 -0700 Subject: [PATCH] test: extract WSL host connector fixture to restore lint (#34132) Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> --- .../wsl/wsl-host-service-test-connector.ts | 74 +++++++++++++++++ src/main/wsl/wsl-host-service.test.ts | 80 +++---------------- 2 files changed, 83 insertions(+), 71 deletions(-) create mode 100644 src/main/wsl/wsl-host-service-test-connector.ts diff --git a/src/main/wsl/wsl-host-service-test-connector.ts b/src/main/wsl/wsl-host-service-test-connector.ts new file mode 100644 index 00000000000..69bb4a6a74a --- /dev/null +++ b/src/main/wsl/wsl-host-service-test-connector.ts @@ -0,0 +1,74 @@ +import { vi } from 'vitest' +import { encodePairingOffer } from '../../shared/pairing' +import type { WslHostServerStop } from '../../shared/wsl-host-types' +import type { WslHostDistroCandidate } from './wsl-host-distro-list' +import type { WslHostConnector } from './wsl-host-service' +import type { WslOrcadConnection } from './wsl-orcad-connection' + +export const KEY = Buffer.alloc(32, 3).toString('base64') +export const UBUNTU_ID = '0e3a7f2c-1b4d-4c5e-9f60-7a8b9c0d1e2f' + +export function pairing(port: number, deviceToken = 'token-1', publicKeyB64 = KEY): string { + return encodePairingOffer({ v: 2, endpoint: `ws://127.0.0.1:${port}`, deviceToken, publicKeyB64 }) +} + +export function connection(distro: string, port = 42000, token = 'token-1'): WslOrcadConnection { + return { + distro, + environmentId: `wsl:${distro.toLowerCase()}`, + localPort: port, + remotePort: 6768, + fullVersion: '1.0.0+abc', + pid: 10, + pairingOffer: pairing(port, token), + deferredUpdate: null, + close: async () => {} + } +} + +export function makeConnector(distros: WslHostDistroCandidate[]) { + const active = new Map() + // What WSL's running list says about the distro when its server isn't answering. + const distro: { state: 'running' | 'stopped' | 'unavailable' } = { state: 'running' } + const connector = { + listDistros: vi.fn(async () => distros), + readOrcadPublicKey: vi.fn(async (_distro: string): Promise => null), + connect: vi.fn(async (distro: string) => { + const opened = connection(distro) + active.set(distro.toLowerCase(), opened) + return opened + }), + get: vi.fn((distro: string) => active.get(distro.toLowerCase()) ?? null), + forgetPairingFailures: vi.fn((_environmentId: string) => {}), + isReachable: vi.fn(async (name: string) => active.has(name.toLowerCase())), + isSettling: vi.fn((_distro: string) => false), + reachability: vi.fn(async (name: string, _environmentId: string) => + active.has(name.toLowerCase()) + ? ('available' as const) + : distro.state === 'running' + ? ('unreachable' as const) + : distro.state === 'stopped' + ? ('stopped' as const) + : ('unavailable' as const) + ), + disconnect: vi.fn(async (distro: string) => { + active.delete(distro.toLowerCase()) + }), + stopServer: vi.fn( + async (_distro: string, _environmentId: string): Promise => ({ + stopped: true + }) + ), + releaseHolder: vi.fn(async (_distro: string) => {}), + adoptHolders: vi.fn(async () => {}), + dispose: vi.fn(async () => {}) + } satisfies WslHostConnector + return Object.assign(connector, { distro }) +} + +export const UBUNTU: WslHostDistroCandidate = { + id: UBUNTU_ID, + name: 'Ubuntu', + state: 'running', + version: 2 +} diff --git a/src/main/wsl/wsl-host-service.test.ts b/src/main/wsl/wsl-host-service.test.ts index 67aafc1894d..745cbf5a446 100644 --- a/src/main/wsl/wsl-host-service.test.ts +++ b/src/main/wsl/wsl-host-service.test.ts @@ -10,83 +10,21 @@ import { import { getEnvironmentStorePath } from '../../shared/runtime-environment-store-file' import { findWslHostDeployment } from '../../shared/runtime-environment-wsl-host-store' import { getPreferredPairingOffer } from '../../shared/runtime-environments' -import type { WslHostServerStop, WslHostStatus } from '../../shared/wsl-host-types' +import type { WslHostStatus } from '../../shared/wsl-host-types' import { OrcadHostUnsupportedError } from '../orcad/orcad-host-unavailable' -import type { WslHostDistroCandidate } from './wsl-host-distro-list' import { WslHostService, type WslHostConnector } from './wsl-host-service' import { wakeRememberedWslHosts } from './wsl-host-early-wake' import { WslOrcadLaunchDeferredError } from './wsl-orcad-connection-errors' import { RemoteRuntimeClientError } from '../../shared/remote-runtime-client-error' import { isRecoverableRemoteRuntimeConnectionError } from '../../shared/remote-runtime/remote-runtime-client-error-classification' -import type { WslOrcadConnection } from './wsl-orcad-connection' - -const KEY = Buffer.alloc(32, 3).toString('base64') -const UBUNTU_ID = '0e3a7f2c-1b4d-4c5e-9f60-7a8b9c0d1e2f' - -function pairing(port: number, deviceToken = 'token-1', publicKeyB64 = KEY): string { - return encodePairingOffer({ v: 2, endpoint: `ws://127.0.0.1:${port}`, deviceToken, publicKeyB64 }) -} - -function connection(distro: string, port = 42000, token = 'token-1'): WslOrcadConnection { - return { - distro, - environmentId: `wsl:${distro.toLowerCase()}`, - localPort: port, - remotePort: 6768, - fullVersion: '1.0.0+abc', - pid: 10, - pairingOffer: pairing(port, token), - deferredUpdate: null, - close: async () => {} - } -} - -function makeConnector(distros: WslHostDistroCandidate[]) { - const active = new Map() - // What WSL's running list says about the distro when its server isn't answering. - const distro: { state: 'running' | 'stopped' | 'unavailable' } = { state: 'running' } - const connector = { - listDistros: vi.fn(async () => distros), - readOrcadPublicKey: vi.fn(async (_distro: string): Promise => null), - connect: vi.fn(async (distro: string) => { - const opened = connection(distro) - active.set(distro.toLowerCase(), opened) - return opened - }), - get: vi.fn((distro: string) => active.get(distro.toLowerCase()) ?? null), - forgetPairingFailures: vi.fn((_environmentId: string) => {}), - isReachable: vi.fn(async (name: string) => active.has(name.toLowerCase())), - isSettling: vi.fn((_distro: string) => false), - reachability: vi.fn(async (name: string, _environmentId: string) => - active.has(name.toLowerCase()) - ? ('available' as const) - : distro.state === 'running' - ? ('unreachable' as const) - : distro.state === 'stopped' - ? ('stopped' as const) - : ('unavailable' as const) - ), - disconnect: vi.fn(async (distro: string) => { - active.delete(distro.toLowerCase()) - }), - stopServer: vi.fn( - async (_distro: string, _environmentId: string): Promise => ({ - stopped: true - }) - ), - releaseHolder: vi.fn(async (_distro: string) => {}), - adoptHolders: vi.fn(async () => {}), - dispose: vi.fn(async () => {}) - } satisfies WslHostConnector - return Object.assign(connector, { distro }) -} - -const UBUNTU: WslHostDistroCandidate = { - id: UBUNTU_ID, - name: 'Ubuntu', - state: 'running', - version: 2 -} +import { + KEY, + UBUNTU_ID, + UBUNTU, + pairing, + connection, + makeConnector +} from './wsl-host-service-test-connector' describe('WslHostService', () => { let userDataPath: string