import { describe, expect, it } from "vitest";
import { tidyMerchant } from "@/lib/merchant-label";

/**
 * Board 212. Every fixture below is a real `display_name` from order_feed.
 * The cases that matter most are the ones that must NOT change: this is a
 * presentational tidy and merging anything here would be a data error wearing
 * a stylesheet.
 */
describe("tidyMerchant", () => {
  it("capitalises a bare domain used as a name", () => {
    expect(tidyMerchant("amazon.com.au")).toBe("Amazon.com.au");
    expect(tidyMerchant("cdkeys.com")).toBe("Cdkeys.com");
  });

  it("NEVER merges the country marketplaces", () => {
    // The single most important assertion in this file. Amazon.in is a
    // different marketplace with different currency and geography, and 289
    // orders sit behind it.
    expect(tidyMerchant("Amazon.in")).toBe("Amazon.in");
    expect(tidyMerchant("amazon.com.au")).not.toBe(tidyMerchant("Amazon.in"));
  });

  it("leaves real brand names that contain a TLD alone", () => {
    for (const brand of ["Kogan.com", "GOG.com", "AliExpress.com", "Catch.com.au", "GeekBuying.com"]) {
      expect(tidyMerchant(brand)).toBe(brand);
    }
  });

  it("does not uppercase a deliberately lower-cased brand", () => {
    // The bug the corpus run found: gating on "starts lower-case" rather than
    // on "is a bare domain" turned this into "EBay Commerce Australia".
    expect(tidyMerchant("eBay Commerce Australia Pty Ltd.")).toBe("eBay Commerce Australia");
    expect(tidyMerchant("iRobot Australia Pty Ltd")).toBe("iRobot Australia");
  });

  it("drops trailing corporate suffixes", () => {
    expect(tidyMerchant("Apple Pty Ltd.")).toBe("Apple");
    expect(tidyMerchant("Microsoft Pty. Limited")).toBe("Microsoft");
    expect(tidyMerchant("Meta Platforms, Inc.")).toBe("Meta Platforms");
    expect(tidyMerchant("Domino's Pizza Enterprises Limited")).toBe("Domino's Pizza Enterprises");
    expect(tidyMerchant("Blinks Labs GmbH")).toBe("Blinks Labs");
    expect(tidyMerchant("Rasier New Zealand Limited")).toBe("Rasier New Zealand");
  });

  it("strips two suffixes when a name carries two", () => {
    expect(tidyMerchant("Samsung Electronics Co. Ltd.")).toBe("Samsung Electronics");
    expect(tidyMerchant("DiDi Mobility Information Technology Pte. Ltd."))
      .toBe("DiDi Mobility Information Technology");
  });

  it("does not leave a dangling connector", () => {
    // 'Co' is part of this brand. Stripping it produced "Coburger &".
    expect(tidyMerchant("Coburger & Co")).toBe("Coburger & Co");
  });

  it("keeps a name that would be reduced to almost nothing", () => {
    // "UT" is too short to stand on its own; better scruffy than cryptic.
    expect(tidyMerchant("UT LLC")).toBe("UT LLC");
  });

  it("normalises whitespace and tolerates junk", () => {
    expect(tidyMerchant("  Yaffa   Media  Pty Ltd ")).toBe("Yaffa Media");
    expect(tidyMerchant("")).toBe("");
  });

  it("is idempotent", () => {
    for (const n of ["amazon.com.au", "Apple Pty Ltd.", "Coburger & Co", "Amazon.in"]) {
      expect(tidyMerchant(tidyMerchant(n))).toBe(tidyMerchant(n));
    }
  });
});
