import { describe, expect, it, beforeEach, afterEach, vi } from "vitest";
import { setPublishedAt } from "./set-published-at";

describe("setPublishedAt", () => {
  beforeEach(() => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date("2026-05-04T12:00:00.000Z"));
  });

  afterEach(() => {
    vi.useRealTimers();
  });

  it("sets publishedAt to now when status is published and publishedAt is missing", () => {
    const result = setPublishedAt({ data: { status: "published" } });
    expect(result?.publishedAt).toBe("2026-05-04T12:00:00.000Z");
  });

  it("does not overwrite an existing publishedAt", () => {
    const result = setPublishedAt({
      data: { status: "published", publishedAt: "2025-01-01T00:00:00.000Z" },
    });
    expect(result?.publishedAt).toBe("2025-01-01T00:00:00.000Z");
  });

  it("does not set publishedAt when status is not published", () => {
    const result = setPublishedAt({ data: { status: "draft" } });
    expect(result?.publishedAt).toBeUndefined();
  });

  it("returns data unchanged when data is missing", () => {
    expect(setPublishedAt({})).toBeUndefined();
  });
});
