import { describe, it, expect } from "vitest";
import { diffRoomLikes } from "@/lib/roomRepository";

describe("diffRoomLikes", () => {
  it("returns incremental create/delete changes", () => {
    const current = {
      r1: ["u1", "u2"],
      r2: ["u3"],
    };
    const updated = {
      r1: ["u1", "u3"],
      r3: ["u2"],
    };

    const result = diffRoomLikes(current, updated);

    expect(result.toDelete).toEqual(
      expect.arrayContaining([
        { userId: "u2", restaurantId: "r1" },
        { userId: "u3", restaurantId: "r2" },
      ]),
    );
    expect(result.toCreate).toEqual(
      expect.arrayContaining([
        { userId: "u3", restaurantId: "r1" },
        { userId: "u2", restaurantId: "r3" },
      ]),
    );
  });

  it("deduplicates repeated user likes", () => {
    const current = {
      r1: ["u1", "u1", "u1"],
    };
    const updated = {
      r1: ["u1"],
    };

    const result = diffRoomLikes(current, updated);

    expect(result.toCreate).toEqual([]);
    expect(result.toDelete).toEqual([]);
  });

  it("returns empty arrays when likes are unchanged", () => {
    const current = {
      r1: ["u1", "u2"],
    };
    const updated = {
      r1: ["u1", "u2"],
    };

    const result = diffRoomLikes(current, updated);

    expect(result.toCreate).toEqual([]);
    expect(result.toDelete).toEqual([]);
  });
});
