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

git.test.ts

174 lines5.7 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 WORKTREE_BRANCH_PREFIX,
11} from "./git.ts";
12​
13describe("normalizeGitRemoteUrl", () => {
14 it("canonicalizes equivalent GitHub remotes across protocol variants", () => {
15 expect(normalizeGitRemoteUrl("git@github.com:T3Tools/T3Code.git")).toBe(
16 "github.com/t3tools/t3code",
17 );
18 expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git")).toBe(
19 "github.com/t3tools/t3code",
20 );
21 expect(normalizeGitRemoteUrl("ssh://git@github.com/T3Tools/T3Code")).toBe(
22 "github.com/t3tools/t3code",
23 );
24 });
25​
26 it("preserves nested group paths for providers like GitLab", () => {
27 expect(normalizeGitRemoteUrl("git@gitlab.com:T3Tools/platform/T3Code.git")).toBe(
28 "gitlab.com/t3tools/platform/t3code",
29 );
30 expect(normalizeGitRemoteUrl("https://gitlab.com/T3Tools/platform/T3Code.git")).toBe(
31 "gitlab.com/t3tools/platform/t3code",
32 );
33 });
34​
35 it("drops explicit ports from URL-shaped remotes", () => {
36 expect(normalizeGitRemoteUrl("https://gitlab.company.com:8443/team/project.git")).toBe(
37 "gitlab.company.com/team/project",
38 );
39 expect(normalizeGitRemoteUrl("ssh://git@gitlab.company.com:2222/team/project.git")).toBe(
40 "gitlab.company.com/team/project",
41 );
42 });
43​
44 it("normalizes SCP-like remotes with non-git SSH users", () => {
45 expect(normalizeGitRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(
46 "gitlab.example.com/group/project",
47 );
48 expect(normalizeGitRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(
49 "bitbucket.org/workspace/repo",
50 );
51 });
52});
53​
54describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
55 it("extracts the owner and repository from common GitHub remote shapes", () => {
56 expect(
57 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:T3Tools/T3Code.git"),
58 ).toBe("T3Tools/T3Code");
59 expect(
60 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
61 ).toBe("T3Tools/T3Code");
62 });
63});
64​
65describe("isTemporaryWorktreeBranch", () => {
66 it("matches the generated temporary worktree refName format", () => {
67 expect(
68 isTemporaryWorktreeBranch(
69 buildTemporaryWorktreeBranchName((byteLength) => {
70 expect(byteLength).toBe(4);
71 return "DEADBEEF";
72 }),
73 ),
74 ).toBe(true);
75 });
76​
77 it("matches generated temporary worktree refs", () => {
78 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
79 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
80 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
81 });
82​
83 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
84 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
85 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
86 );
87 });
88​
89 it("matches legacy UUID-shaped temporary worktree refs from older mobile builds", () => {
90 expect(
91 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
92 ).toBe(true);
93 });
94​
95 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
96 // version nibble is not 4
97 expect(
98 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12`),
99 ).toBe(false);
100 // variant nibble is not [89ab]
101 expect(
102 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12`),
103 ).toBe(false);
104 });
105​
106 it("rejects non-temporary refName names", () => {
107 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
108 expect(isTemporaryWorktreeBranch("main")).toBe(false);
109 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
110 });
111});
112​
113describe("applyGitStatusStreamEvent", () => {
114 it("treats a remote-only update as a repository when local state is missing", () => {
115 const remote: VcsStatusRemoteResult = {
116 hasUpstream: true,
117 aheadCount: 2,
118 behindCount: 1,
119 pr: null,
120 };
121​
122 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
123 isRepo: true,
124 hasPrimaryRemote: false,
125 isDefaultRef: false,
126 refName: null,
127 hasWorkingTreeChanges: false,
128 workingTree: { files: [], insertions: 0, deletions: 0 },
129 hasUpstream: true,
130 aheadCount: 2,
131 behindCount: 1,
132 pr: null,
133 });
134 });
135​
136 it("preserves local-only fields when applying a remote update", () => {
137 const current: VcsStatusResult = {
138 isRepo: true,
139 sourceControlProvider: {
140 kind: "github",
141 name: "GitHub",
142 baseUrl: "https://github.com",
143 },
144 hasPrimaryRemote: true,
145 isDefaultRef: false,
146 refName: "feature/demo",
147 hasWorkingTreeChanges: true,
148 workingTree: {
149 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
150 insertions: 1,
151 deletions: 0,
152 },
153 hasUpstream: false,
154 aheadCount: 0,
155 behindCount: 0,
156 pr: null,
157 };
158​
159 const remote: VcsStatusRemoteResult = {
160 hasUpstream: true,
161 aheadCount: 2,
162 behindCount: 1,
163 pr: null,
164 };
165​
166 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
167 ...current,
168 hasUpstream: true,
169 aheadCount: 2,
170 behindCount: 1,
171 pr: null,
172 });
173 });
174});