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

git.test.ts

135 lines4.4 KB

1import type { GitStatusRemoteResult, GitStatusResult } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
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 branch format", () => {
58 expect(isTemporaryWorktreeBranch(buildTemporaryWorktreeBranchName())).toBe(true);
59 });
60​
61 it("matches generated temporary worktree branches", () => {
62 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
63 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
64 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
65 });
66​
67 it("rejects non-temporary branch names", () => {
68 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
69 expect(isTemporaryWorktreeBranch("main")).toBe(false);
70 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
71 });
72});
73​
74describe("applyGitStatusStreamEvent", () => {
75 it("treats a remote-only update as a repository when local state is missing", () => {
76 const remote: GitStatusRemoteResult = {
77 hasUpstream: true,
78 aheadCount: 2,
79 behindCount: 1,
80 pr: null,
81 };
82​
83 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
84 isRepo: true,
85 hasOriginRemote: false,
86 isDefaultBranch: false,
87 branch: null,
88 hasWorkingTreeChanges: false,
89 workingTree: { files: [], insertions: 0, deletions: 0 },
90 hasUpstream: true,
91 aheadCount: 2,
92 behindCount: 1,
93 pr: null,
94 });
95 });
96​
97 it("preserves local-only fields when applying a remote update", () => {
98 const current: GitStatusResult = {
99 isRepo: true,
100 hostingProvider: {
101 kind: "github",
102 name: "GitHub",
103 baseUrl: "https://github.com",
104 },
105 hasOriginRemote: true,
106 isDefaultBranch: false,
107 branch: "feature/demo",
108 hasWorkingTreeChanges: true,
109 workingTree: {
110 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
111 insertions: 1,
112 deletions: 0,
113 },
114 hasUpstream: false,
115 aheadCount: 0,
116 behindCount: 0,
117 pr: null,
118 };
119​
120 const remote: GitStatusRemoteResult = {
121 hasUpstream: true,
122 aheadCount: 2,
123 behindCount: 1,
124 pr: null,
125 };
126​
127 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
128 ...current,
129 hasUpstream: true,
130 aheadCount: 2,
131 behindCount: 1,
132 pr: null,
133 });
134 });
135});