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

RepositoryIdentityResolver.test.ts

591 lines26.4 KB

1// @effect-diagnostics nodeBuiltinImport:off - realpathSync.native resolves Windows 8.3 short names, which the Effect realPath does not.
2import * as NodeFS from "node:fs";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { expect, it } from "@effect/vitest";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10import { vi } from "vite-plus/test";
11​
12import * as ProcessRunner from "../processRunner.ts";
13import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
14import * as RepositoryIdentityResolver from "./RepositoryIdentityResolver.ts";
15​
16const normalizePathSeparators = (value: string) => value.replaceAll("\\", "/");
17const normalizeResolvedPath = (value: string) => normalizePathSeparators(value);
18​
19// Windows forbids CR and LF in file names.
20const lineBreakSuffixes = HostProcess.Platform.defaultValue() === "win32" ? [] : ["\r", "\n"];
21const bareLayouts = [
22 "bare clone",
23 "dot-bare",
24 ...lineBreakSuffixes.map((suffix) => `bare clone${suffix}`),
25];
26​
27const git = (cwd: string, args: ReadonlyArray<string>) =>
28 Effect.gen(function* () {
29 const processRunner = yield* ProcessRunner.ProcessRunner;
30 return yield* processRunner.run({
31 command: "git",
32 args: ["-C", cwd, ...args],
33 });
34 }).pipe(Effect.provide(ProcessRunner.layer));
35​
36it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
37 it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () =>
38 Effect.gen(function* () {
39 const fileSystem = yield* FileSystem.FileSystem;
40 const path = yield* Path.Path;
41 const cwd = yield* fileSystem.makeTempDirectoryScoped({
42 prefix: "t3-repository-identity-test-",
43 });
44​
45 yield* git(cwd, ["init"]);
46 yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
47​
48 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
49 const identity = yield* resolver.resolve(cwd);
50 // Native realpath matches git's long paths when Windows temp dirs use 8.3 names.
51 const resolvedIdentityRoot =
52 identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
53 const resolvedCwd = NodeFS.realpathSync.native(cwd);
54​
55 expect(identity).not.toBeNull();
56 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
57 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(normalizeResolvedPath(resolvedCwd));
58 expect(identity?.displayName).toBe("T3Tools/t3code");
59 expect(identity?.provider).toBe("github");
60 expect(identity?.owner).toBe("T3Tools");
61 expect(identity?.name).toBe("t3code");
62 expect(yield* resolver.resolve(path.join(cwd, ".git"))).toBeNull();
63 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
64 );
65​
66 it.effect("identifies the host of an SSH remote that leaves its user to ssh config", () =>
67 Effect.gen(function* () {
68 const fileSystem = yield* FileSystem.FileSystem;
69 const cwd = yield* fileSystem.makeTempDirectoryScoped({
70 prefix: "t3-repository-identity-userless-remote-test-",
71 });
72​
73 yield* git(cwd, ["init"]);
74 yield* git(cwd, ["remote", "add", "origin", "github.com:T3Tools/t3code.git"]);
75​
76 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
77 const identity = yield* resolver.resolve(cwd);
78​
79 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
80 expect(identity?.provider).toBe("github");
81 expect(identity?.displayName).toBe("T3Tools/t3code");
82 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
83 );
84​
85 it.effect("keys a remote behind an SSH host alias by the host ssh config resolves it to", () =>
86 Effect.gen(function* () {
87 const fileSystem = yield* FileSystem.FileSystem;
88 const aliasDir = yield* fileSystem.makeTempDirectoryScoped({
89 prefix: "t3-repository-identity-ssh-alias-test-",
90 });
91 const plainDir = yield* fileSystem.makeTempDirectoryScoped({
92 prefix: "t3-repository-identity-ssh-plain-test-",
93 });
94​
95 yield* git(aliasDir, ["init"]);
96 yield* git(aliasDir, ["remote", "add", "origin", "git@github.com-personal:owner/repo.git"]);
97 yield* git(plainDir, ["init"]);
98 yield* git(plainDir, ["remote", "add", "origin", "git@github.com:owner/repo.git"]);
99​
100 const runner = yield* ProcessRunner.ProcessRunner;
101 // Stands in for `Host github.com-personal` / `HostName github.com` in ~/.ssh/config.
102 const resolver = yield* RepositoryIdentityResolver.make().pipe(
103 Effect.provideService(ProcessRunner.ProcessRunner, {
104 run: (input) =>
105 input.command === "ssh" && input.args.includes("-G")
106 ? Effect.succeed({
107 stdout: input.args.some((arg) => arg.includes("github.com-personal"))
108 ? "user git\nhostname github.com\nport 22\n"
109 : `hostname ${input.args.at(-1) ?? ""}\nport 22\n`,
110 stderr: "",
111 code: ChildProcessSpawner.ExitCode(0),
112 timedOut: false,
113 stdoutTruncated: false,
114 stderrTruncated: false,
115 stdoutInvalidUtf8: false,
116 stderrInvalidUtf8: false,
117 })
118 : runner.run(input),
119 }),
120 );
121​
122 const aliasIdentity = yield* resolver.resolve(aliasDir);
123 const plainIdentity = yield* resolver.resolve(plainDir);
124​
125 expect(plainIdentity?.canonicalKey).toBe("github.com/owner/repo");
126 expect(aliasIdentity?.canonicalKey).toBe(plainIdentity?.canonicalKey);
127 expect(aliasIdentity?.locator.remoteUrl).toBe("git@github.com-personal:owner/repo.git");
128 }).pipe(Effect.provide(ProcessRunner.layer)),
129 );
130​
131 it.effect.each(["origin", "team\u3000"])(
132 "keys remote %j by its configured URL when insteadOf rewrites it",
133 (remoteName) =>
134 Effect.gen(function* () {
135 const fileSystem = yield* FileSystem.FileSystem;
136 const cwd = yield* fileSystem.makeTempDirectoryScoped({
137 prefix: "t3-repository-identity-instead-of-test-",
138 });
139​
140 yield* git(cwd, ["init"]);
141 yield* git(cwd, ["remote", "add", remoteName, "https://github.com/T3Tools/t3code.git"]);
142 yield* git(cwd, ["config", "url.https://proxy.example/.insteadOf", "https://github.com/"]);
143 yield* git(cwd, ["config", `remote.${remoteName}.gh-resolved`, "base"]);
144 const remotes = yield* git(cwd, ["remote", "-v"]);
145 expect(remotes.stdout).toContain("https://proxy.example/T3Tools/t3code.git (fetch)");
146​
147 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
148 const identity = yield* resolver.resolve(cwd);
149​
150 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
151 expect(identity?.provider).toBe("github");
152 expect(identity?.displayName).toBe("T3Tools/t3code");
153 expect(identity?.locator).toEqual({
154 source: "git-remote",
155 remoteName,
156 remoteUrl: "https://github.com/T3Tools/t3code.git",
157 });
158​
159 const shorthandCwd = yield* fileSystem.makeTempDirectoryScoped({
160 prefix: "t3-repository-identity-instead-of-shorthand-test-",
161 });
162 yield* git(shorthandCwd, ["init"]);
163 yield* git(shorthandCwd, ["remote", "add", "origin", "gh:T3Tools/t3code.git"]);
164 yield* git(shorthandCwd, ["config", "url.https://github.com/.insteadOf", "gh:"]);
165​
166 const shorthandIdentity = yield* resolver.resolve(shorthandCwd);
167​
168 expect(shorthandIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
169 expect(shorthandIdentity?.provider).toBe("github");
170 expect(shorthandIdentity?.locator.remoteUrl).toBe("https://github.com/T3Tools/t3code.git");
171 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
172 );
173​
174 it.effect("keeps a literal .git in an Azure DevOps repository name", () =>
175 Effect.gen(function* () {
176 const fileSystem = yield* FileSystem.FileSystem;
177 const cwd = yield* fileSystem.makeTempDirectoryScoped({
178 prefix: "t3-repository-identity-azure-git-suffix-test-",
179 });
180​
181 yield* git(cwd, ["init"]);
182 yield* git(cwd, [
183 "remote",
184 "add",
185 "origin",
186 "https://dev.azure.com/Acme/Platform/_git/Tool.git",
187 ]);
188​
189 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
190 const identity = yield* resolver.resolve(cwd);
191​
192 expect(identity?.displayName).toBe("Acme/Platform/_git/Tool.git");
193 expect(identity?.name).toBe("Tool.git");
194 expect(sourceControlDefinitions.repositorySelector(identity)).toBe("Tool.git");
195 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
196 );
197​
198 it.effect("resolves the fetch remote of a partial clone", () =>
199 Effect.gen(function* () {
200 const fileSystem = yield* FileSystem.FileSystem;
201 const cwd = yield* fileSystem.makeTempDirectoryScoped({
202 prefix: "t3-repository-identity-partial-clone-test-",
203 });
204​
205 yield* git(cwd, ["init"]);
206 yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
207 yield* git(cwd, ["remote", "set-url", "--push", "origin", "git@github.com:T3Tools/push.git"]);
208 yield* git(cwd, ["config", "remote.origin.promisor", "true"]);
209 yield* git(cwd, ["config", "remote.origin.partialclonefilter", "blob:none"]);
210​
211 const remotes = yield* git(cwd, ["remote", "-v"]);
212 expect(remotes.stdout).toContain("(fetch) [blob:none]");
213​
214 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
215 const identity = yield* resolver.resolve(cwd);
216 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
217 expect(identity?.locator.remoteUrl).toBe("git@github.com:T3Tools/t3code.git");
218 expect(identity?.locator.remoteName).toBe("origin");
219 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
220 );
221​
222 it.effect("resolves a remote whose URL contains a space", () =>
223 Effect.gen(function* () {
224 const fileSystem = yield* FileSystem.FileSystem;
225 const cwd = yield* fileSystem.makeTempDirectoryScoped({
226 prefix: "t3-repository-identity-spaced-url-test-",
227 });
228​
229 yield* git(cwd, ["init"]);
230 yield* git(cwd, ["remote", "add", "origin", "ssh://git@git.example.test/team repos/app.git"]);
231​
232 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
233 const identity = yield* resolver.resolve(cwd);
234 expect(identity?.locator.remoteUrl).toBe("ssh://git@git.example.test/team repos/app.git");
235 expect(identity?.locator.remoteName).toBe("origin");
236 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
237 );
238​
239 it.effect("keeps a login embedded in a remote URL out of the identity", () =>
240 Effect.gen(function* () {
241 const fileSystem = yield* FileSystem.FileSystem;
242 const cwd = yield* fileSystem.makeTempDirectoryScoped({
243 prefix: "t3-repository-identity-credentials-test-",
244 });
245​
246 yield* git(cwd, ["init"]);
247 yield* git(cwd, [
248 "remote",
249 "add",
250 "origin",
251 "https://me:origin-secret@github.com/me/t3code.git",
252 ]);
253 yield* git(cwd, [
254 "remote",
255 "add",
256 "upstream",
257 "https://x-access-token:upstream-secret@github.com/T3Tools/t3code.git?token=query-secret",
258 ]);
259​
260 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
261 const identity = yield* resolver.resolve(cwd);
262 expect(identity?.locator.remoteUrl).toBe("https://github.com/T3Tools/t3code.git");
263 expect(identity?.origin?.remoteUrl).toBe("https://github.com/me/t3code.git");
264 expect(JSON.stringify(identity)).not.toContain("secret");
265 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
266 );
267​
268 it.effect.each(["repo", "repo ", ...lineBreakSuffixes.map((suffix) => `repo${suffix}`)])(
269 "returns the git top-level root path when resolving from a nested workspace in %j",
270 (rootName) =>
271 Effect.gen(function* () {
272 const fileSystem = yield* FileSystem.FileSystem;
273 const path = yield* Path.Path;
274 const tempDir = yield* fileSystem.makeTempDirectoryScoped({
275 prefix: "t3-repository-identity-nested-root-test-",
276 });
277 const repoRoot = path.join(tempDir, rootName);
278 const nestedWorkspace = path.join(repoRoot, "packages", "web");
279​
280 yield* fileSystem.makeDirectory(nestedWorkspace, { recursive: true });
281 yield* git(repoRoot, ["init"]);
282 yield* git(repoRoot, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
283​
284 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
285 const identity = yield* resolver.resolve(nestedWorkspace);
286 const resolvedIdentityRoot =
287 identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
288 const resolvedRepoRoot = NodeFS.realpathSync.native(repoRoot);
289​
290 expect(identity).not.toBeNull();
291 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
292 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
293 normalizeResolvedPath(resolvedRepoRoot),
294 );
295 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
296 );
297​
298 it.effect.each(bareLayouts)(
299 "resolves a %j to its Git directory and the same identity as a checkout",
300 (layout) =>
301 Effect.gen(function* () {
302 const fileSystem = yield* FileSystem.FileSystem;
303 const path = yield* Path.Path;
304 const checkout = yield* fileSystem.makeTempDirectoryScoped({
305 prefix: "t3-repository-identity-checkout-",
306 });
307 const parent = yield* fileSystem.makeTempDirectoryScoped({
308 prefix: "t3-repository-identity-bare-",
309 });
310 const root = path.join(parent, layout);
311 yield* fileSystem.makeDirectory(root);
312 const bareGitDir = layout === "dot-bare" ? path.join(root, ".bare") : root;
313​
314 yield* git(checkout, ["init"]);
315 yield* git(checkout, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
316 yield* git(root, ["init", "--bare", bareGitDir]);
317 if (layout === "dot-bare") {
318 yield* fileSystem.writeFileString(path.join(root, ".git"), "gitdir: ./.bare\n");
319 }
320 yield* git(root, [
321 "--git-dir",
322 bareGitDir,
323 "remote",
324 "add",
325 "origin",
326 "https://github.com/T3Tools/t3code.git",
327 ]);
328​
329 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
330 const checkoutIdentity = yield* resolver.resolve(checkout);
331 expect(checkoutIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
332 const resolvedBareGitDir = NodeFS.realpathSync.native(bareGitDir);
333 const projectRoots = layout === "dot-bare" ? [root, bareGitDir] : [root];
334​
335 for (const projectRoot of projectRoots) {
336 const identity = yield* resolver.resolve(projectRoot);
337 const resolvedIdentityRoot =
338 identity?.rootPath === undefined ? "" : NodeFS.realpathSync.native(identity.rootPath);
339​
340 expect(identity).not.toBeNull();
341 expect(identity?.canonicalKey).toBe(checkoutIdentity?.canonicalKey);
342 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
343 normalizeResolvedPath(resolvedBareGitDir),
344 );
345 }
346​
347 yield* git(root, ["--git-dir", bareGitDir, "remote", "remove", "origin"]);
348 for (const projectRoot of projectRoots) {
349 expect(yield* resolver.resolve(projectRoot, { refresh: true })).toBeNull();
350 }
351 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
352 );
353​
354 it.effect(
355 "returns null for non-git, missing and no-remote folders, probing only what can exist",
356 () =>
357 Effect.gen(function* () {
358 const fileSystem = yield* FileSystem.FileSystem;
359 const path = yield* Path.Path;
360 const nonGitDir = yield* fileSystem.makeTempDirectoryScoped({
361 prefix: "t3-repository-identity-non-git-",
362 });
363 const gitDir = yield* fileSystem.makeTempDirectoryScoped({
364 prefix: "t3-repository-identity-no-remote-",
365 });
366​
367 yield* git(gitDir, ["init"]);
368​
369 const runner = yield* ProcessRunner.ProcessRunner;
370 const runs: Array<ReadonlyArray<string>> = [];
371 const resolver = yield* RepositoryIdentityResolver.make().pipe(
372 Effect.provideService(ProcessRunner.ProcessRunner, {
373 run: (input) =>
374 Effect.suspend(() => {
375 runs.push(input.args);
376 return runner.run(input);
377 }),
378 }),
379 );
380 const bareProbes = () =>
381 runs.filter((args) => args.some((arg) => arg.startsWith("--git-dir=")));
382​
383 expect(yield* resolver.resolve(path.join(nonGitDir, "deleted"))).toBeNull();
384 expect(runs).toEqual([]);
385​
386 expect(yield* resolver.resolve(nonGitDir)).toBeNull();
387 expect(bareProbes()).toEqual([]);
388​
389 expect(yield* resolver.resolve(gitDir)).toBeNull();
390 }).pipe(Effect.provide(ProcessRunner.layer)),
391 );
392​
393 it.effect("ignores local-path remotes such as GitButler's gb-local", () =>
394 Effect.gen(function* () {
395 const fileSystem = yield* FileSystem.FileSystem;
396 const localOnlyDir = yield* fileSystem.makeTempDirectoryScoped({
397 prefix: "t3-repository-identity-local-remote-",
398 });
399 const mixedDir = yield* fileSystem.makeTempDirectoryScoped({
400 prefix: "t3-repository-identity-mixed-remote-",
401 });
402​
403 yield* git(localOnlyDir, ["init"]);
404 yield* git(localOnlyDir, ["remote", "add", "gb-local", "."]);
405 yield* git(localOnlyDir, ["remote", "add", "backup", "/srv/git/repo.git"]);
406 yield* git(localOnlyDir, ["remote", "add", "archive", "file:///srv/git/repo.git"]);
407 yield* git(mixedDir, ["init"]);
408 yield* git(mixedDir, ["remote", "add", "gb-local", "."]);
409 yield* git(mixedDir, ["remote", "add", "mirror", "git@github.com:T3Tools/t3code.git"]);
410​
411 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
412 expect(yield* resolver.resolve(localOnlyDir)).toBeNull();
413​
414 const mixedIdentity = yield* resolver.resolve(mixedDir);
415 expect(mixedIdentity?.locator.remoteName).toBe("mirror");
416 expect(mixedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
417 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
418 );
419​
420 it.effect.each(["add", "replace"] as const)(
421 "refreshes the primary upstream after %s before cache expiry",
422 (change) =>
423 Effect.gen(function* () {
424 const fileSystem = yield* FileSystem.FileSystem;
425 const cwd = yield* fileSystem.makeTempDirectoryScoped({
426 prefix: "t3-repository-identity-upstream-test-",
427 });
428​
429 yield* git(cwd, ["init"]);
430 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code.git"]);
431 if (change === "replace") {
432 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/previous.git"]);
433 }
434​
435 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
436 const initialIdentity = yield* resolver.resolve(cwd);
437 expect(initialIdentity?.canonicalKey).toBe(
438 change === "add" ? "github.com/julius/t3code" : "github.com/t3tools/previous",
439 );
440​
441 yield* git(cwd, [
442 "remote",
443 change === "add" ? "add" : "set-url",
444 "upstream",
445 "git@github.com:T3Tools/t3code.git",
446 ]);
447 expect(yield* resolver.resolve(cwd)).toEqual(initialIdentity);
448 const identity = yield* resolver.resolve(cwd, { refresh: true });
449​
450 expect(identity).not.toBeNull();
451 expect(identity?.locator.remoteName).toBe("upstream");
452 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
453 expect(identity?.displayName).toBe("T3Tools/t3code");
454 expect(yield* resolver.resolve(cwd)).toEqual(identity);
455 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
456 );
457​
458 it.effect("reports a fork's own remote as origin next to the upstream identity", () =>
459 Effect.gen(function* () {
460 const fileSystem = yield* FileSystem.FileSystem;
461 const cwd = yield* fileSystem.makeTempDirectoryScoped({
462 prefix: "t3-repository-identity-fork-test-",
463 });
464​
465 yield* git(cwd, ["init"]);
466 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
467 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
468​
469 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
470 const identity = yield* resolver.resolve(cwd);
471​
472 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
473 expect(identity?.displayName).toBe("T3Tools/t3code");
474 expect(identity?.origin).toEqual({
475 canonicalKey: "github.com/julius/t3code-fork",
476 remoteUrl: "git@github.com:julius/t3code-fork.git",
477 displayName: "julius/t3code-fork",
478 });
479 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
480 );
481​
482 it.effect("uses the fork as the identity when gh repo set-default points at it", () =>
483 Effect.gen(function* () {
484 const fileSystem = yield* FileSystem.FileSystem;
485 const cwd = yield* fileSystem.makeTempDirectoryScoped({
486 prefix: "t3-repository-identity-gh-default-test-",
487 });
488​
489 yield* git(cwd, ["init"]);
490 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
491 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
492 yield* git(cwd, ["config", "remote.origin.gh-resolved", "base"]);
493​
494 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
495 const identity = yield* resolver.resolve(cwd);
496​
497 expect(identity?.canonicalKey).toBe("github.com/julius/t3code-fork");
498 expect(identity?.locator.remoteName).toBe("origin");
499 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
500 );
501​
502 it.effect("follows an OWNER/REPO mark only to a remote that fetches that repository", () =>
503 Effect.gen(function* () {
504 const fileSystem = yield* FileSystem.FileSystem;
505 const cwd = yield* fileSystem.makeTempDirectoryScoped({
506 prefix: "t3-repository-identity-gh-parent-test-",
507 });
508 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
509 const resolveFresh = () => resolver.resolve(cwd, { refresh: true });
510​
511 yield* git(cwd, ["init"]);
512 yield* git(cwd, ["remote", "add", "fork", "https://github.com/julius/t3code-fork.git"]);
513 yield* git(cwd, ["remote", "add", "main", "https://github.com/T3Tools/t3code.git"]);
514 yield* git(cwd, ["remote", "add", "upstream", "https://github.com/other/t3code.git"]);
515 yield* git(cwd, ["config", "remote.fork.gh-resolved", "t3tools/t3code"]);
516 const parent = yield* resolveFresh();
517 expect(parent?.canonicalKey).toBe("github.com/t3tools/t3code");
518 expect(parent?.locator).toEqual({
519 source: "git-remote",
520 remoteName: "main",
521 remoteUrl: "https://github.com/T3Tools/t3code.git",
522 });
523​
524 // No remote fetches the marked repository, and several marks leave gh's choice unknown.
525 yield* git(cwd, ["config", "remote.fork.gh-resolved", "someone/else"]);
526 expect((yield* resolveFresh())?.locator.remoteName).toBe("upstream");
527 yield* git(cwd, ["config", "remote.fork.gh-resolved", "base"]);
528 yield* git(cwd, ["config", "remote.main.gh-resolved", "base"]);
529 expect((yield* resolveFresh())?.locator.remoteName).toBe("upstream");
530​
531 // The same path on another HTTPS port is a different server.
532 yield* git(cwd, ["config", "--unset", "remote.fork.gh-resolved"]);
533 yield* git(cwd, ["config", "--unset", "remote.main.gh-resolved"]);
534 yield* git(cwd, ["remote", "add", "backup", "https://ghe.example.com:9443/acme/web.git"]);
535 yield* git(cwd, ["remote", "add", "ghe-fork", "https://ghe.example.com:8443/me/web.git"]);
536 yield* git(cwd, ["remote", "add", "ghe-parent", "https://ghe.example.com:8443/acme/web.git"]);
537 yield* git(cwd, ["config", "remote.ghe-fork.gh-resolved", "acme/web"]);
538 expect((yield* resolveFresh())?.locator.remoteName).toBe("ghe-parent");
539 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
540 );
541​
542 it.effect("reads the project's gh default when the server inherited GIT_DIR", () =>
543 Effect.gen(function* () {
544 const fileSystem = yield* FileSystem.FileSystem;
545 const path = yield* Path.Path;
546 const cwd = yield* fileSystem.makeTempDirectoryScoped({
547 prefix: "t3-repository-identity-gh-default-git-dir-",
548 });
549 const other = yield* fileSystem.makeTempDirectoryScoped({
550 prefix: "t3-repository-identity-inherited-git-dir-",
551 });
552​
553 yield* git(cwd, ["init"]);
554 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code-fork.git"]);
555 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
556 yield* git(cwd, ["config", "remote.origin.gh-resolved", "base"]);
557 yield* git(other, ["init"]);
558 yield* git(other, ["config", "remote.upstream.gh-resolved", "base"]);
559​
560 vi.stubEnv("GIT_DIR", path.join(other, ".git"));
561 yield* Effect.addFinalizer(() => Effect.sync(() => vi.unstubAllEnvs()));
562​
563 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
564 const identity = yield* resolver.resolve(cwd);
565​
566 expect(identity?.canonicalKey).toBe("github.com/julius/t3code-fork");
567 expect(identity?.locator.remoteName).toBe("origin");
568 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
569 );
570​
571 it.effect("uses the last remote path segment as the repository name for nested groups", () =>
572 Effect.gen(function* () {
573 const fileSystem = yield* FileSystem.FileSystem;
574 const cwd = yield* fileSystem.makeTempDirectoryScoped({
575 prefix: "t3-repository-identity-nested-group-test-",
576 });
577​
578 yield* git(cwd, ["init"]);
579 yield* git(cwd, ["remote", "add", "origin", "git@gitlab.com:T3Tools/platform/t3code.git"]);
580​
581 const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
582 const identity = yield* resolver.resolve(cwd);
583​
584 expect(identity).not.toBeNull();
585 expect(identity?.canonicalKey).toBe("gitlab.com/t3tools/platform/t3code");
586 expect(identity?.displayName).toBe("T3Tools/platform/t3code");
587 expect(identity?.owner).toBe("T3Tools");
588 expect(identity?.name).toBe("t3code");
589 }).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
590 );
591});