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

RepositoryIdentityResolver.test.ts

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