The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off - The test observes a real cleanup-shell spawn failure. |
| 2 | import * as NodeChildProcess from "node:child_process"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import { assert, it } from "@effect/vitest"; |
| 5 | import * as Config from "effect/Config"; |
| 6 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as TestConsole from "effect/testing/TestConsole"; |
| 12 | import { Command } from "effect/cli"; |
| 13 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 14 | import * as NetService from "@t3tools/shared/Net"; |
| 15 | import { afterEach, expect, vi } from "vite-plus/test"; |
| 16 | |
| 17 | import packageJson from "../../package.json" with { type: "json" }; |
| 18 | import * as BootService from "../cloud/bootService.ts"; |
| 19 | import { posixLauncherScript } from "../cloud/pinnedRuntime.ts"; |
| 20 | import { |
| 21 | findOwnedLauncher, |
| 22 | findRecordedLaunchers, |
| 23 | spawnWindowsRemoval, |
| 24 | uninstallCommand, |
| 25 | } from "./uninstall.ts"; |
| 26 | |
| 27 | vi.mock("node:child_process", { spy: true }); |
| 28 | |
| 29 | afterEach(() => vi.restoreAllMocks()); |
| 30 | |
| 31 | it.live("reports failure when the Windows cleanup shell cannot start", () => |
| 32 | Effect.gen(function* () { |
| 33 | const fs = yield* FileSystem.FileSystem; |
| 34 | const path = yield* Path.Path; |
| 35 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-shell-" }); |
| 36 | const runtimeDir = path.join(baseDir, "runtime"); |
| 37 | yield* fs.makeDirectory(runtimeDir); |
| 38 | vi.spyOn(BootService, "layer").mockReturnValue( |
| 39 | Layer.succeed( |
| 40 | BootService.BootService, |
| 41 | BootService.BootService.of({ |
| 42 | status: Effect.succeed({ |
| 43 | supported: false, |
| 44 | installed: false, |
| 45 | current: false, |
| 46 | unitPath: "", |
| 47 | logPath: "", |
| 48 | }), |
| 49 | withStopped: (operation) => operation, |
| 50 | restart: Effect.die("unexpected restart"), |
| 51 | install: () => Effect.die("unexpected install"), |
| 52 | uninstall: Effect.die("unexpected uninstall"), |
| 53 | }), |
| 54 | ), |
| 55 | ); |
| 56 | const actual = yield* Effect.promise(() => |
| 57 | vi.importActual<typeof import("node:child_process")>("node:child_process"), |
| 58 | ); |
| 59 | const spawn = actual.spawn; |
| 60 | const failedSpawn = new Promise<Error>((resolve) => { |
| 61 | vi.spyOn(NodeChildProcess, "spawn").mockImplementation((...args) => { |
| 62 | const child = spawn(...args); |
| 63 | // Observe the real spawn error without letting it terminate the test runner. |
| 64 | child.once("error", resolve); |
| 65 | return child; |
| 66 | }); |
| 67 | }); |
| 68 | const result = yield* Command.runWith(uninstallCommand, { version: packageJson.version })([ |
| 69 | "--base-dir", |
| 70 | baseDir, |
| 71 | "--yes", |
| 72 | ]).pipe( |
| 73 | Effect.result, |
| 74 | Effect.provideService(HostProcess.Environment, { |
| 75 | ComSpec: path.join(baseDir, "missing-cmd.exe"), |
| 76 | }), |
| 77 | Effect.provideService(HostProcess.Platform, "win32"), |
| 78 | Effect.provideService(HostProcess.IsExecutable, false), |
| 79 | Effect.provide( |
| 80 | Layer.mergeAll( |
| 81 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), |
| 82 | Layer.fresh(TestConsole.layer), |
| 83 | ), |
| 84 | ), |
| 85 | ); |
| 86 | yield* Effect.promise(() => failedSpawn); |
| 87 | expect(result._tag, "an unavailable cleanup shell must fail uninstall").toBe("Failure"); |
| 88 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NetService.layer))), |
| 89 | ); |
| 90 | |
| 91 | it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("t3 uninstall command", (it) => { |
| 92 | it.effect("names another home's service it leaves installed", () => |
| 93 | Effect.gen(function* () { |
| 94 | const fs = yield* FileSystem.FileSystem; |
| 95 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-cli-test-" }); |
| 96 | const uninstalls: Array<true> = []; |
| 97 | vi.spyOn(BootService, "layer").mockReturnValue( |
| 98 | Layer.succeed( |
| 99 | BootService.BootService, |
| 100 | BootService.BootService.of({ |
| 101 | status: Effect.succeed({ |
| 102 | supported: true, |
| 103 | installed: true, |
| 104 | current: true, |
| 105 | installedBaseDir: "/home/me/.t3", |
| 106 | unitPath: "/home/me/.config/systemd/user/t3code.service", |
| 107 | logPath: "/home/me/.t3/userdata/logs/boot-service.log", |
| 108 | }), |
| 109 | withStopped: (operation) => operation, |
| 110 | restart: Effect.die("unexpected restart"), |
| 111 | install: () => Effect.die("unexpected install"), |
| 112 | uninstall: Effect.sync(() => { |
| 113 | uninstalls.push(true); |
| 114 | return true; |
| 115 | }), |
| 116 | }), |
| 117 | ), |
| 118 | ); |
| 119 | |
| 120 | const output = yield* Command.runWith(uninstallCommand, { version: packageJson.version })([ |
| 121 | "--base-dir", |
| 122 | baseDir, |
| 123 | "--yes", |
| 124 | ]).pipe( |
| 125 | Effect.andThen(TestConsole.logLines), |
| 126 | Effect.map((lines) => lines.join("\n")), |
| 127 | Effect.provideService(HostProcess.Environment, {}), |
| 128 | Effect.provideService(HostProcess.IsExecutable, false), |
| 129 | Effect.provide( |
| 130 | Layer.mergeAll( |
| 131 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), |
| 132 | Layer.fresh(TestConsole.layer), |
| 133 | ), |
| 134 | ), |
| 135 | ); |
| 136 | assert.deepStrictEqual(uninstalls, []); |
| 137 | assert.include(output, "--base-dir '/home/me/.t3'"); |
| 138 | }), |
| 139 | ); |
| 140 | }); |
| 141 | |
| 142 | it.layer(NodeServices.layer)("t3 uninstall launcher", (it) => { |
| 143 | it.effect("claims only a launcher that points into this home's runtime tree", () => |
| 144 | Effect.gen(function* () { |
| 145 | const fs = yield* FileSystem.FileSystem; |
| 146 | const path = yield* Path.Path; |
| 147 | // macOS spells the temp folder /var but resolves it to /private/var. |
| 148 | const root = yield* fs |
| 149 | .makeTempDirectoryScoped({ prefix: "t3-uninstall-" }) |
| 150 | .pipe(Effect.flatMap(fs.realPath)); |
| 151 | const versionsDir = path.join(root, "runtime/versions"); |
| 152 | const exe = path.join(versionsDir, "1.0.0/t3"); |
| 153 | const otherExe = path.join(root, "other/runtime/versions/1.0.0/t3"); |
| 154 | const copy = path.join(root, "copy/t3"); |
| 155 | for (const file of [exe, otherExe, copy]) { |
| 156 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 157 | yield* fs.writeFileString(file, ""); |
| 158 | } |
| 159 | const ours = path.join(root, "bin/t3"); |
| 160 | const theirs = path.join(root, "other/bin/t3"); |
| 161 | yield* fs.makeDirectory(path.dirname(ours), { recursive: true }); |
| 162 | yield* fs.makeDirectory(path.dirname(theirs), { recursive: true }); |
| 163 | yield* fs.symlink(exe, ours); |
| 164 | yield* fs.symlink(otherExe, theirs); |
| 165 | |
| 166 | assert.equal(yield* findOwnedLauncher({ launchedAs: ours, versionsDir }), ours); |
| 167 | assert.isUndefined(yield* findOwnedLauncher({ launchedAs: theirs, versionsDir })); |
| 168 | assert.isUndefined(yield* findOwnedLauncher({ launchedAs: copy, versionsDir })); |
| 169 | assert.isUndefined(yield* findOwnedLauncher({ launchedAs: undefined, versionsDir })); |
| 170 | // A script launcher execs by absolute path, so argv0 names the executable. |
| 171 | const scriptDir = path.join(root, "script-bin"); |
| 172 | const script = path.join(scriptDir, "t3"); |
| 173 | yield* fs.makeDirectory(scriptDir); |
| 174 | yield* fs.writeFileString(script, posixLauncherScript(exe), { mode: 0o755 }); |
| 175 | assert.equal( |
| 176 | yield* findOwnedLauncher({ launchedAs: exe, versionsDir }).pipe( |
| 177 | Effect.provideService(HostProcess.Environment, { PATH: scriptDir }), |
| 178 | ), |
| 179 | script, |
| 180 | ); |
| 181 | const editedScript = path.join(scriptDir, "t3-edited"); |
| 182 | yield* fs.writeFileString(editedScript, `${posixLauncherScript(exe)}# mine\n`); |
| 183 | |
| 184 | // On Windows, only the shim exactly as written is ours; a user's wrapper stays. |
| 185 | const shimDir = path.join(root, "win-bin"); |
| 186 | const shim = path.join(shimDir, "t3.cmd"); |
| 187 | yield* fs.makeDirectory(shimDir); |
| 188 | const windowsOwner = (contents: string) => |
| 189 | fs |
| 190 | .writeFileString(shim, contents) |
| 191 | .pipe( |
| 192 | Effect.andThen(findOwnedLauncher({ launchedAs: exe, versionsDir })), |
| 193 | Effect.provideService(HostProcess.Platform, "win32"), |
| 194 | Effect.provideService(HostProcess.Environment, { PATH: shimDir }), |
| 195 | ); |
| 196 | assert.equal(yield* windowsOwner(`@echo off\r\n"${exe}" %*`), shim); |
| 197 | assert.isUndefined(yield* windowsOwner(`@echo off\r\nset FOO=1\r\n"${exe}" %*`)); |
| 198 | |
| 199 | // npx can run another version than the installed one, so uninstall also reads the |
| 200 | // recorded launchers: through a symlinked home, and dangling after a prune. |
| 201 | const throughAlias = path.join(root, "bin/t3-alias"); |
| 202 | const pruned = path.join(root, "bin/t3-pruned"); |
| 203 | yield* fs.symlink(root, path.join(root, "alias")); |
| 204 | yield* fs.symlink(path.join(root, "alias/runtime/versions/1.0.0/t3"), throughAlias); |
| 205 | yield* fs.symlink(path.join(versionsDir, "0.9.0/t3"), pruned); |
| 206 | // Another program inside a version is not a launcher. |
| 207 | const sibling = path.join(root, "bin/t3-sibling"); |
| 208 | yield* fs.symlink(path.join(versionsDir, "1.0.0/node"), sibling); |
| 209 | yield* fs.writeFileString( |
| 210 | path.join(root, "runtime/launchers"), |
| 211 | [ |
| 212 | ours, |
| 213 | theirs, |
| 214 | copy, |
| 215 | throughAlias, |
| 216 | pruned, |
| 217 | sibling, |
| 218 | script, |
| 219 | editedScript, |
| 220 | path.join(root, "missing"), |
| 221 | ].join("\n") + "\n", |
| 222 | ); |
| 223 | for (const dir of [versionsDir, path.join(root, "alias/runtime/versions")]) { |
| 224 | assert.deepStrictEqual(yield* findRecordedLaunchers(dir), [ |
| 225 | ours, |
| 226 | throughAlias, |
| 227 | pruned, |
| 228 | script, |
| 229 | ]); |
| 230 | } |
| 231 | |
| 232 | // On Windows only an unedited t3.cmd is ours; a hand-edited wrapper belongs to the user. |
| 233 | const cmd = path.join(root, "bin/t3.cmd"); |
| 234 | const edited = path.join(root, "bin/edited.cmd"); |
| 235 | yield* fs.writeFileString(cmd, `@echo off\r\n"${exe}" %*`); |
| 236 | yield* fs.writeFileString(edited, `@echo off\r\nset "T3CODE_HOME=D:\\other"\r\n"${exe}" %*`); |
| 237 | yield* fs.writeFileString(path.join(root, "runtime/launchers"), `${cmd}\r\n${edited}\r\n`); |
| 238 | assert.deepStrictEqual( |
| 239 | yield* findRecordedLaunchers(versionsDir).pipe( |
| 240 | Effect.provideService(HostProcess.Platform, "win32"), |
| 241 | ), |
| 242 | [cmd], |
| 243 | ); |
| 244 | }).pipe(Effect.provideService(HostProcess.Platform, "linux")), |
| 245 | ); |
| 246 | }); |
| 247 | |
| 248 | it.live.runIf(HostProcess.Platform.defaultValue() === "win32")( |
| 249 | "t3 uninstall has cmd.exe remove the runtime directory, preserving spaces and literal environment-variable syntax", |
| 250 | () => |
| 251 | Effect.gen(function* () { |
| 252 | const fs = yield* FileSystem.FileSystem; |
| 253 | const path = yield* Path.Path; |
| 254 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-" }); |
| 255 | const runtimeDir = path.join(root, "t3 home %COMSPEC% !COMSPEC! & data", "runtime"); |
| 256 | yield* fs.makeDirectory(path.join(runtimeDir, "versions", "1.0.0"), { recursive: true }); |
| 257 | yield* fs.writeFileString(path.join(runtimeDir, "versions", "1.0.0", "t3.exe"), ""); |
| 258 | |
| 259 | const child = spawnWindowsRemoval( |
| 260 | yield* Config.String("ComSpec").pipe(Config.withDefault("cmd.exe")), |
| 261 | runtimeDir, |
| 262 | ); |
| 263 | yield* Effect.callback<void, Error>((resume) => { |
| 264 | child.once("error", (error) => resume(Effect.fail(error))); |
| 265 | child.once("exit", (code) => |
| 266 | resume(code === 0 ? Effect.void : Effect.die(`cmd.exe exited with ${code}`)), |
| 267 | ); |
| 268 | }); |
| 269 | |
| 270 | assert.isFalse(yield* fs.exists(runtimeDir)); |
| 271 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 272 | ); |