Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

doctor.test.ts

57 lines2.2 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5​
6import * as BootService from "../cloud/bootService.ts";
7import { runDoctor } from "./doctor.ts";
8import { DatabaseMaintenance } from "../persistence/DatabaseMaintenance.ts";
9​
10it.effect(
11 "--fix reclaims space with the service paused and keeps the V1 database, and --dry-run changes nothing",
12 () =>
13 Effect.gen(function* () {
14 const calls: string[] = [];
15 const doctor = (dryRun: boolean) =>
16 runDoctor("/test/statev2.sqlite", { fix: true, dryRun, interactive: false }).pipe(
17 Effect.provide(
18 Layer.mergeAll(
19 NodeServices.layer,
20 Layer.mock(BootService.BootService)({
21 withStopped: (operation) =>
22 Effect.sync(() => calls.push("pause")).pipe(
23 Effect.andThen(operation),
24 Effect.ensuring(Effect.sync(() => calls.push("resume"))),
25 ),
26 }),
27 Layer.succeed(DatabaseMaintenance, {
28 // An unconverted database: reclaiming space is recommended.
29 // A database from before 0.1.0 sits beside it; --fix must keep it.
30 inspect: () =>
31 Effect.succeed({
32 databaseBytes: 2000,
33 reclaimableBytes: 1000,
34 incrementalVacuumEnabled: false,
35 legacyDatabaseBytes: 4000,
36 }),
37 compact: () =>
38 Effect.sync(() => {
39 calls.push("compact");
40 return {
41 beforeBytes: 2000,
42 afterBytes: 1000,
43 deletedEventCount: 1,
44 deletedReceiptCount: 0,
45 };
46 }),
47 removeLegacyDatabase: () => Effect.sync(() => calls.push("remove legacy")),
48 }),
49 ),
50 ),
51 );
52 yield* doctor(true);
53 assert.deepEqual(calls, []);
54 yield* doctor(false);
55 assert.deepEqual(calls, ["pause", "compact", "resume"]);
56 }),
57);