fix(admin): show each channel's cause in the notification test toast

The admin test response carried a code and status per channel that nothing rendered. The toast now lists each failing channel with the translated cause and the HTTP status or SMTP code. The relay reply is no longer part of that response, and the ntfy error no longer echoes the topic, which the sender treats as a secret.

Refs #947
This commit is contained in:
Marc-André Bombeck committed 2026-09-13 14:50:33 +02:00
1 parent 2480c9fa55
commit 32eaf7a29e
5 files changed
+157 -25

No files matched your search

@@ -0,0 +1,76 @@
/**
* The admin notification test toast names each failing channel's cause
* (review L5): the code the route sends is translated and the status is
* shown, so the per-channel fields in the response have a reader.
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { describeChannelTestFailures } from "../notification-test-failures";
const MESSAGES: Record<string, string> = {
"settings.testConnection.errors.upstream_rejected":
"The other end refused the message",
"settings.testConnection.errors.credentials_rejected": "Credentials rejected",
"admin.notificationTestFailed": "Failed to send test notification",
};
const t = (key: string) => MESSAGES[key] ?? key;
describe("describeChannelTestFailures", () => {
it("lists only failing channels, with the translated cause and the status", () => {
expect(
describeChannelTestFailures(
[
{ channel: "APNS", success: true },
{
channel: "WEBHOOK",
success: false,
error: "The webhook answered HTTP 400.",
errorCode: "upstream_rejected",
upstreamStatus: 400,
},
{
channel: "EMAIL",
success: false,
error: "The mail server rejected the credentials (SMTP 535).",
errorCode: "credentials_rejected",
smtpCode: 535,
},
],
t,
),
).toEqual([
"Webhook: The other end refused the message (HTTP 400)",
"Email: Credentials rejected (SMTP 535)",
]);
});
it("falls back to the route's sentence when there is no known code", () => {
expect(
describeChannelTestFailures(
[
{
channel: "NTFY",
success: false,
error: "SYSTEM_ALERT disabled in settings",
},
{ channel: "MYSTERY", success: false, errorCode: "unlisted" },
],
t,
),
).toEqual([
"ntfy: SYSTEM_ALERT disabled in settings",
"MYSTERY: Failed to send test notification",
]);
});
it("is what the admin toast renders", () => {
const src = readFileSync(
join(process.cwd(), "src/components/admin/reminders-section.tsx"),
"utf8",
);
expect(src).toMatch(/describeChannelTestFailures\(data\?\.results/);
expect(src).toMatch(/description:/);
});
});
@@ -0,0 +1,46 @@
import {
CHANNEL_TYPE_LABELS,
type ChannelType,
} from "@/lib/notifications/types";
/** One channel's row in the admin notification test response. */
export interface ChannelTestResult {
channel: string;
success: boolean;
error?: string;
errorCode?: string;
upstreamStatus?: number;
smtpCode?: number;
}
/**
* One line per failing channel for the admin test toast: the channel name,
* the cause in the admin's language (the same `settings.testConnection.errors`
* vocabulary the settings cards use), and the HTTP status or SMTP reply code.
* A row without a known code falls back to the route's own sentence.
*/
export function describeChannelTestFailures(
results: ReadonlyArray<ChannelTestResult>,
t: (key: string) => string,
): string[] {
return results
.filter((r) => !r.success)
.map((r) => {
const label = CHANNEL_TYPE_LABELS[r.channel as ChannelType] ?? r.channel;
const key = r.errorCode
? `settings.testConnection.errors.${r.errorCode}`
: null;
const translated = key ? t(key) : null;
const cause =
translated && translated !== key
? translated
: (r.error ?? t("admin.notificationTestFailed"));
const code =
typeof r.upstreamStatus === "number"
? ` (HTTP ${r.upstreamStatus})`
: typeof r.smtpCode === "number"
? ` (SMTP ${r.smtpCode})`
: "";
return `${label}: ${cause}${code}`;
});
}
+16 -8
View File
@@ -22,6 +22,10 @@ import { useTranslations } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import { useAdminSettings, useUpdateSettings } from "./_shared";
import { apiGet, apiPost } from "@/lib/api/api-fetch";
import {
describeChannelTestFailures,
type ChannelTestResult,
} from "./notification-test-failures";
interface NotificationHealth {
windowHours: number;
@@ -132,19 +136,23 @@ export function RemindersSection() {
return apiPost<
| {
message?: string;
results?: Array<{
channel: string;
success: boolean;
error?: string;
}>;
results?: ChannelTestResult[];
}
| undefined
>("/api/admin/notifications/test");
},
onSuccess: (data) => {
const hasFailures = data?.results?.some((r) => !r.success);
if (hasFailures) {
toast.error(data?.message ?? t("admin.notificationTestFailed"));
const failures = describeChannelTestFailures(data?.results ?? [], t);
if (failures.length > 0) {
toast.error(data?.message ?? t("admin.notificationTestFailed"), {
description: (
<ul className="space-y-0.5">
{failures.map((line) => (
<li key={line}>{line}</li>
))}
</ul>
),
});
} else {
toast.success(data?.message ?? t("admin.notificationTestSuccess"));
}