|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { getRepositoryId, clearRepositoryIdCache } from "./repository-id"; |
| 3 | +import { execSync } from "child_process"; |
| 4 | + |
| 5 | +vi.mock("child_process"); |
| 6 | + |
| 7 | +describe("getRepositoryId", () => { |
| 8 | + const originalEnv = process.env; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + process.env = { ...originalEnv }; |
| 12 | + vi.clearAllMocks(); |
| 13 | + clearRepositoryIdCache(); // Clear cache between tests |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + process.env = originalEnv; |
| 18 | + }); |
| 19 | + |
| 20 | + describe("CI environment variables", () => { |
| 21 | + it("should detect GitHub repository from GITHUB_REPOSITORY", () => { |
| 22 | + process.env.GITHUB_REPOSITORY = "owner/repo"; |
| 23 | + expect(getRepositoryId()).toBe("github:owner/repo"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should detect GitLab repository from CI_PROJECT_PATH", () => { |
| 27 | + process.env.CI_PROJECT_PATH = "namespace/project"; |
| 28 | + expect(getRepositoryId()).toBe("gitlab:namespace/project"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should detect Bitbucket repository from BITBUCKET_REPO_FULL_NAME", () => { |
| 32 | + process.env.BITBUCKET_REPO_FULL_NAME = "workspace/repo"; |
| 33 | + expect(getRepositoryId()).toBe("bitbucket:workspace/repo"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should prioritize CI environment variables over git remote", () => { |
| 37 | + process.env.GITHUB_REPOSITORY = "owner/repo"; |
| 38 | + vi.mocked(execSync).mockReturnValue( |
| 39 | + "git@gitlab.com:other/project.git" as any, |
| 40 | + ); |
| 41 | + expect(getRepositoryId()).toBe("github:owner/repo"); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe("git remote URL parsing", () => { |
| 46 | + it("should parse GitHub SSH URL", () => { |
| 47 | + vi.mocked(execSync).mockReturnValue("git@github.com:owner/repo.git" as any); |
| 48 | + expect(getRepositoryId()).toBe("github:owner/repo"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should parse GitHub HTTPS URL", () => { |
| 52 | + vi.mocked(execSync).mockReturnValue( |
| 53 | + "https://github.com/owner/repo.git" as any, |
| 54 | + ); |
| 55 | + expect(getRepositoryId()).toBe("github:owner/repo"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should parse GitLab SSH URL", () => { |
| 59 | + vi.mocked(execSync).mockReturnValue( |
| 60 | + "git@gitlab.com:namespace/project.git" as any, |
| 61 | + ); |
| 62 | + expect(getRepositoryId()).toBe("gitlab:namespace/project"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should parse GitLab HTTPS URL", () => { |
| 66 | + vi.mocked(execSync).mockReturnValue( |
| 67 | + "https://gitlab.com/namespace/project.git" as any, |
| 68 | + ); |
| 69 | + expect(getRepositoryId()).toBe("gitlab:namespace/project"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should parse Bitbucket SSH URL", () => { |
| 73 | + vi.mocked(execSync).mockReturnValue( |
| 74 | + "git@bitbucket.org:workspace/repo.git" as any, |
| 75 | + ); |
| 76 | + expect(getRepositoryId()).toBe("bitbucket:workspace/repo"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should parse Bitbucket HTTPS URL", () => { |
| 80 | + vi.mocked(execSync).mockReturnValue( |
| 81 | + "https://bitbucket.org/workspace/repo.git" as any, |
| 82 | + ); |
| 83 | + expect(getRepositoryId()).toBe("bitbucket:workspace/repo"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should parse self-hosted git URL with generic prefix", () => { |
| 87 | + vi.mocked(execSync).mockReturnValue( |
| 88 | + "git@custom-git.company.com:team/project.git" as any, |
| 89 | + ); |
| 90 | + expect(getRepositoryId()).toBe("git:team/project"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should handle URLs without .git extension", () => { |
| 94 | + vi.mocked(execSync).mockReturnValue("git@github.com:owner/repo" as any); |
| 95 | + expect(getRepositoryId()).toBe("github:owner/repo"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should return null when git command fails", () => { |
| 99 | + vi.mocked(execSync).mockImplementation(() => { |
| 100 | + throw new Error("not a git repository"); |
| 101 | + }); |
| 102 | + expect(getRepositoryId()).toBe(null); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should return null when git remote is empty", () => { |
| 106 | + vi.mocked(execSync).mockReturnValue("" as any); |
| 107 | + expect(getRepositoryId()).toBe(null); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should return null when git remote URL is invalid", () => { |
| 111 | + vi.mocked(execSync).mockReturnValue("invalid-url" as any); |
| 112 | + expect(getRepositoryId()).toBe(null); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments