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

serviceLauncher.test.ts

199 lines7.6 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} from "./cloud/serviceProtocol.ts";
13​
14it("accepts only exact semantic versions", () => {
15 for (const version of ["0.0.0", "1.2.3", "1.2.3-alpha.1", "1.2.3-0", "1.2.3+001"]) {
16 assert.isTrue(isExactServiceVersion(version), version);
17 }
18 for (const version of ["latest", "01.2.3", "1.2.3-01", "1.2.3-alpha..1", "1.2.3+."]) {
19 assert.isFalse(isExactServiceVersion(version), version);
20 }
21});
22​
23it("orders exact semantic versions without treating build metadata as precedence", () => {
24 assert.equal(compareExactServiceVersions("1.2.3", "1.2.3"), 0);
25 assert.equal(compareExactServiceVersions("1.2.4", "1.2.3"), 1);
26 assert.equal(compareExactServiceVersions("2.0.0-alpha.1", "2.0.0-alpha.2"), -1);
27 assert.equal(compareExactServiceVersions("2.0.0-alpha.2", "2.0.0-alpha.beta"), -1);
28 assert.equal(compareExactServiceVersions("2.0.0-alpha-beta", "2.0.0-alpha-alpha"), 1);
29 assert.equal(compareExactServiceVersions("2.0.0", "2.0.0-rc.1"), 1);
30 assert.equal(compareExactServiceVersions("2.0.0+one", "2.0.0+two"), 0);
31});
32​
33it("rejects contradictory service state", () => {
34 assert.isUndefined(
35 decodeServiceState({
36 protocol: 1,
37 activeVersion: "0.0.31",
38 update: {
39 id: "update-1",
40 fromVersion: "0.0.30",
41 targetVersion: "0.0.32",
42 status: "pending",
43 },
44 }),
45 );
46​
47 assert.isUndefined(
48 decodeServiceState({
49 protocol: 1,
50 activeVersion: "1.0.0",
51 update: {
52 id: "update-2",
53 fromVersion: "1.0.0",
54 targetVersion: "0.9.0",
55 status: "pending",
56 },
57 }),
58 );
59});
60​
61it.layer(NodeServices.layer)("service state persistence", (it) => {
62 it.effect("durably replaces and strictly reads one state document", () =>
63 Effect.gen(function* () {
64 const fs = yield* FileSystem.FileSystem;
65 const path = yield* Path.Path;
66 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-test-" });
67 const statePath = path.join(root, "runtime", "service-state.json");
68 const state = {
69 protocol: 1,
70 activeVersion: "0.0.31",
71 } as const;
72​
73 yield* Effect.promise(() => writeServiceState(statePath, state));
74 assert.deepEqual(yield* Effect.promise(() => readServiceState(statePath)), state);
75 }),
76 );
77​
78 it.effect("serializes shutdown with launcher recovery", () =>
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-stop-" });
83 const statePath = path.join(root, "runtime", "service-state.json");
84 const versionDir = path.join(root, "runtime", "versions", "1.0.0");
85 const entryPath = path.join(versionDir, "node_modules", "t3", "dist", "bin.mjs");
86 yield* fs.makeDirectory(path.dirname(entryPath), { recursive: true });
87 yield* fs.writeFileString(entryPath, "setInterval(() => {}, 1_000);\n");
88 yield* fs.writeFileString(path.join(versionDir, ".install-complete"), "1.0.0\n");
89 yield* Effect.promise(() =>
90 writeServiceState(statePath, {
91 protocol: 1,
92 activeVersion: "1.0.0",
93 }),
94 );
95​
96 const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
97 const running = launcher.run();
98 yield* Effect.promise(() => launcher.stop("SIGTERM"));
99 yield* Effect.promise(() => running);
100 }),
101 );
102​
103 it.effect("commits only after the trial reports prepared", () =>
104 Effect.gen(function* () {
105 const fs = yield* FileSystem.FileSystem;
106 const path = yield* Path.Path;
107 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-flow-" });
108 const statePath = path.join(root, "runtime", "service-state.json");
109 const childSource = `
110const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);
111if (context.update?.status === "pending") {
112 process.send({ type: "prepared", updateId: context.update.id });
113 process.on("message", (message) => {
114 if (message.type === "committed") process.exit(0);
115 });
116} else if (context.update === undefined) {
117 process.send({ type: "request-update", targetVersion: "1.1.0" });
118 setInterval(() => {}, 1_000);
119} else {
120 process.exit(0);
122`;
123 for (const version of ["1.0.0", "1.1.0"]) {
124 const versionDir = path.join(root, "runtime", "versions", version);
125 const entryPath = path.join(versionDir, "node_modules", "t3", "dist", "bin.mjs");
126 yield* fs.makeDirectory(path.dirname(entryPath), { recursive: true });
127 yield* fs.writeFileString(entryPath, childSource);
128 yield* fs.writeFileString(path.join(versionDir, ".install-complete"), `${version}\n`);
129 }
130 yield* Effect.promise(() =>
131 writeServiceState(statePath, {
132 protocol: 1,
133 activeVersion: "1.0.0",
134 }),
135 );
136​
137 const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
138 yield* Effect.promise(() =>
139 launcher.run().then(
140 () => Promise.reject(new Error("launcher unexpectedly completed")),
141 () => Promise.resolve(),
142 ),
143 );
144​
145 const state = yield* Effect.promise(() => readServiceState(statePath));
146 assert.equal(state.activeVersion, "1.1.0");
147 assert.equal(state.update?.status, "committed");
148 }),
149 );
150​
151 it.effect("rolls back a trial that reports the wrong update ID", () =>
152 Effect.gen(function* () {
153 const fs = yield* FileSystem.FileSystem;
154 const path = yield* Path.Path;
155 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-rollback-" });
156 const statePath = path.join(root, "runtime", "service-state.json");
157 const childSource = `
158const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);
159if (context.update?.status === "pending") {
160 process.send({ type: "prepared", updateId: "wrong-update" });
161} else if (context.update === undefined) {
162 process.send({ type: "request-update", targetVersion: "1.1.0" });
163 setInterval(() => {}, 1_000);
164} else {
165 process.exit(0);
167`;
168 for (const version of ["1.0.0", "1.1.0"]) {
169 const versionDir = path.join(root, "runtime", "versions", version);
170 const entryPath = path.join(versionDir, "node_modules", "t3", "dist", "bin.mjs");
171 yield* fs.makeDirectory(path.dirname(entryPath), { recursive: true });
172 yield* fs.writeFileString(entryPath, childSource);
173 yield* fs.writeFileString(path.join(versionDir, ".install-complete"), `${version}\n`);
174 }
175 yield* Effect.promise(() =>
176 writeServiceState(statePath, {
177 protocol: 1,
178 activeVersion: "1.0.0",
179 }),
180 );
181​
182 const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
183 yield* Effect.promise(() =>
184 launcher.run().then(
185 () => Promise.reject(new Error("launcher unexpectedly completed")),
186 () => Promise.resolve(),
187 ),
188 );
189​
190 const state = yield* Effect.promise(() => readServiceState(statePath));
191 assert.equal(state.activeVersion, "1.0.0");
192 assert.equal(state.update?.status, "rolled-back");
193 assert.equal(
194 state.update?.status === "rolled-back" ? state.update.reason : undefined,
195 "invalid-prepared",
196 );
197 }),
198 );
199});