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

import { ExceptionHandler } from "@/core/errors";

import { ApiError } from "../api_result";
import { getCharacterErrorCode } from "../character_error_code";

describe("getCharacterErrorCode", () => {
  it("reads nested backend detail through AppException causes", () => {
    const error = ExceptionHandler.toError(
      new ApiError("HTTP_ERROR", "Mismatch", 409, {
        detail: { errorCode: "CHARACTER_MISMATCH" },
      }),
    );

    expect(getCharacterErrorCode(error)).toBe("CHARACTER_MISMATCH");
  });

  it("ignores unrelated backend codes", () => {
    expect(
      getCharacterErrorCode(
        new ApiError("HTTP_ERROR", "No", 400, {
          detail: { errorCode: "OTHER_ERROR" },
        }),
      ),
    ).toBeNull();
  });
});
