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

sync-reference-repos.test.ts

180 lines5.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 Layer from "effect/Layer";
6import * as Path from "effect/Path";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import { ChildProcessSpawner } from "effect/unstable/process";
10​
11import { referenceRepos } from "./lib/reference-repos.ts";
12import {
13 planReferenceRepoSync,
14 resolveReferenceRepoRef,
15 syncReferenceRepos,
16} from "./sync-reference-repos.ts";
17​
18const encoder = new TextEncoder();
19const effectSmol = referenceRepos[0]!;
20const alchemyEffect = referenceRepos[1]!;
21​
22function mockHandle() {
23 return ChildProcessSpawner.makeHandle({
24 pid: ChildProcessSpawner.ProcessId(1),
25 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
26 isRunning: Effect.succeed(false),
27 kill: () => Effect.void,
28 unref: Effect.succeed(Effect.void),
29 stdin: Sink.drain,
30 stdout: Stream.make(encoder.encode("done\n")),
31 stderr: Stream.empty,
32 all: Stream.empty,
33 getInputFd: () => Sink.drain,
34 getOutputFd: () => Stream.empty,
35 });
37​
38function mockSpawnerLayer(
39 commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>,
40) {
41 return Layer.succeed(
42 ChildProcessSpawner.ChildProcessSpawner,
43 ChildProcessSpawner.make((command) => {
44 const childProcess = command as unknown as {
45 readonly command: string;
46 readonly args: ReadonlyArray<string>;
47 };
48 commands.push({
49 command: childProcess.command,
50 args: childProcess.args,
51 });
52 return Effect.succeed(mockHandle());
53 }),
54 );
56​
57it.layer(NodeServices.layer)("sync-reference-repos", (it) => {
58 it.effect("resolves the effect-smol tag from the root catalog", () =>
59 Effect.gen(function* () {
60 const fs = yield* FileSystem.FileSystem;
61 const path = yield* Path.Path;
62 const rootDir = yield* fs.makeTempDirectoryScoped({
63 prefix: "sync-reference-repos-version-",
64 });
65 yield* fs.writeFileString(
66 path.join(rootDir, "pnpm-workspace.yaml"),
67 "catalog:\n effect: 4.0.0-beta.73\n",
68 );
69​
70 assert.equal(
71 yield* resolveReferenceRepoRef(effectSmol, rootDir, false),
72 "effect@4.0.0-beta.73",
73 );
74 }),
75 );
76​
77 it.effect("uses the latest branch without reading package versions", () =>
78 Effect.gen(function* () {
79 const fs = yield* FileSystem.FileSystem;
80 const rootDir = yield* fs.makeTempDirectoryScoped({
81 prefix: "sync-reference-repos-latest-",
82 });
83​
84 assert.equal(yield* resolveReferenceRepoRef(effectSmol, rootDir, true), "main");
85 }),
86 );
87​
88 it.effect("resolves the alchemy-effect tag from the relay package", () =>
89 Effect.gen(function* () {
90 const fs = yield* FileSystem.FileSystem;
91 const path = yield* Path.Path;
92 const rootDir = yield* fs.makeTempDirectoryScoped({
93 prefix: "sync-reference-repos-alchemy-version-",
94 });
95 yield* fs.makeDirectory(path.join(rootDir, "infra", "relay"), { recursive: true });
96 yield* fs.writeFileString(
97 path.join(rootDir, "infra", "relay", "package.json"),
98 '{"dependencies":{"alchemy":"2.0.0-beta.49"}}',
99 );
100​
101 assert.equal(yield* resolveReferenceRepoRef(alchemyEffect, rootDir, false), "v2.0.0-beta.49");
102 }),
103 );
104​
105 it.effect("plans an add for a missing subtree and a pull for an existing subtree", () =>
106 Effect.gen(function* () {
107 const fs = yield* FileSystem.FileSystem;
108 const path = yield* Path.Path;
109 const rootDir = yield* fs.makeTempDirectoryScoped({
110 prefix: "sync-reference-repos-plan-",
111 });
112 yield* fs.writeFileString(
113 path.join(rootDir, "pnpm-workspace.yaml"),
114 "catalog:\n effect: 4.0.0-beta.73\n",
115 );
116​
117 const addPlan = yield* planReferenceRepoSync(effectSmol, rootDir, false);
118 assert.equal(addPlan.action, "add");
119 assert.deepStrictEqual(addPlan.args, [
120 "subtree",
121 "add",
122 "--prefix=.repos/effect-smol",
123 "https://github.com/Effect-TS/effect-smol.git",
124 "effect@4.0.0-beta.73",
125 "--squash",
126 ]);
127​
128 yield* fs.makeDirectory(path.join(rootDir, effectSmol.prefix), { recursive: true });
129 assert.equal((yield* planReferenceRepoSync(effectSmol, rootDir, false)).action, "pull");
130 }),
131 );
132​
133 it.effect("runs the planned git subtree command through the process service", () => {
134 const commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }> = [];
135​
136 return Effect.gen(function* () {
137 const fs = yield* FileSystem.FileSystem;
138 const path = yield* Path.Path;
139 const rootDir = yield* fs.makeTempDirectoryScoped({
140 prefix: "sync-reference-repos-run-",
141 });
142 yield* fs.writeFileString(
143 path.join(rootDir, "pnpm-workspace.yaml"),
144 "catalog:\n effect: 4.0.0-beta.73\n",
145 );
146​
147 yield* syncReferenceRepos({ rootDir, repoId: "effect-smol" }).pipe(
148 Effect.provide(mockSpawnerLayer(commands)),
149 );
150​
151 assert.deepStrictEqual(commands, [
152 {
153 command: "git",
154 args: [
155 "subtree",
156 "add",
157 "--prefix=.repos/effect-smol",
158 "https://github.com/Effect-TS/effect-smol.git",
159 "effect@4.0.0-beta.73",
160 "--squash",
161 ],
162 },
163 ]);
164 });
165 });
166​
167 it.effect("rejects unknown repo selectors", () =>
168 Effect.gen(function* () {
169 const error = yield* syncReferenceRepos({
170 repoId: "missing",
171 dryRun: true,
172 }).pipe(Effect.flip);
173​
174 assert.equal(
175 error.message,
176 "Unknown reference repo 'missing'. Expected one of: effect-smol, alchemy-effect.",
177 );
178 }),
179 );
180});