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

RepositoryIdentityResolver.test.ts

193 lines7.3 KB

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