Open History to see the last commit
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
55describe("parseOriginUrlFromGitConfig", () => {
56 it("reads the origin url and ignores other remotes", () => {
57 const config = [
58 "[core]",
59 "\trepositoryformatversion = 0",
60 '[remote "upstream"]',
61 "\turl = https://github.com/other/repo.git",
62 '[remote "origin"]',
63 "\turl = git@github.com:pingdotgg/t3code.git",
64 "\tfetch = +refs/heads/*:refs/remotes/origin/*",
65 '[branch "main"]',
66 "\tremote = origin",
67 ].join("\n");
68 expect(parseOriginUrlFromGitConfig(config)).toBe("git@github.com:pingdotgg/t3code.git");
69 });
70
71 it("strips inline comments and quotes from the url value", () => {
72 expect(
73 parseOriginUrlFromGitConfig(
74 '[remote "origin"]\n\turl = https://github.com/acme/repo.git # mirror\n',
75 ),
76 ).toBe("https://github.com/acme/repo.git");
77 expect(
78 parseOriginUrlFromGitConfig('[remote "origin"]\n\turl = "git@github.com:acme/repo.git"\n'),
79 ).toBe("git@github.com:acme/repo.git");
80 });
81
82 it("accepts legacy dotted headers, header comments, and line continuations", () => {
83 expect(parseOriginUrlFromGitConfig("[remote.origin]\n\turl = git@github.com:a/b.git\n")).toBe(
84 "git@github.com:a/b.git",
85 );
86 // Git folds the dotted form to lowercase but keeps quoted names as written.
87 expect(parseOriginUrlFromGitConfig("[remote.Origin]\n\turl = git@github.com:a/b.git\n")).toBe(
88 "git@github.com:a/b.git",
89 );
90 expect(
91 parseOriginUrlFromGitConfig(
92 '[remote "Origin"]\n\turl = git@github.com:x/y.git\n[remote "origin"]\n\turl = git@github.com:a/b.git\n',
93 ),
94 ).toBe("git@github.com:a/b.git");
95 expect(
96 parseOriginUrlFromGitConfig('[remote "origin"] # primary\n\turl = git@github.com:a/b.git\n'),
97 ).toBe("git@github.com:a/b.git");
98 expect(
99 parseOriginUrlFromGitConfig(
100 '[remote "origin"]\n\turl = https://github.com/acme/\\\n\t\trepo.git\n',
101 ),
102 ).toBe("https://github.com/acme/repo.git");
103 });
104
105 it("falls back to the first remote when there is no origin", () => {
106 const config = [
107 '[remote "upstream"]',
108 "\turl = https://github.com/acme/repo.git",
109 '[remote "fork"]',
110 "\turl = https://github.com/me/repo.git",
111 ].join("\n");
112 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
113 });
114
115 it("returns null when there is no remote section", () => {
116 expect(parseOriginUrlFromGitConfig("[core]\n\tbare = false\n")).toBeNull();
117 expect(parseOriginUrlFromGitConfig("")).toBeNull();
118 });
119});
120
121describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
122 it("extracts the owner and repository from common GitHub remote shapes", () => {
123 expect(
124 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:T3Tools/T3Code.git"),
125 ).toBe("T3Tools/T3Code");
126 expect(
127 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
128 ).toBe("T3Tools/T3Code");
129 expect(
130 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("ssh://github.com/T3Tools/T3Code.git"),
131 ).toBe("T3Tools/T3Code");
132 });
133});
134
135describe("isTemporaryWorktreeBranch", () => {
136 it("matches the generated temporary worktree refName format", () => {
137 expect(
138 isTemporaryWorktreeBranch(
139 buildTemporaryWorktreeBranchName((byteLength) => {
140 expect(byteLength).toBe(4);
141 return "DEADBEEF";
142 }),
143 ),
144 ).toBe(true);
145 });
146
147 it("matches generated temporary worktree refs", () => {
148 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
149 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
150 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
151 });
152
153 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
154 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
155 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
156 );
157 });
158
159 it("matches legacy UUID-shaped temporary worktree refs from older mobile builds", () => {
160 expect(
161 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
162 ).toBe(true);
163 });
164
165 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
166 // version nibble is not 4
167 expect(
168 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12`),
169 ).toBe(false);
170 // variant nibble is not [89ab]
171 expect(
172 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12`),
173 ).toBe(false);
174 });
175
176 it("rejects non-temporary refName names", () => {
177 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
178 expect(isTemporaryWorktreeBranch("main")).toBe(false);
179 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
180 });
181});
182
183describe("applyGitStatusStreamEvent", () => {
184 it("treats a remote-only update as a repository when local state is missing", () => {
185 const remote: VcsStatusRemoteResult = {
186 hasUpstream: true,
187 aheadCount: 2,
188 behindCount: 1,
189 pr: null,
190 };
191
192 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
193 isRepo: true,
194 hasPrimaryRemote: false,
195 isDefaultRef: false,
196 refName: null,
197 hasWorkingTreeChanges: false,
198 workingTree: { files: [], insertions: 0, deletions: 0 },
199 hasUpstream: true,
200 aheadCount: 2,
201 behindCount: 1,
202 pr: null,
203 });
204 });
205
206 it("preserves local-only fields when applying a remote update", () => {
207 const current: VcsStatusResult = {
208 isRepo: true,
209 sourceControlProvider: {
210 kind: "github",
211 name: "GitHub",
212 baseUrl: "https://github.com",
213 },
214 hasPrimaryRemote: true,
215 isDefaultRef: false,
216 refName: "feature/demo",
217 hasWorkingTreeChanges: true,
218 workingTree: {
219 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
220 insertions: 1,
221 deletions: 0,
222 },
223 hasUpstream: false,
224 aheadCount: 0,
225 behindCount: 0,
226 pr: null,
227 };
228
229 const remote: VcsStatusRemoteResult = {
230 hasUpstream: true,
231 aheadCount: 2,
232 behindCount: 1,
233 pr: null,
234 };
235
236 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
237 ...current,
238 hasUpstream: true,
239 aheadCount: 2,
240 behindCount: 1,
241 pr: null,
242 });
243 });
244});