Files
immich-redaktions-plugin/src/app/admin/redaktionen/[id]/actions.test.ts
T
Leon TonhäuserandClaude 089f5f7035 fix: album assignment + approval error handling
Album assignment 500'd with IMMICH_UNAVAILABLE because grantAlbum verified the
album via getAlbum, which pulled the full album incl. all assets — large albums
blew the 15s timeout. Request ?withoutAssets=true so Immich returns metadata
only (no consumer reads .assets); make assets optional in the schema.

Album thumbnails 500'd because next/image rejected the ?albumId= query string
(images.localPatterns). Switch the album page to a plain <img>, matching the
editorial catalog (next/image was used in exactly one place).

Overhaul action error handling: grantAlbumAction/revokeAlbumAction/approveEditorialAction
returned a thrown Error, which Next.js turns into an opaque error-digest page —
the German message never reached the admin. They now return {ok,message}; new
client AlbumGrantForm + ApproveForm use useActionState to show the message
inline. Auth (requireAdmin) still throws (guarded by the layout). Update both
action test files to the result contract.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-11 02:09:16 +02:00

100 lines
3.3 KiB
TypeScript

import { beforeEach, expect, test, vi } from "vitest";
beforeEach(() => {
vi.clearAllMocks();
});
const mocks = vi.hoisted(() => ({
approveEditorial: vi.fn(),
headers: vi.fn(),
requireAdmin: vi.fn(),
revalidatePath: vi.fn(),
}));
vi.mock("next/headers", () => ({ headers: mocks.headers }));
vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath }));
vi.mock("@/modules/auth/principal", () => ({ requireAdmin: mocks.requireAdmin }));
vi.mock("@/modules/editorials/service", () => ({
editorialService: { approveEditorial: mocks.approveEditorial },
EditorialServiceError: class EditorialServiceError extends Error {
constructor(readonly code: string) {
super(code);
}
},
}));
import { approveEditorialAction } from "./actions";
const initial = { ok: true, message: "" };
test("the approve action rejects unauthenticated callers before touching the service", async () => {
mocks.headers.mockResolvedValue(new Headers());
mocks.requireAdmin.mockRejectedValue(new Error("ADMIN_REQUIRED"));
const formData = new FormData();
formData.set("priceEuros", "10.00");
await expect(
approveEditorialAction("00000000-0000-4000-8000-000000000001", initial, formData),
).rejects.toThrow("ADMIN_REQUIRED");
expect(mocks.approveEditorial).not.toHaveBeenCalled();
});
test("the approve action forwards the authenticated principal and cents price to the service", async () => {
mocks.headers.mockResolvedValue(new Headers());
mocks.requireAdmin.mockResolvedValue({ userId: "admin-1" });
mocks.approveEditorial.mockResolvedValue(undefined);
const formData = new FormData();
formData.set("priceEuros", "12,50");
const result = await approveEditorialAction(
"00000000-0000-4000-8000-000000000001",
initial,
formData,
);
expect(result).toEqual({ ok: true, message: "" });
expect(mocks.approveEditorial).toHaveBeenCalledWith({
editorialId: "00000000-0000-4000-8000-000000000001",
priceCents: 1250,
adminUserId: "admin-1",
});
expect(mocks.revalidatePath).toHaveBeenCalledWith(
"/admin/redaktionen/00000000-0000-4000-8000-000000000001",
);
});
test("the approve action returns a German message for a non-positive price before calling the service", async () => {
mocks.headers.mockResolvedValue(new Headers());
mocks.requireAdmin.mockResolvedValue({ userId: "admin-1" });
const formData = new FormData();
formData.set("priceEuros", "0");
const result = await approveEditorialAction(
"00000000-0000-4000-8000-000000000001",
initial,
formData,
);
expect(result).toEqual({ ok: false, message: "Bitte geben Sie einen gültigen Nettopreis an." });
expect(mocks.approveEditorial).not.toHaveBeenCalled();
});
test("the approve action translates a service error into a German message", async () => {
mocks.headers.mockResolvedValue(new Headers());
mocks.requireAdmin.mockResolvedValue({ userId: "admin-1" });
const { EditorialServiceError } = await import("@/modules/editorials/service");
mocks.approveEditorial.mockRejectedValue(new EditorialServiceError("ALBUM_GRANT_REQUIRED"));
const formData = new FormData();
formData.set("priceEuros", "10.00");
const result = await approveEditorialAction(
"00000000-0000-4000-8000-000000000001",
initial,
formData,
);
expect(result).toEqual({
ok: false,
message: "Bitte weisen Sie zuerst mindestens ein Album zu.",
});
});