The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 3 | import * as Context from "effect/Context"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import { beforeEach, vi } from "vite-plus/test"; |
| 7 | |
| 8 | const { |
| 9 | appendSwitchMock, |
| 10 | getSwitchValueMock, |
| 11 | hasSwitchMock, |
| 12 | registerSchemesMock, |
| 13 | setDesktopNameMock, |
| 14 | mkdirSyncMock, |
| 15 | writeFileSyncMock, |
| 16 | copyFileSyncMock, |
| 17 | execFileSyncMock, |
| 18 | readFileSyncMock, |
| 19 | symlinkSyncMock, |
| 20 | } = vi.hoisted(() => ({ |
| 21 | appendSwitchMock: vi.fn(), |
| 22 | getSwitchValueMock: vi.fn(), |
| 23 | hasSwitchMock: vi.fn(), |
| 24 | registerSchemesMock: vi.fn(), |
| 25 | setDesktopNameMock: vi.fn(), |
| 26 | mkdirSyncMock: vi.fn(), |
| 27 | writeFileSyncMock: vi.fn(), |
| 28 | copyFileSyncMock: vi.fn(), |
| 29 | execFileSyncMock: vi.fn(), |
| 30 | readFileSyncMock: vi.fn(), |
| 31 | symlinkSyncMock: vi.fn(), |
| 32 | })); |
| 33 | |
| 34 | vi.mock("electron", () => ({ |
| 35 | app: { |
| 36 | setDesktopName: setDesktopNameMock, |
| 37 | getVersion: () => "0.0.37", |
| 38 | isPackaged: true, |
| 39 | getAppPath: () => "/tmp/.mount_T3/resources/app.asar", |
| 40 | commandLine: { |
| 41 | appendSwitch: appendSwitchMock, |
| 42 | getSwitchValue: getSwitchValueMock, |
| 43 | hasSwitch: hasSwitchMock, |
| 44 | }, |
| 45 | }, |
| 46 | protocol: { |
| 47 | registerSchemesAsPrivileged: registerSchemesMock, |
| 48 | }, |
| 49 | })); |
| 50 | |
| 51 | vi.mock("node:fs", () => ({ |
| 52 | readFileSync: readFileSyncMock, |
| 53 | mkdirSync: mkdirSyncMock, |
| 54 | writeFileSync: writeFileSyncMock, |
| 55 | copyFileSync: copyFileSyncMock, |
| 56 | symlinkSync: symlinkSyncMock, |
| 57 | })); |
| 58 | |
| 59 | vi.mock("node:child_process", () => ({ |
| 60 | execFileSync: execFileSyncMock, |
| 61 | })); |
| 62 | |
| 63 | import * as DesktopConfig from "./DesktopConfig.ts"; |
| 64 | import * as DesktopPreReadyPlatform from "./DesktopPreReadyPlatform.ts"; |
| 65 | |
| 66 | describe("DesktopPreReadyPlatform", () => { |
| 67 | beforeEach(() => { |
| 68 | appendSwitchMock.mockReset(); |
| 69 | getSwitchValueMock.mockReset(); |
| 70 | hasSwitchMock.mockReset(); |
| 71 | registerSchemesMock.mockReset(); |
| 72 | setDesktopNameMock.mockReset(); |
| 73 | mkdirSyncMock.mockReset(); |
| 74 | writeFileSyncMock.mockReset(); |
| 75 | copyFileSyncMock.mockReset(); |
| 76 | execFileSyncMock.mockReset(); |
| 77 | readFileSyncMock.mockReset().mockReturnValue("{}"); |
| 78 | symlinkSyncMock.mockReset(); |
| 79 | }); |
| 80 | |
| 81 | it.effect("persists Linux scaling synchronously across relaunches and settings rewrites", () => { |
| 82 | vi.stubEnv("T3CODE_HOME", "/isolated"); |
| 83 | vi.stubEnv("VITE_DEV_SERVER_URL", ""); |
| 84 | const settingsPath = "/isolated/userdata/desktop-settings.json"; |
| 85 | const files = new Map([[settingsPath, "{}"]]); |
| 86 | const commandLine = new Map<string, string>(); |
| 87 | readFileSyncMock.mockImplementation((path: string) => { |
| 88 | const contents = files.get(path); |
| 89 | if (contents === undefined) throw new Error("ENOENT"); |
| 90 | return contents; |
| 91 | }); |
| 92 | writeFileSyncMock.mockImplementation((path: string, contents: string) => { |
| 93 | files.set(path, contents); |
| 94 | }); |
| 95 | hasSwitchMock.mockImplementation((name: string) => commandLine.has(name)); |
| 96 | getSwitchValueMock.mockImplementation((name: string) => commandLine.get(name) ?? ""); |
| 97 | appendSwitchMock.mockImplementation((name: string, value: string) => { |
| 98 | commandLine.set(name, value); |
| 99 | }); |
| 100 | const launch = Effect.gen(function* () { |
| 101 | const startup = Promise.resolve().then(() => commandLine.get("force-device-scale-factor")); |
| 102 | yield* DesktopPreReadyPlatform.make; |
| 103 | return yield* Effect.promise(() => startup); |
| 104 | }); |
| 105 | |
| 106 | return Effect.gen(function* () { |
| 107 | for (const explicitScale of ["1.75", "1"]) { |
| 108 | commandLine.clear(); |
| 109 | commandLine.set("force-device-scale-factor", explicitScale); |
| 110 | appendSwitchMock.mockClear(); |
| 111 | assert.equal(yield* launch, explicitScale); |
| 112 | assert.isFalse( |
| 113 | appendSwitchMock.mock.calls.some(([name]) => name === "force-device-scale-factor"), |
| 114 | ); |
| 115 | |
| 116 | // The settings service discards fields it does not own when rewriting its document. |
| 117 | files.set(settingsPath, JSON.stringify({ linuxPasswordStore: "basic" })); |
| 118 | commandLine.clear(); |
| 119 | assert.equal(yield* launch, explicitScale); |
| 120 | } |
| 121 | }).pipe( |
| 122 | Effect.provideService(HostProcess.Platform, "linux"), |
| 123 | Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())), |
| 124 | ); |
| 125 | }); |
| 126 | |
| 127 | it.effect.each([ |
| 128 | { previousEntry: undefined, label: "missing" }, |
| 129 | { previousEntry: 'Exec="/Applications/deleted-previous.AppImage" %U', label: "stale" }, |
| 130 | ])("prepares a $label Linux desktop entry before startup yields", ({ previousEntry }) => { |
| 131 | vi.stubEnv("VITE_DEV_SERVER_URL", ""); |
| 132 | getSwitchValueMock.mockReturnValue(""); |
| 133 | let desktopName = "t3code.desktop"; |
| 134 | let desktopEntry = previousEntry; |
| 135 | let iconInstalled = false; |
| 136 | const iconSource = "/tmp/.mount_T3/resources/app.asar/apps/desktop/prod-resources/icon.png"; |
| 137 | const iconPath = "/xdg/icons/com.t3tools.T3Code.desktop.png"; |
| 138 | const iconBytes = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]); |
| 139 | readFileSyncMock.mockImplementation((path: string) => { |
| 140 | if (path === iconSource) return iconBytes; |
| 141 | return "{}"; |
| 142 | }); |
| 143 | setDesktopNameMock.mockImplementation((name: string) => { |
| 144 | desktopName = name; |
| 145 | }); |
| 146 | writeFileSyncMock.mockImplementation((path: string, contents: string | Buffer) => { |
| 147 | if (path === "/xdg/applications/com.t3tools.T3Code.desktop") desktopEntry = String(contents); |
| 148 | if (path === iconPath) iconInstalled = Buffer.compare(Buffer.from(contents), iconBytes) === 0; |
| 149 | }); |
| 150 | |
| 151 | return Effect.scoped( |
| 152 | Effect.gen(function* () { |
| 153 | const portalIdentity = Promise.resolve().then(() => ({ |
| 154 | desktopName, |
| 155 | desktopEntry, |
| 156 | iconInstalled, |
| 157 | })); |
| 158 | yield* Layer.build( |
| 159 | DesktopPreReadyPlatform.layer.pipe( |
| 160 | Layer.provide(Layer.succeed(HostProcess.Platform, "linux")), |
| 161 | Layer.provide( |
| 162 | DesktopConfig.layerTest({ |
| 163 | XDG_DATA_HOME: "/xdg", |
| 164 | APPIMAGE: "/Applications/current.AppImage", |
| 165 | }), |
| 166 | ), |
| 167 | ), |
| 168 | ); |
| 169 | const identity = yield* Effect.promise(() => portalIdentity); |
| 170 | assert.equal(identity.desktopName, "com.t3tools.T3Code.desktop"); |
| 171 | assert.include(identity.desktopEntry ?? "", "Exec=/Applications/current.AppImage %U"); |
| 172 | assert.include(identity.desktopEntry ?? "", "Name=T3 Code (Beta)"); |
| 173 | assert.include(identity.desktopEntry ?? "", "MimeType=x-scheme-handler/t3code;"); |
| 174 | assert.include( |
| 175 | identity.desktopEntry ?? "", |
| 176 | "Icon=/xdg/icons/com.t3tools.T3Code.desktop.png", |
| 177 | ); |
| 178 | assert.equal( |
| 179 | copyFileSyncMock.mock.calls.length, |
| 180 | 0, |
| 181 | "icon must not be copied out of the app bundle", |
| 182 | ); |
| 183 | assert.isTrue(identity.iconInstalled, "icon must be written from the bundled bytes"); |
| 184 | }), |
| 185 | ).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs()))); |
| 186 | }); |
| 187 | |
| 188 | it.effect.each([ |
| 189 | { relaunchedByUpdater: "true", restored: true }, |
| 190 | { relaunchedByUpdater: "", restored: false }, |
| 191 | ])( |
| 192 | "restores the versioned AppImage path an update removed (updater relaunch: $relaunchedByUpdater)", |
| 193 | ({ relaunchedByUpdater, restored }) => { |
| 194 | vi.stubEnv("VITE_DEV_SERVER_URL", ""); |
| 195 | getSwitchValueMock.mockReturnValue(""); |
| 196 | const entryPath = "/xdg/applications/com.t3tools.T3Code.desktop"; |
| 197 | const files = new Map([[entryPath, "Exec=/Applications/T3-Code-0.1.8-x86_64.AppImage %U\n"]]); |
| 198 | readFileSyncMock.mockImplementation((path: string) => files.get(path) ?? "{}"); |
| 199 | writeFileSyncMock.mockImplementation((path: string, contents: string | Buffer) => { |
| 200 | files.set(path, String(contents)); |
| 201 | }); |
| 202 | |
| 203 | return Effect.scoped( |
| 204 | Effect.gen(function* () { |
| 205 | yield* Layer.build( |
| 206 | DesktopPreReadyPlatform.layer.pipe( |
| 207 | Layer.provide(Layer.succeed(HostProcess.Platform, "linux")), |
| 208 | Layer.provide( |
| 209 | DesktopConfig.layerTest({ |
| 210 | XDG_DATA_HOME: "/xdg", |
| 211 | APPIMAGE: "/Applications/T3-Code-x86_64.AppImage", |
| 212 | APPIMAGE_SILENT_INSTALL: relaunchedByUpdater, |
| 213 | }), |
| 214 | ), |
| 215 | ), |
| 216 | ); |
| 217 | assert.deepEqual( |
| 218 | symlinkSyncMock.mock.calls, |
| 219 | restored |
| 220 | ? [["T3-Code-x86_64.AppImage", "/Applications/T3-Code-0.1.8-x86_64.AppImage"]] |
| 221 | : [], |
| 222 | ); |
| 223 | assert.include(files.get(entryPath), "Exec=/Applications/T3-Code-x86_64.AppImage %U"); |
| 224 | }), |
| 225 | ).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs()))); |
| 226 | }, |
| 227 | ); |
| 228 | |
| 229 | it.effect("points the early portal entry at a configured distribution launcher", () => { |
| 230 | vi.stubEnv("VITE_DEV_SERVER_URL", ""); |
| 231 | getSwitchValueMock.mockReturnValue(""); |
| 232 | |
| 233 | return Effect.scoped( |
| 234 | Effect.gen(function* () { |
| 235 | yield* Layer.build( |
| 236 | DesktopPreReadyPlatform.layer.pipe( |
| 237 | Layer.provide(Layer.succeed(HostProcess.Platform, "linux")), |
| 238 | Layer.provide( |
| 239 | DesktopConfig.layerTest({ |
| 240 | XDG_DATA_HOME: "/xdg", |
| 241 | APPIMAGE: "/Applications/current.AppImage", |
| 242 | T3CODE_DESKTOP_LAUNCHER_PATH: " /opt/T3 Code/t3code ", |
| 243 | }), |
| 244 | ), |
| 245 | ), |
| 246 | ); |
| 247 | const entry = writeFileSyncMock.mock.calls.find( |
| 248 | ([path]) => path === "/xdg/applications/com.t3tools.T3Code.desktop", |
| 249 | )?.[1]; |
| 250 | assert.include(entry, 'Exec="/opt/T3 Code/t3code" %U'); |
| 251 | }), |
| 252 | ).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs()))); |
| 253 | }); |
| 254 | |
| 255 | it.effect( |
| 256 | "acquires a synchronous pre-ready layer before an asynchronous Clerk-shaped layer", |
| 257 | () => |
| 258 | Effect.gen(function* () { |
| 259 | class ClerkShaped extends Context.Service<ClerkShaped, { readonly ready: true }>()( |
| 260 | "@t3tools/desktop/app/DesktopPreReadyPlatform.test/ClerkShaped", |
| 261 | ) {} |
| 262 | |
| 263 | const events: Array<string> = []; |
| 264 | registerSchemesMock.mockImplementation(() => { |
| 265 | events.push("pre-ready"); |
| 266 | }); |
| 267 | |
| 268 | const layerPreReady = DesktopPreReadyPlatform.layer.pipe( |
| 269 | Layer.provide(Layer.succeed(HostProcess.Platform, "darwin")), |
| 270 | ); |
| 271 | |
| 272 | const layerClerkShaped = Layer.effect( |
| 273 | ClerkShaped, |
| 274 | Effect.promise(() => Promise.resolve()).pipe( |
| 275 | Effect.map(() => { |
| 276 | events.push("clerk"); |
| 277 | return { ready: true as const }; |
| 278 | }), |
| 279 | ), |
| 280 | ); |
| 281 | |
| 282 | const layerRuntime = layerClerkShaped.pipe( |
| 283 | Layer.flatMap((clerkContext) => Layer.succeedContext(clerkContext)), |
| 284 | Layer.provideMerge(layerPreReady), |
| 285 | ); |
| 286 | |
| 287 | const result = yield* Effect.all({ |
| 288 | clerk: ClerkShaped, |
| 289 | preReady: DesktopPreReadyPlatform.DesktopPreReadyElectronOptions, |
| 290 | }).pipe(Effect.provide(layerRuntime)); |
| 291 | |
| 292 | assert.deepEqual(result, { |
| 293 | clerk: { ready: true }, |
| 294 | preReady: { |
| 295 | linux: null, |
| 296 | linuxPasswordStoreCommandLine: null, |
| 297 | }, |
| 298 | }); |
| 299 | assert.deepEqual(events, ["pre-ready", "clerk"]); |
| 300 | assert.equal(registerSchemesMock.mock.calls.length, 1); |
| 301 | assert.equal(appendSwitchMock.mock.calls.length, 0); |
| 302 | assert.equal(setDesktopNameMock.mock.calls.length, 0); |
| 303 | }), |
| 304 | ); |
| 305 | }); |