gitBranchSwitcherDisplay.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildGitBranchAutoCommitMessage,
  4. buildGitBranchCommitPreviewFiles,
  5. getGitDirtyFileCount,
  6. getGitBranchCommitTotals,
  7. isGitBranchCommitAssistIssue,
  8. matchesGitBranchSearch,
  9. resolveGitBranchIssueMessageId,
  10. resolveGitBranchSuccessMessageId,
  11. resolveGitBranchTriggerLabel,
  12. selectGitBranchAffectedFiles,
  13. } from "../src/git-branch-switcher/display.js";
  14. describe("getGitDirtyFileCount", () => {
  15. it("deduplicates files that appear in both staged and unstaged datasets", () => {
  16. expect(
  17. getGitDirtyFileCount({
  18. unstaged: {
  19. sections: [
  20. {
  21. changes: [{ path: "src/app.ts" }, { path: "src/main.ts" }],
  22. },
  23. ],
  24. },
  25. staged: {
  26. sections: [
  27. {
  28. changes: [{ path: "src/app.ts" }, { path: "README.md" }],
  29. },
  30. ],
  31. },
  32. }),
  33. ).toBe(3);
  34. });
  35. });
  36. describe("resolveGitBranchTriggerLabel", () => {
  37. it("shows detached label when HEAD is detached", () => {
  38. expect(
  39. resolveGitBranchTriggerLabel({
  40. headRefType: "detached",
  41. currentBranchName: "main",
  42. detachedLabel: "Detached HEAD",
  43. fallbackLabel: "Branch",
  44. }),
  45. ).toBe("Detached HEAD");
  46. });
  47. it("falls back when current branch name is empty", () => {
  48. expect(
  49. resolveGitBranchTriggerLabel({
  50. headRefType: "branch",
  51. currentBranchName: " ",
  52. detachedLabel: "Detached HEAD",
  53. fallbackLabel: "Branch",
  54. }),
  55. ).toBe("Branch");
  56. });
  57. });
  58. describe("matchesGitBranchSearch", () => {
  59. it("matches branch names case-insensitively", () => {
  60. expect(matchesGitBranchSearch("feature/Git-UI", "git")).toBe(true);
  61. expect(matchesGitBranchSearch("feature/Git-UI", "main")).toBe(false);
  62. });
  63. });
  64. describe("branch mutation message helpers", () => {
  65. it("maps known issue codes to i18n ids", () => {
  66. expect(
  67. resolveGitBranchIssueMessageId({
  68. code: "tracked-changes-would-be-overwritten",
  69. message: "blocked",
  70. }),
  71. ).toBe("git.branchSwitcher.error.trackedOverwrite");
  72. expect(
  73. resolveGitBranchIssueMessageId({
  74. code: "unknown",
  75. message: "blocked",
  76. }),
  77. ).toBeNull();
  78. });
  79. it("returns different success ids for switch and create-and-switch", () => {
  80. expect(
  81. resolveGitBranchSuccessMessageId({
  82. action: "switch",
  83. created: false,
  84. didChange: true,
  85. }),
  86. ).toBe("git.branchSwitcher.toast.switchSuccess");
  87. expect(
  88. resolveGitBranchSuccessMessageId({
  89. action: "create-and-switch",
  90. created: true,
  91. didChange: true,
  92. }),
  93. ).toBe("git.branchSwitcher.toast.createSuccess");
  94. expect(
  95. resolveGitBranchSuccessMessageId({
  96. action: "switch",
  97. created: false,
  98. didChange: false,
  99. }),
  100. ).toBeNull();
  101. });
  102. });
  103. describe("branch switch commit assist helpers", () => {
  104. it("recognizes overwrite issues as commit-assist candidates", () => {
  105. expect(isGitBranchCommitAssistIssue("tracked-changes-would-be-overwritten")).toBe(true);
  106. expect(isGitBranchCommitAssistIssue("untracked-changes-would-be-overwritten")).toBe(true);
  107. expect(isGitBranchCommitAssistIssue("operation-in-progress")).toBe(false);
  108. });
  109. it("merges staged and unstaged copies of the same file into one preview row", () => {
  110. const files = buildGitBranchCommitPreviewFiles([
  111. {
  112. path: "/repo/src/app.ts",
  113. repoRelativePath: "src/app.ts",
  114. workspaceRelativePath: "src/app.ts",
  115. kind: "modified",
  116. section: "unstaged",
  117. added: 3,
  118. removed: 1,
  119. isStaged: false,
  120. isUntracked: false,
  121. isConflicted: false,
  122. },
  123. {
  124. path: "/repo/src/app.ts",
  125. repoRelativePath: "src/app.ts",
  126. workspaceRelativePath: "src/app.ts",
  127. kind: "modified",
  128. section: "staged",
  129. added: 2,
  130. removed: 4,
  131. isStaged: true,
  132. isUntracked: false,
  133. isConflicted: false,
  134. },
  135. {
  136. path: "/repo/src/other.ts",
  137. repoRelativePath: "src/other.ts",
  138. workspaceRelativePath: "src/other.ts",
  139. kind: "added",
  140. section: "unstaged",
  141. added: 5,
  142. removed: 0,
  143. isStaged: false,
  144. isUntracked: true,
  145. isConflicted: false,
  146. },
  147. ]);
  148. expect(files).toEqual([
  149. {
  150. stagePath: "/repo/src/app.ts",
  151. repoRelativePath: "src/app.ts",
  152. workspaceRelativePath: "src/app.ts",
  153. kind: "modified",
  154. added: 5,
  155. removed: 5,
  156. },
  157. {
  158. stagePath: "/repo/src/other.ts",
  159. repoRelativePath: "src/other.ts",
  160. workspaceRelativePath: "src/other.ts",
  161. kind: "added",
  162. added: 5,
  163. removed: 0,
  164. },
  165. ]);
  166. expect(getGitBranchCommitTotals(files)).toEqual({
  167. fileCount: 2,
  168. totalAdded: 10,
  169. totalRemoved: 5,
  170. });
  171. });
  172. it("falls back to raw issue paths when a blocking file is outside the loaded preview list", () => {
  173. expect(
  174. selectGitBranchAffectedFiles({
  175. files: [
  176. {
  177. stagePath: "/repo/src/app.ts",
  178. repoRelativePath: "src/app.ts",
  179. workspaceRelativePath: "src/app.ts",
  180. kind: "modified",
  181. added: 1,
  182. removed: 2,
  183. },
  184. ],
  185. issuePaths: ["src/app.ts", "packages/ui/src/GitBranchSwitcher.tsx"],
  186. }),
  187. ).toEqual([
  188. {
  189. stagePath: "/repo/src/app.ts",
  190. repoRelativePath: "src/app.ts",
  191. workspaceRelativePath: "src/app.ts",
  192. kind: "modified",
  193. added: 1,
  194. removed: 2,
  195. },
  196. {
  197. stagePath: "packages/ui/src/GitBranchSwitcher.tsx",
  198. repoRelativePath: "packages/ui/src/GitBranchSwitcher.tsx",
  199. workspaceRelativePath: "packages/ui/src/GitBranchSwitcher.tsx",
  200. kind: "modified",
  201. added: 0,
  202. removed: 0,
  203. },
  204. ]);
  205. });
  206. it("builds a stable autogenerated checkpoint commit message", () => {
  207. expect(buildGitBranchAutoCommitMessage("feature/git-ui")).toBe(
  208. "chore: checkpoint before switching to feature/git-ui",
  209. );
  210. expect(buildGitBranchAutoCommitMessage(" ")).toBe(
  211. "chore: checkpoint before switching branches",
  212. );
  213. });
  214. });