The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { expect, it } from "@effect/vitest"; |
| 4 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 5 | import * as NetService from "@t3tools/shared/Net"; |
| 6 | import * as Cause from "effect/Cause"; |
| 7 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Option from "effect/Option"; |
| 12 | import * as Queue from "effect/Queue"; |
| 13 | import * as Terminal from "effect/Terminal"; |
| 14 | import * as TestConsole from "effect/testing/TestConsole"; |
| 15 | import { Command } from "effect/cli"; |
| 16 | import { afterEach, vi } from "vite-plus/test"; |
| 17 | |
| 18 | import packageJson from "../../package.json" with { type: "json" }; |
| 19 | import * as BootService from "../cloud/bootService.ts"; |
| 20 | import { upsertServiceEnv } from "./service.ts"; |
| 21 | import { connectEnabled, planRoutes, statusCommand, upCommand } from "./up.ts"; |
| 22 | |
| 23 | const none = Option.none<boolean>(); |
| 24 | |
| 25 | it.each([ |
| 26 | { |
| 27 | label: "a new machine starts from T3 Connect and asks", |
| 28 | setup: { installed: false, connect: false, lan: false }, |
| 29 | flags: { connect: none, lan: none }, |
| 30 | expected: { routes: { connect: true, lan: false }, ask: ["connect", "lan"] }, |
| 31 | }, |
| 32 | { |
| 33 | label: "a build without T3 Connect never offers it", |
| 34 | setup: { installed: false, connect: null, lan: false }, |
| 35 | flags: { connect: none, lan: none }, |
| 36 | expected: { routes: { connect: false, lan: false }, ask: ["lan"] }, |
| 37 | }, |
| 38 | { |
| 39 | label: "a rerun starts from the current setup", |
| 40 | setup: { installed: true, connect: false, lan: true }, |
| 41 | flags: { connect: none, lan: none }, |
| 42 | expected: { routes: { connect: false, lan: true }, ask: ["connect", "lan"] }, |
| 43 | }, |
| 44 | { |
| 45 | label: "a route flag on a new machine turns on only that route", |
| 46 | setup: { installed: false, connect: false, lan: false }, |
| 47 | flags: { connect: none, lan: Option.some(true) }, |
| 48 | expected: { routes: { connect: false, lan: true }, ask: [] }, |
| 49 | }, |
| 50 | { |
| 51 | label: "a route flag asks nothing and keeps the other route", |
| 52 | setup: { installed: true, connect: true, lan: true }, |
| 53 | flags: { connect: none, lan: Option.some(false) }, |
| 54 | expected: { routes: { connect: true, lan: false }, ask: [] }, |
| 55 | }, |
| 56 | ])("$label", ({ setup, flags, expected }) => { |
| 57 | expect(planRoutes(setup, flags)).toEqual(expected); |
| 58 | }); |
| 59 | |
| 60 | const SERVICE = { |
| 61 | supported: true, |
| 62 | installed: true, |
| 63 | current: true, |
| 64 | unitPath: "/home/me/.config/systemd/user/t3code.service", |
| 65 | logPath: "/home/me/.t3/userdata/logs/boot-service.log", |
| 66 | } as const; |
| 67 | |
| 68 | it.each([ |
| 69 | { |
| 70 | label: "a CLI managed link", |
| 71 | cliDesired: true, |
| 72 | cliMode: "managed", |
| 73 | managedTunnel: false, |
| 74 | revoked: false, |
| 75 | on: true, |
| 76 | }, |
| 77 | { |
| 78 | label: "a link the app made", |
| 79 | cliDesired: false, |
| 80 | cliMode: "managed", |
| 81 | managedTunnel: true, |
| 82 | revoked: false, |
| 83 | on: true, |
| 84 | }, |
| 85 | { |
| 86 | label: "a publish-only link", |
| 87 | cliDesired: true, |
| 88 | cliMode: "publish_only", |
| 89 | managedTunnel: false, |
| 90 | revoked: false, |
| 91 | on: false, |
| 92 | }, |
| 93 | { |
| 94 | label: "a publish-only link with an old tunnel config", |
| 95 | cliDesired: true, |
| 96 | cliMode: "publish_only", |
| 97 | managedTunnel: true, |
| 98 | revoked: false, |
| 99 | on: false, |
| 100 | }, |
| 101 | { |
| 102 | label: "a revoked link", |
| 103 | cliDesired: true, |
| 104 | cliMode: "managed", |
| 105 | managedTunnel: true, |
| 106 | revoked: true, |
| 107 | on: false, |
| 108 | }, |
| 109 | { |
| 110 | label: "no link", |
| 111 | cliDesired: false, |
| 112 | cliMode: "managed", |
| 113 | managedTunnel: false, |
| 114 | revoked: false, |
| 115 | on: false, |
| 116 | }, |
| 117 | ] as const)("T3 Connect is on for $label: $on", ({ label: _, on, ...link }) => { |
| 118 | expect(connectEnabled(link)).toBe(on); |
| 119 | }); |
| 120 | |
| 121 | // Prompts run only when both streams are terminals. |
| 122 | const asTerminal = Effect.acquireRelease( |
| 123 | Effect.sync(() => |
| 124 | [process.stdin, process.stdout].map((stream) => { |
| 125 | const descriptor = Object.getOwnPropertyDescriptor(stream, "isTTY"); |
| 126 | Object.defineProperty(stream, "isTTY", { configurable: true, value: true }); |
| 127 | return { stream, descriptor }; |
| 128 | }), |
| 129 | ), |
| 130 | (saved) => |
| 131 | Effect.sync(() => { |
| 132 | for (const { stream, descriptor } of saved) { |
| 133 | if (descriptor === undefined) Reflect.deleteProperty(stream, "isTTY"); |
| 134 | else Object.defineProperty(stream, "isTTY", descriptor); |
| 135 | } |
| 136 | }), |
| 137 | ); |
| 138 | |
| 139 | const answering = (text: string) => |
| 140 | Terminal.make({ |
| 141 | columns: Effect.succeed(80), |
| 142 | rows: Effect.succeed(24), |
| 143 | size: Queue.unbounded<Terminal.Size, Cause.Done>(), |
| 144 | readInput: Effect.gen(function* () { |
| 145 | const input = yield* Queue.unbounded<Terminal.UserInput, Cause.Done>(); |
| 146 | yield* Queue.offer(input, { |
| 147 | input: Option.some(text), |
| 148 | key: { name: text, ctrl: false, meta: false, shift: false }, |
| 149 | }); |
| 150 | return input; |
| 151 | }), |
| 152 | readLine: Effect.succeed(text), |
| 153 | display: () => Effect.void, |
| 154 | }); |
| 155 | |
| 156 | it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))("t3 up", (it) => { |
| 157 | afterEach(() => vi.restoreAllMocks()); |
| 158 | |
| 159 | it.effect.each<{ |
| 160 | readonly label: string; |
| 161 | readonly installed: boolean; |
| 162 | readonly status?: Partial<BootService.BootServiceStatus>; |
| 163 | readonly installFails?: boolean; |
| 164 | readonly restartFails?: boolean; |
| 165 | readonly fails?: boolean; |
| 166 | readonly reportStatus?: boolean; |
| 167 | /** Runs as if in a terminal, answering each question with this text. */ |
| 168 | readonly answer?: string; |
| 169 | /** Lines the run prints, with `<baseDir>` standing for the test's home. */ |
| 170 | readonly prints?: ReadonlyArray<string>; |
| 171 | readonly omits?: string; |
| 172 | readonly json?: object; |
| 173 | readonly env: string; |
| 174 | readonly args: ReadonlyArray<string>; |
| 175 | readonly installs: number; |
| 176 | readonly restarts: number; |
| 177 | readonly envAfter: string; |
| 178 | }>([ |
| 179 | { |
| 180 | label: "nothing changed", |
| 181 | installed: true, |
| 182 | env: "T3CODE_LAN=false\n", |
| 183 | args: ["--no-lan"], |
| 184 | installs: 0, |
| 185 | restarts: 0, |
| 186 | envAfter: "T3CODE_LAN=false\n", |
| 187 | }, |
| 188 | { |
| 189 | label: "the local network opens", |
| 190 | prints: ["Run `t3 pair --base-dir <baseDir>`."], |
| 191 | installed: true, |
| 192 | env: "T3CODE_LAN=false\n", |
| 193 | args: ["--lan"], |
| 194 | installs: 0, |
| 195 | restarts: 1, |
| 196 | envAfter: "T3CODE_LAN=true\n", |
| 197 | }, |
| 198 | { |
| 199 | label: "a new service starts with its settings", |
| 200 | installed: false, |
| 201 | env: "", |
| 202 | args: ["--lan"], |
| 203 | installs: 1, |
| 204 | restarts: 0, |
| 205 | envAfter: "T3CODE_LAN=true\n", |
| 206 | }, |
| 207 | { |
| 208 | label: "a route turns off with no service to run", |
| 209 | prints: ["Run `t3 --base-dir <baseDir>` to use T3 Code here."], |
| 210 | installed: false, |
| 211 | env: "T3CODE_LAN=true\n", |
| 212 | args: ["--no-lan"], |
| 213 | installs: 0, |
| 214 | restarts: 0, |
| 215 | envAfter: "T3CODE_LAN=false\n", |
| 216 | }, |
| 217 | { |
| 218 | label: "a stopped newer service starts as it is", |
| 219 | installed: true, |
| 220 | status: { current: false, installedVersion: "999.0.0", problems: ["service-stopped"] }, |
| 221 | env: "T3CODE_LAN=false\n", |
| 222 | args: ["--yes"], |
| 223 | installs: 0, |
| 224 | restarts: 1, |
| 225 | envAfter: "T3CODE_LAN=false\n", |
| 226 | }, |
| 227 | { |
| 228 | label: "a failed update keeps service.env matching the old service", |
| 229 | // The old service still runs, so no foreground server is offered. |
| 230 | prints: ["Fix that, then run `t3 up --base-dir <baseDir>` again."], |
| 231 | installed: true, |
| 232 | status: { current: false }, |
| 233 | installFails: true, |
| 234 | env: "", |
| 235 | args: ["--no-lan"], |
| 236 | installs: 0, |
| 237 | restarts: 0, |
| 238 | envAfter: "", |
| 239 | }, |
| 240 | { |
| 241 | label: "a failed restart puts service.env back and restarts on it", |
| 242 | installed: true, |
| 243 | restartFails: true, |
| 244 | env: "T3CODE_LAN=false\n", |
| 245 | args: ["--lan"], |
| 246 | installs: 0, |
| 247 | restarts: 2, |
| 248 | envAfter: "T3CODE_LAN=false\n", |
| 249 | }, |
| 250 | { |
| 251 | label: "declining an update changes nothing", |
| 252 | installed: true, |
| 253 | status: { current: false }, |
| 254 | answer: "n", |
| 255 | env: "T3CODE_LAN=false\n", |
| 256 | args: ["--lan"], |
| 257 | installs: 0, |
| 258 | restarts: 0, |
| 259 | envAfter: "T3CODE_LAN=false\n", |
| 260 | }, |
| 261 | { |
| 262 | label: "a service for another home is refused without a restart", |
| 263 | installed: true, |
| 264 | status: { current: false, otherBaseDir: "/srv/t3 home" }, |
| 265 | fails: true, |
| 266 | answer: "y", |
| 267 | env: "T3CODE_LAN=false\n", |
| 268 | args: ["--lan"], |
| 269 | installs: 0, |
| 270 | restarts: 0, |
| 271 | envAfter: "T3CODE_LAN=false\n", |
| 272 | }, |
| 273 | { |
| 274 | label: "another home's service supplies no local network default", |
| 275 | installed: true, |
| 276 | status: { current: false, otherBaseDir: "/srv/t3 home" }, |
| 277 | reportStatus: true, |
| 278 | json: { |
| 279 | service: { otherBaseDir: "/srv/t3 home" }, |
| 280 | lan: { enabled: false, inheritedDefault: false }, |
| 281 | }, |
| 282 | env: "", |
| 283 | args: ["--json"], |
| 284 | installs: 0, |
| 285 | restarts: 0, |
| 286 | envAfter: "", |
| 287 | }, |
| 288 | { |
| 289 | label: "status hints keep the caller's --base-dir", |
| 290 | installed: true, |
| 291 | reportStatus: true, |
| 292 | prints: [ |
| 293 | "Turn it off: `t3 up --base-dir <baseDir> --no-lan`.", |
| 294 | "Change access: `t3 up --base-dir <baseDir>`.", |
| 295 | ], |
| 296 | env: "", |
| 297 | args: [], |
| 298 | installs: 0, |
| 299 | restarts: 0, |
| 300 | envAfter: "", |
| 301 | }, |
| 302 | { |
| 303 | label: "a host override gets no --no-lan hint it would ignore", |
| 304 | installed: true, |
| 305 | reportStatus: true, |
| 306 | prints: ["overridden · T3CODE_HOST=0.0.0.0"], |
| 307 | omits: "--no-lan", |
| 308 | env: "T3CODE_HOST=0.0.0.0\n", |
| 309 | args: [], |
| 310 | installs: 0, |
| 311 | restarts: 0, |
| 312 | envAfter: "T3CODE_HOST=0.0.0.0\n", |
| 313 | }, |
| 314 | { |
| 315 | label: "no service manager still records the choice", |
| 316 | installed: false, |
| 317 | status: { supported: false }, |
| 318 | env: "", |
| 319 | args: ["--lan"], |
| 320 | installs: 0, |
| 321 | restarts: 0, |
| 322 | envAfter: "T3CODE_LAN=true\n", |
| 323 | }, |
| 324 | { |
| 325 | label: "the old default is kept", |
| 326 | installed: true, |
| 327 | env: "", |
| 328 | args: ["--yes"], |
| 329 | installs: 0, |
| 330 | restarts: 0, |
| 331 | envAfter: "T3CODE_LAN=true\n", |
| 332 | }, |
| 333 | ])("keeps service changes and access settings consistent: $label", (input) => |
| 334 | Effect.gen(function* () { |
| 335 | const fs = yield* FileSystem.FileSystem; |
| 336 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-up-test-" }); |
| 337 | yield* fs.writeFileString(`${baseDir}/service.env`, input.env); |
| 338 | let installs = 0; |
| 339 | let restarts = 0; |
| 340 | const status = { |
| 341 | ...SERVICE, |
| 342 | installed: input.installed, |
| 343 | current: input.installed, |
| 344 | ...input.status, |
| 345 | }; |
| 346 | vi.spyOn(BootService, "layer").mockReturnValue( |
| 347 | Layer.succeed( |
| 348 | BootService.BootService, |
| 349 | BootService.BootService.of({ |
| 350 | status: Effect.succeed(status), |
| 351 | withStopped: (operation) => operation, |
| 352 | restart: Effect.sync(() => restarts++).pipe( |
| 353 | Effect.andThen( |
| 354 | input.restartFails === true |
| 355 | ? Effect.fail(new BootService.BootServiceCommandError({ step: "restart" })) |
| 356 | : Effect.succeed("restarted" as const), |
| 357 | ), |
| 358 | ), |
| 359 | install: () => |
| 360 | status.otherBaseDir !== undefined |
| 361 | ? Effect.fail( |
| 362 | new BootService.BootServiceOtherBaseDirError({ |
| 363 | installedBaseDir: status.otherBaseDir, |
| 364 | baseDir, |
| 365 | }), |
| 366 | ) |
| 367 | : input.installFails === true |
| 368 | ? Effect.fail( |
| 369 | new BootService.BootServicePrerequisiteError({ problem: "linger-disabled" }), |
| 370 | ) |
| 371 | : Effect.sync(() => { |
| 372 | installs++; |
| 373 | return { |
| 374 | program: ["t3"], |
| 375 | baseDir, |
| 376 | unitPath: status.unitPath, |
| 377 | logPath: status.logPath, |
| 378 | }; |
| 379 | }), |
| 380 | uninstall: Effect.succeed(false), |
| 381 | }), |
| 382 | ), |
| 383 | ); |
| 384 | |
| 385 | if (input.answer !== undefined) yield* asTerminal; |
| 386 | const run = input.reportStatus |
| 387 | ? Command.runWith(statusCommand, { version: packageJson.version }) |
| 388 | : Command.runWith(upCommand, { version: packageJson.version }); |
| 389 | const { exit, lines } = yield* run(["--base-dir", baseDir, ...input.args]).pipe( |
| 390 | Effect.provideService(HostProcess.Environment, {}), |
| 391 | input.answer === undefined |
| 392 | ? (effect) => effect |
| 393 | : Effect.provideService(Terminal.Terminal, answering(input.answer)), |
| 394 | Effect.exit, |
| 395 | Effect.flatMap((exit) => |
| 396 | Effect.map(TestConsole.logLines, (lines) => ({ exit, lines: lines.map(String) })), |
| 397 | ), |
| 398 | Effect.provide( |
| 399 | Layer.mergeAll( |
| 400 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), |
| 401 | Layer.fresh(TestConsole.layer), |
| 402 | ), |
| 403 | ), |
| 404 | ); |
| 405 | for (const line of input.prints ?? []) { |
| 406 | expect(lines.join("\n")).toContain(line.replace("<baseDir>", baseDir)); |
| 407 | } |
| 408 | if (input.omits !== undefined) { |
| 409 | expect(lines.join("\n")).not.toContain(input.omits); |
| 410 | } |
| 411 | if (input.json !== undefined) { |
| 412 | expect(JSON.parse(lines.join("\n"))).toMatchObject(input.json); |
| 413 | } |
| 414 | expect(exit._tag).toBe( |
| 415 | input.restartFails === true || input.fails === true ? "Failure" : "Success", |
| 416 | ); |
| 417 | expect({ installs, restarts }).toEqual({ |
| 418 | installs: input.installs, |
| 419 | restarts: input.restarts, |
| 420 | }); |
| 421 | expect(yield* fs.readFileString(`${baseDir}/service.env`)).toBe(input.envAfter); |
| 422 | }), |
| 423 | ); |
| 424 | }); |
| 425 | |
| 426 | it("rewrites only the named service.env keys and keeps the user's lines", () => { |
| 427 | const before = ["# my settings", "T3CODE_PORT=4000", "export T3CODE_LAN=0", "T3CODE_LAN=1", ""]; |
| 428 | expect(upsertServiceEnv(before.join("\n"), { T3CODE_LAN: "true", T3CODE_HOST: undefined })).toBe( |
| 429 | ["# my settings", "T3CODE_PORT=4000", "T3CODE_LAN=true", ""].join("\n"), |
| 430 | ); |
| 431 | expect(upsertServiceEnv("", { T3CODE_LAN: "false" })).toBe("T3CODE_LAN=false\n"); |
| 432 | expect( |
| 433 | upsertServiceEnv('T3CODE_LAN="a\nb"\nCUSTOM="x\nT3CODE_LAN=y"\nT3CODE_LAN=true\n', { |
| 434 | T3CODE_LAN: "false", |
| 435 | }), |
| 436 | ).toBe('T3CODE_LAN=false\nCUSTOM="x\nT3CODE_LAN=y"\n'); |
| 437 | expect( |
| 438 | upsertServiceEnv('T3CODE_LAN=\u00a0"unused\nCUSTOM=keep\nEND="\nT3CODE_LAN=true\n', { |
| 439 | T3CODE_LAN: "false", |
| 440 | }), |
| 441 | ).toBe('T3CODE_LAN=false\nCUSTOM=keep\nEND="\n'); |
| 442 | const long = `CUSTOM="${"\n".repeat(130_000)}"\n`; |
| 443 | expect(upsertServiceEnv(`${long}T3CODE_LAN=true\n`, { T3CODE_LAN: "false" })).toBe( |
| 444 | `${long}T3CODE_LAN=false\n`, |
| 445 | ); |
| 446 | }); |