| 1 | import { describe, expect, it, vi, beforeEach } from "vitest"; |
| 2 | |
| 3 | import { getResponsiveRepoText, getRepoInfo } from "./useTUIChatHooks.js"; |
| 4 | |
| 5 | // Mock the git utilities |
| 6 | vi.mock("../../util/git.js", () => ({ |
| 7 | isGitRepo: vi.fn(() => true), |
| 8 | getGitRemoteUrl: vi.fn(() => "https://github.com/testuser/testrepo.git"), |
| 9 | getGitBranch: vi.fn(() => "feature/test-branch"), |
| 10 | })); |
| 11 | |
| 12 | describe("Responsive repo text functionality", () => { |
| 13 | beforeEach(async () => { |
| 14 | // Reset mocks before each test |
| 15 | const gitModule = await import("../../util/git.js"); |
| 16 | vi.mocked(gitModule.isGitRepo).mockReturnValue(true); |
| 17 | vi.mocked(gitModule.getGitRemoteUrl).mockReturnValue( |
| 18 | "https://github.com/testuser/testrepo.git", |
| 19 | ); |
| 20 | vi.mocked(gitModule.getGitBranch).mockReturnValue("feature/test-branch"); |
| 21 | }); |
| 22 | describe("getRepoInfo", () => { |
| 23 | it("should extract repo info correctly", () => { |
| 24 | const info = getRepoInfo(); |
| 25 | |
| 26 | expect(info.repoName).toBe("testuser/testrepo"); |
| 27 | expect(info.branchName).toBe("feature/test-branch"); |
| 28 | expect(info.isGitRepo).toBe(true); |
| 29 | }); |
| 30 | |
| 31 | it("should handle remote URL override", () => { |
| 32 | const info = getRepoInfo("https://github.com/override/repo.git"); |
| 33 | |
| 34 | expect(info.repoName).toBe("override/repo"); |
| 35 | expect(info.branchName).toBe("feature/test-branch"); |
| 36 | expect(info.isGitRepo).toBe(true); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | describe("getResponsiveRepoText", () => { |
| 41 | it("should return empty string when no width provided", () => { |
| 42 | const result = getResponsiveRepoText(); |
| 43 | expect(result).toBe(""); |
| 44 | }); |
| 45 | |
| 46 | it("should return full text when width allows", () => { |
| 47 | const result = getResponsiveRepoText(undefined, 100); |
| 48 | expect(result).toBe("testuser/testrepo ⊦feature/test-branch"); |
| 49 | }); |
| 50 | |
| 51 | it("should prefer branch when both fit individually", () => { |
| 52 | // Full text: "testuser/testrepo ⊦ feature/test-branch" (38 chars) |
| 53 | // Repo only: "testuser/testrepo" (17 chars) |
| 54 | // Branch only: "feature/test-branch" (18 chars) |
| 55 | // With width 20, both repo and branch fit, but we prefer branch |
| 56 | const result = getResponsiveRepoText(undefined, 20); |
| 57 | expect(result).toBe("feature/test-branch"); |
| 58 | }); |
| 59 | |
| 60 | it("should fallback to repo when branch doesn't fit but repo does", async () => { |
| 61 | // Use a long branch name |
| 62 | const gitModule = await import("../../util/git.js"); |
| 63 | vi.mocked(gitModule.getGitBranch).mockReturnValue( |
| 64 | "very-long-feature-branch-name-that-wont-fit", |
| 65 | ); |
| 66 | |
| 67 | const result = getResponsiveRepoText(undefined, 20); |
| 68 | // Branch is too long, repo should fit |
| 69 | expect(result).toBe("testuser/testrepo"); |
| 70 | }); |
| 71 | |
| 72 | it("should return branch only when repo doesn't fit but branch does", async () => { |
| 73 | // When space is very limited, prefer shorter option |
| 74 | const gitModule = await import("../../util/git.js"); |
| 75 | vi.mocked(gitModule.getGitRemoteUrl).mockReturnValue( |
| 76 | "https://github.com/very-long-repo-name/testrepo.git", |
| 77 | ); |
| 78 | vi.mocked(gitModule.getGitBranch).mockReturnValue("main"); |
| 79 | |
| 80 | const result = getResponsiveRepoText(undefined, 10); |
| 81 | // "main" (4 chars) should fit, "very-long-repo-name/testrepo" won't |
| 82 | expect(result).toBe("main"); |
| 83 | }); |
| 84 | |
| 85 | it("should return empty string when nothing fits", () => { |
| 86 | const result = getResponsiveRepoText(undefined, 5); |
| 87 | expect(result).toBe(""); |
| 88 | }); |
| 89 | |
| 90 | it("should return empty string when width is too small", () => { |
| 91 | const result = getResponsiveRepoText(undefined, 2); |
| 92 | expect(result).toBe(""); |
| 93 | }); |
| 94 | |
| 95 | it("should handle non-git repos", async () => { |
| 96 | const gitModule = await import("../../util/git.js"); |
| 97 | vi.mocked(gitModule.isGitRepo).mockReturnValue(false); |
| 98 | vi.mocked(gitModule.getGitBranch).mockReturnValue(null); |
| 99 | |
| 100 | const result = getResponsiveRepoText( |
| 101 | "https://github.com/user/repo.git", |
| 102 | 50, |
| 103 | ); |
| 104 | expect(result).toBe("user/repo"); |
| 105 | }); |
| 106 | |
| 107 | it("should return empty string when repo name is too long in non-git scenario", async () => { |
| 108 | const gitModule = await import("../../util/git.js"); |
| 109 | vi.mocked(gitModule.isGitRepo).mockReturnValue(false); |
| 110 | |
| 111 | const result = getResponsiveRepoText( |
| 112 | "https://github.com/very-long-user/repo.git", |
| 113 | 8, |
| 114 | ); |
| 115 | expect(result).toBe(""); |
| 116 | }); |
| 117 | }); |
| 118 | }); |