| 1 | // @effect-diagnostics-next-line nodeBuiltinImport:off - observes the children the launcher spawns with Node built-ins. |
| 2 | import type * as NodeChildProcess from "node:child_process"; |
| 3 | |
| 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | import { assert, it } from "@effect/vitest"; |
| 6 | import { SERVER_EXIT_CODE_STATE_DIR_OWNED } from "@t3tools/contracts"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import { vi } from "vite-plus/test"; |
| 11 | |
| 12 | import { |
| 13 | databaseBackupBlocker, |
| 14 | Launcher, |
| 15 | PREPARED_TIMEOUT_MS, |
| 16 | type BackupSpaceProbe, |
| 17 | readServiceState, |
| 18 | writeServiceState, |
| 19 | } from "./serviceLauncher.ts"; |
| 20 | import { |
| 21 | compareExactServiceVersions, |
| 22 | decodeServiceState, |
| 23 | isExactServiceVersion, |
| 24 | SERVICE_LAUNCHER_PROTOCOL, |
| 25 | SERVICE_RESTART_PENDING_FILE, |
| 26 | SERVICE_STOP_MARKER_FILE, |
| 27 | } from "./cloud/serviceProtocol.ts"; |
| 28 | |
| 29 | const spawnedChildren = vi.hoisted((): Array<NodeChildProcess.ChildProcess> => []); |
| 30 | |
| 31 | vi.mock("node:child_process", async (importOriginal) => { |
| 32 | const actual = await importOriginal<typeof NodeChildProcess>(); |
| 33 | const spawn = (...args: Parameters<typeof actual.spawn>) => { |
| 34 | const child = actual.spawn(...args); |
| 35 | spawnedChildren.push(child); |
| 36 | return child; |
| 37 | }; |
| 38 | return { ...actual, spawn: spawn as typeof actual.spawn }; |
| 39 | }); |
| 40 | |
| 41 | const writeSparseFile = (filePath: string, size: number) => |
| 42 | Effect.gen(function* () { |
| 43 | const fileSystem = yield* FileSystem.FileSystem; |
| 44 | yield* fileSystem.writeFileString(filePath, ""); |
| 45 | yield* fileSystem.truncate(filePath, size); |
| 46 | }); |
| 47 | |
| 48 | it("accepts only exact semantic versions", () => { |
| 49 | for (const version of ["0.0.0", "1.2.3", "1.2.3-alpha.1", "1.2.3-0", "1.2.3+001"]) { |
| 50 | assert.isTrue(isExactServiceVersion(version), version); |
| 51 | } |
| 52 | for (const version of ["latest", "01.2.3", "1.2.3-01", "1.2.3-alpha..1", "1.2.3+."]) { |
| 53 | assert.isFalse(isExactServiceVersion(version), version); |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | it("orders exact semantic versions without treating build metadata as precedence", () => { |
| 58 | assert.equal(compareExactServiceVersions("1.2.3", "1.2.3"), 0); |
| 59 | assert.equal(compareExactServiceVersions("1.2.4", "1.2.3"), 1); |
| 60 | assert.equal(compareExactServiceVersions("2.0.0-alpha.1", "2.0.0-alpha.2"), -1); |
| 61 | assert.equal(compareExactServiceVersions("2.0.0-alpha.2", "2.0.0-alpha.beta"), -1); |
| 62 | assert.equal(compareExactServiceVersions("2.0.0-alpha-beta", "2.0.0-alpha-alpha"), 1); |
| 63 | assert.equal(compareExactServiceVersions("2.0.0", "2.0.0-rc.1"), 1); |
| 64 | assert.equal(compareExactServiceVersions("2.0.0+one", "2.0.0+two"), 0); |
| 65 | }); |
| 66 | |
| 67 | it("rejects contradictory service state", () => { |
| 68 | assert.isUndefined( |
| 69 | decodeServiceState({ |
| 70 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 71 | activeVersion: "0.0.31", |
| 72 | update: { |
| 73 | id: "update-1", |
| 74 | fromVersion: "0.0.30", |
| 75 | targetVersion: "0.0.32", |
| 76 | dbPath: "/tmp/state.sqlite", |
| 77 | status: "pending", |
| 78 | }, |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | assert.isUndefined( |
| 83 | decodeServiceState({ |
| 84 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 85 | activeVersion: "1.0.0", |
| 86 | update: { |
| 87 | id: "update-3", |
| 88 | fromVersion: "1.0.0", |
| 89 | targetVersion: "1.1.0", |
| 90 | status: "pending", |
| 91 | }, |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | assert.isUndefined( |
| 96 | decodeServiceState({ |
| 97 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 98 | activeVersion: "1.0.0", |
| 99 | update: { |
| 100 | id: "update-2", |
| 101 | fromVersion: "1.0.0", |
| 102 | targetVersion: "0.9.0", |
| 103 | dbPath: "/tmp/state.sqlite", |
| 104 | status: "pending", |
| 105 | }, |
| 106 | }), |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | // A pinned runtime is an executable at <versionDir>/t3. The tests stand one up |
| 111 | // as a Node shebang script so the launcher spawns it the way it spawns the |
| 112 | // real single-executable, IPC channel included. |
| 113 | const writeFakeRuntime = ( |
| 114 | fs: FileSystem.FileSystem, |
| 115 | path: Path.Path, |
| 116 | versionDir: string, |
| 117 | childSource: string, |
| 118 | ) => |
| 119 | Effect.gen(function* () { |
| 120 | const entryPath = path.join(versionDir, "t3"); |
| 121 | yield* fs.makeDirectory(versionDir, { recursive: true }); |
| 122 | yield* fs.writeFileString(entryPath, `#!${process.execPath}\n${childSource}`); |
| 123 | yield* fs.chmod(entryPath, 0o755); |
| 124 | yield* fs.writeFileString( |
| 125 | path.join(versionDir, ".install-complete"), |
| 126 | `${path.basename(versionDir)}\n`, |
| 127 | ); |
| 128 | return entryPath; |
| 129 | }); |
| 130 | |
| 131 | it.layer(NodeServices.layer)("service state persistence", (it) => { |
| 132 | it.effect("durably replaces and strictly reads one state document", () => |
| 133 | Effect.gen(function* () { |
| 134 | const fs = yield* FileSystem.FileSystem; |
| 135 | const path = yield* Path.Path; |
| 136 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-test-" }); |
| 137 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 138 | const state = { |
| 139 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 140 | activeVersion: "0.0.31", |
| 141 | } as const; |
| 142 | |
| 143 | yield* Effect.promise(() => writeServiceState(statePath, state)); |
| 144 | assert.deepEqual(yield* Effect.promise(() => readServiceState(statePath)), state); |
| 145 | }), |
| 146 | ); |
| 147 | |
| 148 | it.effect("a fresh launcher clears a restart deferred by t3 update", () => |
| 149 | Effect.gen(function* () { |
| 150 | const fs = yield* FileSystem.FileSystem; |
| 151 | const path = yield* Path.Path; |
| 152 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-restart-" }); |
| 153 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 154 | const restartPending = path.join(root, "runtime", SERVICE_RESTART_PENDING_FILE); |
| 155 | yield* writeFakeRuntime( |
| 156 | fs, |
| 157 | path, |
| 158 | path.join(root, "runtime", "versions", "1.0.0"), |
| 159 | "setInterval(() => {}, 1_000);\n", |
| 160 | ); |
| 161 | yield* Effect.promise(() => |
| 162 | writeServiceState(statePath, { |
| 163 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 164 | activeVersion: "1.0.0", |
| 165 | }), |
| 166 | ); |
| 167 | const run = () => |
| 168 | Effect.gen(function* () { |
| 169 | const launcher = new Launcher( |
| 170 | root, |
| 171 | yield* Effect.promise(() => readServiceState(statePath)), |
| 172 | ); |
| 173 | const running = launcher.run(); |
| 174 | yield* Effect.promise(() => launcher.stop("SIGTERM")); |
| 175 | yield* Effect.promise(() => running); |
| 176 | }); |
| 177 | |
| 178 | // A launcher that is still the old version leaves a marker that waits |
| 179 | // for a newer one. |
| 180 | yield* fs.writeFileString(restartPending, "1.0.1\n"); |
| 181 | yield* run(); |
| 182 | assert.isTrue(yield* fs.exists(restartPending)); |
| 183 | |
| 184 | // Whoever restarted the service, the launcher now runs what the unit |
| 185 | // names, so the deferred-restart marker is gone. |
| 186 | yield* fs.writeFileString(restartPending, "1.0.0\n"); |
| 187 | yield* run(); |
| 188 | assert.isFalse(yield* fs.exists(restartPending)); |
| 189 | }), |
| 190 | ); |
| 191 | |
| 192 | it.effect("stays idle instead of exiting when another server owns the T3 home", () => |
| 193 | Effect.gen(function* () { |
| 194 | const fs = yield* FileSystem.FileSystem; |
| 195 | const path = yield* Path.Path; |
| 196 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-refused-" }); |
| 197 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 198 | yield* writeFakeRuntime( |
| 199 | fs, |
| 200 | path, |
| 201 | path.join(root, "runtime", "versions", "1.0.0"), |
| 202 | `process.exit(${SERVER_EXIT_CODE_STATE_DIR_OWNED});\n`, |
| 203 | ); |
| 204 | yield* Effect.promise(() => |
| 205 | writeServiceState(statePath, { |
| 206 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 207 | activeVersion: "1.0.0", |
| 208 | }), |
| 209 | ); |
| 210 | const refused = Promise.withResolvers<void>(); |
| 211 | yield* Effect.acquireRelease( |
| 212 | Effect.sync(() => |
| 213 | vi.spyOn(process.stderr, "write").mockImplementation((chunk) => { |
| 214 | if (String(chunk).includes("owns this T3 home")) refused.resolve(); |
| 215 | return true; |
| 216 | }), |
| 217 | ), |
| 218 | (spy) => Effect.sync(() => spy.mockRestore()), |
| 219 | ); |
| 220 | const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath))); |
| 221 | let completed = false; |
| 222 | const running = launcher.run().finally(() => { |
| 223 | completed = true; |
| 224 | }); |
| 225 | yield* Effect.promise(() => refused.promise); |
| 226 | assert.isFalse(completed); |
| 227 | yield* Effect.promise(() => launcher.stop("SIGTERM")); |
| 228 | yield* Effect.promise(() => running); |
| 229 | }), |
| 230 | ); |
| 231 | |
| 232 | it.effect("serializes shutdown with launcher recovery", () => |
| 233 | Effect.gen(function* () { |
| 234 | const fs = yield* FileSystem.FileSystem; |
| 235 | const path = yield* Path.Path; |
| 236 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-stop-" }); |
| 237 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 238 | yield* writeFakeRuntime( |
| 239 | fs, |
| 240 | path, |
| 241 | path.join(root, "runtime", "versions", "1.0.0"), |
| 242 | "setInterval(() => {}, 1_000);\n", |
| 243 | ); |
| 244 | yield* Effect.promise(() => |
| 245 | writeServiceState(statePath, { |
| 246 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 247 | activeVersion: "1.0.0", |
| 248 | }), |
| 249 | ); |
| 250 | |
| 251 | const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath))); |
| 252 | const running = launcher.run(); |
| 253 | const stopping = launcher.stop("SIGTERM"); |
| 254 | // An explicit stop leaves the marker that tells a child shutting down |
| 255 | // mid-update that no replacement server is coming. It is present as |
| 256 | // soon as stop() returns its promise, before queued transitions run. |
| 257 | assert.isTrue(yield* fs.exists(path.join(root, "runtime", SERVICE_STOP_MARKER_FILE))); |
| 258 | yield* Effect.promise(() => stopping); |
| 259 | yield* Effect.promise(() => running); |
| 260 | }), |
| 261 | ); |
| 262 | |
| 263 | it.effect("commits only after the trial reports prepared", () => |
| 264 | Effect.gen(function* () { |
| 265 | const fs = yield* FileSystem.FileSystem; |
| 266 | const path = yield* Path.Path; |
| 267 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-flow-" }); |
| 268 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 269 | const databasePath = path.join(root, "userdata", "state.sqlite"); |
| 270 | yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); |
| 271 | yield* fs.writeFileString(databasePath, "before trial"); |
| 272 | const encodedDatabasePath = JSON.stringify(databasePath); |
| 273 | const childSource = ` |
| 274 | const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); |
| 275 | if (context.update?.status === "pending") { |
| 276 | process.send({ type: "prepared", updateId: context.update.id }); |
| 277 | process.on("message", (message) => { |
| 278 | if (message.type === "committed") process.exit(0); |
| 279 | }); |
| 280 | } else if (context.update === undefined) { |
| 281 | process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: ${encodedDatabasePath} }); |
| 282 | setInterval(() => {}, 1_000); |
| 283 | } else { |
| 284 | process.exit(0); |
| 285 | } |
| 286 | `; |
| 287 | for (const version of ["1.0.0", "1.1.0"]) { |
| 288 | yield* writeFakeRuntime( |
| 289 | fs, |
| 290 | path, |
| 291 | path.join(root, "runtime", "versions", version), |
| 292 | childSource, |
| 293 | ); |
| 294 | } |
| 295 | yield* Effect.promise(() => |
| 296 | writeServiceState(statePath, { |
| 297 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 298 | activeVersion: "1.0.0", |
| 299 | }), |
| 300 | ); |
| 301 | |
| 302 | const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath))); |
| 303 | yield* Effect.promise(() => |
| 304 | launcher.run().then( |
| 305 | () => Promise.reject(new Error("launcher unexpectedly completed")), |
| 306 | () => Promise.resolve(), |
| 307 | ), |
| 308 | ); |
| 309 | |
| 310 | const state = yield* Effect.promise(() => readServiceState(statePath)); |
| 311 | assert.equal(state.activeVersion, "1.1.0"); |
| 312 | assert.equal(state.update?.status, "committed"); |
| 313 | }), |
| 314 | ); |
| 315 | |
| 316 | it.effect("rolls back a trial that reports the wrong update ID", () => |
| 317 | Effect.gen(function* () { |
| 318 | const fs = yield* FileSystem.FileSystem; |
| 319 | const path = yield* Path.Path; |
| 320 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-rollback-" }); |
| 321 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 322 | const databasePath = path.join(root, "userdata", "state.sqlite"); |
| 323 | yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); |
| 324 | yield* fs.writeFileString(databasePath, "before trial"); |
| 325 | const encodedDatabasePath = JSON.stringify(databasePath); |
| 326 | const childSource = ` |
| 327 | const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); |
| 328 | if (context.update?.status === "pending") { |
| 329 | process.send({ type: "prepared", updateId: "wrong-update" }); |
| 330 | } else if (context.update === undefined) { |
| 331 | process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: ${encodedDatabasePath} }); |
| 332 | setInterval(() => {}, 1_000); |
| 333 | } else { |
| 334 | process.exit(0); |
| 335 | } |
| 336 | `; |
| 337 | for (const version of ["1.0.0", "1.1.0"]) { |
| 338 | yield* writeFakeRuntime( |
| 339 | fs, |
| 340 | path, |
| 341 | path.join(root, "runtime", "versions", version), |
| 342 | childSource, |
| 343 | ); |
| 344 | } |
| 345 | yield* Effect.promise(() => |
| 346 | writeServiceState(statePath, { |
| 347 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 348 | activeVersion: "1.0.0", |
| 349 | }), |
| 350 | ); |
| 351 | |
| 352 | const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath))); |
| 353 | yield* Effect.promise(() => |
| 354 | launcher.run().then( |
| 355 | () => Promise.reject(new Error("launcher unexpectedly completed")), |
| 356 | () => Promise.resolve(), |
| 357 | ), |
| 358 | ); |
| 359 | |
| 360 | const state = yield* Effect.promise(() => readServiceState(statePath)); |
| 361 | assert.equal(state.activeVersion, "1.0.0"); |
| 362 | assert.equal(state.update?.status, "rolled-back"); |
| 363 | assert.equal( |
| 364 | state.update?.status === "rolled-back" ? state.update.reason : undefined, |
| 365 | "invalid-prepared", |
| 366 | ); |
| 367 | }), |
| 368 | ); |
| 369 | |
| 370 | // Migrations on a large database can keep a trial busy for minutes before it |
| 371 | // reports prepared. Only a trial that stays stuck past the window rolls back. |
| 372 | it.effect.each([ |
| 373 | { prepares: true, status: "committed", activeVersion: "1.1.0" }, |
| 374 | { prepares: false, status: "rolled-back", activeVersion: "1.0.0" }, |
| 375 | ] as const)( |
| 376 | "gives a trial the full prepared window (prepares: $prepares)", |
| 377 | ({ prepares, status, activeVersion }) => |
| 378 | Effect.gen(function* () { |
| 379 | const fs = yield* FileSystem.FileSystem; |
| 380 | const path = yield* Path.Path; |
| 381 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-slow-" }); |
| 382 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 383 | const databasePath = path.join(root, "userdata", "state.sqlite"); |
| 384 | yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); |
| 385 | yield* fs.writeFileString(databasePath, "before trial"); |
| 386 | // The trial stands in for slow migrations: it reports prepared only |
| 387 | // when the test tells it to. |
| 388 | const childSource = ` |
| 389 | const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); |
| 390 | if (context.update?.status !== "pending") process.exit(0); |
| 391 | process.on("message", (message) => { |
| 392 | if (message.type === "test-prepare") process.send({ type: "prepared", updateId: context.update.id }); |
| 393 | if (message.type === "committed") process.exit(0); |
| 394 | }); |
| 395 | setInterval(() => {}, 1_000); |
| 396 | `; |
| 397 | for (const version of ["1.0.0", "1.1.0"]) { |
| 398 | yield* writeFakeRuntime( |
| 399 | fs, |
| 400 | path, |
| 401 | path.join(root, "runtime", "versions", version), |
| 402 | childSource, |
| 403 | ); |
| 404 | } |
| 405 | yield* Effect.promise(() => |
| 406 | writeServiceState(statePath, { |
| 407 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 408 | activeVersion: "1.0.0", |
| 409 | update: { |
| 410 | id: "update-1", |
| 411 | fromVersion: "1.0.0", |
| 412 | targetVersion: "1.1.0", |
| 413 | dbPath: databasePath, |
| 414 | status: "pending", |
| 415 | }, |
| 416 | }), |
| 417 | ); |
| 418 | |
| 419 | // Effect schedules on setImmediate, so faking only timeouts leaves it running. |
| 420 | vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); |
| 421 | yield* Effect.addFinalizer(() => Effect.sync(() => vi.useRealTimers())); |
| 422 | const launcher = new Launcher( |
| 423 | root, |
| 424 | yield* Effect.promise(() => readServiceState(statePath)), |
| 425 | ); |
| 426 | // Each run ends when the final active child exits on its own. |
| 427 | const running = launcher.run().then( |
| 428 | () => Promise.reject(new Error("launcher unexpectedly completed")), |
| 429 | () => Promise.resolve(), |
| 430 | ); |
| 431 | // The prepared timer is armed once the trial has spawned. |
| 432 | yield* Effect.promise(() => |
| 433 | vi.waitFor(() => assert.equal(vi.getTimerCount(), 1), { timeout: 10_000 }), |
| 434 | ); |
| 435 | yield* Effect.promise(() => vi.advanceTimersByTimeAsync(121_000)); |
| 436 | if (prepares) spawnedChildren.at(-1)!.send({ type: "test-prepare" }); |
| 437 | else yield* Effect.promise(() => vi.advanceTimersByTimeAsync(PREPARED_TIMEOUT_MS)); |
| 438 | yield* Effect.promise(() => running); |
| 439 | |
| 440 | const state = yield* Effect.promise(() => readServiceState(statePath)); |
| 441 | assert.equal(state.activeVersion, activeVersion); |
| 442 | assert.equal(state.update?.status, status); |
| 443 | if (!prepares) { |
| 444 | assert.equal( |
| 445 | state.update?.status === "rolled-back" ? state.update.reason : undefined, |
| 446 | "prepared-timeout", |
| 447 | ); |
| 448 | } |
| 449 | }), |
| 450 | ); |
| 451 | |
| 452 | it.effect.each(["file", "symlink", "dangling symlink"] as const)( |
| 453 | "restores the database when a migrating trial exits, with the database a %s", |
| 454 | (kind) => |
| 455 | Effect.gen(function* () { |
| 456 | const fs = yield* FileSystem.FileSystem; |
| 457 | const path = yield* Path.Path; |
| 458 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-db-" }); |
| 459 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 460 | const databasePath = path.join(root, "userdata", "state.sqlite"); |
| 461 | // A symlinked database keeps its link: rollback restores the file it points to, |
| 462 | // even one the trial left missing, as a crash during a macOS clone restore can. |
| 463 | // The dangling case reaches its file through a directory link and a relative |
| 464 | // `..` link, which only resolves correctly by following the directory link. |
| 465 | const databaseFile = |
| 466 | kind === "file" ? databasePath : path.join(root, "data", "original.sqlite"); |
| 467 | const linkTarget = kind === "dangling symlink" ? "../original.sqlite" : databaseFile; |
| 468 | const original = "database before migration"; |
| 469 | yield* fs.makeDirectory(path.dirname(databaseFile), { recursive: true }); |
| 470 | if (kind === "dangling symlink") { |
| 471 | yield* fs.makeDirectory(path.join(root, "data", "sub")); |
| 472 | yield* fs.symlink(path.join(root, "data", "sub"), path.dirname(databasePath)); |
| 473 | } else { |
| 474 | yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); |
| 475 | } |
| 476 | yield* fs.writeFileString(databaseFile, original); |
| 477 | if (kind !== "file") yield* fs.symlink(linkTarget, databasePath); |
| 478 | const encodedDatabasePath = JSON.stringify(databasePath); |
| 479 | const childSource = ` |
| 480 | import { realpathSync, unlinkSync, writeFileSync } from "node:fs"; |
| 481 | const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); |
| 482 | if (context.update?.status === "pending") { |
| 483 | writeFileSync(context.update.dbPath, "database after migration"); |
| 484 | writeFileSync(context.update.dbPath + "-wal", "trial wal"); |
| 485 | writeFileSync(context.update.dbPath + "-shm", "trial shm"); |
| 486 | ${kind === "dangling symlink" ? "unlinkSync(realpathSync(context.update.dbPath));" : ""} |
| 487 | process.exit(1); |
| 488 | } else if (context.update === undefined) { |
| 489 | process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: ${encodedDatabasePath} }); |
| 490 | setInterval(() => {}, 1_000); |
| 491 | } else { |
| 492 | process.exit(0); |
| 493 | } |
| 494 | `; |
| 495 | for (const version of ["1.0.0", "1.1.0"]) { |
| 496 | yield* writeFakeRuntime( |
| 497 | fs, |
| 498 | path, |
| 499 | path.join(root, "runtime", "versions", version), |
| 500 | childSource, |
| 501 | ); |
| 502 | } |
| 503 | yield* Effect.promise(() => |
| 504 | writeServiceState(statePath, { |
| 505 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 506 | activeVersion: "1.0.0", |
| 507 | }), |
| 508 | ); |
| 509 | |
| 510 | const launcher = new Launcher( |
| 511 | root, |
| 512 | yield* Effect.promise(() => readServiceState(statePath)), |
| 513 | ); |
| 514 | yield* Effect.promise(() => |
| 515 | launcher.run().then( |
| 516 | () => Promise.reject(new Error("launcher unexpectedly completed")), |
| 517 | () => Promise.resolve(), |
| 518 | ), |
| 519 | ); |
| 520 | |
| 521 | const state = yield* Effect.promise(() => readServiceState(statePath)); |
| 522 | assert.equal(state.activeVersion, "1.0.0"); |
| 523 | assert.equal(state.update?.status, "rolled-back"); |
| 524 | assert.equal(yield* fs.readFileString(databaseFile), original); |
| 525 | if (kind !== "file") assert.equal(yield* fs.readLink(databasePath), linkTarget); |
| 526 | assert.isFalse(yield* fs.exists(path.join(root, "original.sqlite"))); |
| 527 | assert.isFalse(yield* fs.exists(`${databasePath}-wal`)); |
| 528 | assert.isFalse(yield* fs.exists(`${databasePath}-shm`)); |
| 529 | const updateId = state.update?.id; |
| 530 | assert.isDefined(updateId); |
| 531 | assert.isFalse(yield* fs.exists(path.join(root, "runtime", "db-backup", updateId))); |
| 532 | }), |
| 533 | ); |
| 534 | |
| 535 | const requestRejectedUpdate = (backupSpaceProbe: Partial<BackupSpaceProbe>) => |
| 536 | Effect.gen(function* () { |
| 537 | const fs = yield* FileSystem.FileSystem; |
| 538 | const path = yield* Path.Path; |
| 539 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-space-" }); |
| 540 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 541 | const databasePath = path.join(root, "userdata", "state.sqlite"); |
| 542 | const replyPath = path.join(root, "rejection.txt"); |
| 543 | const trialMarker = path.join(root, "trial-started"); |
| 544 | yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); |
| 545 | yield* fs.writeFileString(databasePath, "database"); |
| 546 | const encoded = JSON.stringify({ databasePath, replyPath, trialMarker }); |
| 547 | const childSource = ` |
| 548 | import { writeFileSync } from "node:fs"; |
| 549 | const paths = ${encoded}; |
| 550 | const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); |
| 551 | if (context.update?.status === "pending") { |
| 552 | writeFileSync(paths.trialMarker, "started"); |
| 553 | process.exit(1); |
| 554 | } else if (context.update === undefined) { |
| 555 | process.on("message", (message) => { |
| 556 | if (message.type === "update-rejected") { |
| 557 | writeFileSync(paths.replyPath, message.reason); |
| 558 | process.exit(0); |
| 559 | } |
| 560 | }); |
| 561 | process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: paths.databasePath }); |
| 562 | setInterval(() => {}, 1_000); |
| 563 | } else { |
| 564 | process.exit(0); |
| 565 | } |
| 566 | `; |
| 567 | for (const version of ["1.0.0", "1.1.0"]) { |
| 568 | yield* writeFakeRuntime( |
| 569 | fs, |
| 570 | path, |
| 571 | path.join(root, "runtime", "versions", version), |
| 572 | childSource, |
| 573 | ); |
| 574 | } |
| 575 | yield* Effect.promise(() => |
| 576 | writeServiceState(statePath, { |
| 577 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 578 | activeVersion: "1.0.0", |
| 579 | }), |
| 580 | ); |
| 581 | |
| 582 | const launcher = new Launcher( |
| 583 | root, |
| 584 | yield* Effect.promise(() => readServiceState(statePath)), |
| 585 | { backupSpaceProbe }, |
| 586 | ); |
| 587 | yield* Effect.promise(() => |
| 588 | launcher.run().then( |
| 589 | () => Promise.reject(new Error("launcher unexpectedly completed")), |
| 590 | () => Promise.resolve(), |
| 591 | ), |
| 592 | ); |
| 593 | |
| 594 | return { |
| 595 | reason: yield* fs.readFileString(replyPath), |
| 596 | trialStarted: yield* fs.exists(trialMarker), |
| 597 | state: yield* Effect.promise(() => readServiceState(statePath)), |
| 598 | }; |
| 599 | }); |
| 600 | |
| 601 | it.effect("refuses a backup that cannot fit and leaves the server running", () => |
| 602 | Effect.gen(function* () { |
| 603 | const { reason, trialStarted, state } = yield* requestRejectedUpdate({ |
| 604 | readFreeBytes: () => Promise.resolve(0), |
| 605 | canClone: () => Promise.resolve(true), |
| 606 | }); |
| 607 | |
| 608 | assert.include(reason, "Not enough free disk space"); |
| 609 | assert.include(reason, "The server was not stopped"); |
| 610 | assert.isFalse(trialStarted); |
| 611 | assert.equal(state.activeVersion, "1.0.0"); |
| 612 | assert.isUndefined(state.update); |
| 613 | }), |
| 614 | ); |
| 615 | |
| 616 | it.effect("refuses the update when free space cannot be checked", () => |
| 617 | Effect.gen(function* () { |
| 618 | const { reason, trialStarted, state } = yield* requestRejectedUpdate({ |
| 619 | readFreeBytes: () => Promise.reject(new Error("statfs unavailable")), |
| 620 | }); |
| 621 | |
| 622 | assert.include( |
| 623 | reason, |
| 624 | "Could not check that the database backup fits before updating: statfs unavailable", |
| 625 | ); |
| 626 | assert.isFalse(trialStarted); |
| 627 | assert.equal(state.activeVersion, "1.0.0"); |
| 628 | assert.isUndefined(state.update); |
| 629 | }), |
| 630 | ); |
| 631 | |
| 632 | it.effect("records no update when the server exits during the backup check", () => |
| 633 | Effect.gen(function* () { |
| 634 | const fs = yield* FileSystem.FileSystem; |
| 635 | const path = yield* Path.Path; |
| 636 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-exit-" }); |
| 637 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 638 | const databasePath = path.join(root, "userdata", "state.sqlite"); |
| 639 | yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); |
| 640 | yield* fs.writeFileString(databasePath, "database"); |
| 641 | const encoded = JSON.stringify({ databasePath }); |
| 642 | const childSource = ` |
| 643 | const paths = ${encoded}; |
| 644 | process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: paths.databasePath }, () => |
| 645 | process.exit(3), |
| 646 | ); |
| 647 | setInterval(() => {}, 1_000); |
| 648 | `; |
| 649 | for (const version of ["1.0.0", "1.1.0"]) { |
| 650 | yield* writeFakeRuntime( |
| 651 | fs, |
| 652 | path, |
| 653 | path.join(root, "runtime", "versions", version), |
| 654 | childSource, |
| 655 | ); |
| 656 | } |
| 657 | yield* Effect.promise(() => |
| 658 | writeServiceState(statePath, { |
| 659 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 660 | activeVersion: "1.0.0", |
| 661 | }), |
| 662 | ); |
| 663 | const launcher = new Launcher( |
| 664 | root, |
| 665 | yield* Effect.promise(() => readServiceState(statePath)), |
| 666 | { |
| 667 | backupSpaceProbe: { |
| 668 | // Answer only once the server that asked has disconnected. |
| 669 | readFreeBytes: () => { |
| 670 | const child = spawnedChildren.at(-1)!; |
| 671 | return new Promise((resolve) => { |
| 672 | if (!child.connected) resolve(Number.MAX_SAFE_INTEGER); |
| 673 | else child.once("disconnect", () => resolve(Number.MAX_SAFE_INTEGER)); |
| 674 | }); |
| 675 | }, |
| 676 | }, |
| 677 | }, |
| 678 | ); |
| 679 | const failure = yield* Effect.promise(() => |
| 680 | launcher.run().then( |
| 681 | () => new Error("launcher unexpectedly completed"), |
| 682 | (cause: unknown) => cause as Error, |
| 683 | ), |
| 684 | ); |
| 685 | |
| 686 | assert.include(failure.message, "Active child exited unexpectedly (3)"); |
| 687 | const state = yield* Effect.promise(() => readServiceState(statePath)); |
| 688 | assert.equal(state.activeVersion, "1.0.0"); |
| 689 | assert.isUndefined(state.update); |
| 690 | }), |
| 691 | ); |
| 692 | |
| 693 | it.effect("checks database backup space against clone or full-copy needs", () => |
| 694 | Effect.gen(function* () { |
| 695 | const fs = yield* FileSystem.FileSystem; |
| 696 | const path = yield* Path.Path; |
| 697 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-blocker-" }); |
| 698 | const databasePath = path.join(root, "state.sqlite"); |
| 699 | yield* writeSparseFile(databasePath, 10 * 1024 ** 3); |
| 700 | const check = (free: number, cloneable: boolean) => |
| 701 | Effect.promise(() => |
| 702 | databaseBackupBlocker(root, databasePath, { |
| 703 | readFreeBytes: () => Promise.resolve(free), |
| 704 | canClone: () => Promise.resolve(cloneable), |
| 705 | }), |
| 706 | ); |
| 707 | |
| 708 | // A full copy fits: no clone needed. |
| 709 | assert.isUndefined(yield* check(11 * 1024 ** 3, false)); |
| 710 | // A clone needs 10% headroom for the trial's writes, at least 1 GiB. |
| 711 | assert.isUndefined(yield* check(1.5 * 1024 ** 3, true)); |
| 712 | assert.include((yield* check(0.5 * 1024 ** 3, true)) ?? "", "1.0 GB needed, 0.5 GB free"); |
| 713 | // A filesystem that cannot clone needs room for the full copy. |
| 714 | const blocker = yield* check(8 * 1024 ** 3, false); |
| 715 | assert.include(blocker ?? "", "Not enough free disk space"); |
| 716 | assert.include(blocker ?? "", "10.5 GB needed, 8.0 GB free"); |
| 717 | assert.include(blocker ?? "", "The server was not stopped"); |
| 718 | }), |
| 719 | ); |
| 720 | |
| 721 | it.effect("fails the backup check when the database cannot be inspected", () => |
| 722 | Effect.gen(function* () { |
| 723 | const fs = yield* FileSystem.FileSystem; |
| 724 | const path = yield* Path.Path; |
| 725 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-blocker-" }); |
| 726 | const error = yield* Effect.promise(() => |
| 727 | databaseBackupBlocker(root, path.join(root, "missing.sqlite"), { |
| 728 | readFreeBytes: () => Promise.resolve(0), |
| 729 | }).then( |
| 730 | () => undefined, |
| 731 | (cause: unknown) => cause, |
| 732 | ), |
| 733 | ); |
| 734 | assert.equal((error as NodeJS.ErrnoException | undefined)?.code, "ENOENT"); |
| 735 | }), |
| 736 | ); |
| 737 | |
| 738 | it.effect("recognizes a clone into the backup directory on this filesystem", () => |
| 739 | Effect.gen(function* () { |
| 740 | const fs = yield* FileSystem.FileSystem; |
| 741 | const path = yield* Path.Path; |
| 742 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-clone-" }); |
| 743 | const databasePath = path.join(root, "userdata", "state.sqlite"); |
| 744 | yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); |
| 745 | yield* fs.writeFileString(databasePath, "database"); |
| 746 | // With no free space, the result shows which requirement applied. |
| 747 | const blocker = yield* Effect.promise(() => |
| 748 | databaseBackupBlocker(root, databasePath, { readFreeBytes: () => Promise.resolve(0) }), |
| 749 | ); |
| 750 | // A tiny database: 1.0 GB is the clone minimum, 0.0 GB the full copy. The |
| 751 | // macOS temp directory is on APFS, so the clone requirement applies there. |
| 752 | assert.include( |
| 753 | blocker ?? "", |
| 754 | // oxlint-disable-next-line t3code/no-global-process-runtime -- mirrors the standalone launcher's own platform check. |
| 755 | process.platform === "darwin" ? "1.0 GB needed, 0.0 GB free" : "GB needed, 0.0 GB free", |
| 756 | ); |
| 757 | // Linux probes leave nothing behind in the backup directory. |
| 758 | assert.deepEqual(yield* fs.readDirectory(path.join(root, "runtime", "db-backup")), []); |
| 759 | }), |
| 760 | ); |
| 761 | }); |