import { getRepositoryInfo, parseRepositoryFromRemoteUrl } from "./telemetry.js";
describe("parseRepositoryFromRemoteUrl", () => {
    it("parses an HTTPS remote with .git suffix", () => {
        expect(parseRepositoryFromRemoteUrl("https://github.com/acme-corp/their-api.git")).toEqual({ repository: "acme-corp/their-api", repositoryOwner: "acme-corp" });
    });
    it("parses an HTTPS remote without .git suffix", () => {
        expect(parseRepositoryFromRemoteUrl("https://github.com/acme-corp/their-api")).toEqual({ repository: "acme-corp/their-api", repositoryOwner: "acme-corp" });
    });
    it("parses an SSH remote", () => {
        expect(parseRepositoryFromRemoteUrl("git@github.com:acme-corp/their-api.git")).toEqual({ repository: "acme-corp/their-api", repositoryOwner: "acme-corp" });
    });
    it("trims trailing whitespace/newline from git output", () => {
        expect(parseRepositoryFromRemoteUrl("git@github.com:acme-corp/their-api.git\n")).toEqual({ repository: "acme-corp/their-api", repositoryOwner: "acme-corp" });
    });
    it("returns undefined for an unparseable URL", () => {
        expect(parseRepositoryFromRemoteUrl("not-a-remote")).toBeUndefined();
    });
});
describe("getRepositoryInfo", () => {
    const originalEnv = { ...process.env };
    afterEach(() => {
        process.env = { ...originalEnv };
    });
    describe("in GitHub Actions", () => {
        beforeEach(() => {
            process.env.GITHUB_ACTIONS = "true";
        });
        it("returns repository and owner from env vars", async () => {
            process.env.GITHUB_REPOSITORY = "acme-corp/their-api";
            process.env.GITHUB_REPOSITORY_OWNER = "acme-corp";
            await expect(getRepositoryInfo()).resolves.toEqual({
                repository: "acme-corp/their-api",
                repositoryOwner: "acme-corp",
            });
        });
        it("includes only the fields that are present", async () => {
            process.env.GITHUB_REPOSITORY = "acme-corp/their-api";
            delete process.env.GITHUB_REPOSITORY_OWNER;
            await expect(getRepositoryInfo()).resolves.toEqual({
                repository: "acme-corp/their-api",
            });
        });
    });
    describe("local / IDE", () => {
        beforeEach(() => {
            delete process.env.GITHUB_ACTIONS;
        });
        it("derives repository info from the local git remote", async () => {
            // The test suite itself runs inside the mcp git repo (origin = letsramp/mcp),
            // so a local lookup should resolve owner=letsramp, repo=mcp.
            const info = await getRepositoryInfo();
            expect(info.repositoryOwner).toBe("letsramp");
            expect(info.repository).toBe("letsramp/mcp");
        });
        it("uses the caller-supplied cwd when provided", async () => {
            // Passing the repo path explicitly (as tools do via repositoryPath)
            // resolves the same remote even if process.cwd() differs.
            const info = await getRepositoryInfo(process.cwd());
            expect(info.repository).toBe("letsramp/mcp");
        });
        it("ignores GITHUB_REPOSITORY env when not in GitHub Actions", async () => {
            // A stray env var must not be trusted outside Actions; the git remote is
            // the source of truth locally.
            process.env.GITHUB_REPOSITORY = "spoofed/repo";
            process.env.GITHUB_REPOSITORY_OWNER = "spoofed";
            const info = await getRepositoryInfo();
            expect(info.repository).not.toBe("spoofed/repo");
        });
    });
});
