Files
immich-redaktions-plugin/src/modules/albums/catalog.test.ts
T

179 lines
6.2 KiB
TypeScript

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]?.albums).toEqual([{ id: "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();
});