173 lines
6.8 KiB
TypeScript
173 lines
6.8 KiB
TypeScript
// @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", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("./notifications")>()),
|
|
notify: vi.fn(() => 0),
|
|
}));
|
|
vi.mock("./errorDialog", () => ({ reportError: vi.fn() }));
|
|
|
|
import { HttpError, api } from "./api";
|
|
import { alreadyReported } from "./api/core";
|
|
import { msg, notify, raw, sayNotif } from "./notifications";
|
|
import { localizeDetail } from "./api/refusalText";
|
|
import { reportError } from "./errorDialog";
|
|
import { failureReason, notifyActionError, notifyKnownFailure } from "./actionErrors";
|
|
import i18n from "../i18n";
|
|
|
|
const notifyMock = vi.mocked(notify);
|
|
beforeEach(() => {
|
|
notifyMock.mockClear();
|
|
vi.mocked(reportError).mockClear();
|
|
});
|
|
|
|
describe("a Plex collection name that is already taken", () => {
|
|
it("is reported once, in the reader's language", async () => {
|
|
// The 409 was a bare English string: the global error dialog showed it in English, and the
|
|
// editor's own toast said the same thing again, translated.
|
|
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
|
new Response(JSON.stringify({ detail: { code: "collection", reason: "name_taken" } }), {
|
|
status: 409,
|
|
headers: { "content-type": "application/json" },
|
|
}),
|
|
);
|
|
const err = await api.plexCreateCollection("1", "Marvel", "m1").catch((e: unknown) => e);
|
|
notifyMock.mockClear();
|
|
notifyActionError(err, raw("EDIT_FAILED"));
|
|
expect(notifyMock.mock.calls).toEqual([
|
|
[
|
|
{
|
|
level: "error",
|
|
title: raw("EDIT_FAILED"),
|
|
message: { detail: { code: "collection", reason: "name_taken" } },
|
|
},
|
|
],
|
|
]);
|
|
expect(sayNotif(notifyMock.mock.calls[0]![0].message)).toBe(
|
|
i18n.t("plex.collection.nameTaken"),
|
|
);
|
|
expect(reportError).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("notifyActionError", () => {
|
|
it("does not toast a fault the request layer already put on screen", async () => {
|
|
// A 500 opens the blocking error dialog inside `req`; the watched toggles, the saved views and a
|
|
// playlist delete then toasted the same failure as well.
|
|
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
|
new Response(JSON.stringify({ detail: "boom" }), {
|
|
status: 500,
|
|
headers: { "content-type": "application/json" },
|
|
}),
|
|
);
|
|
const err = await api.plexSetState("1", "watched").catch((e: unknown) => e);
|
|
notifyMock.mockClear();
|
|
notifyActionError(err, raw("FAILED"));
|
|
expect(notifyMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does toast one the caller was left to handle", () => {
|
|
notifyActionError(new HttpError(404, "gone"), raw("FAILED"));
|
|
expect(notifyMock).toHaveBeenCalled();
|
|
});
|
|
|
|
it("reports one failure once, however many callers try", () => {
|
|
// What keeps the app-wide mutation reporter quiet for a mutation whose own onError already spoke.
|
|
const err = new HttpError(404, "gone");
|
|
notifyActionError(err, raw("FIRST"));
|
|
notifyActionError(err, raw("SECOND"));
|
|
expect(notifyMock).toHaveBeenCalledTimes(1);
|
|
expect(alreadyReported(err)).toBe(true);
|
|
});
|
|
|
|
it("says nothing about an upload the user cancelled", () => {
|
|
notifyActionError(new HttpError(0, "aborted"), raw("FAILED"));
|
|
expect(notifyMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("says a bare-English server reason in the reader's language, not the server's", () => {
|
|
// `reportFailure` leaves 403/404/429 to the caller, so for exactly those statuses whatever
|
|
// English a route hand-wrote went straight into the toast — "No user with that email." in an
|
|
// otherwise Hungarian UI, while the caller's translated fallback sat unused.
|
|
notifyActionError(new HttpError(404, "No user with that email."), raw("TRANSLATED"));
|
|
expect(notifyMock).toHaveBeenCalledWith({
|
|
level: "error",
|
|
title: raw("TRANSLATED"),
|
|
message: msg("errors.notFound.generic"),
|
|
});
|
|
});
|
|
|
|
it("still prefers a STRUCTURED reason, kept as the refusal and said in the reader's language", () => {
|
|
// The notice keeps `{code, reason}` and says it through localizeDetail when shown: in the
|
|
// reader's language AND more specific than anything a status can say — quota numbers, which row
|
|
// is gone.
|
|
const err = new HttpError(404, "Ez a csatorna már nincs a könyvtárban.", {
|
|
code: "not_found",
|
|
reason: "channel",
|
|
});
|
|
notifyActionError(err, raw("TRANSLATED FALLBACK"));
|
|
expect(notifyMock).toHaveBeenCalledWith({
|
|
level: "error",
|
|
title: raw("TRANSLATED FALLBACK"),
|
|
message: { detail: { code: "not_found", reason: "channel" } },
|
|
});
|
|
// Said at display through localizeDetail, in the language of that moment.
|
|
expect(sayNotif(notifyMock.mock.calls[0]![0].message)).toBe(
|
|
localizeDetail({ code: "not_found", reason: "channel" }),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("failureReason — why a write failed, when the server did not say", () => {
|
|
it.each([
|
|
[403, "errors.reason.forbidden"],
|
|
[404, "errors.notFound.generic"],
|
|
[429, "errors.reason.rateLimited"],
|
|
[502, "errors.reason.unreachable"],
|
|
[503, "errors.reason.unreachable"],
|
|
[504, "errors.reason.unreachable"],
|
|
[0, "errors.reason.unreachable"], // uploadReq's network failure
|
|
[500, "errors.generic"],
|
|
])("a %s reads %s", (status, key) => {
|
|
const reason = failureReason(new HttpError(status, "hand-written English"));
|
|
expect(reason).toEqual(msg(key));
|
|
expect(sayNotif(reason)).toBe(i18n.t(key));
|
|
});
|
|
|
|
it("a request that never reached the server reads as unreachable", () => {
|
|
expect(sayNotif(failureReason(new TypeError("Failed to fetch")))).toBe(
|
|
i18n.t("errors.reason.unreachable"),
|
|
);
|
|
});
|
|
|
|
it("anything else is the app's own fault, not the server's", () => {
|
|
expect(sayNotif(failureReason(new Error("key import failed")))).toBe(
|
|
i18n.t("errors.reason.unexpected"),
|
|
);
|
|
});
|
|
|
|
it("every reason exists in both languages", () => {
|
|
for (const lng of ["en", "hu"])
|
|
for (const key of [
|
|
"errors.reason.forbidden",
|
|
"errors.reason.rateLimited",
|
|
"errors.reason.unreachable",
|
|
"errors.reason.unexpected",
|
|
])
|
|
expect(i18n.exists(key, { lng }), `${lng}:${key}`).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("notifyKnownFailure", () => {
|
|
it("shows the caller's specific sentence and marks the failure reported", () => {
|
|
const err = new HttpError(409, "x", { code: "resend", reason: "email_off" });
|
|
notifyKnownFailure(err, raw("EMAIL IS OFF"));
|
|
expect(notifyMock).toHaveBeenCalledWith({ level: "error", message: raw("EMAIL IS OFF") });
|
|
notifyActionError(err, raw("GENERIC"));
|
|
expect(notifyMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|