The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { expect, it } from "@effect/vitest"; |
| 3 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | import * as NetService from "@t3tools/shared/Net"; |
| 5 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as TestConsole from "effect/testing/TestConsole"; |
| 10 | import { Command } from "effect/cli"; |
| 11 | import { afterEach, vi } from "vite-plus/test"; |
| 12 | |
| 13 | import packageJson from "../../package.json" with { type: "json" }; |
| 14 | import * as BootService from "../cloud/bootService.ts"; |
| 15 | import { resolveHostCliCommand } from "./invocation.ts"; |
| 16 | import { serviceCommand } from "./service.ts"; |
| 17 | |
| 18 | afterEach(() => vi.restoreAllMocks()); |
| 19 | |
| 20 | const status = { |
| 21 | supported: true, |
| 22 | installed: true, |
| 23 | current: true, |
| 24 | unitPath: "/home/me/.config/systemd/user/t3code.service", |
| 25 | logPath: "/home/me/.t3/userdata/logs/boot-service.log", |
| 26 | } as const; |
| 27 | |
| 28 | const newerServiceStatus = { ...status, current: false, installedVersion: "999.0.0" }; |
| 29 | |
| 30 | function makeTestService( |
| 31 | serviceStatus: BootService.BootServiceStatus, |
| 32 | restartOutcome: "restarted" | "queued" = "restarted", |
| 33 | ) { |
| 34 | const installOptions: Array<Parameters<BootService.BootService["Service"]["install"]>[0]> = []; |
| 35 | const restarts: Array<true> = []; |
| 36 | const uninstalls: Array<true> = []; |
| 37 | const service = BootService.BootService.of({ |
| 38 | status: Effect.succeed(serviceStatus), |
| 39 | withStopped: (operation) => operation, |
| 40 | restart: Effect.sync(() => { |
| 41 | restarts.push(true); |
| 42 | return serviceStatus.installed ? restartOutcome : "skipped"; |
| 43 | }), |
| 44 | install: (options) => |
| 45 | Effect.sync(() => { |
| 46 | installOptions.push(options); |
| 47 | return { |
| 48 | program: ["/test/t3/runtime/versions/1.0.0/t3", "__service-launcher"], |
| 49 | baseDir: "/test/t3", |
| 50 | unitPath: serviceStatus.unitPath, |
| 51 | logPath: serviceStatus.logPath, |
| 52 | }; |
| 53 | }), |
| 54 | uninstall: Effect.sync(() => { |
| 55 | uninstalls.push(true); |
| 56 | return serviceStatus.installed; |
| 57 | }), |
| 58 | }); |
| 59 | return { service, installOptions, restarts, uninstalls }; |
| 60 | } |
| 61 | |
| 62 | it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands", (it) => { |
| 63 | it.effect.each( |
| 64 | (["install", "update"] as const).flatMap((command) => [ |
| 65 | { |
| 66 | command, |
| 67 | label: "a downgrade", |
| 68 | status: newerServiceStatus, |
| 69 | error: { |
| 70 | _tag: "BootServiceDowngradeRefusedError", |
| 71 | installedVersion: "999.0.0", |
| 72 | targetVersion: packageJson.version, |
| 73 | }, |
| 74 | }, |
| 75 | // This home's newer state must not ask for --allow-downgrade first. |
| 76 | { |
| 77 | command, |
| 78 | label: "another home's service", |
| 79 | status: { ...newerServiceStatus, otherBaseDir: "/srv/t3" }, |
| 80 | error: { _tag: "BootServiceOtherBaseDirError", installedBaseDir: "/srv/t3" }, |
| 81 | }, |
| 82 | ]), |
| 83 | )( |
| 84 | "$command refuses $label before changing the service", |
| 85 | ({ command, status: serviceStatus, error: expected }) => |
| 86 | Effect.gen(function* () { |
| 87 | const fs = yield* FileSystem.FileSystem; |
| 88 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" }); |
| 89 | const { service, installOptions } = makeTestService(serviceStatus); |
| 90 | vi.spyOn(BootService, "layer").mockReturnValue( |
| 91 | Layer.succeed(BootService.BootService, service), |
| 92 | ); |
| 93 | |
| 94 | const error = yield* Command.runWith(serviceCommand, { version: packageJson.version })([ |
| 95 | command, |
| 96 | "--base-dir", |
| 97 | baseDir, |
| 98 | ]).pipe( |
| 99 | Effect.provideService(HostProcess.Environment, {}), |
| 100 | Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))), |
| 101 | Effect.flip, |
| 102 | ); |
| 103 | |
| 104 | expect(error).toMatchObject(expected); |
| 105 | expect(installOptions).toEqual([]); |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | it.effect.each([ |
| 110 | { label: "a new service", installed: false, env: "", expected: "T3CODE_LAN=false\n" }, |
| 111 | // Services installed before T3CODE_LAN existed keep listening on the local network. |
| 112 | { label: "an older service", installed: true, env: "", expected: "" }, |
| 113 | { |
| 114 | label: "a set host", |
| 115 | installed: false, |
| 116 | env: "T3CODE_HOST=::\n", |
| 117 | expected: "T3CODE_HOST=::\n", |
| 118 | }, |
| 119 | { |
| 120 | label: "a quoted multiline value", |
| 121 | installed: false, |
| 122 | env: 'CUSTOM="first\nT3CODE_LAN=example\nlast"\n', |
| 123 | expected: 'CUSTOM="first\nT3CODE_LAN=example\nlast"\nT3CODE_LAN=false\n', |
| 124 | }, |
| 125 | ])("install keeps $label off the local network only when it is new", (input) => |
| 126 | Effect.gen(function* () { |
| 127 | const fs = yield* FileSystem.FileSystem; |
| 128 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" }); |
| 129 | yield* fs.writeFileString(`${baseDir}/service.env`, input.env); |
| 130 | const { service } = makeTestService({ |
| 131 | ...status, |
| 132 | installed: input.installed, |
| 133 | current: false, |
| 134 | }); |
| 135 | vi.spyOn(BootService, "layer").mockReturnValue( |
| 136 | Layer.succeed(BootService.BootService, service), |
| 137 | ); |
| 138 | |
| 139 | const [lines, up] = yield* Command.runWith(serviceCommand, { version: packageJson.version })([ |
| 140 | "install", |
| 141 | "--base-dir", |
| 142 | baseDir, |
| 143 | ]).pipe( |
| 144 | Effect.andThen( |
| 145 | Effect.all([TestConsole.logLines, resolveHostCliCommand("up", ["--base-dir", baseDir])]), |
| 146 | ), |
| 147 | Effect.provideService(HostProcess.Environment, {}), |
| 148 | Effect.provide( |
| 149 | Layer.mergeAll( |
| 150 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), |
| 151 | Layer.fresh(TestConsole.layer), |
| 152 | ), |
| 153 | ), |
| 154 | ); |
| 155 | expect(yield* fs.readFileString(`${baseDir}/service.env`)).toBe(input.expected); |
| 156 | expect(lines.map(String).filter((line) => line.includes("other devices"))).toEqual( |
| 157 | input.installed ? [] : [`Run \`${up.command}\` to choose how your other devices reach it.`], |
| 158 | ); |
| 159 | }), |
| 160 | ); |
| 161 | |
| 162 | it.effect("protect refuses root before creating any T3 directories", () => |
| 163 | Effect.gen(function* () { |
| 164 | const fs = yield* FileSystem.FileSystem; |
| 165 | const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" }); |
| 166 | const baseDir = `${parent}/t3`; |
| 167 | vi.spyOn(BootService, "layer").mockReturnValue( |
| 168 | Layer.succeed(BootService.BootService, makeTestService(status).service), |
| 169 | ); |
| 170 | |
| 171 | const output = yield* Command.runWith(serviceCommand, { version: packageJson.version })([ |
| 172 | "protect", |
| 173 | "--base-dir", |
| 174 | baseDir, |
| 175 | ]).pipe( |
| 176 | Effect.andThen(TestConsole.logLines), |
| 177 | Effect.map((lines) => lines.join("\n")), |
| 178 | Effect.provideService(HostProcess.Environment, {}), |
| 179 | Effect.provideService(HostProcess.Platform, "linux"), |
| 180 | Effect.provideService(HostProcess.UserId, 0), |
| 181 | Effect.provide( |
| 182 | Layer.mergeAll( |
| 183 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), |
| 184 | Layer.fresh(TestConsole.layer), |
| 185 | ), |
| 186 | ), |
| 187 | ); |
| 188 | expect(output).toContain("without sudo"); |
| 189 | expect(yield* fs.exists(baseDir)).toBe(false); |
| 190 | }), |
| 191 | ); |
| 192 | |
| 193 | // regression: #13239 a stale nvm-installed CLI reported "already using" its own version |
| 194 | it.effect.each(["install", "update"] as const)( |
| 195 | "%s names the CLI that found nothing to change", |
| 196 | (command) => |
| 197 | Effect.gen(function* () { |
| 198 | const fs = yield* FileSystem.FileSystem; |
| 199 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" }); |
| 200 | const { service, installOptions } = makeTestService(status); |
| 201 | vi.spyOn(BootService, "layer").mockReturnValue( |
| 202 | Layer.succeed(BootService.BootService, service), |
| 203 | ); |
| 204 | const node = "/home/me/.nvm/versions/node/v26.8.2/bin/node"; |
| 205 | const script = "/home/me/.nvm/versions/node/v26.8.2/lib/node_modules/t3/dist/bin.mjs"; |
| 206 | |
| 207 | const output = yield* Command.runWith(serviceCommand, { version: packageJson.version })([ |
| 208 | command, |
| 209 | "--base-dir", |
| 210 | baseDir, |
| 211 | ]).pipe( |
| 212 | Effect.andThen(TestConsole.logLines), |
| 213 | Effect.map((lines) => lines.join("\n")), |
| 214 | Effect.provideService(HostProcess.Environment, {}), |
| 215 | Effect.provideService(HostProcess.IsExecutable, false), |
| 216 | Effect.provideService(HostProcess.ExecutablePath, node), |
| 217 | Effect.provideService(HostProcess.Arguments, [node, script, "service", command]), |
| 218 | Effect.provide( |
| 219 | Layer.mergeAll( |
| 220 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), |
| 221 | Layer.fresh(TestConsole.layer), |
| 222 | ), |
| 223 | ), |
| 224 | ); |
| 225 | expect(output).toContain(`t3@${packageJson.version}`); |
| 226 | expect(output).toContain(script); |
| 227 | expect(installOptions).toEqual([]); |
| 228 | }), |
| 229 | ); |
| 230 | |
| 231 | it.effect.each([ |
| 232 | { |
| 233 | label: "a missing service", |
| 234 | status: { ...status, installed: false }, |
| 235 | hint: "service install", |
| 236 | args: [], |
| 237 | }, |
| 238 | { label: "a newer service", status: newerServiceStatus, hint: "update", args: ["999.0.0"] }, |
| 239 | { |
| 240 | label: "a stopped service", |
| 241 | status: { ...status, problems: ["service-stopped"] as const }, |
| 242 | hint: "service install", |
| 243 | args: [], |
| 244 | }, |
| 245 | { |
| 246 | label: "a restart-pending service", |
| 247 | status: { ...status, problems: ["restart-pending"] as const }, |
| 248 | hint: "service restart", |
| 249 | args: [], |
| 250 | }, |
| 251 | ])("status of $label names its next step with --base-dir", (input) => |
| 252 | Effect.gen(function* () { |
| 253 | const fs = yield* FileSystem.FileSystem; |
| 254 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" }); |
| 255 | vi.spyOn(BootService, "layer").mockReturnValue( |
| 256 | Layer.succeed(BootService.BootService, makeTestService(input.status).service), |
| 257 | ); |
| 258 | |
| 259 | const [lines, next] = yield* Command.runWith(serviceCommand, { |
| 260 | version: packageJson.version, |
| 261 | })(["status", "--base-dir", baseDir]).pipe( |
| 262 | Effect.andThen( |
| 263 | Effect.all([ |
| 264 | TestConsole.logLines, |
| 265 | resolveHostCliCommand(input.hint, ["--base-dir", baseDir, ...input.args]), |
| 266 | ]), |
| 267 | ), |
| 268 | Effect.provideService(HostProcess.Environment, {}), |
| 269 | Effect.provide( |
| 270 | Layer.mergeAll( |
| 271 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), |
| 272 | Layer.fresh(TestConsole.layer), |
| 273 | ), |
| 274 | ), |
| 275 | ); |
| 276 | expect(lines.join("\n")).toContain(`\`${next.command}\``); |
| 277 | }), |
| 278 | ); |
| 279 | |
| 280 | it.effect.each([ |
| 281 | { label: "this home's service", otherHome: false, unreadable: false, removed: [true] }, |
| 282 | { label: "another home's service", otherHome: true, unreadable: false, removed: [] }, |
| 283 | { label: "an unreadable service", otherHome: false, unreadable: true, removed: [true] }, |
| 284 | ])("uninstall removes only $label", (input) => |
| 285 | Effect.gen(function* () { |
| 286 | const fs = yield* FileSystem.FileSystem; |
| 287 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" }); |
| 288 | const { service, uninstalls } = makeTestService({ |
| 289 | ...status, |
| 290 | installedBaseDir: input.otherHome ? "/home/me/.t3" : baseDir, |
| 291 | }); |
| 292 | vi.spyOn(BootService, "layer").mockReturnValue( |
| 293 | Layer.succeed( |
| 294 | BootService.BootService, |
| 295 | input.unreadable |
| 296 | ? { |
| 297 | ...service, |
| 298 | status: Effect.fail(new BootService.BootServiceInstallError({ cause: "EACCES" })), |
| 299 | } |
| 300 | : service, |
| 301 | ), |
| 302 | ); |
| 303 | |
| 304 | const output = yield* Command.runWith(serviceCommand, { version: packageJson.version })([ |
| 305 | "uninstall", |
| 306 | "--base-dir", |
| 307 | baseDir, |
| 308 | ]).pipe( |
| 309 | Effect.andThen(TestConsole.logLines), |
| 310 | Effect.map((lines) => lines.join("\n")), |
| 311 | Effect.provideService(HostProcess.Environment, {}), |
| 312 | Effect.provide( |
| 313 | Layer.mergeAll( |
| 314 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), |
| 315 | Layer.fresh(TestConsole.layer), |
| 316 | ), |
| 317 | ), |
| 318 | ); |
| 319 | expect(uninstalls).toEqual(input.removed); |
| 320 | expect(output).toContain(input.otherHome ? "--base-dir '/home/me/.t3'" : "Removed"); |
| 321 | }), |
| 322 | ); |
| 323 | }); |