| ... | @@ -1,12 +1,22 @@ | ... | @@ -1,12 +1,22 @@ |
| | 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 | |
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; | 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, it } from "@effect/vitest"; | 5 | import { assert, it } from "@effect/vitest"; |
| 3 | import { SERVER_EXIT_CODE_STATE_DIR_OWNED } from "@t3tools/contracts"; | 6 | import { SERVER_EXIT_CODE_STATE_DIR_OWNED } from "@t3tools/contracts"; |
| 4 | import { vi } from "vite-plus/test"; | | |
| 5 | import * as Effect from "effect/Effect"; | 7 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; | 8 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Path from "effect/Path"; | 9 | import * as Path from "effect/Path"; |
| | 10 | import { vi } from "vite-plus/test"; |
| 8 | | 11 | |
| 9 | import { Launcher, readServiceState, writeServiceState } from "./serviceLauncher.ts"; | 12 | import { |
| | 13 | databaseBackupBlocker, |
| | 14 | Launcher, |
| | 15 | PREPARED_TIMEOUT_MS, |
| | 16 | type BackupSpaceProbe, |
| | 17 | readServiceState, |
| | 18 | writeServiceState, |
| | 19 | } from "./serviceLauncher.ts"; |
| 10 | import { | 20 | import { |
| 11 | compareExactServiceVersions, | 21 | compareExactServiceVersions, |
| 12 | decodeServiceState, | 22 | decodeServiceState, |
| ... | @@ -16,6 +26,25 @@ import { | ... | @@ -16,6 +26,25 @@ import { |
| 16 | SERVICE_STOP_MARKER_FILE, | 26 | SERVICE_STOP_MARKER_FILE, |
| 17 | } from "./cloud/serviceProtocol.ts"; | 27 | } from "./cloud/serviceProtocol.ts"; |
| 18 | | 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 | |
| 19 | it("accepts only exact semantic versions", () => { | 48 | it("accepts only exact semantic versions", () => { |
| 20 | for (const version of ["0.0.0", "1.2.3", "1.2.3-alpha.1", "1.2.3-0", "1.2.3+001"]) { | 49 | for (const version of ["0.0.0", "1.2.3", "1.2.3-alpha.1", "1.2.3-0", "1.2.3+001"]) { |
| 21 | assert.isTrue(isExactServiceVersion(version), version); | 50 | assert.isTrue(isExactServiceVersion(version), version); |
| ... | @@ -338,27 +367,198 @@ if (context.update?.status === "pending") { | ... | @@ -338,27 +367,198 @@ if (context.update?.status === "pending") { |
| 338 | }), | 367 | }), |
| 339 | ); | 368 | ); |
| 340 | | 369 | |
| 341 | it.effect("restores the database when a migrating trial exits", () => | 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>) => |
| 342 | Effect.gen(function* () { | 536 | Effect.gen(function* () { |
| 343 | const fs = yield* FileSystem.FileSystem; | 537 | const fs = yield* FileSystem.FileSystem; |
| 344 | const path = yield* Path.Path; | 538 | const path = yield* Path.Path; |
| 345 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-db-" }); | 539 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-space-" }); |
| 346 | const statePath = path.join(root, "runtime", "service-state.json"); | 540 | const statePath = path.join(root, "runtime", "service-state.json"); |
| 347 | const databasePath = path.join(root, "userdata", "state.sqlite"); | 541 | const databasePath = path.join(root, "userdata", "state.sqlite"); |
| 348 | const original = "database before migration"; | 542 | const replyPath = path.join(root, "rejection.txt"); |
| | 543 | const trialMarker = path.join(root, "trial-started"); |
| 349 | yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); | 544 | yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true }); |
| 350 | yield* fs.writeFileString(databasePath, original); | 545 | yield* fs.writeFileString(databasePath, "database"); |
| 351 | const encodedDatabasePath = JSON.stringify(databasePath); | 546 | const encoded = JSON.stringify({ databasePath, replyPath, trialMarker }); |
| 352 | const childSource = ` | 547 | const childSource = ` |
| 353 | import { writeFileSync } from "node:fs"; | 548 | import { writeFileSync } from "node:fs"; |
| | 549 | const paths = ${encoded}; |
| 354 | const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); | 550 | const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT); |
| 355 | if (context.update?.status === "pending") { | 551 | if (context.update?.status === "pending") { |
| 356 | writeFileSync(context.update.dbPath, "database after migration"); | 552 | writeFileSync(paths.trialMarker, "started"); |
| 357 | writeFileSync(context.update.dbPath + "-wal", "trial wal"); | | |
| 358 | writeFileSync(context.update.dbPath + "-shm", "trial shm"); | | |
| 359 | process.exit(1); | 553 | process.exit(1); |
| 360 | } else if (context.update === undefined) { | 554 | } else if (context.update === undefined) { |
| 361 | process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: ${encodedDatabasePath} }); | 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 }); |
| 362 | setInterval(() => {}, 1_000); | 562 | setInterval(() => {}, 1_000); |
| 363 | } else { | 563 | } else { |
| 364 | process.exit(0); | 564 | process.exit(0); |
| ... | @@ -379,7 +579,11 @@ if (context.update?.status === "pending") { | ... | @@ -379,7 +579,11 @@ if (context.update?.status === "pending") { |
| 379 | }), | 579 | }), |
| 380 | ); | 580 | ); |
| 381 | | 581 | |
| 382 | const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath))); | 582 | const launcher = new Launcher( |
| | 583 | root, |
| | 584 | yield* Effect.promise(() => readServiceState(statePath)), |
| | 585 | { backupSpaceProbe }, |
| | 586 | ); |
| 383 | yield* Effect.promise(() => | 587 | yield* Effect.promise(() => |
| 384 | launcher.run().then( | 588 | launcher.run().then( |
| 385 | () => Promise.reject(new Error("launcher unexpectedly completed")), | 589 | () => Promise.reject(new Error("launcher unexpectedly completed")), |
| ... | @@ -387,15 +591,171 @@ if (context.update?.status === "pending") { | ... | @@ -387,15 +591,171 @@ if (context.update?.status === "pending") { |
| 387 | ), | 591 | ), |
| 388 | ); | 592 | ); |
| 389 | | 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)"); |
| 390 | const state = yield* Effect.promise(() => readServiceState(statePath)); | 687 | const state = yield* Effect.promise(() => readServiceState(statePath)); |
| 391 | assert.equal(state.activeVersion, "1.0.0"); | 688 | assert.equal(state.activeVersion, "1.0.0"); |
| 392 | assert.equal(state.update?.status, "rolled-back"); | 689 | assert.isUndefined(state.update); |
| 393 | assert.equal(yield* fs.readFileString(databasePath), original); | 690 | }), |
| 394 | assert.isFalse(yield* fs.exists(`${databasePath}-wal`)); | 691 | ); |
| 395 | assert.isFalse(yield* fs.exists(`${databasePath}-shm`)); | 692 | |
| 396 | const updateId = state.update?.id; | 693 | it.effect("checks database backup space against clone or full-copy needs", () => |
| 397 | assert.isDefined(updateId); | 694 | Effect.gen(function* () { |
| 398 | assert.isFalse(yield* fs.exists(path.join(root, "runtime", "db-backup", updateId))); | 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")), []); |
| 399 | }), | 759 | }), |
| 400 | ); | 760 | ); |
| 401 | }); | 761 | }); |