// @vitest-environment jsdom // // ONE test, ONE file, for the same reason CommandPalette.ordering.dom.test.tsx has its own: it // ASSERTS on a reconcile issuing a REAL, unstubbed programmatic history.back(), and history.ts's // overlay bookkeeping is module-global. history.dom.test.ts keeps its shared state clean by never // leaving a real back() in flight — its layers close by a genuine Back, a stubbed back(), or a // consumed burial mark — and this test would break that. import { describe, it, expect, vi } from "vitest"; import { renderHook, act } from "@testing-library/react"; import { navigateOverOverlays, useBackToClose } from "./history"; const settle = () => act(async () => { await Promise.resolve(); }); describe("navigateOverOverlays · burying a navigation that never happened", () => { // The round-2 defect. `navigateOverOverlays` marked every live overlay entry buried BEFORE it knew // whether the navigation pushed anything, and `setPage` early-returns when the requested page is // already current. Home is the default landing page, so Shift+W pressed while already on Home with // the player open buried the player's entry under a push that never happened: its unmount then // consumed the mark instead of reclaiming the entry with history.back(), leaving an orphan. The // user's next browser Back popped onto an identical `sfPage` with an empty overlay stack — a press // that changed nothing on screen, one more per play → Shift+W → close cycle. it("leaves the overlay's entry reclaimable when the navigation reports it pushed nothing", async () => { const overlay = renderHook(() => useBackToClose(() => {})); await settle(); const back = vi.spyOn(window.history, "back"); try { // What `setPage` returns from its early return: asked for the page we are already on. const navigate = vi.fn(() => false); act(() => navigateOverOverlays(navigate)); expect(navigate).toHaveBeenCalledTimes(1); overlay.unmount(); await settle(); // Nothing was pushed over it, so the entry is still ours — reclaimed, not silently orphaned. expect(back).toHaveBeenCalledTimes(1); } finally { back.mockRestore(); } }); });