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

SourceControlRepositoryService.test.ts

320 lines10.5 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 { ChildProcessSpawner } from "effect/unstable/process";
7​
8import { GitCommandError, type SourceControlProviderError } from "@t3tools/contracts";
9​
10import { ServerConfig } from "../config.ts";
11import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
12import type * as SourceControlProvider from "./SourceControlProvider.ts";
13import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
14import * as SourceControlRepositoryService from "./SourceControlRepositoryService.ts";
15​
16const CLONE_URLS = {
17 nameWithOwner: "octocat/t3code",
18 url: "https://github.com/octocat/t3code",
19 sshUrl: "git@github.com:octocat/t3code.git",
20};
21​
22function makeProvider(
23 overrides: Partial<SourceControlProvider.SourceControlProviderShape> = {},
24): SourceControlProvider.SourceControlProviderShape {
25 const unsupported = (operation: string) =>
26 Effect.die(`unexpected provider operation ${operation}`) as Effect.Effect<
27 never,
28 SourceControlProviderError
29 >;
30​
31 return {
32 kind: "github",
33 listChangeRequests: () => unsupported("listChangeRequests"),
34 getChangeRequest: () => unsupported("getChangeRequest"),
35 createChangeRequest: () => unsupported("createChangeRequest"),
36 getRepositoryCloneUrls: () => Effect.succeed(CLONE_URLS),
37 createRepository: () => Effect.succeed(CLONE_URLS),
38 getDefaultBranch: () => Effect.succeed(null),
39 checkoutChangeRequest: () => unsupported("checkoutChangeRequest"),
40 ...overrides,
41 };
43​
44function processOutput(): GitVcsDriver.ExecuteGitResult {
45 return {
46 exitCode: ChildProcessSpawner.ExitCode(0),
47 stdout: "",
48 stderr: "",
49 stdoutTruncated: false,
50 stderrTruncated: false,
51 };
53​
54function makeLayer(input: {
55 readonly provider?: SourceControlProvider.SourceControlProviderShape;
56 readonly git?: Partial<GitVcsDriver.GitVcsDriverShape>;
57}) {
58 return SourceControlRepositoryService.layer.pipe(
59 Layer.provide(
60 Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
61 get: () => Effect.succeed(input.provider ?? makeProvider()),
62 }),
63 ),
64 Layer.provide(
65 Layer.mock(GitVcsDriver.GitVcsDriver)({
66 execute: () => Effect.succeed(processOutput()),
67 ensureRemote: () => Effect.succeed("origin"),
68 pushCurrentBranch: () =>
69 Effect.succeed({
70 status: "pushed" as const,
71 branch: "feature/remote-v1",
72 upstreamBranch: "origin/feature/remote-v1",
73 setUpstream: true,
74 }),
75 ...input.git,
76 }),
77 ),
78 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-repos-" })),
79 Layer.provideMerge(NodeServices.layer),
80 );
82​
83it.effect("looks up repositories through the requested provider without search", () => {
84 const calls: Array<{ cwd: string; repository: string }> = [];
85 const provider = makeProvider({
86 getRepositoryCloneUrls: (input) =>
87 Effect.sync(() => {
88 calls.push({ cwd: input.cwd, repository: input.repository });
89 return CLONE_URLS;
90 }),
91 });
92​
93 return Effect.gen(function* () {
94 const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
95 const result = yield* service.lookupRepository({
96 provider: "github",
97 repository: "octocat/t3code",
98 cwd: "/workspace",
99 });
100​
101 assert.deepStrictEqual(result, { provider: "github", ...CLONE_URLS });
102 assert.deepStrictEqual(calls, [{ cwd: "/workspace", repository: "octocat/t3code" }]);
103 }).pipe(Effect.provide(makeLayer({ provider })));
104});
105​
106it.effect("clones a looked-up repository into the requested destination", () =>
107 Effect.gen(function* () {
108 const fs = yield* FileSystem.FileSystem;
109 const parent = yield* fs.makeTempDirectoryScoped({
110 prefix: "t3-source-control-clone-parent-",
111 });
112 const destinationPath = `${parent}/t3code`;
113 const cloneCalls: Array<{ cwd: string; args: ReadonlyArray<string> }> = [];
114​
115 yield* Effect.gen(function* () {
116 const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
117 const result = yield* service.cloneRepository({
118 provider: "github",
119 repository: "octocat/t3code",
120 destinationPath,
121 protocol: "https",
122 });
123​
124 assert.deepStrictEqual(result, {
125 cwd: destinationPath,
126 remoteUrl: CLONE_URLS.url,
127 repository: { provider: "github", ...CLONE_URLS },
128 });
129 assert.deepStrictEqual(cloneCalls, [
130 {
131 cwd: parent,
132 args: ["clone", CLONE_URLS.url, "t3code"],
133 },
134 ]);
135 }).pipe(
136 Effect.provide(
137 makeLayer({
138 git: {
139 execute: (input) =>
140 Effect.sync(() => {
141 cloneCalls.push({ cwd: input.cwd, args: input.args });
142 return processOutput();
143 }),
144 },
145 }),
146 ),
147 );
148 }).pipe(Effect.provide(NodeServices.layer)),
149);
150​
151it.effect("publishes by creating the repository, adding a remote, and pushing upstream", () => {
152 const createCalls: Array<{ cwd: string; repository: string; visibility: string }> = [];
153 const remoteCalls: Array<{ cwd: string; preferredName: string; url: string }> = [];
154 const pushCalls: Array<{ cwd: string; remoteName: string | null | undefined }> = [];
155 const provider = makeProvider({
156 createRepository: (input) =>
157 Effect.sync(() => {
158 createCalls.push({
159 cwd: input.cwd,
160 repository: input.repository,
161 visibility: input.visibility,
162 });
163 return CLONE_URLS;
164 }),
165 });
166​
167 return Effect.gen(function* () {
168 const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
169 const result = yield* service.publishRepository({
170 cwd: "/workspace",
171 provider: "github",
172 repository: "octocat/t3code",
173 visibility: "private",
174 remoteName: "origin",
175 protocol: "ssh",
176 });
177​
178 assert.deepStrictEqual(result, {
179 repository: { provider: "github", ...CLONE_URLS },
180 remoteName: "origin",
181 remoteUrl: CLONE_URLS.sshUrl,
182 branch: "feature/remote-v1",
183 upstreamBranch: "origin/feature/remote-v1",
184 status: "pushed",
185 });
186 assert.deepStrictEqual(createCalls, [
187 { cwd: "/workspace", repository: "octocat/t3code", visibility: "private" },
188 ]);
189 assert.deepStrictEqual(remoteCalls, [
190 { cwd: "/workspace", preferredName: "origin", url: CLONE_URLS.sshUrl },
191 ]);
192 assert.deepStrictEqual(pushCalls, [{ cwd: "/workspace", remoteName: "origin" }]);
193 }).pipe(
194 Effect.provide(
195 makeLayer({
196 provider,
197 git: {
198 ensureRemote: (input) =>
199 Effect.sync(() => {
200 remoteCalls.push(input);
201 return "origin";
202 }),
203 pushCurrentBranch: (cwd, _fallbackBranch, options) =>
204 Effect.sync(() => {
205 pushCalls.push({ cwd, remoteName: options?.remoteName });
206 return {
207 status: "pushed" as const,
208 branch: "feature/remote-v1",
209 upstreamBranch: "origin/feature/remote-v1",
210 setUpstream: true,
211 };
212 }),
213 },
214 }),
215 ),
216 );
217});
218​
219it.effect("publishes to the remote name returned by ensureRemote", () => {
220 const pushCalls: Array<{ cwd: string; remoteName: string | null | undefined }> = [];
221​
222 return Effect.gen(function* () {
223 const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
224 const result = yield* service.publishRepository({
225 cwd: "/workspace",
226 provider: "github",
227 repository: "octocat/t3code",
228 visibility: "private",
229 remoteName: "origin",
230 protocol: "ssh",
231 });
232​
233 assert.equal(result.remoteName, "origin-1");
234 assert.deepStrictEqual(pushCalls, [{ cwd: "/workspace", remoteName: "origin-1" }]);
235 }).pipe(
236 Effect.provide(
237 makeLayer({
238 git: {
239 ensureRemote: () => Effect.succeed("origin-1"),
240 pushCurrentBranch: (cwd, _fallbackBranch, options) =>
241 Effect.sync(() => {
242 pushCalls.push({ cwd, remoteName: options?.remoteName });
243 return {
244 status: "pushed" as const,
245 branch: "feature/remote-v1",
246 upstreamBranch: `${options?.remoteName ?? "missing"}/feature/remote-v1`,
247 setUpstream: true,
248 };
249 }),
250 },
251 }),
252 ),
253 );
254});
255​
256it.effect("publish succeeds with status remote_added when the local repo has no commits", () => {
257 let pushCalls = 0;
258 return Effect.gen(function* () {
259 const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
260 const result = yield* service.publishRepository({
261 cwd: "/workspace",
262 provider: "github",
263 repository: "octocat/t3code",
264 visibility: "private",
265 remoteName: "origin",
266 protocol: "ssh",
267 });
268​
269 assert.deepStrictEqual(result, {
270 repository: { provider: "github", ...CLONE_URLS },
271 remoteName: "origin",
272 remoteUrl: CLONE_URLS.sshUrl,
273 branch: "main",
274 status: "remote_added",
275 });
276 assert.strictEqual(pushCalls, 0);
277 }).pipe(
278 Effect.provide(
279 makeLayer({
280 git: {
281 execute: (input) =>
282 input.args[0] === "rev-parse"
283 ? Effect.fail(
284 new GitCommandError({
285 operation: input.operation,
286 command: "git rev-parse --verify HEAD",
287 cwd: input.cwd,
288 detail: "fatal: Needed a single revision",
289 }),
290 )
291 : Effect.succeed(processOutput()),
292 statusDetails: () =>
293 Effect.succeed({
294 isRepo: true,
295 hasOriginRemote: true,
296 isDefaultBranch: true,
297 branch: "main",
298 upstreamRef: null,
299 hasWorkingTreeChanges: false,
300 workingTree: { files: [], insertions: 0, deletions: 0 },
301 hasUpstream: false,
302 aheadCount: 0,
303 behindCount: 0,
304 aheadOfDefaultCount: 0,
305 }),
306 pushCurrentBranch: () =>
307 Effect.sync(() => {
308 pushCalls += 1;
309 return {
310 status: "pushed" as const,
311 branch: "main",
312 upstreamBranch: "origin/main",
313 setUpstream: true,
314 };
315 }),
316 },
317 }),
318 ),
319 );
320});