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

servicePreflight.test.ts

47 lines1.9 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { expect, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import * as NodeSqlite from "node:sqlite";
8​
9import { migrationManifest } from "../persistence/Migrations.ts";
10import { runServicePreflight } from "./servicePreflight.ts";
11​
12it.layer(NodeServices.layer)("service update preflight", (it) => {
13 it.effect("requires exact migration-manifest equality without mutating the database", () =>
14 Effect.gen(function* () {
15 const fs = yield* FileSystem.FileSystem;
16 const path = yield* Path.Path;
17 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-preflight-test-" });
18 const databasePath = path.join(root, "state.sqlite");
19 const database = new NodeSqlite.DatabaseSync(databasePath);
20 database.exec("CREATE TABLE effect_sql_migrations (migration_id INTEGER, name TEXT)");
21 const insert = database.prepare(
22 "INSERT INTO effect_sql_migrations (migration_id, name) VALUES (?, ?)",
23 );
24 for (const [id, name] of migrationManifest) insert.run(id, name);
25 database.close();
26​
27 expect(runServicePreflight({ databasePath, launcherProtocol: 1, version: "1.2.3" })).toEqual({
28 status: "ready",
29 version: "1.2.3",
30 launcherProtocol: 1,
31 });
32​
33 const changed = new NodeSqlite.DatabaseSync(databasePath);
34 changed.exec("DELETE FROM effect_sql_migrations WHERE migration_id = 35");
35 changed.close();
36 const blocked = runServicePreflight({
37 databasePath,
38 launcherProtocol: 1,
39 version: "1.2.3",
40 });
41 expect(blocked.status).toBe("blocked");
42 if (blocked.status === "blocked") {
43 expect(blocked.reason).toContain("npx t3@1.2.3 service update");
44 }
45 }),
46 );
47});