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

import { mapAdresseApiResponseToSuggestions } from "@/lib/addressAutocomplete";

describe("address autocomplete mapping", () => {
  it("maps valid API features to UI suggestions", () => {
    const suggestions = mapAdresseApiResponseToSuggestions({
      features: [
        {
          properties: {
            label: "15 Rue de la Paix 75002 Paris",
            housenumber: "15",
            street: "Rue de la Paix",
            postcode: "75002",
            city: "Paris",
          },
        },
      ],
    });

    expect(suggestions).toEqual([
      {
        label: "15 Rue de la Paix 75002 Paris",
        addressLine: "15 Rue de la Paix",
        postcode: "75002",
        city: "Paris",
      },
    ]);
  });

  it("falls back to name when street details are missing", () => {
    const suggestions = mapAdresseApiResponseToSuggestions({
      features: [
        {
          properties: {
            label: "Bourg de Sinnamary 97315 Sinnamary",
            name: "Bourg de Sinnamary",
            postcode: "97315",
            city: "Sinnamary",
          },
        },
      ],
    });

    expect(suggestions[0]?.addressLine).toBe("Bourg de Sinnamary");
  });

  it("ignores incomplete features", () => {
    const suggestions = mapAdresseApiResponseToSuggestions({
      features: [
        {
          properties: {
            label: "Adresse incomplete",
            postcode: "97310",
          },
        },
      ],
    });

    expect(suggestions).toEqual([]);
  });
});
