Files
poli0981andClaude Opus 4.8 12b17dbed0 feat(errors): add internationalized error pages + offline detection
Add a reusable, router-agnostic ErrorPage (404/403/419/500/offline/runtime)
driven by a pure config/error-pages.ts kind->meta map. Wire standalone
error routes (/403, /419, /500, /offline) plus a catch-all '*' -> 404 as
siblings of the AppShell group, so they render full-screen with no shell —
closing the long-standing gap where unknown hash routes rendered blank.

Add a navigator.onLine hook + non-blocking, dismissible OfflineBanner (the
app works fully offline, so it never blocks input). Redesign the
ErrorBoundary fallback to reuse ErrorPage's 'contained' runtime variant, so
render crashes share the same localized visual language.

120 new locale strings (20 keys x 6 languages) + _schema.json; validator
now expects 927 ui keys per locale. +6 pure config tests (494 -> 500).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 19:04:02 +07:00

71 lines
2.4 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
ERROR_PAGES,
resolveErrorMeta,
severityTextClass,
type ErrorKind,
} from "@config/error-pages";
import enUI from "@i18n/locales/en/ui.json";
/**
* Guards for the pure error-page config that drives ErrorPage + the routes in
* App.tsx. TypeScript already enforces the kind→meta shape via `satisfies`;
* these catch what types can't: a code typo, a duplicate keyPrefix, or a
* keyPrefix with no matching i18n copy (which would render a raw key string).
* Complements `validate:locales`, which only proves key *parity* across
* locales, not that the config points at keys that exist.
*/
const KINDS: ErrorKind[] = [
"notFound",
"forbidden",
"expired",
"serverError",
"offline",
"runtime",
];
describe("ERROR_PAGES config", () => {
it("maps exactly the six known kinds", () => {
expect(Object.keys(ERROR_PAGES).sort()).toEqual([...KINDS].sort());
});
it("assigns the expected HTTP codes (null for non-HTTP states)", () => {
expect(ERROR_PAGES.notFound.code).toBe(404);
expect(ERROR_PAGES.forbidden.code).toBe(403);
expect(ERROR_PAGES.expired.code).toBe(419);
expect(ERROR_PAGES.serverError.code).toBe(500);
expect(ERROR_PAGES.offline.code).toBeNull();
expect(ERROR_PAGES.runtime.code).toBeNull();
});
it("has a unique keyPrefix per kind", () => {
const prefixes = KINDS.map((k) => ERROR_PAGES[k].keyPrefix);
expect(new Set(prefixes).size).toBe(prefixes.length);
});
it("resolveErrorMeta returns the matching entry", () => {
for (const k of KINDS) {
expect(resolveErrorMeta(k)).toBe(ERROR_PAGES[k]);
}
});
it("severityTextClass maps every severity to a text-colour token", () => {
expect(severityTextClass("danger")).toBe("text-danger");
expect(severityTextClass("warning")).toBe("text-warning");
expect(severityTextClass("accent")).toBe("text-accent");
});
it("every kind has a localized title + description in en/ui.json", () => {
const errorPages = (
enUI as unknown as {
errorPages: Record<string, { title?: string; description?: string }>;
}
).errorPages;
for (const k of KINDS) {
const prefix = ERROR_PAGES[k].keyPrefix;
expect(errorPages[prefix]?.title, `${prefix}.title`).toBeTruthy();
expect(errorPages[prefix]?.description, `${prefix}.description`).toBeTruthy();
}
});
});