/**
 * Unit tests for scheduleDateRedirect guard utility.
 *
 * Clock frozen at 2026-05-15 (noon local). Schedule window: 2026-05-14 … 2027-04-10.
 */

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

describe("scheduleDateRedirect (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(scheduleDateRedirect("ru-ru", "20260515")).toBeNull();
  });

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

  it("returns null for the latest allowed date (today + 330)", () => {
    // 2026-05-15 + 330 days = 2027-04-10
    expect(scheduleDateRedirect("ru-ru", "20270410")).toBeNull();
  });

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

  it("redirects when date is too late (today + 331)", () => {
    // 2026-05-15 + 331 days = 2027-04-11
    expect(scheduleDateRedirect("ru-ru", "20270411")).toBe("/ru-ru/schedule");
  });

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

  it("returns null for a mid-window date (today + 100)", () => {
    // 2026-05-15 + 100 = 2026-08-23
    expect(scheduleDateRedirect("ru-ru", "20260823")).toBeNull();
  });
});
