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

servicePreflight.test.ts

80 lines3.5 KB

1import * as NodeSqlite from "node:sqlite";
2​
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { expect, it } from "@effect/vitest";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8​
9import {
10 BUILD_MIGRATION_HEAD,
11 databaseMigrationHead,
12} from "../persistence/databaseMigrationHead.ts";
13import { migrationManifest } from "../persistence/Migrations.ts";
14​
15import { runServicePreflight } from "./servicePreflight.ts";
16import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
17​
18// regression: #13239 a stale CLI shadowing the updated one kept "updating" to itself
19it.each([1])("blocks legacy launcher protocol %i with exact recovery steps", (launcherProtocol) => {
20 const result = runServicePreflight({
21 databasePath: "/missing/state.sqlite",
22 launcherProtocol,
23 version: "9.8.7",
24 });
25​
26 expect(result).toMatchObject({ status: "blocked", version: "9.8.7" });
27 const reason = result.status === "blocked" ? result.reason : "";
28 expect(reason).toMatch(new RegExp(`\\b${launcherProtocol}\\b`));
29 expect(reason).toMatch(new RegExp(`\\b${SERVICE_LAUNCHER_PROTOCOL}\\b`));
30 expect(reason).toContain("curl -fsSL https://t3.codes/install.sh | T3CODE_VERSION=9.8.7 sh");
31 expect(reason).toContain("~/.local/bin/t3 service install");
32 expect(reason).toContain("t3 --version");
33 expect(reason).toContain("which -a t3");
34});
35​
36it.layer(NodeServices.layer)("service preflight database", (it) => {
37 it.effect("blocks a database that a newer build migrated", () =>
38 Effect.gen(function* () {
39 const fs = yield* FileSystem.FileSystem;
40 const path = yield* Path.Path;
41 const databasePath = path.join(
42 yield* fs.makeTempDirectoryScoped({ prefix: "t3-preflight-" }),
43 "state.sqlite",
44 );
45 const db = new NodeSqlite.DatabaseSync(databasePath);
46 db.exec("CREATE TABLE effect_sql_migrations (migration_id INTEGER PRIMARY KEY, name TEXT)");
47 const insert = db.prepare("INSERT INTO effect_sql_migrations VALUES (?, ?)");
48 for (const [id, name] of migrationManifest) insert.run(id, name);
49 const preflight = () =>
50 runServicePreflight({ databasePath, launcherProtocol: SERVICE_LAUNCHER_PROTOCOL });
51​
52 expect(preflight().status).toBe("ready");
53 expect(databaseMigrationHead(databasePath)).toBe(BUILD_MIGRATION_HEAD);
54​
55 const newerId = Math.max(...migrationManifest.map(([id]) => id)) + 1;
56 insert.run(newerId, "FromNightly");
57 db.close();
58 const result = preflight();
59 expect(result.status).toBe("blocked");
60 expect(result.status === "blocked" ? result.reason : "").toContain(`${newerId}_FromNightly`);
61 // Servers report this head, so clients see a database a newer build migrated.
62 expect(databaseMigrationHead(databasePath)).toBe(newerId);
63​
64 // A database that exists but cannot be read has an unknown history, so it blocks.
65 const unreadable = path.join(path.dirname(databasePath), "unreadable.sqlite");
66 yield* fs.writeFileString(unreadable, "not a database");
67 expect(databaseMigrationHead(unreadable)).toBeUndefined();
68 expect(
69 runServicePreflight({
70 databasePath: unreadable,
71 launcherProtocol: SERVICE_LAUNCHER_PROTOCOL,
72 }).status,
73 ).toBe("blocked");
74 // A missing database is a fresh install.
75 expect(databaseMigrationHead(path.join(path.dirname(databasePath), "missing.sqlite"))).toBe(
76 BUILD_MIGRATION_HEAD,
77 );
78 }),
79 );
80});