Showing the changes to apps/server/src/cloud/bootService.test.ts (1 of 12 files). Show all
Unified · Split
| @@ -12,6 +12,7 @@ import { · 11 unmodified lines | ||
| 12 | 12 | HostProcessArguments, |
| 13 | 13 | HostProcessExecutablePath, |
| 14 | 14 | HostProcessPlatform, |
| 15 | + HostProcessUserId, | |
| 15 | 16 | } from "@t3tools/shared/hostProcess"; |
| 16 | 17 | |
| 17 | 18 | import { reconcileService } from "../cli/service.ts"; |
| @@ -64,6 +65,7 @@ const provideHostRefs = (home: string, platform: NodeJS.Platform = "linux") => · 46 unmodified lines | ||
| 64 | 65 | Effect.provide( |
| 65 | 66 | Layer.mergeAll( |
| 66 | 67 | Layer.succeed(HostProcessPlatform, platform), |
| 68 | + Layer.succeed(HostProcessUserId, 501), | |
| 67 | 69 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: { HOME: home } })), |
| 68 | 70 | ), |
| 69 | 71 | ); |
| @@ -143,6 +145,34 @@ it("quotes systemd values containing spaces and escapes percent specifiers", () · 73 unmodified lines | ||
| 143 | 145 | assert.include(unit, "StandardError=append:/home/me/100%%logs/boot.log"); |
| 144 | 146 | }); |
| 145 | 147 | |
| 148 | +it("renders a restartable per-user macOS LaunchAgent", () => { | |
| 149 | + const launchAgent = BootService.renderBootServiceLaunchAgent({ | |
| 150 | + nodePath: "/opt/homebrew/bin/node", | |
| 151 | + t3EntryPath: "/Users/theo/T3 & Data/runtime/service/current/node_modules/t3/dist/bin.mjs", | |
| 152 | + baseDir: "/Users/theo/T3 & Data", | |
| 153 | + logPath: "/Users/theo/T3 & Data/userdata/logs/boot-service.log", | |
| 154 | + unitPath: "/Users/theo/Library/LaunchAgents/com.t3tools.t3code.server.plist", | |
| 155 | + pathEnvironment: "/opt/homebrew/bin:/usr/bin:/bin", | |
| 156 | + }); | |
| 157 | + | |
| 158 | + assert.include(launchAgent, "<string>com.t3tools.t3code.server</string>"); | |
| 159 | + assert.include(launchAgent, "<key>RunAtLoad</key>\n <true/>"); | |
| 160 | + assert.include(launchAgent, "<key>KeepAlive</key>\n <true/>"); | |
| 161 | + assert.include(launchAgent, "<string>/Users/theo/T3 & Data</string>"); | |
| 162 | + assert.include( | |
| 163 | + launchAgent, | |
| 164 | + "<string>/Users/theo/T3 & Data/runtime/service/current/node_modules/t3/dist/bin.mjs</string>", | |
| 165 | + ); | |
| 166 | + assert.include( | |
| 167 | + launchAgent, | |
| 168 | + "<key>T3_BOOT_SERVICE_UNIT</key>\n <string>com.t3tools.t3code.server</string>", | |
| 169 | + ); | |
| 170 | + assert.include( | |
| 171 | + launchAgent, | |
| 172 | + "<key>PATH</key>\n <string>/opt/homebrew/bin:/usr/bin:/bin</string>", | |
| 173 | + ); | |
| 174 | +}); | |
| 175 | + | |
| 146 | 176 | it("flags package-manager cache entry points as ephemeral", () => { |
| 147 | 177 | assert.isTrue( |
| 148 | 178 | BootService.isEphemeralCacheEntry("/home/theo/.npm/_npx/abc123/node_modules/t3/dist/bin.mjs"), |
| @@ -388,7 +418,7 @@ it.layer(NodeServices.layer)("BootService", (it) => { · 239 unmodified lines | ||
| 388 | 418 | }), |
| 389 | 419 | ); |
| 390 | 420 | |
| 391 | − it.effect("fails on non-Linux platforms without touching the filesystem", () => | |
| 421 | + it.effect("installs and starts a pinned macOS LaunchAgent", () => | |
| 392 | 422 | Effect.gen(function* () { |
| 393 | 423 | const { dirs, fs, path } = yield* makeTestContext(); |
| 394 | 424 | const commands: Array<RecordedCommand> = []; |
| @@ -396,12 +426,122 @@ it.layer(NodeServices.layer)("BootService", (it) => { · 1 unmodified line | ||
| 396 | 426 | baseDir: dirs.baseDir, |
| 397 | 427 | logsDir: dirs.logsDir, |
| 398 | 428 | cliVersion: "0.0.27", |
| 399 | − host: makeHost("/usr/local/lib/node_modules/t3/dist/bin.mjs"), | |
| 429 | + host: makeHost(dirs.stableEntry), | |
| 400 | 430 | }).pipe( |
| 401 | 431 | Effect.provide(makeRecordingRunnerLayer(commands)), |
| 402 | 432 | provideHostRefs(dirs.home, "darwin"), |
| 403 | 433 | ); |
| 404 | 434 | |
| 435 | + const plan = yield* service.install; | |
| 436 | + const runtimeDir = path.join(dirs.baseDir, "runtime", "versions", "0.0.27"); | |
| 437 | + const activeRuntimeLink = path.join(dirs.baseDir, "runtime", "service", "current"); | |
| 438 | + const launchAgentPath = path.join( | |
| 439 | + dirs.home, | |
| 440 | + "Library", | |
| 441 | + "LaunchAgents", | |
| 442 | + "com.t3tools.t3code.server.plist", | |
| 443 | + ); | |
| 444 | + | |
| 445 | + assert.equal( | |
| 446 | + plan.t3EntryPath, | |
| 447 | + path.join(activeRuntimeLink, "node_modules", "t3", "dist", "bin.mjs"), | |
| 448 | + ); | |
| 449 | + assert.equal(yield* fs.readLink(activeRuntimeLink), runtimeDir); | |
| 450 | + assert.deepEqual( | |
| 451 | + commands.map((entry) => [entry.command, ...entry.args].join(" ")), | |
| 452 | + [ | |
| 453 | + `npm install --prefix ${runtimeDir} --no-fund --no-audit t3@0.0.27`, | |
| 454 | + "/bin/launchctl enable gui/501/com.t3tools.t3code.server", | |
| 455 | + "/bin/launchctl bootout gui/501/com.t3tools.t3code.server", | |
| 456 | + `/bin/launchctl bootstrap gui/501 ${launchAgentPath}`, | |
| 457 | + "/bin/launchctl print gui/501/com.t3tools.t3code.server", | |
| 458 | + ], | |
| 459 | + ); | |
| 460 | + | |
| 461 | + const plist = yield* fs.readFileString(launchAgentPath); | |
| 462 | + assert.include(plist, `<string>${plan.t3EntryPath}</string>`); | |
| 463 | + assert.notInclude(plist, dirs.stableEntry); | |
| 464 | + | |
| 465 | + // The recording npm runner does not materialize files, so complete the | |
| 466 | + // managed runtime before checking the current-state contract. | |
| 467 | + yield* fs.makeDirectory(path.dirname(path.join(runtimeDir, "node_modules/t3/dist/bin.mjs")), { | |
| 468 | + recursive: true, | |
| 469 | + }); | |
| 470 | + yield* fs.writeFileString( | |
| 471 | + path.join(runtimeDir, "node_modules", "t3", "dist", "bin.mjs"), | |
| 472 | + "#!/usr/bin/env node\n", | |
| 473 | + ); | |
| 474 | + const status = yield* service.status; | |
| 475 | + assert.isTrue(status.supported); | |
| 476 | + assert.isTrue(status.installed); | |
| 477 | + assert.isTrue(status.current); | |
| 478 | + | |
| 479 | + assert.isTrue(yield* service.uninstall); | |
| 480 | + assert.isFalse(yield* fs.exists(launchAgentPath)); | |
| 481 | + }), | |
| 482 | + ); | |
| 483 | + | |
| 484 | + it.effect("restores the previous macOS runtime when LaunchAgent activation fails", () => | |
| 485 | + Effect.gen(function* () { | |
| 486 | + const { dirs, fs, path } = yield* makeTestContext(); | |
| 487 | + const initialCommands: Array<RecordedCommand> = []; | |
| 488 | + const initialService = yield* BootService.make({ | |
| 489 | + baseDir: dirs.baseDir, | |
| 490 | + logsDir: dirs.logsDir, | |
| 491 | + cliVersion: "0.0.27", | |
| 492 | + host: makeHost(dirs.stableEntry), | |
| 493 | + }).pipe( | |
| 494 | + Effect.provide(makeRecordingRunnerLayer(initialCommands)), | |
| 495 | + provideHostRefs(dirs.home, "darwin"), | |
| 496 | + ); | |
| 497 | + yield* initialService.install; | |
| 498 | + | |
| 499 | + const activeRuntimeLink = path.join(dirs.baseDir, "runtime", "service", "current"); | |
| 500 | + const previousRuntime = yield* fs.readLink(activeRuntimeLink); | |
| 501 | + const launchAgentPath = path.join( | |
| 502 | + dirs.home, | |
| 503 | + "Library", | |
| 504 | + "LaunchAgents", | |
| 505 | + "com.t3tools.t3code.server.plist", | |
| 506 | + ); | |
| 507 | + const previousLaunchAgent = yield* fs.readFileString(launchAgentPath); | |
| 508 | + const repairCommands: Array<RecordedCommand> = []; | |
| 509 | + const repairService = yield* BootService.make({ | |
| 510 | + baseDir: dirs.baseDir, | |
| 511 | + logsDir: dirs.logsDir, | |
| 512 | + cliVersion: "0.0.28", | |
| 513 | + host: makeHost(dirs.stableEntry), | |
| 514 | + }).pipe( | |
| 515 | + Effect.provide( | |
| 516 | + makeRecordingRunnerLayer(repairCommands, { | |
| 517 | + failWhen: (command, args) => command === "/bin/launchctl" && args.includes("bootstrap"), | |
| 518 | + }), | |
| 519 | + ), | |
| 520 | + provideHostRefs(dirs.home, "darwin"), | |
| 521 | + ); | |
| 522 | + | |
| 523 | + const error = yield* repairService.install.pipe(Effect.flip); | |
| 524 | + | |
| 525 | + assert.isTrue(isCommandError(error)); | |
| 526 | + assert.equal(yield* fs.readLink(activeRuntimeLink), previousRuntime); | |
| 527 | + assert.equal(yield* fs.readFileString(launchAgentPath), previousLaunchAgent); | |
| 528 | + }), | |
| 529 | + ); | |
| 530 | + | |
| 531 | + it.effect("fails on unsupported platforms without touching the filesystem", () => | |
| 532 | + Effect.gen(function* () { | |
| 533 | + const { dirs, fs, path } = yield* makeTestContext(); | |
| 534 | + const commands: Array<RecordedCommand> = []; | |
| 535 | + const service = yield* BootService.make({ | |
| 536 | + baseDir: dirs.baseDir, | |
| 537 | + logsDir: dirs.logsDir, | |
| 538 | + cliVersion: "0.0.27", | |
| 539 | + host: makeHost("/usr/local/lib/node_modules/t3/dist/bin.mjs"), | |
| 540 | + }).pipe( | |
| 541 | + Effect.provide(makeRecordingRunnerLayer(commands)), | |
| 542 | + provideHostRefs(dirs.home, "win32"), | |
| 543 | + ); | |
| 544 | + | |
| 405 | 545 | const error = yield* service.install.pipe(Effect.flip); |
| 406 | 546 | assert.isTrue(isUnsupportedError(error)); |
| 407 | 547 | assert.lengthOf(commands, 0); |