/**
 * Regression tests for community timestamps.
 *
 * Written out per language rather than through `moment.fromNow()` because the
 * Arabic wording is the client's and because moment's own `ar` locale rewrites
 * digits into Arabic-Indic, which this product never uses. These tests pin the
 * singular/plural boundaries in both languages — the part that a rewrite to
 * `fromNow()` would silently change.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import moment from "moment";
import { relativeTime } from "@/features/community/relativeTime";

/* Identity stand-in for i18next; only the invalid-date branch uses it. */
const t = (key: string) => key;
const ago = (amount: number, unit: moment.unitOfTime.DurationConstructor) =>
  moment().subtract(amount, unit).toISOString();

describe("relativeTime — English", () => {
  test("under a minute reads as seconds", () => {
    assert.equal(relativeTime(ago(5, "seconds"), "en", t), "few seconds ago");
  });

  test("singular at exactly one minute, plural after", () => {
    assert.equal(relativeTime(ago(1, "minute"), "en", t), "1 minute ago");
    assert.equal(relativeTime(ago(2, "minutes"), "en", t), "2 minutes ago");
    assert.equal(relativeTime(ago(59, "minutes"), "en", t), "59 minutes ago");
  });

  test("singular at exactly one hour, plural after", () => {
    assert.equal(relativeTime(ago(1, "hour"), "en", t), "1 hour ago");
    assert.equal(relativeTime(ago(3, "hours"), "en", t), "3 hours ago");
    assert.equal(relativeTime(ago(23, "hours"), "en", t), "23 hours ago");
  });

  test("singular at exactly one day, plural after", () => {
    assert.equal(relativeTime(ago(1, "day"), "en", t), "1 day ago");
    assert.equal(relativeTime(ago(9, "days"), "en", t), "9 days ago");
  });

  test("days keep counting rather than rolling into weeks or months", () => {
    assert.equal(relativeTime(ago(45, "days"), "en", t), "45 days ago");
    assert.equal(relativeTime(ago(400, "days"), "en", t), "400 days ago");
  });
});

describe("relativeTime — Arabic", () => {
  test("under a minute reads as seconds", () => {
    assert.equal(relativeTime(ago(5, "seconds"), "ar", t), "منذ ثوانٍ");
  });

  test("uses the singular noun at one and the plural after", () => {
    assert.equal(relativeTime(ago(1, "minute"), "ar", t), "منذ 1 دقيقة");
    assert.equal(relativeTime(ago(4, "minutes"), "ar", t), "منذ 4 دقائق");
    assert.equal(relativeTime(ago(1, "hour"), "ar", t), "منذ 1 ساعة");
    assert.equal(relativeTime(ago(5, "hours"), "ar", t), "منذ 5 ساعات");
    assert.equal(relativeTime(ago(1, "day"), "ar", t), "منذ 1 يوم");
    assert.equal(relativeTime(ago(6, "days"), "ar", t), "منذ 6 أيام");
  });

  test("the number stays in Western digits", () => {
    const output = relativeTime(ago(7, "hours"), "ar", t);
    assert.ok(output.includes("7"), output);
    // No Arabic-Indic digits: moment's `ar` postformat is overridden app-wide.
    assert.ok(!/[٠-٩]/.test(output), output);
  });
});

describe("relativeTime — bad input", () => {
  test("falls back to the loading key when the date is missing", () => {
    assert.equal(relativeTime(undefined, "en", t), "COMMON.LOADING_TIME");
    assert.equal(relativeTime("", "en", t), "COMMON.LOADING_TIME");
  });

  test("falls back when the date cannot be parsed", () => {
    assert.equal(relativeTime("not-a-date", "en", t), "COMMON.LOADING_TIME");
  });

  test("an unknown language takes the English branch", () => {
    assert.equal(relativeTime(ago(2, "hours"), "fr", t), "2 hours ago");
  });
});
