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

git.test.ts

165 lines5.3 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​
45describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
46 it("extracts the owner and repository from common GitHub remote shapes", () => {
47 expect(
48 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:T3Tools/T3Code.git"),
49 ).toBe("T3Tools/T3Code");
50 expect(
51 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
52 ).toBe("T3Tools/T3Code");
53 });
54});
55​
56describe("isTemporaryWorktreeBranch", () => {
57 it("matches the generated temporary worktree refName format", () => {
58 expect(
59 isTemporaryWorktreeBranch(
60 buildTemporaryWorktreeBranchName((byteLength) => {
61 expect(byteLength).toBe(4);
62 return "DEADBEEF";
63 }),
64 ),
65 ).toBe(true);
66 });
67​
68 it("matches generated temporary worktree refs", () => {
69 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
70 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
71 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
72 });
73​
74 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
75 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
76 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
77 );
78 });
79​
80 it("matches legacy UUID-shaped temporary worktree refs from older mobile builds", () => {
81 expect(
82 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
83 ).toBe(true);
84 });
85​
86 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
87 // version nibble is not 4
88 expect(
89 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12`),
90 ).toBe(false);
91 // variant nibble is not [89ab]
92 expect(
93 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12`),
94 ).toBe(false);
95 });
96​
97 it("rejects non-temporary refName names", () => {
98 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
99 expect(isTemporaryWorktreeBranch("main")).toBe(false);
100 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
101 });
102});
103​
104describe("applyGitStatusStreamEvent", () => {
105 it("treats a remote-only update as a repository when local state is missing", () => {
106 const remote: VcsStatusRemoteResult = {
107 hasUpstream: true,
108 aheadCount: 2,
109 behindCount: 1,
110 pr: null,
111 };
112​
113 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
114 isRepo: true,
115 hasPrimaryRemote: false,
116 isDefaultRef: false,
117 refName: null,
118 hasWorkingTreeChanges: false,
119 workingTree: { files: [], insertions: 0, deletions: 0 },
120 hasUpstream: true,
121 aheadCount: 2,
122 behindCount: 1,
123 pr: null,
124 });
125 });
126​
127 it("preserves local-only fields when applying a remote update", () => {
128 const current: VcsStatusResult = {
129 isRepo: true,
130 sourceControlProvider: {
131 kind: "github",
132 name: "GitHub",
133 baseUrl: "https://github.com",
134 },
135 hasPrimaryRemote: true,
136 isDefaultRef: false,
137 refName: "feature/demo",
138 hasWorkingTreeChanges: true,
139 workingTree: {
140 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
141 insertions: 1,
142 deletions: 0,
143 },
144 hasUpstream: false,
145 aheadCount: 0,
146 behindCount: 0,
147 pr: null,
148 };
149​
150 const remote: VcsStatusRemoteResult = {
151 hasUpstream: true,
152 aheadCount: 2,
153 behindCount: 1,
154 pr: null,
155 };
156​
157 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
158 ...current,
159 hasUpstream: true,
160 aheadCount: 2,
161 behindCount: 1,
162 pr: null,
163 });
164 });
165});