import { expect, test, vi } from "vitest"; import { AlbumCatalogError, createAlbumCatalog, } from "./catalog"; import type { ImmichClient } from "@/modules/immich/types"; const allowedAlbum = { id: "allowed", albumName: "Heimspiel gegen Nord", description: null, albumThumbnailAssetId: "cover-1", assetCount: 12, }; function createCatalog(options?: { grantedAlbumIds?: string[]; searchError?: Error; }) { const grants = { getGrantedAlbumIds: vi.fn().mockResolvedValue(options?.grantedAlbumIds ?? ["allowed"]), }; const immich = { listAlbums: vi.fn().mockResolvedValue([allowedAlbum, { ...allowedAlbum, id: "other" }]), searchPhotos: vi.fn().mockImplementation(async () => { if (options?.searchError) throw options.searchError; return { items: [], total: 0, nextPage: null, previousPage: null }; }), }; return { catalog: createAlbumCatalog({ grants, immich }), grants, immich, }; } test("ignores a caller-supplied album outside the editorial grants", async () => { const { catalog, immich } = createCatalog({ grantedAlbumIds: ["allowed"] }); await catalog.searchVisiblePhotos("editorial-1", { requestedAlbumId: "forbidden", description: "tor", page: 1, }); expect(immich.searchPhotos).toHaveBeenCalledWith( expect.objectContaining({ albumIds: ["allowed"], description: "tor", type: "IMAGE" }), ); }); test("uses a requested album only when it is in the current editorial grants", async () => { const { catalog, immich } = createCatalog({ grantedAlbumIds: ["allowed", "also-allowed"] }); await catalog.searchVisiblePhotos("editorial-1", { requestedAlbumId: "also-allowed", page: 1 }); expect(immich.searchPhotos).toHaveBeenCalledWith( expect.objectContaining({ albumIds: ["also-allowed"], type: "IMAGE" }), ); }); test("does not call Immich when an editorial has no current album grants", async () => { const { catalog, immich } = createCatalog({ grantedAlbumIds: [] }); await expect(catalog.searchVisiblePhotos("editorial-1", { page: 1 })).resolves.toEqual({ items: [], total: 0, nextPage: null, previousPage: null, }); expect(immich.searchPhotos).not.toHaveBeenCalled(); }); test("reads grants for every search so a revoked grant is immediately excluded", async () => { const { catalog, grants, immich } = createCatalog({ grantedAlbumIds: ["allowed"] }); grants.getGrantedAlbumIds.mockResolvedValueOnce(["allowed"]).mockResolvedValueOnce([]); await catalog.searchVisiblePhotos("editorial-1", { page: 1 }); await catalog.searchVisiblePhotos("editorial-1", { page: 1 }); expect(immich.searchPhotos).toHaveBeenCalledTimes(1); }); test("normalizes descriptions and bounds invalid pagination before sending an upstream request", async () => { const { catalog, immich } = createCatalog(); await catalog.searchVisiblePhotos("editorial-1", { description: " Tor\n des\tSieges ", page: Number.POSITIVE_INFINITY, }); expect(immich.searchPhotos).toHaveBeenCalledWith({ albumIds: ["allowed"], description: "Tor des Sieges", type: "IMAGE", page: 1, size: 60, }); await catalog.searchVisiblePhotos("editorial-1", { page: 100_000 }); expect(immich.searchPhotos).toHaveBeenLastCalledWith( expect.objectContaining({ page: 1_000 }), ); }); test("only returns the current editorial's albums from the Immich album list", async () => { const { catalog } = createCatalog({ grantedAlbumIds: ["allowed"] }); await expect(catalog.getVisibleAlbums("editorial-1")).resolves.toEqual([allowedAlbum]); }); test("maps an Immich search outage to the catalog's stable unavailable state", async () => { const { catalog } = createCatalog({ searchError: new Error("upstream down") }); await expect(catalog.searchVisiblePhotos("editorial-1", { page: 1 })).rejects.toEqual( new AlbumCatalogError("IMMICH_UNAVAILABLE"), ); }); test("does not expose a caller-controlled asset type in the catalog contract", async () => { const { catalog, immich } = createCatalog(); await catalog.searchVisiblePhotos("editorial-1", { page: 2, type: "VIDEO" } as never); expect(immich.searchPhotos).toHaveBeenCalledWith(expect.objectContaining({ type: "IMAGE" })); });