@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 118 lines 4.66 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off -- Intercepts the spawn electron-updater requires natively.
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import { assert, describe, it } from "@effect/vitest";
4 import * as Deferred from "effect/Deferred";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8 import * as HostProcess from "@t3tools/shared/HostProcess";
9 import { autoUpdater } from "electron-updater";
10 import type * as NodeChildProcess from "node:child_process";
11 import * as NodeModule from "node:module";
12 import { vi } from "vite-plus/test";
13
14 // electron-updater requires Electron natively, outside vi.mock's reach, and builds its
15 // autoUpdater singleton as soon as it is imported.
16 vi.hoisted(() => {
17 const NodeModule = process.getBuiltinModule("node:module");
18 const NodeEvents = process.getBuiltinModule("node:events");
19 const require = NodeModule.createRequire(import.meta.url);
20 const electronPath = NodeModule.createRequire(require.resolve("electron-updater")).resolve(
21 "electron",
22 );
23 const electronModule = new NodeModule.Module(electronPath);
24 electronModule.filename = electronPath;
25 electronModule.loaded = true;
26 electronModule.exports = {
27 app: {
28 getVersion: () => "1.0.0",
29 getName: () => "T3 Code",
30 isPackaged: true,
31 quit: () => undefined,
32 once: () => undefined,
33 },
34 autoUpdater: new NodeEvents.EventEmitter(),
35 };
36 require.cache[electronPath] = electronModule;
37 });
38
39 import * as ElectronUpdater from "./ElectronUpdater.ts";
40
41 const childProcess = NodeModule.createRequire(import.meta.url)("node:child_process") as {
42 spawn: (
43 command: string,
44 args: ReadonlyArray<string>,
45 options: NodeChildProcess.SpawnOptions,
46 ) => NodeChildProcess.ChildProcess;
47 };
48
49 describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
50 "ElectronUpdater AppImage install",
51 () => {
52 it.live("relaunches the installed AppImage without the updating process's descriptors", () =>
53 Effect.gen(function* () {
54 const fileSystem = yield* FileSystem.FileSystem;
55 const path = yield* Path.Path;
56 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-appimage-install-" });
57 const appImagePath = path.join(dir, "T3-Code.AppImage");
58 const stagedPath = path.join(dir, "T3-Code-1.0.1.AppImage");
59 const fdReportPath = path.join(dir, "relaunched-fds.txt");
60 yield* fileSystem.writeFileString(appImagePath, "old AppImage");
61 yield* fileSystem.writeFileString(
62 stagedPath,
63 `#!/bin/sh\nls /proc/self/fd > '${fdReportPath}'\n`,
64 );
65 yield* fileSystem.chmod(stagedPath, 0o755);
66
67 yield* Effect.acquireRelease(
68 Effect.sync(() => vi.stubEnv("APPIMAGE", appImagePath)),
69 () => Effect.sync(() => vi.unstubAllEnvs()),
70 );
71
72 autoUpdater.logger = null;
73 Object.assign(autoUpdater, {
74 downloadedUpdateHelper: {
75 file: stagedPath,
76 downloadedFileInfo: { isAdminRightsRequired: false },
77 },
78 });
79
80 // Electron's main process holds the AppImage runtime's keepalive pipe and some
81 // Chromium files without O_CLOEXEC. Node cannot hold such a descriptor, so the
82 // relaunch runs with fd 7 opened that way to stand in for them.
83 const relaunched = yield* Deferred.make<void>();
84 const realSpawn = childProcess.spawn;
85 yield* Effect.acquireRelease(
86 Effect.sync(() => {
87 childProcess.spawn = (command, args, options) => {
88 const child = realSpawn(
89 "bash",
90 ["-c", 'exec 7</dev/null; exec "$@"', "leaky-parent", command, ...args],
91 options,
92 );
93 child.once("exit", () => Deferred.doneUnsafe(relaunched, Effect.void));
94 return child;
95 };
96 NodeModule.syncBuiltinESMExports();
97 }),
98 () =>
99 Effect.sync(() => {
100 childProcess.spawn = realSpawn;
101 NodeModule.syncBuiltinESMExports();
102 }),
103 );
104
105 const electronUpdater = yield* ElectronUpdater.ElectronUpdater;
106 yield* electronUpdater.quitAndInstall({ isSilent: true, isForceRunAfter: true });
107 yield* Deferred.await(relaunched);
108
109 const descriptors = (yield* fileSystem.readFileString(fdReportPath)).trim().split("\n");
110 assert.notInclude(
111 descriptors,
112 "7",
113 `relaunched AppImage inherited the updating process's fd 7 (open fds: ${descriptors.join(" ")})`,
114 );
115 }).pipe(Effect.provide([ElectronUpdater.layer, NodeServices.layer])),
116 );
117 },
118 );