import { describe, expect, it } from "vitest";
import { createActor, waitFor } from "xstate";

import {
  createTestPrivateRoomMachine,
  loadPrivateRoom,
  makeAlbum,
  makeAlbumsResponse,
} from "./private-room-machine.test-utils";

describe("private room album flow", () => {
  it("loads only the first non-paginated album page", async () => {
    const actor = await loadPrivateRoom({
      loadResponse: makeAlbumsResponse({
        items: Array.from({ length: 20 }, (_, index) => makeAlbum(index)),
      }),
    });
    expect(actor.getSnapshot().context.items).toHaveLength(20);
    expect(actor.getSnapshot().context).not.toHaveProperty("hasMore");
    expect(actor.getSnapshot().context).not.toHaveProperty("nextCursor");
    actor.stop();
  });

  it("moves to failed when the album actor rejects", async () => {
    const actor = createActor(
      createTestPrivateRoomMachine({
        loadError: new Error("albums unavailable"),
      }),
    ).start();
    actor.send({ type: "PrivateRoomInit" });
    await waitFor(actor, (snapshot) => snapshot.matches("failed"));
    expect(actor.getSnapshot().context.errorMessage).toBe(
      "albums unavailable",
    );
    actor.stop();
  });
});
