Open History to see the last commit
1import type { VcsStatusRemoteResult, VcsStatusResult } from "@v12/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:V12/V12.git")).toBe("github.com/v12/v12");
16 expect(normalizeGitRemoteUrl("https://github.com/V12/V12.git")).toBe("github.com/v12/v12");
17 expect(normalizeGitRemoteUrl("ssh://git@github.com/V12/V12")).toBe("github.com/v12/v12");
18 });
19
20 it("preserves nested group paths for providers like GitLab", () => {
21 expect(normalizeGitRemoteUrl("git@gitlab.com:V12/platform/V12.git")).toBe(
22 "gitlab.com/v12/platform/v12",
23 );
24 expect(normalizeGitRemoteUrl("https://gitlab.com/V12/platform/V12.git")).toBe(
25 "gitlab.com/v12/platform/v12",
26 );
27 });
28
29 it("drops explicit ports from URL-shaped remotes", () => {
30 expect(normalizeGitRemoteUrl("https://gitlab.company.com:8443/team/project.git")).toBe(
31 "gitlab.company.com/team/project",
32 );
33 expect(normalizeGitRemoteUrl("ssh://git@gitlab.company.com:2222/team/project.git")).toBe(
34 "gitlab.company.com/team/project",
35 );
36 });
37});
38
39describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
40 it("extracts the owner and repository from common GitHub remote shapes", () => {
41 expect(parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:V12/V12.git")).toBe(
42 "V12/V12",
43 );
44 expect(parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/V12/V12.git")).toBe(
45 "V12/V12",
46 );
47 });
48});
49
50describe("isTemporaryWorktreeBranch", () => {
51 it("matches the generated temporary worktree refName format", () => {
52 expect(
53 isTemporaryWorktreeBranch(
54 buildTemporaryWorktreeBranchName((byteLength) => {
55 expect(byteLength).toBe(4);
56 return "DEADBEEF";
57 }),
58 ),
59 ).toBe(true);
60 });
61
62 it("matches generated temporary worktree refs", () => {
63 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
64 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
65 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
66 });
67
68 it("rejects non-temporary refName names", () => {
69 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
70 expect(isTemporaryWorktreeBranch("main")).toBe(false);
71 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
72 });
73});
74
75describe("applyGitStatusStreamEvent", () => {
76 it("treats a remote-only update as a repository when local state is missing", () => {
77 const remote: VcsStatusRemoteResult = {
78 hasUpstream: true,
79 aheadCount: 2,
80 behindCount: 1,
81 pr: null,
82 };
83
84 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
85 isRepo: true,
86 hasPrimaryRemote: false,
87 isDefaultRef: false,
88 refName: null,
89 hasWorkingTreeChanges: false,
90 workingTree: { files: [], insertions: 0, deletions: 0 },
91 hasUpstream: true,
92 aheadCount: 2,
93 behindCount: 1,
94 pr: null,
95 });
96 });
97
98 it("preserves local-only fields when applying a remote update", () => {
99 const current: VcsStatusResult = {
100 isRepo: true,
101 sourceControlProvider: {
102 kind: "github",
103 name: "GitHub",
104 baseUrl: "https://github.com",
105 },
106 hasPrimaryRemote: true,
107 isDefaultRef: false,
108 refName: "feature/demo",
109 hasWorkingTreeChanges: true,
110 workingTree: {
111 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
112 insertions: 1,
113 deletions: 0,
114 },
115 hasUpstream: false,
116 aheadCount: 0,
117 behindCount: 0,
118 pr: null,
119 };
120
121 const remote: VcsStatusRemoteResult = {
122 hasUpstream: true,
123 aheadCount: 2,
124 behindCount: 1,
125 pr: null,
126 };
127
128 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
129 ...current,
130 hasUpstream: true,
131 aheadCount: 2,
132 behindCount: 1,
133 pr: null,
134 });
135 });
136});