import { describe, expect, it } from "vitest";
import { GitErrorType, parseGitError } from "../error-parser.js";
describe("parseGitError", () => {
    it("should detect auth failure", () => {
        const result = parseGitError("fatal: Authentication failed for 'https://github.com/user/repo'");
        expect(result.type).toBe(GitErrorType.AUTH_FAILED);
        expect(result.message).toBe("Authentication failed");
        expect(result.hint).toBeDefined();
    });
    it("should detect network error from unresolved host", () => {
        const result = parseGitError("fatal: unable to access 'https://github.com/user/repo': Could not resolve host: github.com");
        expect(result.type).toBe(GitErrorType.NETWORK_ERROR);
        expect(result.message).toContain("Network error");
        expect(result.hint).toBeDefined();
    });
    it("should detect network error from connection refused", () => {
        const result = parseGitError("fatal: unable to access 'https://github.com/user/repo': Connection refused");
        expect(result.type).toBe(GitErrorType.NETWORK_ERROR);
        expect(result.message).toContain("Network error");
    });
    it("should detect merge conflict", () => {
        const result = parseGitError("CONFLICT (content): Merge conflict in src/index.ts");
        expect(result.type).toBe(GitErrorType.MERGE_CONFLICT);
        expect(result.message).toContain("conflict");
        expect(result.hint).toContain("Resolve conflicts");
    });
    it("should detect uncommitted changes", () => {
        const result = parseGitError("error: cannot pull with rebase: You have uncommitted changes.");
        expect(result.type).toBe(GitErrorType.UNCOMMITTED_CHANGES);
        expect(result.message).toContain("uncommitted");
        expect(result.hint).toContain("stash");
    });
    it("should detect branch already exists", () => {
        const result = parseGitError("fatal: A branch named 'feature' already exists.");
        expect(result.type).toBe(GitErrorType.BRANCH_EXISTS);
        expect(result.message).toContain("already exists");
        expect(result.hint).toBeDefined();
    });
    it("should detect branch not found", () => {
        const result = parseGitError("error: pathspec 'nonexistent-branch' did not match any file(s) known to git");
        expect(result.type).toBe(GitErrorType.BRANCH_NOT_FOUND);
        expect(result.message).toContain("not found");
        expect(result.hint).toBeDefined();
    });
    it("should detect rejected push", () => {
        const result = parseGitError("To github.com:user/repo.git\n ! [rejected]        main -> main (fetch first)\nerror: failed to push some refs");
        expect(result.type).toBe(GitErrorType.REMOTE_REJECTED);
        expect(result.message).toContain("rejected");
        expect(result.hint).toContain("Pull");
    });
    it("should detect protected branch rejection", () => {
        const result = parseGitError("remote: error: GH006: Protected branch update failed for refs/heads/main.\nremote: error: Changes must be made through a pull request. rejected push to protected branch main");
        expect(result.type).toBe(GitErrorType.PROTECTED_BRANCH);
        expect(result.message).toContain("protected");
        expect(result.hint).toContain("pull request");
    });
    it("should detect nothing to commit", () => {
        const result = parseGitError("On branch main\nnothing to commit, working tree clean");
        expect(result.type).toBe(GitErrorType.NOTHING_TO_COMMIT);
        expect(result.message).toContain("Nothing to commit");
        expect(result.hint).toContain("Stage");
    });
    it("should return UNKNOWN for unrecognized errors", () => {
        const stderr = "fatal: some completely unexpected git error";
        const result = parseGitError(stderr);
        expect(result.type).toBe(GitErrorType.UNKNOWN);
        expect(result.message).toBe(stderr);
        expect(result.hint).toBeUndefined();
    });
    it("should handle empty stderr", () => {
        const result = parseGitError("");
        expect(result.type).toBe(GitErrorType.UNKNOWN);
        expect(result.message).toBe("");
    });
    it("should match patterns case-insensitively", () => {
        const result = parseGitError("AUTHENTICATION FAILED for remote");
        expect(result.type).toBe(GitErrorType.AUTH_FAILED);
    });
    it("should return first matching pattern for multi-error stderr", () => {
        // Authentication failed appears before CONFLICT in the pattern list
        const result = parseGitError("Authentication failed\nCONFLICT in file.ts");
        expect(result.type).toBe(GitErrorType.AUTH_FAILED);
    });
});
//# sourceMappingURL=error-parser.test.js.map