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