Files
OmniRoute/tests/unit/notification-toast-coercion.test.ts
Diego Rodrigues de Sa e Souza e44f125992 fix(dashboard): stop model-test error freezing the page (React #31 object toast) (#6161)
Clicking 'test' on a provider model (e.g. a ClinePass flash model) could freeze
the entire dashboard. Root cause: POST /api/models/test returned an OBJECT in
`error` on the Zod-validation and invalid-JSON paths (`validation.error.format()`
/ a details object). The client does `notify.error(data.error)`, and
NotificationToast renders the message directly as a React child — an object throws
React #31 ('Objects are not valid as a React child'), crashing the tree = frozen
page instead of a toast.

Fixed in three layers (defense in depth):
1. Server (root cause): /api/models/test now returns a STRING `error` on every
   path — flattens Zod issues to text, returns 'Invalid JSON body' for bad JSON.
2. Client: onTestModel funnels the response through extractApiErrorMessage() so any
   object-shaped error is coerced to a string before notify.error.
3. Toast: NotificationToast coerces title/message via toToastText() — a resilient
   catch-all so no future caller can freeze the page with a non-string.

Tests (Rule #18, both node:test / blocking suite):
- tests/unit/models-test-error-shape.test.ts — asserts STRING error on Zod-fail,
  missing-field, and invalid-JSON (fails on the pre-fix route: 3/3 red -> green).
- tests/unit/notification-toast-coercion.test.ts — toToastText coercion matrix.
2026-07-05 02:29:20 -03:00

36 lines
1.3 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { toToastText } from "@/shared/components/NotificationToast";
// Regression guard: a toast message/title that is NOT a string (e.g. a raw API
// error body — a Zod `.format()` object) must be coerced to a string. Rendering
// an object as a React child throws React #31 and freezes the whole page. This
// was the "test model → screen froze" bug on the provider page.
test("returns strings unchanged", () => {
assert.equal(toToastText("hello"), "hello");
assert.equal(toToastText(""), "");
});
test("returns empty string for null/undefined (never crashes render)", () => {
assert.equal(toToastText(null), "");
assert.equal(toToastText(undefined), "");
});
test("prefers a nested string .message on an object error body", () => {
assert.equal(toToastText({ message: "Rate limited" }), "Rate limited");
});
test("JSON-stringifies an arbitrary object instead of throwing (Zod .format() shape)", () => {
const zodish = { modelId: { _errors: ["Required"] }, _errors: [] };
const out = toToastText(zodish);
assert.equal(typeof out, "string");
assert.ok(out.includes("_errors"));
});
test("coerces numbers/booleans to string", () => {
assert.equal(toToastText(42), "42");
assert.equal(toToastText(true), "true");
});