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

SourceControlRepositoryService.test.ts

318 lines10.5 KB

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