import { describe, expect, it } from "vitest";
import { dataPackageAgeDays, dataPackageStatus } from "./dataPackageStatus";

const NOW = Date.parse("2026-07-05T00:00:00.000Z");

describe("dataPackageStatus", () => {
  it("computes age in whole days", () => {
    expect(dataPackageAgeDays("2026-07-01T00:00:00.000Z", NOW)).toBe(4);
    expect(dataPackageAgeDays("2026-07-05T00:00:00.000Z", NOW)).toBe(0);
  });

  it("returns null for missing or invalid timestamps", () => {
    expect(dataPackageAgeDays("", NOW)).toBeNull();
    expect(dataPackageAgeDays("not-a-date", NOW)).toBeNull();
  });

  it("clamps future timestamps to zero", () => {
    expect(dataPackageAgeDays("2026-08-01T00:00:00.000Z", NOW)).toBe(0);
  });

  it("does not warn for a fresh package", () => {
    const status = dataPackageStatus("2026-06-20T00:00:00.000Z", "genshin-db@5.2.12", NOW);
    expect(status.stale).toBe(false);
    expect(status.warning).toBe("");
    expect(status.ageDays).toBe(15);
  });

  it("warns for a package older than the max age", () => {
    const status = dataPackageStatus("2026-04-01T00:00:00.000Z", "genshin-db@5.2.12", NOW, 45);
    expect(status.stale).toBe(true);
    expect(status.warning).toContain("Datenpaket");
    expect(status.warning).toContain("aktualisieren");
  });

  it("stays quiet when the generation date is unknown", () => {
    const status = dataPackageStatus("", "unknown", NOW);
    expect(status.stale).toBe(false);
    expect(status.warning).toBe("");
    expect(status.ageDays).toBeNull();
  });
});
