import { SyncRunStatus } from "@prisma/client";
import { describe, expect, it } from "vitest";
import { resolveSyncStatus } from "@/lib/licitations/sync-status";
import type { SyncStats } from "@/lib/licitations/types";

function makeStats(overrides?: Partial<SyncStats>): SyncStats {
  return {
    totalFetched: 0,
    inserted: 0,
    updated: 0,
    skipped: 0,
    errors: 0,
    latestPublishDate: null,
    ...overrides,
  };
}

describe("resolveSyncStatus", () => {
  it("returns success when there are no errors and no blocking warnings", () => {
    const status = resolveSyncStatus(makeStats({ totalFetched: 4, inserted: 2, updated: 1 }), []);
    expect(status).toBe(SyncRunStatus.SUCCESS);
  });

  it("returns partial when no records were fetched and warnings exist", () => {
    const status = resolveSyncStatus(makeStats({ totalFetched: 0 }), ["anti-bot challenge"]);
    expect(status).toBe(SyncRunStatus.PARTIAL);
  });

  it("returns failed when there are errors and no persisted records", () => {
    const status = resolveSyncStatus(makeStats({ errors: 2, inserted: 0, updated: 0 }), []);
    expect(status).toBe(SyncRunStatus.FAILED);
  });

  it("returns partial when there are errors but some records were persisted", () => {
    const status = resolveSyncStatus(makeStats({ errors: 1, inserted: 3, updated: 0 }), []);
    expect(status).toBe(SyncRunStatus.PARTIAL);
  });
});
