Showing the changes to apps/server/src/cloud/selfUpdate.test.ts (1 of 12 files). Show all
Unified · Split
| @@ -13,13 +13,17 @@ import { · 12 unmodified lines | ||
| 13 | 13 | HostProcessEnvironment, |
| 14 | 14 | HostProcessExecutablePath, |
| 15 | 15 | HostProcessPlatform, |
| 16 | + HostProcessUserId, | |
| 16 | 17 | } from "@t3tools/shared/hostProcess"; |
| 17 | 18 | |
| 18 | 19 | import * as ServerConfig from "../config.ts"; |
| 19 | 20 | import * as ProcessRunner from "../processRunner.ts"; |
| 20 | 21 | import { |
| 22 | + BOOT_SERVICE_LAUNCH_AGENT_LABEL, | |
| 21 | 23 | BOOT_SERVICE_UNIT_ENV, |
| 22 | 24 | BOOT_SERVICE_UNIT_FILE, |
| 25 | + launchAgentPaths, | |
| 26 | + renderBootServiceLaunchAgent, | |
| 23 | 27 | renderBootServiceUnit, |
| 24 | 28 | } from "./bootService.ts"; |
| 25 | 29 | import * as SelfUpdate from "./selfUpdate.ts"; |
| @@ -76,6 +80,7 @@ const provideHostRefs = (input: { · 50 unmodified lines | ||
| 76 | 80 | Layer.succeed(HostProcessEnvironment, input.env), |
| 77 | 81 | Layer.succeed(HostProcessExecutablePath, NODE_PATH), |
| 78 | 82 | Layer.succeed(HostProcessArguments, [NODE_PATH, input.entryPath, "serve"]), |
| 83 | + Layer.succeed(HostProcessUserId, 501), | |
| 79 | 84 | ), |
| 80 | 85 | ); |
| 81 | 86 | |
| @@ -192,6 +197,41 @@ it.layer(NodeServices.layer)("resolveServerSelfUpdateCapability", (it) => { · 110 unmodified lines | ||
| 192 | 197 | }), |
| 193 | 198 | ); |
| 194 | 199 | |
| 200 | + it.effect("reports boot-service for the T3-managed macOS LaunchAgent", () => | |
| 201 | + Effect.gen(function* () { | |
| 202 | + const { fs, home, path } = yield* makeHome(); | |
| 203 | + const baseDir = path.join(home, ".t3"); | |
| 204 | + const launchAgent = launchAgentPaths(path, home, baseDir, 501); | |
| 205 | + yield* fs.makeDirectory(path.dirname(launchAgent.unitPath), { recursive: true }); | |
| 206 | + yield* fs.writeFileString( | |
| 207 | + launchAgent.unitPath, | |
| 208 | + renderBootServiceLaunchAgent({ | |
| 209 | + nodePath: NODE_PATH, | |
| 210 | + t3EntryPath: launchAgent.activeEntryPath, | |
| 211 | + baseDir, | |
| 212 | + logPath: path.join(baseDir, "userdata", "logs", "boot-service.log"), | |
| 213 | + unitPath: launchAgent.unitPath, | |
| 214 | + }), | |
| 215 | + ); | |
| 216 | + | |
| 217 | + const method = yield* SelfUpdate.resolveServerSelfUpdateCapability({ | |
| 218 | + desktopManaged: false, | |
| 219 | + }).pipe( | |
| 220 | + provideHostRefs({ | |
| 221 | + platform: "darwin", | |
| 222 | + env: { | |
| 223 | + HOME: home, | |
| 224 | + T3CODE_HOME: baseDir, | |
| 225 | + [BOOT_SERVICE_UNIT_ENV]: BOOT_SERVICE_LAUNCH_AGENT_LABEL, | |
| 226 | + }, | |
| 227 | + entryPath: launchAgent.activeEntryPath, | |
| 228 | + }), | |
| 229 | + ); | |
| 230 | + | |
| 231 | + assert.equal(method, "boot-service"); | |
| 232 | + }), | |
| 233 | + ); | |
| 234 | + | |
| 195 | 235 | it.effect("reports desktop-managed for desktop-supervised backends", () => |
| 196 | 236 | Effect.gen(function* () { |
| 197 | 237 | const { home, path } = yield* makeHome(); |
| @@ -262,11 +302,21 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 64 unmodified lines | ||
| 262 | 302 | const path = yield* Path.Path; |
| 263 | 303 | const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-self-update-test-" }); |
| 264 | 304 | const baseDir = path.join(home, ".t3"); |
| 305 | + const platform = options?.platform ?? "linux"; | |
| 306 | + const launchdBootService = options?.bootService === true && platform === "darwin"; | |
| 307 | + const launchAgent = launchAgentPaths(path, home, baseDir, 501); | |
| 265 | 308 | const entryPath = |
| 266 | 309 | options?.entryPath ?? |
| 267 | − path.join(home, ".t3/runtime/versions/0.0.28/node_modules/t3/dist/bin.mjs"); | |
| 268 | − const env: NodeJS.ProcessEnv = | |
| 269 | − options?.bootService === true | |
| 310 | + (launchdBootService | |
| 311 | + ? launchAgent.activeEntryPath | |
| 312 | + : path.join(home, ".t3/runtime/versions/0.0.28/node_modules/t3/dist/bin.mjs")); | |
| 313 | + const env: NodeJS.ProcessEnv = launchdBootService | |
| 314 | + ? { | |
| 315 | + HOME: home, | |
| 316 | + T3CODE_HOME: baseDir, | |
| 317 | + [BOOT_SERVICE_UNIT_ENV]: BOOT_SERVICE_LAUNCH_AGENT_LABEL, | |
| 318 | + } | |
| 319 | + : options?.bootService === true | |
| 270 | 320 | ? { |
| 271 | 321 | HOME: home, |
| 272 | 322 | INVOCATION_ID: "abc123", |
| @@ -274,18 +324,37 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 1 unmodified line | ||
| 274 | 324 | } |
| 275 | 325 | : { HOME: home }; |
| 276 | 326 | if (options?.bootService === true) { |
| 277 | − const unitDir = path.join(home, ".config", "systemd", "user"); | |
| 278 | − yield* fs.makeDirectory(unitDir, { recursive: true }); | |
| 279 | − yield* fs.writeFileString( | |
| 280 | − path.join(unitDir, "t3code.service"), | |
| 281 | − renderBootServiceUnit({ | |
| 282 | − nodePath: NODE_PATH, | |
| 283 | − t3EntryPath: entryPath, | |
| 284 | − baseDir, | |
| 285 | − logPath: path.join(baseDir, "userdata", "logs", "boot-service.log"), | |
| 286 | − unitPath: path.join(unitDir, "t3code.service"), | |
| 287 | − }), | |
| 288 | − ); | |
| 327 | + if (launchdBootService) { | |
| 328 | + const previousRuntime = path.join(baseDir, "runtime", "versions", "0.0.28"); | |
| 329 | + yield* fs.makeDirectory(path.dirname(launchAgent.unitPath), { recursive: true }); | |
| 330 | + yield* fs.makeDirectory(path.dirname(launchAgent.activeRuntimeLinkPath), { | |
| 331 | + recursive: true, | |
| 332 | + }); | |
| 333 | + yield* fs.symlink(previousRuntime, launchAgent.activeRuntimeLinkPath); | |
| 334 | + yield* fs.writeFileString( | |
| 335 | + launchAgent.unitPath, | |
| 336 | + renderBootServiceLaunchAgent({ | |
| 337 | + nodePath: NODE_PATH, | |
| 338 | + t3EntryPath: entryPath, | |
| 339 | + baseDir, | |
| 340 | + logPath: path.join(baseDir, "userdata", "logs", "boot-service.log"), | |
| 341 | + unitPath: launchAgent.unitPath, | |
| 342 | + }), | |
| 343 | + ); | |
| 344 | + } else { | |
| 345 | + const unitDir = path.join(home, ".config", "systemd", "user"); | |
| 346 | + yield* fs.makeDirectory(unitDir, { recursive: true }); | |
| 347 | + yield* fs.writeFileString( | |
| 348 | + path.join(unitDir, "t3code.service"), | |
| 349 | + renderBootServiceUnit({ | |
| 350 | + nodePath: NODE_PATH, | |
| 351 | + t3EntryPath: entryPath, | |
| 352 | + baseDir, | |
| 353 | + logPath: path.join(baseDir, "userdata", "logs", "boot-service.log"), | |
| 354 | + unitPath: path.join(unitDir, "t3code.service"), | |
| 355 | + }), | |
| 356 | + ); | |
| 357 | + } | |
| 289 | 358 | } |
| 290 | 359 | |
| 291 | 360 | const commands: Array<RecordedCommand> = []; |
| @@ -333,7 +402,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 41 unmodified lines | ||
| 333 | 402 | configLayer, |
| 334 | 403 | ), |
| 335 | 404 | ), |
| 336 | − provideHostRefs({ platform: options?.platform ?? "linux", env, entryPath }), | |
| 405 | + provideHostRefs({ platform, env, entryPath }), | |
| 337 | 406 | ); |
| 338 | 407 | return { |
| 339 | 408 | fs, |
| @@ -341,6 +410,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 1 unmodified line | ||
| 341 | 410 | home, |
| 342 | 411 | baseDir, |
| 343 | 412 | entryPath, |
| 413 | + launchAgent, | |
| 344 | 414 | commands, |
| 345 | 415 | spawns, |
| 346 | 416 | exitCount: () => exited, |
| @@ -520,6 +590,53 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => { · 173 unmodified lines | ||
| 520 | 590 | }).pipe(Effect.provide(TestClock.layer())), |
| 521 | 591 | ); |
| 522 | 592 | |
| 593 | + it.effect("switches the macOS LaunchAgent runtime and restarts it", () => | |
| 594 | + Effect.gen(function* () { | |
| 595 | + const context = yield* makeContext({ | |
| 596 | + platform: "darwin", | |
| 597 | + bootService: true, | |
| 598 | + }); | |
| 599 | + | |
| 600 | + const result = yield* context.service.update({ targetVersion: "0.0.29" }); | |
| 601 | + | |
| 602 | + assert.deepEqual(result, { targetVersion: "0.0.29", method: "boot-service" }); | |
| 603 | + assert.equal( | |
| 604 | + yield* context.fs.readLink(context.launchAgent.activeRuntimeLinkPath), | |
| 605 | + context.path.join(context.baseDir, "runtime", "versions", "0.0.29"), | |
| 606 | + ); | |
| 607 | + assert.deepEqual( | |
| 608 | + context.commands.map((entry) => entry.command), | |
| 609 | + ["npm", NODE_PATH, "/bin/launchctl"], | |
| 610 | + ); | |
| 611 | + assert.deepEqual(context.commands[2], { | |
| 612 | + command: "/bin/launchctl", | |
| 613 | + args: ["kickstart", "-k", "gui/501/com.t3tools.t3code.server"], | |
| 614 | + }); | |
| 615 | + assert.lengthOf(context.spawns, 0); | |
| 616 | + assert.equal(context.exitCount(), 0); | |
| 617 | + }).pipe(Effect.provide(TestClock.layer())), | |
| 618 | + ); | |
| 619 | + | |
| 620 | + it.effect("restores the previous macOS runtime when launchd rejects the restart", () => | |
| 621 | + Effect.gen(function* () { | |
| 622 | + const context = yield* makeContext({ | |
| 623 | + platform: "darwin", | |
| 624 | + bootService: true, | |
| 625 | + failWhen: (command) => command === "/bin/launchctl", | |
| 626 | + }); | |
| 627 | + const previousRuntime = yield* context.fs.readLink(context.launchAgent.activeRuntimeLinkPath); | |
| 628 | + | |
| 629 | + const error = yield* context.service.update({ targetVersion: "0.0.29" }).pipe(Effect.flip); | |
| 630 | + | |
| 631 | + assert.include(error.reason, "Restarting the T3 Code LaunchAgent failed"); | |
| 632 | + assert.equal( | |
| 633 | + yield* context.fs.readLink(context.launchAgent.activeRuntimeLinkPath), | |
| 634 | + previousRuntime, | |
| 635 | + ); | |
| 636 | + assert.lengthOf(context.spawns, 0); | |
| 637 | + }).pipe(Effect.provide(TestClock.layer())), | |
| 638 | + ); | |
| 639 | + | |
| 523 | 640 | it.effect("restores the previous unit and permits a retry when systemd restart fails", () => |
| 524 | 641 | Effect.gen(function* () { |
| 525 | 642 | let failRestart = true; |