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

pinnedRuntime.test.ts

129 lines4.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 Fiber from "effect/Fiber";
6import * as FileSystem from "effect/FileSystem";
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 removePinnedRuntimeInstallation,
15} from "./pinnedRuntime.ts";
16​
17it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
18 it.effect("serializes concurrent installs of the same runtime", () =>
19 Effect.gen(function* () {
20 const fs = yield* FileSystem.FileSystem;
21 const path = yield* Path.Path;
22 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
23 const installStarted = yield* Deferred.make<void>();
24 const allowInstallToFinish = yield* Deferred.make<void>();
25 const paths = pinnedRuntimePaths(path, baseDir, "0.0.29");
26 let npmRuns = 0;
27​
28 const runner = ProcessRunner.ProcessRunner.of({
29 run: (_input) =>
30 Effect.gen(function* () {
31 npmRuns += 1;
32 yield* Deferred.succeed(installStarted, undefined);
33 yield* Deferred.await(allowInstallToFinish);
34 yield* fs
35 .makeDirectory(path.dirname(paths.entryPath), { recursive: true })
36 .pipe(Effect.orDie);
37 yield* fs.writeFileString(paths.entryPath, "export {};\n").pipe(Effect.orDie);
38 return {
39 stdout: "",
40 stderr: "",
41 code: ChildProcessSpawner.ExitCode(0),
42 timedOut: false,
43 stdoutTruncated: false,
44 stderrTruncated: false,
45 };
46 }),
47 });
48 const install = ensurePinnedRuntimeInstalled({
49 baseDir,
50 version: "0.0.29",
51 fs,
52 path,
53 runner,
54 });
55​
56 const first = yield* Effect.forkChild(install, { startImmediately: true });
57 yield* Deferred.await(installStarted);
58 const second = yield* Effect.forkChild(install, { startImmediately: true });
59 yield* Effect.yieldNow;
60 assert.equal(npmRuns, 1);
61​
62 yield* Deferred.succeed(allowInstallToFinish, undefined);
63 yield* Fiber.join(first);
64 yield* Fiber.join(second);
65​
66 assert.equal(npmRuns, 1);
67 assert.isTrue(yield* fs.exists(paths.sentinelPath));
68 assert.isTrue(yield* fs.exists(paths.entryPath));
69 }),
70 );
71​
72 it.effect("waits for an active install before removing its runtime", () =>
73 Effect.gen(function* () {
74 const fs = yield* FileSystem.FileSystem;
75 const path = yield* Path.Path;
76 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
77 const installStarted = yield* Deferred.make<void>();
78 const allowInstallToFinish = yield* Deferred.make<void>();
79 const paths = pinnedRuntimePaths(path, baseDir, "0.0.30");
80 const runner = ProcessRunner.ProcessRunner.of({
81 run: (_input) =>
82 Effect.gen(function* () {
83 yield* Deferred.succeed(installStarted, undefined);
84 yield* Deferred.await(allowInstallToFinish);
85 yield* fs
86 .makeDirectory(path.dirname(paths.entryPath), { recursive: true })
87 .pipe(Effect.orDie);
88 yield* fs.writeFileString(paths.entryPath, "export {};\n").pipe(Effect.orDie);
89 return {
90 stdout: "",
91 stderr: "",
92 code: ChildProcessSpawner.ExitCode(0),
93 timedOut: false,
94 stdoutTruncated: false,
95 stderrTruncated: false,
96 };
97 }),
98 });
99​
100 const installFiber = yield* Effect.forkChild(
101 ensurePinnedRuntimeInstalled({
102 baseDir,
103 version: "0.0.30",
104 fs,
105 path,
106 runner,
107 }),
108 { startImmediately: true },
109 );
110 yield* Deferred.await(installStarted);
111 const removeFiber = yield* Effect.forkChild(
112 removePinnedRuntimeInstallation({
113 baseDir,
114 version: "0.0.30",
115 fs,
116 path,
117 }),
118 { startImmediately: true },
119 );
120 yield* Effect.yieldNow;
121 assert.isTrue(yield* fs.exists(paths.versionDir));
122​
123 yield* Deferred.succeed(allowInstallToFinish, undefined);
124 yield* Fiber.join(installFiber);
125 yield* Fiber.join(removeFiber);
126 assert.isFalse(yield* fs.exists(paths.versionDir));
127 }),
128 );
129});