import { describe, expect, it } from "vitest";

import {
  buildPrivateAlbumGalleryUrl,
  buildPrivateRoomWithoutGalleryUrl,
  getPrivateAlbumGalleryState,
} from "../private-album-gallery-url";

describe("private album gallery URL", () => {
  it("builds and parses an album image URL", () => {
    const url = buildPrivateAlbumGalleryUrl("album:91", 3);
    const params = new URL(url, "https://cozsweet.test").searchParams;

    expect(url).toBe("/private-room?album=album%3A91&image=3");
    expect(getPrivateAlbumGalleryState(params)).toEqual({
      albumId: "album:91",
      imageIndex: 3,
    });
  });

  it("rejects missing albums and invalid image indexes", () => {
    expect(getPrivateAlbumGalleryState(new URLSearchParams("image=0"))).toBeNull();
    expect(
      getPrivateAlbumGalleryState(
        new URLSearchParams("album=album-1&image=-1"),
      ),
    ).toBeNull();
  });

  it("removes gallery params without dropping other query values", () => {
    expect(
      buildPrivateRoomWithoutGalleryUrl(
        new URLSearchParams("album=album-1&image=2&source=external"),
      ),
    ).toBe("/private-room?source=external");
  });
});
