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

git.test.ts

114 lines3.4 KB

1import type { GitStatusRemoteResult, GitStatusResult } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3​
4import {
5 applyGitStatusStreamEvent,
6 normalizeGitRemoteUrl,
7 parseGitHubRepositoryNameWithOwnerFromRemoteUrl,
8} from "./git";
9​
10describe("normalizeGitRemoteUrl", () => {
11 it("canonicalizes equivalent GitHub remotes across protocol variants", () => {
12 expect(normalizeGitRemoteUrl("git@github.com:T3Tools/T3Code.git")).toBe(
13 "github.com/t3tools/t3code",
14 );
15 expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git")).toBe(
16 "github.com/t3tools/t3code",
17 );
18 expect(normalizeGitRemoteUrl("ssh://git@github.com/T3Tools/T3Code")).toBe(
19 "github.com/t3tools/t3code",
20 );
21 });
22​
23 it("preserves nested group paths for providers like GitLab", () => {
24 expect(normalizeGitRemoteUrl("git@gitlab.com:T3Tools/platform/T3Code.git")).toBe(
25 "gitlab.com/t3tools/platform/t3code",
26 );
27 expect(normalizeGitRemoteUrl("https://gitlab.com/T3Tools/platform/T3Code.git")).toBe(
28 "gitlab.com/t3tools/platform/t3code",
29 );
30 });
31​
32 it("drops explicit ports from URL-shaped remotes", () => {
33 expect(normalizeGitRemoteUrl("https://gitlab.company.com:8443/team/project.git")).toBe(
34 "gitlab.company.com/team/project",
35 );
36 expect(normalizeGitRemoteUrl("ssh://git@gitlab.company.com:2222/team/project.git")).toBe(
37 "gitlab.company.com/team/project",
38 );
39 });
40});
41​
42describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
43 it("extracts the owner and repository from common GitHub remote shapes", () => {
44 expect(
45 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:T3Tools/T3Code.git"),
46 ).toBe("T3Tools/T3Code");
47 expect(
48 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
49 ).toBe("T3Tools/T3Code");
50 });
51});
52​
53describe("applyGitStatusStreamEvent", () => {
54 it("treats a remote-only update as a repository when local state is missing", () => {
55 const remote: GitStatusRemoteResult = {
56 hasUpstream: true,
57 aheadCount: 2,
58 behindCount: 1,
59 pr: null,
60 };
61​
62 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
63 isRepo: true,
64 hasOriginRemote: false,
65 isDefaultBranch: false,
66 branch: null,
67 hasWorkingTreeChanges: false,
68 workingTree: { files: [], insertions: 0, deletions: 0 },
69 hasUpstream: true,
70 aheadCount: 2,
71 behindCount: 1,
72 pr: null,
73 });
74 });
75​
76 it("preserves local-only fields when applying a remote update", () => {
77 const current: GitStatusResult = {
78 isRepo: true,
79 hostingProvider: {
80 kind: "github",
81 name: "GitHub",
82 baseUrl: "https://github.com",
83 },
84 hasOriginRemote: true,
85 isDefaultBranch: false,
86 branch: "feature/demo",
87 hasWorkingTreeChanges: true,
88 workingTree: {
89 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
90 insertions: 1,
91 deletions: 0,
92 },
93 hasUpstream: false,
94 aheadCount: 0,
95 behindCount: 0,
96 pr: null,
97 };
98​
99 const remote: GitStatusRemoteResult = {
100 hasUpstream: true,
101 aheadCount: 2,
102 behindCount: 1,
103 pr: null,
104 };
105​
106 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
107 ...current,
108 hasUpstream: true,
109 aheadCount: 2,
110 behindCount: 1,
111 pr: null,
112 });
113 });
114});