Open History to see the last commit
1import type { VcsStatusRemoteResult, VcsStatusResult } 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 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("rejects non-temporary refName names", () => {
75 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
76 expect(isTemporaryWorktreeBranch("main")).toBe(false);
77 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
78 });
79});
80
81describe("applyGitStatusStreamEvent", () => {
82 it("treats a remote-only update as a repository when local state is missing", () => {
83 const remote: VcsStatusRemoteResult = {
84 hasUpstream: true,
85 aheadCount: 2,
86 behindCount: 1,
87 pr: null,
88 };
89
90 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
91 isRepo: true,
92 hasPrimaryRemote: false,
93 isDefaultRef: false,
94 refName: null,
95 hasWorkingTreeChanges: false,
96 workingTree: { files: [], insertions: 0, deletions: 0 },
97 hasUpstream: true,
98 aheadCount: 2,
99 behindCount: 1,
100 pr: null,
101 });
102 });
103
104 it("preserves local-only fields when applying a remote update", () => {
105 const current: VcsStatusResult = {
106 isRepo: true,
107 sourceControlProvider: {
108 kind: "github",
109 name: "GitHub",
110 baseUrl: "https://github.com",
111 },
112 hasPrimaryRemote: true,
113 isDefaultRef: false,
114 refName: "feature/demo",
115 hasWorkingTreeChanges: true,
116 workingTree: {
117 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
118 insertions: 1,
119 deletions: 0,
120 },
121 hasUpstream: false,
122 aheadCount: 0,
123 behindCount: 0,
124 pr: null,
125 };
126
127 const remote: VcsStatusRemoteResult = {
128 hasUpstream: true,
129 aheadCount: 2,
130 behindCount: 1,
131 pr: null,
132 };
133
134 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
135 ...current,
136 hasUpstream: true,
137 aheadCount: 2,
138 behindCount: 1,
139 pr: null,
140 });
141 });
142});