/**
 * Regression tests for the event cards' date range.
 *
 * Two rules, both easy to lose in a refactor and both visible on the home page:
 * a same-month range collapses to one month name, and in Arabic the days of
 * such a range are written right-to-left. The third test records that a
 * cross-month range is **not** reversed in Arabic — the shipped behaviour,
 * pinned so that changing it has to be deliberate.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { formatDateRange } from "@/features/home/components/eventDateRange";

/* Stands in for i18next: returns the key so the month is identifiable. */
const t = (key: string) => key.replace("COMMON.", "");

describe("formatDateRange — same month", () => {
  test("collapses to one month name", () => {
    assert.equal(formatDateRange("2026-03-05", "2026-03-07", "en", t), "05-07 MARCH");
  });

  test("writes the days right-to-left in Arabic", () => {
    assert.equal(formatDateRange("2026-03-05", "2026-03-07", "ar", t), "07-05 MARCH");
  });

  test("a single-day event shows the day only once", () => {
    assert.equal(formatDateRange("2026-03-05", "2026-03-05", "en", t), "05 MARCH");
  });

  test("days are zero-padded", () => {
    assert.equal(formatDateRange("2026-03-01", "2026-03-09", "en", t), "01-09 MARCH");
  });
});

describe("formatDateRange — across months", () => {
  test("names both months", () => {
    assert.equal(
      formatDateRange("2026-03-28", "2026-04-02", "en", t),
      "28 MARCH - 02 APRIL"
    );
  });

  test("is NOT reversed in Arabic — recorded, not endorsed", () => {
    /*
     * The two branches in the source are identical for `ar` and `en`. Only the
     * same-month case swaps the day order. Whether a cross-month range should
     * also swap is a copy decision, so this pins what ships today.
     */
    assert.equal(
      formatDateRange("2026-03-28", "2026-04-02", "ar", t),
      formatDateRange("2026-03-28", "2026-04-02", "en", t)
    );
  });

  test("spans a year boundary", () => {
    assert.equal(
      formatDateRange("2026-12-30", "2027-01-02", "en", t),
      "30 DECEMBER - 02 JANUARY"
    );
  });
});

describe("formatDateRange — month names", () => {
  test("every month maps to its own key", () => {
    const months = [
      "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
      "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER",
    ];
    months.forEach((name, index) => {
      const month = String(index + 1).padStart(2, "0");
      assert.equal(
        formatDateRange(`2026-${month}-01`, `2026-${month}-02`, "en", t),
        `01-02 ${name}`,
        name
      );
    });
  });
});
