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

pinnedRuntime.test.ts

176 lines6.6 KB

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 stdoutInvalidUtf8: false,
35 stderrInvalidUtf8: false,
36 };
37 }),
38 });
39​
40it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
41 it.effect("validates a staging tree before atomically publishing it", () =>
42 Effect.gen(function* () {
43 const fs = yield* FileSystem.FileSystem;
44 const path = yield* Path.Path;
45 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
46 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
47 let validatedDirectory = "";
48​
49 const installed = yield* ensurePinnedRuntimeInstalled({
50 baseDir,
51 version: "1.2.3",
52 fs,
53 path,
54 runner: successfulRunner(fs, path),
55 validate: (staging) =>
56 Effect.gen(function* () {
57 validatedDirectory = staging.versionDir;
58 assert.isFalse(yield* fs.exists(finalPaths.versionDir));
59 assert.isTrue(yield* fs.exists(staging.entryPath));
60 }).pipe(Effect.orDie),
61 });
62​
63 assert.notEqual(validatedDirectory, finalPaths.versionDir);
64 assert.deepEqual(installed, finalPaths);
65 assert.isTrue(yield* fs.exists(finalPaths.entryPath));
66 assert.equal(yield* fs.readFileString(finalPaths.sentinelPath), "1.2.3\n");
67 }),
68 );
69​
70 it.effect("removes staging and leaves no final runtime when validation fails", () =>
71 Effect.gen(function* () {
72 const fs = yield* FileSystem.FileSystem;
73 const path = yield* Path.Path;
74 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
75 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
76​
77 yield* ensurePinnedRuntimeInstalled({
78 baseDir,
79 version: "1.2.3",
80 fs,
81 path,
82 runner: successfulRunner(fs, path),
83 validate: () =>
84 Effect.fail(new PinnedRuntimeInstallError({ step: "validating the staged runtime" })),
85 }).pipe(Effect.flip);
86​
87 assert.isFalse(yield* fs.exists(finalPaths.versionDir));
88 assert.deepEqual(
89 (yield* fs.readDirectory(path.dirname(finalPaths.versionDir))).filter((entry) =>
90 entry.startsWith(".staging-"),
91 ),
92 [],
93 );
94 }),
95 );
96​
97 it.effect("replaces an incomplete pinned runtime", () =>
98 Effect.gen(function* () {
99 const fs = yield* FileSystem.FileSystem;
100 const path = yield* Path.Path;
101 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-repair-" });
102 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
103 yield* fs.makeDirectory(finalPaths.versionDir, { recursive: true });
104 yield* fs.writeFileString(path.join(finalPaths.versionDir, "partial"), "incomplete\n");
105​
106 yield* ensurePinnedRuntimeInstalled({
107 baseDir,
108 version: "1.2.3",
109 fs,
110 path,
111 runner: successfulRunner(fs, path),
112 validate: () => Effect.void,
113 });
114​
115 assert.isFalse(yield* fs.exists(path.join(finalPaths.versionDir, "partial")));
116 assert.isTrue(yield* fs.exists(finalPaths.entryPath));
117 }),
118 );
119​
120 it.effect("preserves a completed runtime when validation fails", () =>
121 Effect.gen(function* () {
122 const fs = yield* FileSystem.FileSystem;
123 const path = yield* Path.Path;
124 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-repair-" });
125 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
126 yield* fs.makeDirectory(path.dirname(finalPaths.entryPath), { recursive: true });
127 yield* fs.writeFileString(finalPaths.entryPath, "broken\n");
128 yield* fs.writeFileString(finalPaths.sentinelPath, "1.2.3\n");
129​
130 let validations = 0;
131 yield* ensurePinnedRuntimeInstalled({
132 baseDir,
133 version: "1.2.3",
134 fs,
135 path,
136 runner: successfulRunner(fs, path),
137 validate: (paths) =>
138 Effect.gen(function* () {
139 validations += 1;
140 const source = yield* fs.readFileString(paths.entryPath).pipe(Effect.orDie);
141 if (source === "broken\n") {
142 return yield* new PinnedRuntimeInstallError({ step: "validating the runtime" });
143 }
144 }),
145 }).pipe(Effect.flip);
146​
147 assert.equal(validations, 1);
148 assert.equal(yield* fs.readFileString(finalPaths.entryPath), "broken\n");
149 }),
150 );
151​
152 it.effect("removes staging when installation is interrupted", () =>
153 Effect.gen(function* () {
154 const fs = yield* FileSystem.FileSystem;
155 const path = yield* Path.Path;
156 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-interrupt-" });
157 const started = yield* Deferred.make<void>();
158 const runner = ProcessRunner.ProcessRunner.of({
159 run: () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)),
160 });
161 const install = yield* ensurePinnedRuntimeInstalled({
162 baseDir,
163 version: "1.2.3",
164 fs,
165 path,
166 runner,
167 validate: () => Effect.void,
168 }).pipe(Effect.forkScoped);
169​
170 yield* Deferred.await(started);
171 yield* Fiber.interrupt(install);
172 const versionsDir = path.join(baseDir, "runtime", "versions");
173 assert.deepEqual(yield* fs.readDirectory(versionsDir), []);
174 }),
175 );
176});