Open History to see the last commit
1import type { GitBranch } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3import {
4 dedupeRemoteBranchesWithLocalMatches,
5 deriveLocalBranchNameFromRemoteRef,
6} from "./BranchToolbar.logic";
7
8describe("deriveLocalBranchNameFromRemoteRef", () => {
9 it("strips the remote prefix from a remote ref", () => {
10 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
11 });
12
13 it("supports remote names that contain slashes", () => {
14 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
15 "upstream/feature/demo",
16 );
17 });
18
19 it("returns the original name when ref is malformed", () => {
20 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
21 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
22 });
23});
24
25describe("dedupeRemoteBranchesWithLocalMatches", () => {
26 it("hides remote refs when the matching local branch exists", () => {
27 const input: GitBranch[] = [
28 {
29 name: "feature/demo",
30 current: false,
31 isDefault: false,
32 worktreePath: null,
33 },
34 {
35 name: "origin/feature/demo",
36 isRemote: true,
37 remoteName: "origin",
38 current: false,
39 isDefault: false,
40 worktreePath: null,
41 },
42 {
43 name: "origin/feature/remote-only",
44 isRemote: true,
45 remoteName: "origin",
46 current: false,
47 isDefault: false,
48 worktreePath: null,
49 },
50 ];
51
52 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
53 "feature/demo",
54 "origin/feature/remote-only",
55 ]);
56 });
57
58 it("keeps all entries when no local match exists for a remote ref", () => {
59 const input: GitBranch[] = [
60 {
61 name: "feature/local",
62 current: false,
63 isDefault: false,
64 worktreePath: null,
65 },
66 {
67 name: "origin/feature/remote-only",
68 isRemote: true,
69 remoteName: "origin",
70 current: false,
71 isDefault: false,
72 worktreePath: null,
73 },
74 ];
75
76 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
77 "feature/local",
78 "origin/feature/remote-only",
79 ]);
80 });
81
82 it("dedupes remote refs for remotes whose names contain slashes", () => {
83 const input: GitBranch[] = [
84 {
85 name: "feature/demo",
86 current: false,
87 isDefault: false,
88 worktreePath: null,
89 },
90 {
91 name: "my-org/upstream/feature/demo",
92 isRemote: true,
93 remoteName: "my-org/upstream",
94 current: false,
95 isDefault: false,
96 worktreePath: null,
97 },
98 ];
99
100 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
101 "feature/demo",
102 ]);
103 });
104
105 it("dedupes remote refs when git tracks with first-slash local naming", () => {
106 const input: GitBranch[] = [
107 {
108 name: "upstream/feature",
109 current: false,
110 isDefault: false,
111 worktreePath: null,
112 },
113 {
114 name: "my-org/upstream/feature",
115 isRemote: true,
116 remoteName: "my-org/upstream",
117 current: false,
118 isDefault: false,
119 worktreePath: null,
120 },
121 ];
122
123 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
124 "upstream/feature",
125 ]);
126 });
127});