// @vitest-environment jsdom
import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { ContextErrorBoundary } from "../../../src/platform/errors/context-error-boundary.tsx";
import { FeatureErrorBoundary } from "../../../src/platform/errors/feature-error-boundary.tsx";
function ThrowsOnRender(): never {
throw new Error("feature render failed");
}
describe("ownership-aligned failure boundaries", () => {
it("keeps sibling feature UI mounted when one feature throws during render", () => {
vi.spyOn(console, "error").mockImplementation(() => {});
render(
healthy-feature
,
);
expect(screen.getByText("healthy-feature")).toBeInTheDocument();
expect(screen.getByTestId("feature-fallback-broken")).toBeInTheDocument();
});
it("keeps sibling contexts mounted when one context fails outside any feature boundary", () => {
vi.spyOn(console, "error").mockImplementation(() => {});
render(
account-context
,
);
expect(screen.getByTestId("context-fallback-commerce")).toBeInTheDocument();
expect(screen.getByText("account-context")).toBeInTheDocument();
expect(screen.getByText("app-shell")).toBeInTheDocument();
});
it("reports once per contained failure and survives a throwing reporter", () => {
vi.spyOn(console, "error").mockImplementation(() => {});
const onError = vi.fn(() => {
throw new Error("diagnostics down");
});
render(
fallback
}>
,
);
expect(onError).toHaveBeenCalledWith({
scope: "feature",
ownerId: "broken",
incidentId: expect.stringMatching(/^[0-9a-f]{8}$/),
});
expect(screen.getByText("fallback")).toBeInTheDocument();
});
// 09-09. The support reference on screen has to find the reported event.
it("gives a function fallback the incident id it reported", () => {
vi.spyOn(console, "error").mockImplementation(() => {});
const onError = vi.fn();
render(
incident {incidentId}
}
>
,
);
const [[report]] = onError.mock.calls as [[{ incidentId: string }]];
expect(screen.getByText(`incident ${report.incidentId}`)).toBeInTheDocument();
});
it("clears a contained failure when its reset key changes", () => {
vi.spyOn(console, "error").mockImplementation(() => {});
let shouldThrow = true;
function Flaky() {
if (shouldThrow) throw new Error("flaky");
return