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 }; }), }; const consumeRateLimit = vi.fn().mockResolvedValue({ allowed: true, retryAfterSeconds: 0 }); return { catalog: createAlbumCatalog({ grants, immich, consumeRateLimit }), grants, immich, consumeRateLimit, }; } 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("restores the queried granted album when Immich omits an asset album association", async () => { const { catalog, immich } = createCatalog({ grantedAlbumIds: ["allowed"] }); immich.searchPhotos.mockResolvedValue({ items: [{ id: "asset-1", type: "IMAGE", originalFileName: "tor.jpg", originalMimeType: "image/jpeg", localDateTime: null, fileCreatedAt: null, albums: [], }], total: 1, nextPage: null, previousPage: null, }); const result = await catalog.searchVisiblePhotos("editorial-1", { page: 1 }); expect(result.items[0]?.albumId).toBe("allowed"); }); test("projects only display fields and the queried granted album for the client gallery", async () => { const { catalog, immich } = createCatalog({ grantedAlbumIds: ["allowed"] }); immich.searchPhotos.mockResolvedValue({ items: [{ id: "asset-1", type: "IMAGE", originalFileName: "tor.jpg", originalMimeType: "image/jpeg", localDateTime: null, fileCreatedAt: null, originalPath: "/private/photos/tor.jpg", exifInfo: { exifImageWidth: 1200, exifImageHeight: 800, latitude: 12 }, albums: [ { id: "other", albumName: "Anderes Album" }, { id: "allowed", albumName: "Erlaubtes Album" }, ], }], total: 1, nextPage: null, previousPage: null, }); const result = await catalog.searchVisiblePhotos("editorial-1", { page: 1 }); expect(result.items).toEqual([{ id: "asset-1", filename: "tor.jpg", takenAt: null, width: 1200, height: 800, albumId: "allowed", albumName: "Heimspiel gegen Nord", }]); }); test("global search keeps hits when a granted album is no longer visible in Immich", async () => { const { catalog, immich } = createCatalog({ grantedAlbumIds: ["allowed", "missing"] }); immich.searchPhotos.mockImplementation(async ({ albumIds: [id] }) => { if (id === "missing") throw new Error("Album no longer accessible"); return { items: [{ id: `photo-${id}`, albums: [] }], total: 1, nextPage: null, previousPage: null, }; }); const result = await catalog.searchVisiblePhotos("editorial-1", { description: "hsv" }); expect(result.items.map(({ id }) => id)).toEqual(["photo-allowed"]); }); 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("passes a bounded decimal page string from the SSR query through to Immich", async () => { const { catalog, immich } = createCatalog(); await catalog.searchVisiblePhotos("editorial-1", { page: "2" }); expect(immich.searchPhotos).toHaveBeenLastCalledWith(expect.objectContaining({ page: 2 })); await catalog.searchVisiblePhotos("editorial-1", { page: "not-a-page" }); expect(immich.searchPhotos).toHaveBeenLastCalledWith(expect.objectContaining({ page: 1 })); await catalog.searchVisiblePhotos("editorial-1", { page: "-2" }); expect(immich.searchPhotos).toHaveBeenLastCalledWith(expect.objectContaining({ page: 1 })); await catalog.searchVisiblePhotos("editorial-1", { page: "100000" }); 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" })); }); test("rate-limits description searches per authenticated editorial", async () => { const { catalog, consumeRateLimit, immich } = createCatalog(); consumeRateLimit.mockResolvedValue({ allowed: false, retryAfterSeconds: 19 }); await expect( catalog.searchVisiblePhotos("editorial-1", { description: "Tor", page: 1 }), ).rejects.toMatchObject({ code: "RATE_LIMITED", retryAfterSeconds: 19 }); expect(consumeRateLimit).toHaveBeenCalledWith("search:editorial-1", expect.any(Object)); expect(immich.searchPhotos).not.toHaveBeenCalled(); });