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

doctor.test.ts

129 lines4.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Console from "effect/Console";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as TestClock from "effect/testing/TestClock";
7​
8import * as BootService from "../cloud/bootService.ts";
9import { runDoctor } from "./doctor.ts";
10import { DatabaseMaintenance } from "../persistence/DatabaseMaintenance.ts";
11​
12it.effect(
13 "--fix reclaims space with the service paused and keeps the V1 database, and --dry-run changes nothing",
14 () =>
15 Effect.gen(function* () {
16 const calls: string[] = [];
17 const doctor = (dryRun: boolean) =>
18 runDoctor("/test/statev2.sqlite", { fix: true, dryRun, interactive: false }).pipe(
19 Effect.provide(
20 Layer.mergeAll(
21 NodeServices.layer,
22 Layer.mock(BootService.BootService)({
23 withStopped: (operation) =>
24 Effect.sync(() => calls.push("pause")).pipe(
25 Effect.andThen(operation),
26 Effect.ensuring(Effect.sync(() => calls.push("resume"))),
27 ),
28 }),
29 Layer.succeed(DatabaseMaintenance, {
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);
58​
59it.effect("warns how long reclaiming can take and reports each step with its elapsed time", () =>
60 Effect.gen(function* () {
61 const output: string[] = [];
62 const steps = [
63 ["prune-events", "65 seconds"],
64 ["vacuum", "701 seconds"],
65 ["rebuild-search", "1 second"],
66 ["checkpoint", "1 second"],
67 ] as const;
68 yield* runDoctor("/test/statev2.sqlite", { fix: true, dryRun: false, interactive: false }).pipe(
69 Effect.provide(
70 Layer.mergeAll(
71 NodeServices.layer,
72 Layer.mock(BootService.BootService)({
73 withStopped: (operation) =>
74 Effect.sync(() => output.push("<pause>")).pipe(
75 Effect.andThen(operation),
76 Effect.ensuring(Effect.sync(() => output.push("<resume>"))),
77 ),
78 }),
79 Layer.succeed(DatabaseMaintenance, {
80 inspect: () =>
81 Effect.succeed({
82 databaseBytes: 12.8 * 1024 ** 3,
83 reclaimableBytes: 0,
84 incrementalVacuumEnabled: false,
85 legacyDatabaseBytes: null,
86 }),
87 compact: (_databasePath, onStep) =>
88 Effect.gen(function* () {
89 for (const [step, duration] of steps) {
90 if (onStep) yield* onStep(step);
91 yield* TestClock.adjust(duration);
92 }
93 return {
94 beforeBytes: 12.8 * 1024 ** 3,
95 afterBytes: 12.8 * 1024 ** 3,
96 deletedEventCount: 0,
97 deletedReceiptCount: 0,
98 };
99 }),
100 removeLegacyDatabase: () => Effect.void,
101 }),
102 ),
103 ),
104 Effect.provideService(Console.Console, {
105 ...globalThis.console,
106 log: (...args: ReadonlyArray<unknown>) => {
107 output.push(...args.join(" ").split("\n"));
108 },
109 }),
110 );
111​
112 const paused = output.indexOf("<pause>");
113 const resumed = output.indexOf("<resume>");
114 assert.isAbove(paused, -1, "the repair must run with the service paused");
115 const beforePause = output.slice(0, paused).join("\n");
116 const description = beforePause.slice(beforePause.indexOf("Reclaim database space"));
117 assert.include(description, "12.80 GiB", "the repair must state the database size up front");
118 assert.match(description, /hour/, "the repair must warn it can take over an hour");
119 const progress = output
120 .slice(paused + 1, resumed)
121 .filter((line) => line.trim() !== "" && !line.trim().startsWith("Done"))
122 .map((line) => /\[(\d+:\d{2})\]/.exec(line)?.[1] ?? `unprefixed: ${line}`);
123 assert.deepEqual(
124 progress,
125 ["0:00", "1:05", "12:46", "12:47"],
126 "each compaction step must print one line with the elapsed time as it starts",
127 );
128 }),
129);