Files
piik/tests/deferred-module-failure.test.ts
T
AAA 9c3650a25b feat(screen): show a picture's Watch and Retry failures with that picture
A failed Watch or Retry picture landed in the page banner, away from the
picture it was about. The failure now stays with the picture: under its
offer while it is not watched, at the lower edge of its tile's wait area,
or in its video status area. It clears on the next attempt at that
picture, when its video composites a frame, or on dismissal, since an
operation result carries a dismiss button.

The result is owned by the page, one entry per publication, because the
offer, the tile, the picture help dialog and the picture panel can all
close or unmount before the operation ends: a failed watch keeps the
source as an offer, and a retry that fails after withdrawing turns its
tile back into one. useKeyedSettingsAction keeps one result per key with
the lifetime rules of useSettingsAction, whose failure-code mapping it now
shares through settingsActionFailure. Watch and retryPicture accept the
session's existing function form of reportError, so a failure whose owner
has gone (another site or session) still reaches the banner instead of
being dropped. All other failures still go to the banner.

Verified with npx tsc -b --pretty false and npx vitest run
tests/channel-session.test.ts tests/channel-presentation.test.ts
tests/watch-presentation.test.ts tests/channel-stage-picture.test.ts
tests/retry-picture-controls.test.ts tests/deferred-module-failure.test.ts
tests/viewer-playback.test.ts, with new tests that a control-owned watch or
retry leaves the banner empty unless its owner is gone, that the failure
renders in the offer, wait area and video status area, and for the shared
failure-code mapping.
2026-11-25 16:25:04 +08:00

36 lines
2.0 KiB
TypeScript

import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { expect, test, vi } from "vitest";
import { ChannelSettingsError, settingsActionFailure } from "../src/client/channel/components/ChannelSettingsDialog";
import { installDeferredModuleFailureTracking, isDeferredModuleFailure } from "../src/client/lib/deferred-module-failure";
import { currentLang, t } from "../src/client/ui/copy";
test("deferred module failures show a user-triggered refresh in the shared result", () => {
const target = new EventTarget();
vi.stubGlobal("window", target);
try {
installDeferredModuleFailureTracking();
} finally {
vi.unstubAllGlobals();
}
const error = new TypeError("The operation failed");
const event = Object.assign(new Event("vite:preloadError", { cancelable: true }), { payload: error });
expect(target.dispatchEvent(event)).toBe(true);
expect(isDeferredModuleFailure(error)).toBe(true);
expect(isDeferredModuleFailure(new TypeError(error.message))).toBe(false);
const html = renderToStaticMarkup(createElement(ChannelSettingsError, { error: "deferred_module_load_failed" }));
expect(html).toContain(t(currentLang(), "channel.error.deferredModuleLoad"));
expect(html).toContain(`aria-label="${t(currentLang(), "common.refresh")}"`);
const ordinaryResult = renderToStaticMarkup(createElement(ChannelSettingsError, { error: "operation_failed" }));
expect(ordinaryResult).toContain(t(currentLang(), "channel.error.actionFailed"));
expect(ordinaryResult).not.toContain("<button");
});
test("an action owner keeps the failure code the banner would show and ignores cancellation", () => {
expect(settingsActionFailure(new DOMException("Cancelled", "AbortError"))).toBeNull();
expect(settingsActionFailure(new DOMException("Declined", "NotAllowedError"))).toBe("NotAllowedError");
expect(settingsActionFailure(new Error("room_host_offline"))).toBe("room_host_offline");
expect(settingsActionFailure("unknown")).toBe("operation_failed");
});