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

RepositoryIdentityResolver.test.ts

223 lines8.7 KB

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