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

git.test.ts

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