import { describe, expect, it } from "vitest";
import {
  US_STATES,
  filterUsStateOptions,
  isValidUsStateCode,
  normalizeUsStateCode,
} from "@/lib/frontend/us-states";

describe("us-states", () => {
  it("包含全部 50 个州码", () => {
    expect(US_STATES).toHaveLength(50);
    expect(new Set(US_STATES).size).toBe(50);
  });

  it("normalizeUsStateCode 支持州码与州名", () => {
    expect(normalizeUsStateCode("ca")).toBe("CA");
    expect(normalizeUsStateCode("California")).toBe("CA");
    expect(normalizeUsStateCode("new york")).toBe("NY");
    expect(normalizeUsStateCode("XX")).toBeNull();
    expect(normalizeUsStateCode("")).toBeNull();
  });

  it("filterUsStateOptions 可按州码或州名过滤", () => {
    const byCode = filterUsStateOptions("tx");
    expect(byCode.some((s) => s.code === "TX")).toBe(true);

    const byName = filterUsStateOptions("carolina");
    expect(byName.map((s) => s.code).sort()).toEqual(["NC", "SC"]);

    expect(filterUsStateOptions("")).toHaveLength(50);
  });

  it("isValidUsStateCode 校验 2 位州码", () => {
    expect(isValidUsStateCode("WY")).toBe(true);
    expect(isValidUsStateCode("wy")).toBe(true);
    expect(isValidUsStateCode("ZZ")).toBe(false);
  });
});
