import { describe, expect, it } from "vitest";

import { AppException, ExceptionCode } from "@/core/errors";

import { Result, toError } from "../result";

describe("Result error normalization", () => {
  it("normalizes unknown thrown values to AppException", async () => {
    const result = await Result.wrap(async () => {
      throw "boom";
    });

    expect(Result.isErr(result)).toBe(true);
    if (Result.isOk(result)) throw new Error("Expected Result.err");

    expect(result.error).toBeInstanceOf(AppException);
    expect(result.error.message).toBe("boom");
    expect((result.error as AppException).code).toBe(ExceptionCode.unknown);
  });

  it("returns AppException from toError", () => {
    const error = toError(new TypeError("Failed to fetch"));

    expect(error).toBeInstanceOf(AppException);
    expect(error.message).toBe("Network connection failed.");
    expect((error as AppException).code).toBe(ExceptionCode.network);
  });
});
