import { AxiosError, type AxiosResponse } from "axios";
import { describe, expect, it } from "vitest";
import { formatAuthError } from "./formatAuthError";

describe("formatAuthError", () => {
  it("shows network message when API is unreachable", () => {
    const message = formatAuthError(new AxiosError("Network Error", "ERR_NETWORK"), "Login failed");
    expect(message).toBe(
      "Cannot reach the API. Make sure the backend is running and reload the page."
    );
  });

  it("extracts password validation message", () => {
    const message = formatAuthError(
      new AxiosError(
        "Validation failed",
        "422",
        undefined,
        undefined,
        {
          status: 422,
          data: {
            detail: [
              {
                loc: ["body", "password"],
                msg: "Value error, Password must include at least one digit"
              }
            ]
          }
        } as AxiosResponse
      ),
      "Registration failed"
    );

    expect(message).toBe("Password must include at least one digit");
  });
});
