/**
 * Unit tests for boardDateRedirect guard utility.
 *
 * Clock frozen at 2026-05-15 (noon local). Board window: 2026-05-14 … 2026-05-29.
 */

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { boardDateRedirect, boardSearchRedirect } from "./_guards.js";

describe("boardDateRedirect (clock frozen 2026-05-15)", () => {
  beforeEach(() => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date(2026, 4, 15, 12)); // May 15 2026 noon
  });
  afterEach(() => {
    vi.useRealTimers();
  });

  it("returns null for a date inside the window (today)", () => {
    expect(boardDateRedirect("ru-ru", "20260515")).toBeNull();
  });

  it("returns null for the earliest allowed date (today - 1)", () => {
    expect(boardDateRedirect("ru-ru", "20260514")).toBeNull();
  });

  it("returns null for the latest allowed date (today + 14)", () => {
    expect(boardDateRedirect("ru-ru", "20260529")).toBeNull();
  });

  it("redirects when date is too early (today - 2)", () => {
    expect(boardDateRedirect("ru-ru", "20260513")).toBe("/ru-ru/onlineboard");
  });

  it("redirects when date is too late (today + 15)", () => {
    expect(boardDateRedirect("ru-ru", "20260530")).toBe("/ru-ru/onlineboard");
  });

  it("uses the supplied locale in the redirect path", () => {
    expect(boardDateRedirect("en-us", "20260101")).toBe("/en-us/onlineboard");
  });
});

describe("boardSearchRedirect time-range guard", () => {
  beforeEach(() => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date(2026, 4, 15, 12)); // May 15 2026 noon
  });
  afterEach(() => {
    vi.useRealTimers();
  });

  it("allows a valid time-range suffix", () => {
    expect(
      boardSearchRedirect("ru-ru", {
        date: "20260515",
        timeFrom: "1400",
        timeTo: "1800",
      }),
    ).toBeNull();
  });

  it("allows 2400 as the upper bound", () => {
    expect(
      boardSearchRedirect("ru-ru", {
        date: "20260515",
        timeFrom: "2359",
        timeTo: "2400",
      }),
    ).toBeNull();
  });

  it("redirects when the time range is reversed", () => {
    expect(
      boardSearchRedirect("ru-ru", {
        date: "20260515",
        timeFrom: "1800",
        timeTo: "1400",
      }),
    ).toBe("/ru-ru/onlineboard");
  });

  it("redirects when a time component is outside HHmm bounds", () => {
    expect(
      boardSearchRedirect("ru-ru", {
        date: "20260515",
        timeFrom: "1460",
        timeTo: "1800",
      }),
    ).toBe("/ru-ru/onlineboard");
  });
});
