test(plex): cover create write-through + the 409->nameTaken mapping (F-F)
Backend: create-collection happy path (write-through + editable) and the create resync-failure 422 (honest reconcile detail), mirroring the rename/remove tests. Frontend: extract notifyCollectionEditError (the 409->nameTaken vs generic-fallback mapping that was inline in the New-collection create()) and unit-test it; wire create() through it.
This commit is contained in:
@@ -4,16 +4,9 @@ import type { TFunction } from "i18next";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { Check, ListMusic, Lock, Plus, Search, X } from "lucide-react";
|
||||
import { qk } from "../lib/queryKeys";
|
||||
import {
|
||||
api,
|
||||
HttpError,
|
||||
isIntrinsicReadOnly,
|
||||
type PlexCard,
|
||||
type PlexCollection,
|
||||
} from "../lib/api";
|
||||
import { api, isIntrinsicReadOnly, type PlexCard, type PlexCollection } from "../lib/api";
|
||||
import { formatRuntime } from "../lib/format";
|
||||
import { notifyActionError } from "../lib/actionErrors";
|
||||
import { notify } from "../lib/notifications";
|
||||
import { notifyCollectionEditError } from "../lib/actionErrors";
|
||||
import { useDebounced } from "../lib/useDebounced";
|
||||
import { LoadingState, StateMessage } from "./QueryState";
|
||||
import PosterTile from "./PosterTile";
|
||||
@@ -102,13 +95,12 @@ function NewCollectionPanel({
|
||||
const col = await api.plexCreateCollection("", trimmed, card.id); // backend derives the library
|
||||
onCreated(col.id);
|
||||
} catch (e) {
|
||||
// The backend is authoritative on per-library name uniqueness — localize its 409 to nameTaken
|
||||
// (notifyActionError would otherwise surface the server's raw English detail).
|
||||
if (e instanceof HttpError && e.status === 409) {
|
||||
notify({ level: "error", message: t("plex.collection.nameTaken") });
|
||||
} else {
|
||||
notifyActionError(e, t("plex.collection.editFailed"));
|
||||
}
|
||||
// The backend is authoritative on per-library name uniqueness — its 409 is localized to nameTaken.
|
||||
notifyCollectionEditError(
|
||||
e,
|
||||
t("plex.collection.editFailed"),
|
||||
t("plex.collection.nameTaken"),
|
||||
);
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// jsdom (not node): importing ../api pulls in i18n, which reads navigator.language at module load.
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
// Spy on the toast sink so we can assert exactly what each error maps to.
|
||||
vi.mock("./notifications", () => ({ notify: vi.fn(() => 0) }));
|
||||
|
||||
import { HttpError } from "./api";
|
||||
import { notify } from "./notifications";
|
||||
import { notifyCollectionEditError } from "./actionErrors";
|
||||
|
||||
const notifyMock = vi.mocked(notify);
|
||||
beforeEach(() => notifyMock.mockClear());
|
||||
|
||||
describe("notifyCollectionEditError", () => {
|
||||
it("maps a backend 409 to the friendly nameTaken message (not the raw server detail)", () => {
|
||||
notifyCollectionEditError(new HttpError(409, "already exists"), "EDIT_FAILED", "NAME_TAKEN");
|
||||
expect(notifyMock).toHaveBeenCalledWith({ level: "error", message: "NAME_TAKEN" });
|
||||
});
|
||||
|
||||
it("falls back to the server detail for a non-409 HttpError", () => {
|
||||
notifyCollectionEditError(new HttpError(500, "server boom"), "EDIT_FAILED", "NAME_TAKEN");
|
||||
expect(notifyMock).toHaveBeenCalledWith({ level: "error", message: "server boom" });
|
||||
});
|
||||
|
||||
it("falls back to the generic edit-failed message for a non-HttpError", () => {
|
||||
notifyCollectionEditError(new Error("network"), "EDIT_FAILED", "NAME_TAKEN");
|
||||
expect(notifyMock).toHaveBeenCalledWith({ level: "error", message: "EDIT_FAILED" });
|
||||
});
|
||||
});
|
||||
@@ -12,3 +12,22 @@ export function notifyActionError(err: unknown, fallbackMessage: string): void {
|
||||
const detail = err instanceof HttpError ? err.detail : null;
|
||||
notify({ level: "error", message: detail || fallbackMessage });
|
||||
}
|
||||
|
||||
/**
|
||||
* Collection write errors. The backend enforces per-library name uniqueness and returns 409 on a
|
||||
* duplicate; localize that specific case to the friendly `nameTaken` copy (notifyActionError would
|
||||
* otherwise surface the server's raw English detail). Everything else falls back to the generic
|
||||
* edit-failed handling (which still prefers the server's reason when present). Both args are already
|
||||
* translated so this stays i18n-agnostic and unit-testable.
|
||||
*/
|
||||
export function notifyCollectionEditError(
|
||||
err: unknown,
|
||||
editFailedMessage: string,
|
||||
nameTakenMessage: string,
|
||||
): void {
|
||||
if (err instanceof HttpError && err.status === 409) {
|
||||
notify({ level: "error", message: nameTakenMessage });
|
||||
return;
|
||||
}
|
||||
notifyActionError(err, editFailedMessage);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user