Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/automation/automationUi.test.tsx
T

91 lines
3.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { TFunction } from "i18next";
import { anomalyMessage, automationStatusLabel, errorMessage, localizedValue, nodeTitle } from "./automationUi";
/**
* Minimal i18next stand-in: resolves the handful of keys these helpers touch and
* mirrors i18next's `defaultValue` behaviour for everything else.
*/
const DICTIONARY: Record<string, string> = {
"automation.execution.unsupportedNode": "Dieser Baustein wird vom Ausführer nicht unterstützt.",
"automation.nodes.portal_invite_user.title": "Portalnutzer einladen",
"automation.guards.triggerVolumePaused": "{{triggerCount}} Auslöser in {{minutes}} Minuten für „{{trigger}}“.",
"automation.nodes.crm_kontakt_created.title": "Neuer Kontakt",
"automation.values.unknownError": "Ein unerwarteter Fehler ist aufgetreten.",
"automation.values.unknownNode": "Unbekannter Baustein",
"automation.values.unknownStatus": "Unbekannter Status",
"automation.values.unknownSummary": "Keine Beschreibung verfügbar.",
"automation.values.unknownWarning": "Ein Hinweis konnte nicht angezeigt werden.",
};
const t = ((key: string, options?: Record<string, unknown>) => {
const entry = DICTIONARY[key];
if (entry === undefined) return (options?.defaultValue as string) ?? key;
return entry.replace(/{{(\w+)}}/g, (_match, name: string) => String(options?.[name] ?? ""));
}) as unknown as TFunction;
describe("errorMessage", () => {
it("translates a catalog key and names the offending block in plain language", () => {
expect(errorMessage(t, "automation.execution.unsupportedNode: portal.invite_user")).toBe(
"Dieser Baustein wird vom Ausführer nicht unterstützt. (Portalnutzer einladen)"
);
});
it("never renders a bare catalog key without its translation", () => {
expect(errorMessage(t, "automation.execution.unsupportedNode")).toBe("Dieser Baustein wird vom Ausführer nicht unterstützt.");
});
it("does not expose a raw backend error sentence", () => {
expect(errorMessage(t, "fetch failed")).toBe("Ein unerwarteter Fehler ist aufgetreten.");
});
it("does not expose an unknown catalog key", () => {
expect(errorMessage(t, "automation.execution.missingTranslation")).toBe("Ein unerwarteter Fehler ist aufgetreten.");
});
it("has nothing to show for an empty error", () => {
expect(errorMessage(t, null)).toBeNull();
expect(errorMessage(t, " ")).toBeNull();
});
});
describe("anomalyMessage", () => {
it("interpolates the meter numbers into the guard sentence", () => {
expect(
anomalyMessage(t, {
anomalyText: "automation.guards.triggerVolumePaused",
anomalyCount: 2000,
anomalyWindowMinutes: 5,
triggerKey: "crm.kontakt_created",
})
).toBe("2000 Auslöser in 5 Minuten für „Neuer Kontakt“.");
});
it("stays silent when no guard paused the rule", () => {
expect(anomalyMessage(t, { anomalyText: null, anomalyCount: null, anomalyWindowMinutes: null, triggerKey: null })).toBeNull();
});
it("does not expose a missing guard key", () => {
expect(
anomalyMessage(t, {
anomalyText: "automation.guards.missingTranslation",
anomalyCount: 1,
anomalyWindowMinutes: 1,
triggerKey: "crm.kontakt_created",
})
).toBe("Ein Hinweis konnte nicht angezeigt werden.");
});
});
describe("safe automation labels", () => {
it("uses localized fallbacks for unknown status, node and summary keys", () => {
expect(automationStatusLabel(t, "future_status")).toBe("Unbekannter Status");
expect(nodeTitle(t, "extension.missing_node")).toBe("Unbekannter Baustein");
expect(localizedValue(t, "automation.execution.missingSummary", "automation.values.unknownSummary")).toBe("Keine Beschreibung verfügbar.");
});
it("keeps user-authored text that is not an i18n key", () => {
expect(localizedValue(t, "Meine eigene Vorlage", "automation.values.unknownSummary")).toBe("Meine eigene Vorlage");
});
});