fix(documents): check that closing a document consumed its history entry

Closing a card-opened document asks the browser to go back, so the entry
the selection pushed is consumed and the reader's next Back reaches the
page before it. A traversal is a request, not a guarantee: one asked for
while another is still settling can be dropped, and nothing raises when
it is. The sheet closes, React stays consistent, and the address bar goes
on naming a document nobody has open, so a reload or a shared link
re-opens the sheet that was just closed.

Pressing Escape straight after the browser's Forward button is exactly
that shape, and it reproduces: ten repeats across both viewports failed
two of twenty with the URL still carrying the id, and none of twenty once
the close verifies itself. The check is guarded by the document id, so a
traversal that landed on a different selection is left alone, and it is
cancelled the moment the sheet re-opens.

The browser test that found it could not say so. It asserted the sheet
was closed through a locator matched on the document TITLE, and the sheet
falls back to an untitled label while its detail read is pending, so the
assertion also passed on an open sheet showing a skeleton, resolved
immediately instead of waiting, and pushed the failure into a URL poll
ten seconds later. It now asserts the dialog role, which is what the
neighbouring deep-link test already does.
This commit is contained in:
Marc-André Bombeck committed 2026-08-07 10:26:25 +02:00
1 parent e2eea55b6e
commit a5b6508961
3 files changed
+110 -2

No files matched your search

@@ -5,6 +5,7 @@ import {
closeDocumentSelectionAfterCoachHandoff,
documentSelectionHistoryState,
documentSelectionHref,
documentSelectionSurvivedClose,
withoutDocumentSelectionHref,
} from "../documents-view";
@@ -173,4 +174,44 @@ describe("document vault URL selection", () => {
expect(history.href).toBe("/documents?episode=ep-1");
});
// ── The dropped traversal ────────────────────────────────────────────────
//
// `history.back()` is a request. A traversal asked for while another is
// still settling can be dropped, and the browser says nothing: the sheet
// closes, React stays consistent, and the address bar keeps naming a
// document nobody has open. Reproduced in the browser suite as a closed
// sheet whose `?doc=` never cleared; this is the predicate the component
// uses to notice it.
it("reports a selection that survived its own close", () => {
expect(
documentSelectionSurvivedClose("?episode=ep-1&doc=doc-1", "doc-1"),
).toBe(true);
});
it("reports nothing to do once the traversal landed", () => {
expect(documentSelectionSurvivedClose("?episode=ep-1", "doc-1")).toBe(
false,
);
});
it("leaves a DIFFERENT selection alone", () => {
// The traversal landed somewhere that carries its own selection. Stripping
// that would close a sheet this close had nothing to do with.
expect(
documentSelectionSurvivedClose("?episode=ep-1&doc=doc-2", "doc-1"),
).toBe(false);
});
it("strips only the selection when the fallback runs", () => {
// What the component does with a true verdict, on the URL shape that
// produced it: every unrelated parameter survives.
expect(
withoutDocumentSelectionHref(
"/documents",
"episode=ep-1&view=compact&q=MRT&doc=doc-1",
),
).toBe("/documents?episode=ep-1&view=compact&q=MRT");
});
});