The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { DesktopWslStateSchema } from "@t3tools/contracts"; |
| 2 | import { assert, describe, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | |
| 7 | import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts"; |
| 8 | import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts"; |
| 9 | import * as DesktopShutdown from "../../app/DesktopShutdown.ts"; |
| 10 | import * as DesktopState from "../../app/DesktopState.ts"; |
| 11 | import * as ElectronApp from "../../electron/ElectronApp.ts"; |
| 12 | import * as ElectronDialog from "../../electron/ElectronDialog.ts"; |
| 13 | import * as ElectronTheme from "../../electron/ElectronTheme.ts"; |
| 14 | import * as ElectronWindow from "../../electron/ElectronWindow.ts"; |
| 15 | import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts"; |
| 16 | import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts"; |
| 17 | import * as DesktopWindow from "../../window/DesktopWindow.ts"; |
| 18 | import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts"; |
| 19 | import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts"; |
| 20 | import { setWslBackendEnabled, setWslDistro, setWslOnly } from "./wsl.ts"; |
| 21 | |
| 22 | const decodeWslState = Schema.decodeUnknownEffect(DesktopWslStateSchema); |
| 23 | |
| 24 | const invokeSetWslBackendEnabled = (enabled: boolean) => |
| 25 | setWslBackendEnabled.handler(enabled).pipe(Effect.flatMap(decodeWslState)); |
| 26 | const invokeSetWslDistro = (distro: string | null) => |
| 27 | setWslDistro.handler(distro).pipe(Effect.flatMap(decodeWslState)); |
| 28 | const invokeSetWslOnly = (enabled: boolean) => |
| 29 | setWslOnly.handler(enabled).pipe(Effect.flatMap(decodeWslState)); |
| 30 | |
| 31 | function layerWslBackend(input: { readonly onReconcile?: Effect.Effect<void> } = {}) { |
| 32 | return Layer.succeed( |
| 33 | DesktopWslBackend.DesktopWslBackend, |
| 34 | DesktopWslBackend.DesktopWslBackend.of({ |
| 35 | reconcile: input.onReconcile ?? Effect.void, |
| 36 | lastPreflightError: Effect.succeedNone, |
| 37 | }), |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | function layerLifecycle(relaunchReasons: Array<string>) { |
| 42 | return Layer.succeed( |
| 43 | DesktopLifecycle.DesktopLifecycle, |
| 44 | DesktopLifecycle.DesktopLifecycle.of({ |
| 45 | relaunch: (reason) => |
| 46 | Effect.sync(() => { |
| 47 | relaunchReasons.push(reason); |
| 48 | }), |
| 49 | register: Effect.void, |
| 50 | }), |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | const layerUnusedLifecycleRuntime = Layer.mergeAll( |
| 55 | DesktopShutdown.layer, |
| 56 | DesktopState.layer, |
| 57 | Layer.succeed( |
| 58 | DesktopEnvironment.DesktopEnvironment, |
| 59 | DesktopEnvironment.DesktopEnvironment.of( |
| 60 | {} as DesktopEnvironment.DesktopEnvironment["Service"], |
| 61 | ), |
| 62 | ), |
| 63 | Layer.succeed( |
| 64 | DesktopWindow.DesktopWindow, |
| 65 | DesktopWindow.DesktopWindow.of({} as DesktopWindow.DesktopWindow["Service"]), |
| 66 | ), |
| 67 | Layer.succeed( |
| 68 | ElectronApp.ElectronApp, |
| 69 | ElectronApp.ElectronApp.of({} as ElectronApp.ElectronApp["Service"]), |
| 70 | ), |
| 71 | Layer.succeed( |
| 72 | ElectronTheme.ElectronTheme, |
| 73 | ElectronTheme.ElectronTheme.of({} as ElectronTheme.ElectronTheme["Service"]), |
| 74 | ), |
| 75 | Layer.succeed( |
| 76 | ElectronDialog.ElectronDialog, |
| 77 | ElectronDialog.ElectronDialog.of({} as ElectronDialog.ElectronDialog["Service"]), |
| 78 | ), |
| 79 | Layer.succeed( |
| 80 | ElectronWindow.ElectronWindow, |
| 81 | ElectronWindow.ElectronWindow.of({} as ElectronWindow.ElectronWindow["Service"]), |
| 82 | ), |
| 83 | DesktopClientSettings.layerTest(), |
| 84 | ); |
| 85 | |
| 86 | describe("WSL IPC", () => { |
| 87 | it.effect("stages WSL-only preferences and relaunches only after enabling", () => { |
| 88 | const relaunchReasons: Array<string> = []; |
| 89 | const layer = Layer.mergeAll( |
| 90 | DesktopAppSettings.layerTest({ |
| 91 | ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS, |
| 92 | wslBackendEnabled: false, |
| 93 | wslOnly: false, |
| 94 | }), |
| 95 | DesktopWslEnvironment.layerTest({ isAvailable: true }), |
| 96 | layerWslBackend(), |
| 97 | layerLifecycle(relaunchReasons), |
| 98 | layerUnusedLifecycleRuntime, |
| 99 | ); |
| 100 | |
| 101 | return Effect.gen(function* () { |
| 102 | const stagedMode = yield* invokeSetWslOnly(true); |
| 103 | assert.equal(stagedMode.enabled, false); |
| 104 | assert.equal(stagedMode.wslOnly, true); |
| 105 | assert.deepEqual(relaunchReasons, []); |
| 106 | |
| 107 | yield* invokeSetWslDistro("Debian"); |
| 108 | assert.deepEqual(relaunchReasons, []); |
| 109 | |
| 110 | const state = yield* invokeSetWslBackendEnabled(true); |
| 111 | assert.deepEqual(state, { |
| 112 | enabled: true, |
| 113 | distro: "Debian", |
| 114 | available: true, |
| 115 | wslOnly: true, |
| 116 | distros: [], |
| 117 | preflightError: null, |
| 118 | }); |
| 119 | assert.deepEqual(relaunchReasons, ["wslBackendEnabled=true"]); |
| 120 | }).pipe(Effect.provide(layer)); |
| 121 | }); |
| 122 | |
| 123 | it.effect("clears wsl-only before relaunching when disabling a WSL-only backend", () => { |
| 124 | const relaunchReasons: Array<string> = []; |
| 125 | let reconcileCount = 0; |
| 126 | const layer = Layer.mergeAll( |
| 127 | DesktopAppSettings.layerTest({ |
| 128 | ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS, |
| 129 | wslBackendEnabled: true, |
| 130 | wslOnly: true, |
| 131 | }), |
| 132 | DesktopWslEnvironment.layerTest({ isAvailable: true }), |
| 133 | layerWslBackend({ |
| 134 | onReconcile: Effect.sync(() => { |
| 135 | reconcileCount += 1; |
| 136 | }), |
| 137 | }), |
| 138 | layerLifecycle(relaunchReasons), |
| 139 | layerUnusedLifecycleRuntime, |
| 140 | ); |
| 141 | |
| 142 | return Effect.gen(function* () { |
| 143 | const state = yield* invokeSetWslBackendEnabled(false); |
| 144 | const appSettings = yield* DesktopAppSettings.DesktopAppSettings; |
| 145 | const settings = yield* appSettings.get; |
| 146 | |
| 147 | assert.deepEqual(state, { |
| 148 | enabled: false, |
| 149 | distro: null, |
| 150 | available: true, |
| 151 | wslOnly: false, |
| 152 | distros: [], |
| 153 | preflightError: null, |
| 154 | }); |
| 155 | assert.equal(settings.wslBackendEnabled, false); |
| 156 | assert.equal(settings.wslOnly, false); |
| 157 | assert.equal(reconcileCount, 0); |
| 158 | assert.deepEqual(relaunchReasons, ["wslBackendEnabled=false"]); |
| 159 | }).pipe(Effect.provide(layer)); |
| 160 | }); |
| 161 | }); |