| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- import { describe, expect, it } from "vitest";
- import {
- buildGitBranchAutoCommitMessage,
- buildGitBranchCommitPreviewFiles,
- getGitDirtyFileCount,
- getGitBranchCommitTotals,
- isGitBranchCommitAssistIssue,
- matchesGitBranchSearch,
- resolveGitBranchIssueMessageId,
- resolveGitBranchSuccessMessageId,
- resolveGitBranchTriggerLabel,
- selectGitBranchAffectedFiles,
- } from "../src/git-branch-switcher/display.js";
- describe("getGitDirtyFileCount", () => {
- it("deduplicates files that appear in both staged and unstaged datasets", () => {
- expect(
- getGitDirtyFileCount({
- unstaged: {
- sections: [
- {
- changes: [{ path: "src/app.ts" }, { path: "src/main.ts" }],
- },
- ],
- },
- staged: {
- sections: [
- {
- changes: [{ path: "src/app.ts" }, { path: "README.md" }],
- },
- ],
- },
- }),
- ).toBe(3);
- });
- });
- describe("resolveGitBranchTriggerLabel", () => {
- it("shows detached label when HEAD is detached", () => {
- expect(
- resolveGitBranchTriggerLabel({
- headRefType: "detached",
- currentBranchName: "main",
- detachedLabel: "Detached HEAD",
- fallbackLabel: "Branch",
- }),
- ).toBe("Detached HEAD");
- });
- it("falls back when current branch name is empty", () => {
- expect(
- resolveGitBranchTriggerLabel({
- headRefType: "branch",
- currentBranchName: " ",
- detachedLabel: "Detached HEAD",
- fallbackLabel: "Branch",
- }),
- ).toBe("Branch");
- });
- });
- describe("matchesGitBranchSearch", () => {
- it("matches branch names case-insensitively", () => {
- expect(matchesGitBranchSearch("feature/Git-UI", "git")).toBe(true);
- expect(matchesGitBranchSearch("feature/Git-UI", "main")).toBe(false);
- });
- });
- describe("branch mutation message helpers", () => {
- it("maps known issue codes to i18n ids", () => {
- expect(
- resolveGitBranchIssueMessageId({
- code: "tracked-changes-would-be-overwritten",
- message: "blocked",
- }),
- ).toBe("git.branchSwitcher.error.trackedOverwrite");
- expect(
- resolveGitBranchIssueMessageId({
- code: "unknown",
- message: "blocked",
- }),
- ).toBeNull();
- });
- it("returns different success ids for switch and create-and-switch", () => {
- expect(
- resolveGitBranchSuccessMessageId({
- action: "switch",
- created: false,
- didChange: true,
- }),
- ).toBe("git.branchSwitcher.toast.switchSuccess");
- expect(
- resolveGitBranchSuccessMessageId({
- action: "create-and-switch",
- created: true,
- didChange: true,
- }),
- ).toBe("git.branchSwitcher.toast.createSuccess");
- expect(
- resolveGitBranchSuccessMessageId({
- action: "switch",
- created: false,
- didChange: false,
- }),
- ).toBeNull();
- });
- });
- describe("branch switch commit assist helpers", () => {
- it("recognizes overwrite issues as commit-assist candidates", () => {
- expect(isGitBranchCommitAssistIssue("tracked-changes-would-be-overwritten")).toBe(true);
- expect(isGitBranchCommitAssistIssue("untracked-changes-would-be-overwritten")).toBe(true);
- expect(isGitBranchCommitAssistIssue("operation-in-progress")).toBe(false);
- });
- it("merges staged and unstaged copies of the same file into one preview row", () => {
- const files = buildGitBranchCommitPreviewFiles([
- {
- path: "/repo/src/app.ts",
- repoRelativePath: "src/app.ts",
- workspaceRelativePath: "src/app.ts",
- kind: "modified",
- section: "unstaged",
- added: 3,
- removed: 1,
- isStaged: false,
- isUntracked: false,
- isConflicted: false,
- },
- {
- path: "/repo/src/app.ts",
- repoRelativePath: "src/app.ts",
- workspaceRelativePath: "src/app.ts",
- kind: "modified",
- section: "staged",
- added: 2,
- removed: 4,
- isStaged: true,
- isUntracked: false,
- isConflicted: false,
- },
- {
- path: "/repo/src/other.ts",
- repoRelativePath: "src/other.ts",
- workspaceRelativePath: "src/other.ts",
- kind: "added",
- section: "unstaged",
- added: 5,
- removed: 0,
- isStaged: false,
- isUntracked: true,
- isConflicted: false,
- },
- ]);
- expect(files).toEqual([
- {
- stagePath: "/repo/src/app.ts",
- repoRelativePath: "src/app.ts",
- workspaceRelativePath: "src/app.ts",
- kind: "modified",
- added: 5,
- removed: 5,
- },
- {
- stagePath: "/repo/src/other.ts",
- repoRelativePath: "src/other.ts",
- workspaceRelativePath: "src/other.ts",
- kind: "added",
- added: 5,
- removed: 0,
- },
- ]);
- expect(getGitBranchCommitTotals(files)).toEqual({
- fileCount: 2,
- totalAdded: 10,
- totalRemoved: 5,
- });
- });
- it("falls back to raw issue paths when a blocking file is outside the loaded preview list", () => {
- expect(
- selectGitBranchAffectedFiles({
- files: [
- {
- stagePath: "/repo/src/app.ts",
- repoRelativePath: "src/app.ts",
- workspaceRelativePath: "src/app.ts",
- kind: "modified",
- added: 1,
- removed: 2,
- },
- ],
- issuePaths: ["src/app.ts", "packages/ui/src/GitBranchSwitcher.tsx"],
- }),
- ).toEqual([
- {
- stagePath: "/repo/src/app.ts",
- repoRelativePath: "src/app.ts",
- workspaceRelativePath: "src/app.ts",
- kind: "modified",
- added: 1,
- removed: 2,
- },
- {
- stagePath: "packages/ui/src/GitBranchSwitcher.tsx",
- repoRelativePath: "packages/ui/src/GitBranchSwitcher.tsx",
- workspaceRelativePath: "packages/ui/src/GitBranchSwitcher.tsx",
- kind: "modified",
- added: 0,
- removed: 0,
- },
- ]);
- });
- it("builds a stable autogenerated checkpoint commit message", () => {
- expect(buildGitBranchAutoCommitMessage("feature/git-ui")).toBe(
- "chore: checkpoint before switching to feature/git-ui",
- );
- expect(buildGitBranchAutoCommitMessage(" ")).toBe(
- "chore: checkpoint before switching branches",
- );
- });
- });
|