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

RepositoryIdentityResolver.test.ts

239 lines9.4 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import { TestClock } from "effect/testing";
9​
10import * as ProcessRunner from "../../processRunner.ts";
11import { RepositoryIdentityResolver } from "../Services/RepositoryIdentityResolver.ts";
12import {
13 makeRepositoryIdentityResolver,
14 RepositoryIdentityResolverLive,
15} from "./RepositoryIdentityResolver.ts";
16​
17const normalizePathSeparators = (value: string) => value.replaceAll("\\", "/");
18const normalizeResolvedPath = (value: string) => normalizePathSeparators(value);
19​
20const git = (cwd: string, args: ReadonlyArray<string>) =>
21 Effect.gen(function* () {
22 const processRunner = yield* ProcessRunner.ProcessRunner;
23 return yield* processRunner.run({
24 command: "git",
25 args: ["-C", cwd, ...args],
26 });
27 }).pipe(Effect.provide(ProcessRunner.layer));
28​
29const makeRepositoryIdentityResolverTestLayer = (options: {
30 readonly positiveCacheTtl?: Duration.Input;
31 readonly negativeCacheTtl?: Duration.Input;
32}) =>
33 Layer.effect(
34 RepositoryIdentityResolver,
35 makeRepositoryIdentityResolver({
36 cacheCapacity: 16,
37 ...options,
38 }),
39 ).pipe(Layer.provide(ProcessRunner.layer));
40​
41it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
42 it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () =>
43 Effect.gen(function* () {
44 const fileSystem = yield* FileSystem.FileSystem;
45 const cwd = yield* fileSystem.makeTempDirectoryScoped({
46 prefix: "t3-repository-identity-test-",
47 });
48​
49 yield* git(cwd, ["init"]);
50 yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
51​
52 const resolver = yield* RepositoryIdentityResolver;
53 const identity = yield* resolver.resolve(cwd);
54 const resolvedIdentityRoot =
55 identity?.rootPath === undefined ? "" : yield* fileSystem.realPath(identity.rootPath);
56 const resolvedCwd = yield* fileSystem.realPath(cwd);
57​
58 expect(identity).not.toBeNull();
59 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
60 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(normalizeResolvedPath(resolvedCwd));
61 expect(identity?.displayName).toBe("t3tools/t3code");
62 expect(identity?.provider).toBe("github");
63 expect(identity?.owner).toBe("t3tools");
64 expect(identity?.name).toBe("t3code");
65 }).pipe(Effect.provide(RepositoryIdentityResolverLive)),
66 );
67​
68 it.effect("returns the git top-level root path when resolving from a nested workspace", () =>
69 Effect.gen(function* () {
70 const fileSystem = yield* FileSystem.FileSystem;
71 const path = yield* Path.Path;
72 const repoRoot = yield* fileSystem.makeTempDirectoryScoped({
73 prefix: "t3-repository-identity-nested-root-test-",
74 });
75 const nestedWorkspace = path.join(repoRoot, "packages", "web");
76​
77 yield* fileSystem.makeDirectory(nestedWorkspace, { recursive: true });
78 yield* git(repoRoot, ["init"]);
79 yield* git(repoRoot, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
80​
81 const resolver = yield* RepositoryIdentityResolver;
82 const identity = yield* resolver.resolve(nestedWorkspace);
83 const resolvedIdentityRoot =
84 identity?.rootPath === undefined ? "" : yield* fileSystem.realPath(identity.rootPath);
85 const resolvedRepoRoot = yield* fileSystem.realPath(repoRoot);
86​
87 expect(identity).not.toBeNull();
88 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
89 expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
90 normalizeResolvedPath(resolvedRepoRoot),
91 );
92 }).pipe(Effect.provide(RepositoryIdentityResolverLive)),
93 );
94​
95 it.effect("returns null for non-git folders and repos without remotes", () =>
96 Effect.gen(function* () {
97 const fileSystem = yield* FileSystem.FileSystem;
98 const nonGitDir = yield* fileSystem.makeTempDirectoryScoped({
99 prefix: "t3-repository-identity-non-git-",
100 });
101 const gitDir = yield* fileSystem.makeTempDirectoryScoped({
102 prefix: "t3-repository-identity-no-remote-",
103 });
104​
105 yield* git(gitDir, ["init"]);
106​
107 const resolver = yield* RepositoryIdentityResolver;
108 const nonGitIdentity = yield* resolver.resolve(nonGitDir);
109 const noRemoteIdentity = yield* resolver.resolve(gitDir);
110​
111 expect(nonGitIdentity).toBeNull();
112 expect(noRemoteIdentity).toBeNull();
113 }).pipe(Effect.provide(RepositoryIdentityResolverLive)),
114 );
115​
116 it.effect("prefers upstream over origin when both remotes are configured", () =>
117 Effect.gen(function* () {
118 const fileSystem = yield* FileSystem.FileSystem;
119 const cwd = yield* fileSystem.makeTempDirectoryScoped({
120 prefix: "t3-repository-identity-upstream-test-",
121 });
122​
123 yield* git(cwd, ["init"]);
124 yield* git(cwd, ["remote", "add", "origin", "git@github.com:julius/t3code.git"]);
125 yield* git(cwd, ["remote", "add", "upstream", "git@github.com:T3Tools/t3code.git"]);
126​
127 const resolver = yield* RepositoryIdentityResolver;
128 const identity = yield* resolver.resolve(cwd);
129​
130 expect(identity).not.toBeNull();
131 expect(identity?.locator.remoteName).toBe("upstream");
132 expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
133 expect(identity?.displayName).toBe("t3tools/t3code");
134 }).pipe(Effect.provide(RepositoryIdentityResolverLive)),
135 );
136​
137 it.effect("uses the last remote path segment as the repository name for nested groups", () =>
138 Effect.gen(function* () {
139 const fileSystem = yield* FileSystem.FileSystem;
140 const cwd = yield* fileSystem.makeTempDirectoryScoped({
141 prefix: "t3-repository-identity-nested-group-test-",
142 });
143​
144 yield* git(cwd, ["init"]);
145 yield* git(cwd, ["remote", "add", "origin", "git@gitlab.com:T3Tools/platform/t3code.git"]);
146​
147 const resolver = yield* RepositoryIdentityResolver;
148 const identity = yield* resolver.resolve(cwd);
149​
150 expect(identity).not.toBeNull();
151 expect(identity?.canonicalKey).toBe("gitlab.com/t3tools/platform/t3code");
152 expect(identity?.displayName).toBe("t3tools/platform/t3code");
153 expect(identity?.owner).toBe("t3tools");
154 expect(identity?.name).toBe("t3code");
155 }).pipe(Effect.provide(RepositoryIdentityResolverLive)),
156 );
157​
158 it.effect(
159 "keeps null identities cached across repeated resolves until the negative TTL expires",
160 () =>
161 Effect.gen(function* () {
162 const fileSystem = yield* FileSystem.FileSystem;
163 const cwd = yield* fileSystem.makeTempDirectoryScoped({
164 prefix: "t3-repository-identity-late-remote-test-",
165 });
166​
167 yield* git(cwd, ["init"]);
168​
169 const resolver = yield* RepositoryIdentityResolver;
170 const initialIdentity = yield* resolver.resolve(cwd);
171 expect(initialIdentity).toBeNull();
172​
173 yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
174​
175 for (const _attempt of [1, 2, 3]) {
176 const cachedIdentity = yield* resolver.resolve(cwd);
177 expect(cachedIdentity).toBeNull();
178 }
179​
180 yield* TestClock.adjust(Duration.millis(120));
181​
182 const refreshedIdentity = yield* resolver.resolve(cwd);
183 expect(refreshedIdentity).not.toBeNull();
184 expect(refreshedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
185 expect(refreshedIdentity?.name).toBe("t3code");
186 }).pipe(
187 Effect.provide(
188 Layer.merge(
189 TestClock.layer(),
190 makeRepositoryIdentityResolverTestLayer({
191 negativeCacheTtl: Duration.millis(50),
192 positiveCacheTtl: Duration.seconds(1),
193 }),
194 ),
195 ),
196 ),
197 );
198​
199 it.effect("refreshes cached identities after the positive TTL when a remote changes", () =>
200 Effect.gen(function* () {
201 const fileSystem = yield* FileSystem.FileSystem;
202 const cwd = yield* fileSystem.makeTempDirectoryScoped({
203 prefix: "t3-repository-identity-remote-change-test-",
204 });
205​
206 yield* git(cwd, ["init"]);
207 yield* git(cwd, ["remote", "add", "origin", "git@github.com:T3Tools/t3code.git"]);
208​
209 const resolver = yield* RepositoryIdentityResolver;
210 const initialIdentity = yield* resolver.resolve(cwd);
211 expect(initialIdentity).not.toBeNull();
212 expect(initialIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
213​
214 yield* git(cwd, ["remote", "set-url", "origin", "git@github.com:T3Tools/t3code-next.git"]);
215​
216 const cachedIdentity = yield* resolver.resolve(cwd);
217 expect(cachedIdentity).not.toBeNull();
218 expect(cachedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
219​
220 yield* TestClock.adjust(Duration.millis(180));
221​
222 const refreshedIdentity = yield* resolver.resolve(cwd);
223 expect(refreshedIdentity).not.toBeNull();
224 expect(refreshedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code-next");
225 expect(refreshedIdentity?.displayName).toBe("t3tools/t3code-next");
226 expect(refreshedIdentity?.name).toBe("t3code-next");
227 }).pipe(
228 Effect.provide(
229 Layer.merge(
230 TestClock.layer(),
231 makeRepositoryIdentityResolverTestLayer({
232 negativeCacheTtl: Duration.millis(50),
233 positiveCacheTtl: Duration.millis(100),
234 }),
235 ),
236 ),
237 ),
238 );
239});