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

serviceLauncher.test.ts

286 lines11.7 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6​
7import { Launcher, readServiceState, writeServiceState } from "./serviceLauncher.ts";
8import {
9 compareExactServiceVersions,
10 decodeServiceState,
11 isExactServiceVersion,
12 SERVICE_LAUNCHER_PROTOCOL,
13} from "./cloud/serviceProtocol.ts";
14​
15it("accepts only exact semantic versions", () => {
16 for (const version of ["0.0.0", "1.2.3", "1.2.3-alpha.1", "1.2.3-0", "1.2.3+001"]) {
17 assert.isTrue(isExactServiceVersion(version), version);
18 }
19 for (const version of ["latest", "01.2.3", "1.2.3-01", "1.2.3-alpha..1", "1.2.3+."]) {
20 assert.isFalse(isExactServiceVersion(version), version);
21 }
22});
23​
24it("orders exact semantic versions without treating build metadata as precedence", () => {
25 assert.equal(compareExactServiceVersions("1.2.3", "1.2.3"), 0);
26 assert.equal(compareExactServiceVersions("1.2.4", "1.2.3"), 1);
27 assert.equal(compareExactServiceVersions("2.0.0-alpha.1", "2.0.0-alpha.2"), -1);
28 assert.equal(compareExactServiceVersions("2.0.0-alpha.2", "2.0.0-alpha.beta"), -1);
29 assert.equal(compareExactServiceVersions("2.0.0-alpha-beta", "2.0.0-alpha-alpha"), 1);
30 assert.equal(compareExactServiceVersions("2.0.0", "2.0.0-rc.1"), 1);
31 assert.equal(compareExactServiceVersions("2.0.0+one", "2.0.0+two"), 0);
32});
33​
34it("rejects contradictory service state", () => {
35 assert.isUndefined(
36 decodeServiceState({
37 protocol: SERVICE_LAUNCHER_PROTOCOL,
38 activeVersion: "0.0.31",
39 update: {
40 id: "update-1",
41 fromVersion: "0.0.30",
42 targetVersion: "0.0.32",
43 dbPath: "/tmp/state.sqlite",
44 status: "pending",
45 },
46 }),
47 );
48​
49 assert.isUndefined(
50 decodeServiceState({
51 protocol: SERVICE_LAUNCHER_PROTOCOL,
52 activeVersion: "1.0.0",
53 update: {
54 id: "update-3",
55 fromVersion: "1.0.0",
56 targetVersion: "1.1.0",
57 status: "pending",
58 },
59 }),
60 );
61​
62 assert.isUndefined(
63 decodeServiceState({
64 protocol: SERVICE_LAUNCHER_PROTOCOL,
65 activeVersion: "1.0.0",
66 update: {
67 id: "update-2",
68 fromVersion: "1.0.0",
69 targetVersion: "0.9.0",
70 dbPath: "/tmp/state.sqlite",
71 status: "pending",
72 },
73 }),
74 );
75});
76​
77it.layer(NodeServices.layer)("service state persistence", (it) => {
78 it.effect("durably replaces and strictly reads one state document", () =>
79 Effect.gen(function* () {
80 const fs = yield* FileSystem.FileSystem;
81 const path = yield* Path.Path;
82 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-test-" });
83 const statePath = path.join(root, "runtime", "service-state.json");
84 const state = {
85 protocol: SERVICE_LAUNCHER_PROTOCOL,
86 activeVersion: "0.0.31",
87 } as const;
88​
89 yield* Effect.promise(() => writeServiceState(statePath, state));
90 assert.deepEqual(yield* Effect.promise(() => readServiceState(statePath)), state);
91 }),
92 );
93​
94 it.effect("serializes shutdown with launcher recovery", () =>
95 Effect.gen(function* () {
96 const fs = yield* FileSystem.FileSystem;
97 const path = yield* Path.Path;
98 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-stop-" });
99 const statePath = path.join(root, "runtime", "service-state.json");
100 const versionDir = path.join(root, "runtime", "versions", "1.0.0");
101 const entryPath = path.join(versionDir, "node_modules", "t3", "dist", "bin.mjs");
102 yield* fs.makeDirectory(path.dirname(entryPath), { recursive: true });
103 yield* fs.writeFileString(entryPath, "setInterval(() => {}, 1_000);\n");
104 yield* fs.writeFileString(path.join(versionDir, ".install-complete"), "1.0.0\n");
105 yield* Effect.promise(() =>
106 writeServiceState(statePath, {
107 protocol: SERVICE_LAUNCHER_PROTOCOL,
108 activeVersion: "1.0.0",
109 }),
110 );
111​
112 const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
113 const running = launcher.run();
114 yield* Effect.promise(() => launcher.stop("SIGTERM"));
115 yield* Effect.promise(() => running);
116 }),
117 );
118​
119 it.effect("commits only after the trial reports prepared", () =>
120 Effect.gen(function* () {
121 const fs = yield* FileSystem.FileSystem;
122 const path = yield* Path.Path;
123 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-flow-" });
124 const statePath = path.join(root, "runtime", "service-state.json");
125 const databasePath = path.join(root, "userdata", "state.sqlite");
126 yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true });
127 yield* fs.writeFileString(databasePath, "before trial");
128 // @effect-diagnostics-next-line preferSchemaOverJson:off - embeds a path in fake child source.
129 const encodedDatabasePath = JSON.stringify(databasePath);
130 const childSource = `
131const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);
132if (context.update?.status === "pending") {
133 process.send({ type: "prepared", updateId: context.update.id });
134 process.on("message", (message) => {
135 if (message.type === "committed") process.exit(0);
136 });
137} else if (context.update === undefined) {
138 process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: ${encodedDatabasePath} });
139 setInterval(() => {}, 1_000);
140} else {
141 process.exit(0);
143`;
144 for (const version of ["1.0.0", "1.1.0"]) {
145 const versionDir = path.join(root, "runtime", "versions", version);
146 const entryPath = path.join(versionDir, "node_modules", "t3", "dist", "bin.mjs");
147 yield* fs.makeDirectory(path.dirname(entryPath), { recursive: true });
148 yield* fs.writeFileString(entryPath, childSource);
149 yield* fs.writeFileString(path.join(versionDir, ".install-complete"), `${version}\n`);
150 }
151 yield* Effect.promise(() =>
152 writeServiceState(statePath, {
153 protocol: SERVICE_LAUNCHER_PROTOCOL,
154 activeVersion: "1.0.0",
155 }),
156 );
157​
158 const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
159 yield* Effect.promise(() =>
160 launcher.run().then(
161 () => Promise.reject(new Error("launcher unexpectedly completed")),
162 () => Promise.resolve(),
163 ),
164 );
165​
166 const state = yield* Effect.promise(() => readServiceState(statePath));
167 assert.equal(state.activeVersion, "1.1.0");
168 assert.equal(state.update?.status, "committed");
169 }),
170 );
171​
172 it.effect("rolls back a trial that reports the wrong update ID", () =>
173 Effect.gen(function* () {
174 const fs = yield* FileSystem.FileSystem;
175 const path = yield* Path.Path;
176 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-rollback-" });
177 const statePath = path.join(root, "runtime", "service-state.json");
178 const databasePath = path.join(root, "userdata", "state.sqlite");
179 yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true });
180 yield* fs.writeFileString(databasePath, "before trial");
181 // @effect-diagnostics-next-line preferSchemaOverJson:off - embeds a path in fake child source.
182 const encodedDatabasePath = JSON.stringify(databasePath);
183 const childSource = `
184const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);
185if (context.update?.status === "pending") {
186 process.send({ type: "prepared", updateId: "wrong-update" });
187} else if (context.update === undefined) {
188 process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: ${encodedDatabasePath} });
189 setInterval(() => {}, 1_000);
190} else {
191 process.exit(0);
193`;
194 for (const version of ["1.0.0", "1.1.0"]) {
195 const versionDir = path.join(root, "runtime", "versions", version);
196 const entryPath = path.join(versionDir, "node_modules", "t3", "dist", "bin.mjs");
197 yield* fs.makeDirectory(path.dirname(entryPath), { recursive: true });
198 yield* fs.writeFileString(entryPath, childSource);
199 yield* fs.writeFileString(path.join(versionDir, ".install-complete"), `${version}\n`);
200 }
201 yield* Effect.promise(() =>
202 writeServiceState(statePath, {
203 protocol: SERVICE_LAUNCHER_PROTOCOL,
204 activeVersion: "1.0.0",
205 }),
206 );
207​
208 const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
209 yield* Effect.promise(() =>
210 launcher.run().then(
211 () => Promise.reject(new Error("launcher unexpectedly completed")),
212 () => Promise.resolve(),
213 ),
214 );
215​
216 const state = yield* Effect.promise(() => readServiceState(statePath));
217 assert.equal(state.activeVersion, "1.0.0");
218 assert.equal(state.update?.status, "rolled-back");
219 assert.equal(
220 state.update?.status === "rolled-back" ? state.update.reason : undefined,
221 "invalid-prepared",
222 );
223 }),
224 );
225​
226 it.effect("restores the database when a migrating trial exits", () =>
227 Effect.gen(function* () {
228 const fs = yield* FileSystem.FileSystem;
229 const path = yield* Path.Path;
230 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-db-" });
231 const statePath = path.join(root, "runtime", "service-state.json");
232 const databasePath = path.join(root, "userdata", "state.sqlite");
233 const original = "database before migration";
234 yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true });
235 yield* fs.writeFileString(databasePath, original);
236 // @effect-diagnostics-next-line preferSchemaOverJson:off - embeds a path in fake child source.
237 const encodedDatabasePath = JSON.stringify(databasePath);
238 const childSource = `
239import { writeFileSync } from "node:fs";
240const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);
241if (context.update?.status === "pending") {
242 writeFileSync(context.update.dbPath, "database after migration");
243 writeFileSync(context.update.dbPath + "-wal", "trial wal");
244 writeFileSync(context.update.dbPath + "-shm", "trial shm");
245 process.exit(1);
246} else if (context.update === undefined) {
247 process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: ${encodedDatabasePath} });
248 setInterval(() => {}, 1_000);
249} else {
250 process.exit(0);
252`;
253 for (const version of ["1.0.0", "1.1.0"]) {
254 const versionDir = path.join(root, "runtime", "versions", version);
255 const entryPath = path.join(versionDir, "node_modules", "t3", "dist", "bin.mjs");
256 yield* fs.makeDirectory(path.dirname(entryPath), { recursive: true });
257 yield* fs.writeFileString(entryPath, childSource);
258 yield* fs.writeFileString(path.join(versionDir, ".install-complete"), `${version}\n`);
259 }
260 yield* Effect.promise(() =>
261 writeServiceState(statePath, {
262 protocol: SERVICE_LAUNCHER_PROTOCOL,
263 activeVersion: "1.0.0",
264 }),
265 );
266​
267 const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
268 yield* Effect.promise(() =>
269 launcher.run().then(
270 () => Promise.reject(new Error("launcher unexpectedly completed")),
271 () => Promise.resolve(),
272 ),
273 );
274​
275 const state = yield* Effect.promise(() => readServiceState(statePath));
276 assert.equal(state.activeVersion, "1.0.0");
277 assert.equal(state.update?.status, "rolled-back");
278 assert.equal(yield* fs.readFileString(databasePath), original);
279 assert.isFalse(yield* fs.exists(`${databasePath}-wal`));
280 assert.isFalse(yield* fs.exists(`${databasePath}-shm`));
281 const updateId = state.update?.id;
282 assert.isDefined(updateId);
283 assert.isFalse(yield* fs.exists(path.join(root, "runtime", "db-backup", updateId)));
284 }),
285 );
286});