import { describe, it, expect } from "vitest";
import { filterNewResults, findParticipantsWithoutResults } from "../admin.sports-seasons.$id.events.$eventId.helpers";

describe("filterNewResults", () => {
  it("returns all rows when no existing results", () => {
    const incoming = [
      { participantId: "p1", placement: 1 },
      { participantId: "p2", placement: 2 },
    ];
    expect(filterNewResults(incoming, new Set())).toEqual(incoming);
  });

  it("skips participantIds already in existingIds", () => {
    const incoming = [
      { participantId: "p1", placement: 1 },
      { participantId: "p2", placement: 2 },
    ];
    const existing = new Set(["p1"]);
    expect(filterNewResults(incoming, existing)).toEqual([
      { participantId: "p2", placement: 2 },
    ]);
  });

  it("returns empty array when all are duplicates", () => {
    const incoming = [{ participantId: "p1", placement: 1 }];
    const existing = new Set(["p1"]);
    expect(filterNewResults(incoming, existing)).toEqual([]);
  });
});

describe("findParticipantsWithoutResults", () => {
  it("returns participantIds not in existingResultIds", () => {
    const allIds = ["p1", "p2", "p3"];
    const existingIds = new Set(["p1"]);
    expect(findParticipantsWithoutResults(allIds, existingIds)).toEqual(["p2", "p3"]);
  });

  it("returns empty array when all participants have results", () => {
    const allIds = ["p1", "p2"];
    const existingIds = new Set(["p1", "p2"]);
    expect(findParticipantsWithoutResults(allIds, existingIds)).toEqual([]);
  });

  it("returns all when none have results", () => {
    const allIds = ["p1", "p2"];
    expect(findParticipantsWithoutResults(allIds, new Set())).toEqual(["p1", "p2"]);
  });
});
