/**
 * Regression tests for age arithmetic.
 *
 * `lib/age.ts` now backs two features that each had their own copy: "age at
 * registration" on the opportunity registration lists, and "is this volunteer
 * under 18" on the mandate form. The month/day adjustment is the whole point —
 * subtracting birth years alone is wrong for anyone whose birthday has not come
 * round yet in the reference year, and that off-by-one decides whether a
 * guardian's details are required.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { ageAt, ageToday, isUnderageToday } from "@/lib/age";

describe("ageAt", () => {
  test("counts whole years when the birthday has passed", () => {
    assert.equal(ageAt("2000-01-15", "2026-06-01"), 26);
  });

  test("subtracts one when the birthday has NOT come round yet", () => {
    // Born in December, measured in June: still 25, not 26.
    assert.equal(ageAt("2000-12-31", "2026-06-01"), 25);
  });

  test("is exact on the birthday itself", () => {
    assert.equal(ageAt("2008-06-01", "2026-06-01"), 18);
  });

  test("is one less the day before the birthday", () => {
    assert.equal(ageAt("2008-06-02", "2026-06-01"), 17);
  });

  test("is one more the day after", () => {
    assert.equal(ageAt("2008-05-31", "2026-06-01"), 18);
  });

  test("handles a same-month, later-day birthday", () => {
    assert.equal(ageAt("2008-06-20", "2026-06-10"), 17);
  });

  test("handles 29 February birthdays", () => {
    assert.equal(ageAt("2008-02-29", "2026-02-28"), 17);
    assert.equal(ageAt("2008-02-29", "2026-03-01"), 18);
  });

  test("accepts a Date as the reference", () => {
    assert.equal(ageAt("2000-01-15", new Date(2026, 5, 1)), 26);
  });

  test("is null when either date is missing", () => {
    assert.equal(ageAt(null, "2026-06-01"), null);
    assert.equal(ageAt("2000-01-15", null), null);
    assert.equal(ageAt(undefined, undefined), null);
    assert.equal(ageAt("", "2026-06-01"), null);
  });

  test("is null rather than NaN for an unparseable date", () => {
    assert.equal(ageAt("not-a-date", "2026-06-01"), null);
    assert.equal(ageAt("2000-01-15", "not-a-date"), null);
  });
});

describe("ageToday / isUnderageToday", () => {
  const yearsAgo = (years: number, extraDays = 0) => {
    const d = new Date();
    d.setFullYear(d.getFullYear() - years);
    d.setDate(d.getDate() - extraDays);
    return d.toISOString().slice(0, 10);
  };

  test("someone born exactly 20 years ago is 20", () => {
    assert.equal(ageToday(yearsAgo(20)), 20);
  });

  test("17 is underage and 18 is not", () => {
    assert.equal(isUnderageToday(yearsAgo(17)), true);
    assert.equal(isUnderageToday(yearsAgo(18)), false);
  });

  test("the day after an 18th birthday is not underage", () => {
    assert.equal(isUnderageToday(yearsAgo(18, 1)), false);
  });

  test("an unknown date of birth is not treated as underage", () => {
    // Deliberate: the form cannot demand a guardian for an unknown age.
    assert.equal(isUnderageToday(null), false);
    assert.equal(isUnderageToday(undefined), false);
    assert.equal(isUnderageToday(""), false);
  });
});
