import { describe, expect, test } from "vitest";
import { formatMillis, nextDayStartMillis, normalizeTimeZone, startOfDayMillis, toEpochMillis } from "./time.js";

describe("time formatting", () => {
  test("formats the same epoch milliseconds in UTC and China time", () => {
    const epochMillis = Date.UTC(2026, 4, 9, 19, 21, 0);

    expect(formatMillis(epochMillis, "UTC")).toBe("2026-05-09 19:21:00");
    expect(formatMillis(epochMillis, "Asia/Shanghai")).toBe("2026-05-10 03:21:00");
  });

  test("keeps ambiguous local date strings out of table rendering", () => {
    expect(toEpochMillis("2026-05-09 19:21:00")).toBeNull();
    expect(formatMillis("2026-05-09T19:21:00Z", "Asia/Shanghai")).toBe("2026-05-10 03:21:00");
  });

  test("normalizes unsupported zones to the product default", () => {
    expect(normalizeTimeZone("UTC")).toBe("UTC");
    expect(normalizeTimeZone("America/Los_Angeles")).toBe("Asia/Shanghai");
  });

  test("calculates the current local day boundary for dashboard statistics", () => {
    const epochMillis = Date.UTC(2026, 4, 9, 19, 21, 0);

    expect(startOfDayMillis(epochMillis, "UTC")).toBe(Date.UTC(2026, 4, 9, 0, 0, 0));
    expect(startOfDayMillis(epochMillis, "Asia/Shanghai")).toBe(Date.UTC(2026, 4, 9, 16, 0, 0));
  });

  test("calculates the next local day boundary used for dashboard rollover", () => {
    const epochMillis = Date.UTC(2026, 4, 9, 19, 21, 0);

    expect(nextDayStartMillis(epochMillis, "UTC")).toBe(Date.UTC(2026, 4, 10, 0, 0, 0));
    expect(nextDayStartMillis(epochMillis, "Asia/Shanghai")).toBe(Date.UTC(2026, 4, 10, 16, 0, 0));
  });
});
