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

git.test.ts

276 lines9.8 KB

1import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 applyGitStatusStreamEvent,
6 buildTemporaryWorktreeBranchName,
7 isTemporaryWorktreeBranch,
8 normalizeGitRemoteUrl,
9 parseGitHubRepositoryNameWithOwnerFromRemoteUrl,
10 parseOriginUrlFromGitConfig,
11 WORKTREE_BRANCH_PREFIX,
12} from "./git.ts";
13​
14describe("normalizeGitRemoteUrl", () => {
15 it("canonicalizes equivalent GitHub remotes across protocol variants", () => {
16 expect(normalizeGitRemoteUrl("git@github.com:T3Tools/T3Code.git")).toBe(
17 "github.com/t3tools/t3code",
18 );
19 expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git")).toBe(
20 "github.com/t3tools/t3code",
21 );
22 expect(normalizeGitRemoteUrl("ssh://git@github.com/T3Tools/T3Code")).toBe(
23 "github.com/t3tools/t3code",
24 );
25 });
26​
27 it("preserves nested group paths for providers like GitLab", () => {
28 expect(normalizeGitRemoteUrl("git@gitlab.com:T3Tools/platform/T3Code.git")).toBe(
29 "gitlab.com/t3tools/platform/t3code",
30 );
31 expect(normalizeGitRemoteUrl("https://gitlab.com/T3Tools/platform/T3Code.git")).toBe(
32 "gitlab.com/t3tools/platform/t3code",
33 );
34 });
35​
36 it("drops explicit ports from URL-shaped remotes", () => {
37 expect(normalizeGitRemoteUrl("https://gitlab.company.com:8443/team/project.git")).toBe(
38 "gitlab.company.com/team/project",
39 );
40 expect(normalizeGitRemoteUrl("ssh://git@gitlab.company.com:2222/team/project.git")).toBe(
41 "gitlab.company.com/team/project",
42 );
43 });
44​
45 it("normalizes SCP-like remotes with non-git SSH users", () => {
46 expect(normalizeGitRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(
47 "gitlab.example.com/group/project",
48 );
49 expect(normalizeGitRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(
50 "bitbucket.org/workspace/repo",
51 );
52 });
53​
54 it("gives an Azure DevOps repository the same key over SSH as over HTTPS", () => {
55 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v3/T3Tools/Platform/T3Code")).toBe(
56 "dev.azure.com/t3tools/platform/_git/t3code",
57 );
58 expect(normalizeGitRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/T3Tools/Platform/T3Code")).toBe(
59 "dev.azure.com/t3tools/platform/_git/t3code",
60 );
61 expect(
62 normalizeGitRemoteUrl("https://T3Tools@dev.azure.com/T3Tools/Platform/_git/T3Code"),
63 ).toBe("dev.azure.com/t3tools/platform/_git/t3code");
64 });
65​
66 it("puts the organization back in the host on the name dev.azure.com replaced", () => {
67 expect(
68 normalizeGitRemoteUrl("T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code"),
69 ).toBe("t3tools.visualstudio.com/platform/_git/t3code");
70 expect(normalizeGitRemoteUrl("https://T3Tools.visualstudio.com/Platform/_git/T3Code")).toBe(
71 "t3tools.visualstudio.com/platform/_git/t3code",
72 );
73 });
74​
75 it("leaves an Azure SSH host it cannot read as the path it was given", () => {
76 // Not `v3`, and not four segments: rewriting either would invent a repository that the web
77 // spelling has no name for, so the remote stands as it arrived.
78 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v4/T3Tools/Platform/T3Code")).toBe(
79 "ssh.dev.azure.com/v4/t3tools/platform/t3code",
80 );
81 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v3/T3Tools/T3Code")).toBe(
82 "ssh.dev.azure.com/v3/t3tools/t3code",
83 );
84 });
85});
86​
87describe("parseOriginUrlFromGitConfig", () => {
88 it("reads the origin url and ignores other remotes", () => {
89 const config = [
90 "[core]",
91 "\trepositoryformatversion = 0",
92 '[remote "upstream"]',
93 "\turl = https://github.com/other/repo.git",
94 '[remote "origin"]',
95 "\turl = git@github.com:pingdotgg/t3code.git",
96 "\tfetch = +refs/heads/*:refs/remotes/origin/*",
97 '[branch "main"]',
98 "\tremote = origin",
99 ].join("\n");
100 expect(parseOriginUrlFromGitConfig(config)).toBe("git@github.com:pingdotgg/t3code.git");
101 });
102​
103 it("strips inline comments and quotes from the url value", () => {
104 expect(
105 parseOriginUrlFromGitConfig(
106 '[remote "origin"]\n\turl = https://github.com/acme/repo.git # mirror\n',
107 ),
108 ).toBe("https://github.com/acme/repo.git");
109 expect(
110 parseOriginUrlFromGitConfig('[remote "origin"]\n\turl = "git@github.com:acme/repo.git"\n'),
111 ).toBe("git@github.com:acme/repo.git");
112 });
113​
114 it("accepts legacy dotted headers, header comments, and line continuations", () => {
115 expect(parseOriginUrlFromGitConfig("[remote.origin]\n\turl = git@github.com:a/b.git\n")).toBe(
116 "git@github.com:a/b.git",
117 );
118 // Git folds the dotted form to lowercase but keeps quoted names as written.
119 expect(parseOriginUrlFromGitConfig("[remote.Origin]\n\turl = git@github.com:a/b.git\n")).toBe(
120 "git@github.com:a/b.git",
121 );
122 expect(
123 parseOriginUrlFromGitConfig(
124 '[remote "Origin"]\n\turl = git@github.com:x/y.git\n[remote "origin"]\n\turl = git@github.com:a/b.git\n',
125 ),
126 ).toBe("git@github.com:a/b.git");
127 expect(
128 parseOriginUrlFromGitConfig('[remote "origin"] # primary\n\turl = git@github.com:a/b.git\n'),
129 ).toBe("git@github.com:a/b.git");
130 expect(
131 parseOriginUrlFromGitConfig(
132 '[remote "origin"]\n\turl = https://github.com/acme/\\\n\t\trepo.git\n',
133 ),
134 ).toBe("https://github.com/acme/repo.git");
135 });
136​
137 it("falls back to the first remote when there is no origin", () => {
138 const config = [
139 '[remote "upstream"]',
140 "\turl = https://github.com/acme/repo.git",
141 '[remote "fork"]',
142 "\turl = https://github.com/me/repo.git",
143 ].join("\n");
144 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
145 });
146​
147 it("returns null when there is no remote section", () => {
148 expect(parseOriginUrlFromGitConfig("[core]\n\tbare = false\n")).toBeNull();
149 expect(parseOriginUrlFromGitConfig("")).toBeNull();
150 });
151});
152​
153describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
154 it("extracts the owner and repository from common GitHub remote shapes", () => {
155 expect(
156 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:T3Tools/T3Code.git"),
157 ).toBe("T3Tools/T3Code");
158 expect(
159 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
160 ).toBe("T3Tools/T3Code");
161 expect(
162 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("ssh://github.com/T3Tools/T3Code.git"),
163 ).toBe("T3Tools/T3Code");
164 });
165});
166​
167describe("isTemporaryWorktreeBranch", () => {
168 it("matches the generated temporary worktree refName format", () => {
169 expect(
170 isTemporaryWorktreeBranch(
171 buildTemporaryWorktreeBranchName((byteLength) => {
172 expect(byteLength).toBe(4);
173 return "DEADBEEF";
174 }),
175 ),
176 ).toBe(true);
177 });
178​
179 it("matches generated temporary worktree refs", () => {
180 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
181 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
182 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
183 });
184​
185 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
186 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
187 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
188 );
189 });
190​
191 it("matches legacy UUID-shaped temporary worktree refs from older mobile builds", () => {
192 expect(
193 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
194 ).toBe(true);
195 });
196​
197 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
198 // version nibble is not 4
199 expect(
200 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12`),
201 ).toBe(false);
202 // variant nibble is not [89ab]
203 expect(
204 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12`),
205 ).toBe(false);
206 });
207​
208 it("rejects non-temporary refName names", () => {
209 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
210 expect(isTemporaryWorktreeBranch("main")).toBe(false);
211 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
212 });
213});
214​
215describe("applyGitStatusStreamEvent", () => {
216 it("treats a remote-only update as a repository when local state is missing", () => {
217 const remote: VcsStatusRemoteResult = {
218 hasUpstream: true,
219 aheadCount: 2,
220 behindCount: 1,
221 pr: null,
222 };
223​
224 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
225 isRepo: true,
226 hasPrimaryRemote: false,
227 isDefaultRef: false,
228 refName: null,
229 hasWorkingTreeChanges: false,
230 workingTree: { files: [], insertions: 0, deletions: 0 },
231 hasUpstream: true,
232 aheadCount: 2,
233 behindCount: 1,
234 pr: null,
235 });
236 });
237​
238 it("preserves local-only fields when applying a remote update", () => {
239 const current: VcsStatusResult = {
240 isRepo: true,
241 sourceControlProvider: {
242 kind: "github",
243 name: "GitHub",
244 baseUrl: "https://github.com",
245 },
246 hasPrimaryRemote: true,
247 isDefaultRef: false,
248 refName: "feature/demo",
249 hasWorkingTreeChanges: true,
250 workingTree: {
251 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
252 insertions: 1,
253 deletions: 0,
254 },
255 hasUpstream: false,
256 aheadCount: 0,
257 behindCount: 0,
258 pr: null,
259 };
260​
261 const remote: VcsStatusRemoteResult = {
262 hasUpstream: true,
263 aheadCount: 2,
264 behindCount: 1,
265 pr: null,
266 };
267​
268 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
269 ...current,
270 hasUpstream: true,
271 aheadCount: 2,
272 behindCount: 1,
273 pr: null,
274 });
275 });
276});