import { describe, it, expect } from "vitest";
import { createError, AppError, toMessageError, ErrorCode } from "../index.js";
describe("createError", () => {
    it("should create formatted error response", () => {
        const error = createError(ErrorCode.UNKNOWN_ERROR, "Something went wrong");
        expect(error).toEqual({
            code: ErrorCode.UNKNOWN_ERROR,
            message: "Something went wrong",
        });
    });
    it("should include error code", () => {
        const error = createError(ErrorCode.FILE_NOT_FOUND, "File not found");
        expect(error.code).toBe(ErrorCode.FILE_NOT_FOUND);
    });
    it("should include details when provided", () => {
        const error = createError(ErrorCode.FILE_TOO_LARGE, "File exceeds 10MB limit", {
            size: 15728640,
            limit: 10485760,
        });
        expect(error.details).toEqual({
            size: 15728640,
            limit: 10485760,
        });
    });
    it("should omit details when not provided", () => {
        const error = createError(ErrorCode.UNAUTHORIZED, "Not authenticated");
        expect(error).not.toHaveProperty("details");
    });
    it("should use all error codes from PRD specification with UPPERCASE values", () => {
        // Verify key error codes exist in the enum with UPPERCASE values (per PRD)
        expect(ErrorCode.OUTSIDE_PROJECT_ROOT).toBe("OUTSIDE_PROJECT_ROOT");
        expect(ErrorCode.INVALID_FILENAME).toBe("INVALID_FILENAME");
        expect(ErrorCode.PATH_NOT_FOUND).toBe("PATH_NOT_FOUND");
        expect(ErrorCode.DAEMON_BUSY).toBe("DAEMON_BUSY");
        expect(ErrorCode.TERMINAL_NOT_FOUND).toBe("TERMINAL_NOT_FOUND");
        expect(ErrorCode.OPENCODE_NOT_INSTALLED).toBe("OPENCODE_NOT_INSTALLED");
        expect(ErrorCode.PERMISSION_DENIED).toBe("PERMISSION_DENIED");
        expect(ErrorCode.FILE_TOO_LARGE).toBe("FILE_TOO_LARGE");
        expect(ErrorCode.UNSUPPORTED_FORMAT).toBe("UNSUPPORTED_FORMAT");
    });
});
describe("AppError", () => {
    it("should extend Error", () => {
        const err = new AppError(ErrorCode.FILE_NOT_FOUND, "Not found");
        expect(err).toBeInstanceOf(Error);
        expect(err).toBeInstanceOf(AppError);
    });
    it("should carry code and message", () => {
        const err = new AppError(ErrorCode.OUTSIDE_PROJECT_ROOT, "Path escapes project");
        expect(err.code).toBe(ErrorCode.OUTSIDE_PROJECT_ROOT);
        expect(err.message).toBe("Path escapes project");
        expect(err.name).toBe("AppError");
    });
    it("should carry optional details", () => {
        const err = new AppError(ErrorCode.FILE_TOO_LARGE, "Too large", {
            size: 15728640,
            limit: 10485760,
        });
        expect(err.details).toEqual({ size: 15728640, limit: 10485760 });
    });
    it("should convert to MessageError via toMessageError()", () => {
        const err = new AppError(ErrorCode.TERMINAL_NOT_FOUND, "Terminal not found", {
            terminalId: "t1",
        });
        const messageError = err.toMessageError();
        expect(messageError).toEqual({
            code: ErrorCode.TERMINAL_NOT_FOUND,
            message: "Terminal not found",
            details: { terminalId: "t1" },
        });
    });
});
describe("toMessageError", () => {
    it("should extract MessageError from AppError", () => {
        const err = new AppError(ErrorCode.INVALID_FILENAME, "Bad filename", {
            filename: "con",
        });
        const result = toMessageError(err);
        expect(result).toEqual({
            code: ErrorCode.INVALID_FILENAME,
            message: "Bad filename",
            details: { filename: "con" },
        });
    });
    it("should wrap regular Error as UNKNOWN_ERROR", () => {
        const err = new Error("Something unexpected");
        const result = toMessageError(err);
        expect(result).toEqual({
            code: ErrorCode.UNKNOWN_ERROR,
            message: "Something unexpected",
        });
    });
    it("should wrap string as UNKNOWN_ERROR", () => {
        const result = toMessageError("string error");
        expect(result).toEqual({
            code: ErrorCode.UNKNOWN_ERROR,
            message: "string error",
        });
    });
    it("should handle null/undefined", () => {
        expect(toMessageError(null).code).toBe(ErrorCode.UNKNOWN_ERROR);
        expect(toMessageError(undefined).code).toBe(ErrorCode.UNKNOWN_ERROR);
    });
});
//# sourceMappingURL=index.test.js.map