Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Deferred from "effect/Deferred";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Fiber from "effect/Fiber";
7import * as Path from "effect/Path";
8import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
9
10import * as ProcessRunner from "../processRunner.ts";
11import {
12 ensurePinnedRuntimeInstalled,
13 pinnedRuntimePaths,
14 PinnedRuntimeInstallError,
15} from "./pinnedRuntime.ts";
16
17const successfulRunner = (fs: FileSystem.FileSystem, path: Path.Path) =>
18 ProcessRunner.ProcessRunner.of({
19 run: (input) =>
20 Effect.gen(function* () {
21 const prefixIndex = input.args.indexOf("--prefix");
22 const stagingDir = input.args[prefixIndex + 1];
23 if (stagingDir === undefined) return yield* Effect.die("missing npm --prefix");
24 const entry = path.join(stagingDir, "node_modules", "t3", "dist", "bin.mjs");
25 yield* fs.makeDirectory(path.dirname(entry), { recursive: true }).pipe(Effect.orDie);
26 yield* fs.writeFileString(entry, "export {};\n").pipe(Effect.orDie);
27 return {
28 stdout: "",
29 stderr: "",
30 code: ChildProcessSpawner.ExitCode(0),
31 timedOut: false,
32 stdoutTruncated: false,
33 stderrTruncated: false,
34 };
35 }),
36 });
37
38it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
39 it.effect("validates a staging tree before atomically publishing it", () =>
40 Effect.gen(function* () {
41 const fs = yield* FileSystem.FileSystem;
42 const path = yield* Path.Path;
43 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
44 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
45 let validatedDirectory = "";
46
47 const installed = yield* ensurePinnedRuntimeInstalled({
48 baseDir,
49 version: "1.2.3",
50 fs,
51 path,
52 runner: successfulRunner(fs, path),
53 validate: (staging) =>
54 Effect.gen(function* () {
55 validatedDirectory = staging.versionDir;
56 assert.isFalse(yield* fs.exists(finalPaths.versionDir));
57 assert.isTrue(yield* fs.exists(staging.entryPath));
58 }).pipe(Effect.orDie),
59 });
60
61 assert.notEqual(validatedDirectory, finalPaths.versionDir);
62 assert.deepEqual(installed, finalPaths);
63 assert.isTrue(yield* fs.exists(finalPaths.entryPath));
64 assert.equal(yield* fs.readFileString(finalPaths.sentinelPath), "1.2.3\n");
65 }),
66 );
67
68 it.effect("removes staging and leaves no final runtime when validation fails", () =>
69 Effect.gen(function* () {
70 const fs = yield* FileSystem.FileSystem;
71 const path = yield* Path.Path;
72 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
73 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
74
75 yield* ensurePinnedRuntimeInstalled({
76 baseDir,
77 version: "1.2.3",
78 fs,
79 path,
80 runner: successfulRunner(fs, path),
81 validate: () =>
82 Effect.fail(new PinnedRuntimeInstallError({ step: "validating the staged runtime" })),
83 }).pipe(Effect.flip);
84
85 assert.isFalse(yield* fs.exists(finalPaths.versionDir));
86 assert.deepEqual(
87 (yield* fs.readDirectory(path.dirname(finalPaths.versionDir))).filter((entry) =>
88 entry.startsWith(".staging-"),
89 ),
90 [],
91 );
92 }),
93 );
94
95 it.effect("replaces an incomplete pinned runtime", () =>
96 Effect.gen(function* () {
97 const fs = yield* FileSystem.FileSystem;
98 const path = yield* Path.Path;
99 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-repair-" });
100 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
101 yield* fs.makeDirectory(finalPaths.versionDir, { recursive: true });
102 yield* fs.writeFileString(path.join(finalPaths.versionDir, "partial"), "incomplete\n");
103
104 yield* ensurePinnedRuntimeInstalled({
105 baseDir,
106 version: "1.2.3",
107 fs,
108 path,
109 runner: successfulRunner(fs, path),
110 validate: () => Effect.void,
111 });
112
113 assert.isFalse(yield* fs.exists(path.join(finalPaths.versionDir, "partial")));
114 assert.isTrue(yield* fs.exists(finalPaths.entryPath));
115 }),
116 );
117
118 it.effect("preserves a completed runtime when validation fails", () =>
119 Effect.gen(function* () {
120 const fs = yield* FileSystem.FileSystem;
121 const path = yield* Path.Path;
122 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-repair-" });
123 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
124 yield* fs.makeDirectory(path.dirname(finalPaths.entryPath), { recursive: true });
125 yield* fs.writeFileString(finalPaths.entryPath, "broken\n");
126 yield* fs.writeFileString(finalPaths.sentinelPath, "1.2.3\n");
127
128 let validations = 0;
129 yield* ensurePinnedRuntimeInstalled({
130 baseDir,
131 version: "1.2.3",
132 fs,
133 path,
134 runner: successfulRunner(fs, path),
135 validate: (paths) =>
136 Effect.gen(function* () {
137 validations += 1;
138 const source = yield* fs.readFileString(paths.entryPath).pipe(Effect.orDie);
139 if (source === "broken\n") {
140 return yield* new PinnedRuntimeInstallError({ step: "validating the runtime" });
141 }
142 }),
143 }).pipe(Effect.flip);
144
145 assert.equal(validations, 1);
146 assert.equal(yield* fs.readFileString(finalPaths.entryPath), "broken\n");
147 }),
148 );
149
150 it.effect("removes staging when installation is interrupted", () =>
151 Effect.gen(function* () {
152 const fs = yield* FileSystem.FileSystem;
153 const path = yield* Path.Path;
154 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-interrupt-" });
155 const started = yield* Deferred.make<void>();
156 const runner = ProcessRunner.ProcessRunner.of({
157 run: () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)),
158 });
159 const install = yield* ensurePinnedRuntimeInstalled({
160 baseDir,
161 version: "1.2.3",
162 fs,
163 path,
164 runner,
165 validate: () => Effect.void,
166 }).pipe(Effect.forkScoped);
167
168 yield* Deferred.await(started);
169 yield* Fiber.interrupt(install);
170 const versionsDir = path.join(baseDir, "runtime", "versions");
171 assert.deepEqual(yield* fs.readDirectory(versionsDir), []);
172 }),
173 );
174});