Files
HealthLog/src/__tests__/url-state-writes-guard.test.ts
T
Marc-André Bombeck 3eb5aa9364 refactor(documents): split the vault page into filters, library, selection and detail
DocumentsView carried 970 lines of state before its first element. The
state moves to components/documents/vault/:

- selection-history: the `?doc=` history entry helpers (pure, tested)
- use-vault-filters: URL filters, the debounced search and the `/` key
- use-vault-library: usage, the paged list and its processing poll,
  layout, uploads and their intake paths, the corpus-index affordance
- use-vault-filter-choices: conditions, procedures, visits, years
- use-vault-selection and use-vault-bulk: the selection with ranges and
  Escape, the confirmed delete, re-filing, linking and the bulk share
- use-vault-detail: the detail sheet driven by `?doc=`, its close and
  the check that the close took
- vault-page-actions, vault-drop-overlay

The page composes them in about 300 lines; markup and behaviour are
unchanged, and the guards follow the code they watch.
2026-10-23 12:35:46 +02:00

74 lines
2.7 KiB
TypeScript

/**
* Every write of page state into the query string goes through
* `@/lib/url-state`, which derives it from the live address and holds it
* until the router has the previous one.
*
* Next 16.4 adopts a native history write a render later, and a write
* computed from `useSearchParams` starts from the URL before the last write.
* Each surface that wrote on its own met one of the two: a quick second
* step of the day undone, a closed document sheet left open, cleared vault
* filters coming back as `?encounter=`. A raw `history.pushState` /
* `replaceState`, or a router write built from the rendered search params,
* reopens the class, so both are refused outside the helper and the few
* places listed here with their reason.
*/
import { describe, expect, it } from "vitest";
import { readSourceFolder } from "@/__tests__/helpers/source-files";
const RAW_HISTORY_WRITE = /\bhistory\s*\.\s*(?:pushState|replaceState)\s*\(/;
const RENDERED_SEARCH = /\bsearchParams\??\.toString\(\)/;
const ROUTER_WRITE = /\brouter\s*\.\s*(?:push|replace)\s*\(/;
const ALLOWED: Record<string, string> = {
"lib/url-state.ts": "the helper itself",
"components/clinician/share-unlock-gate.tsx":
"scrubs a passphrase from the fragment synchronously, before the unlock request leaves; no query state",
"components/documents/vault/selection-history.ts":
"closeDocumentSelectionHistoryEntry takes a history-shaped argument for its tests; the page passes `urlStateHistory`",
};
describe("query-string writes go through @/lib/url-state", () => {
const files = readSourceFolder("src", {
floor: 500,
include: (rel) =>
!rel.startsWith("generated/") &&
!rel.includes("__tests__/") &&
!/\.test\.tsx?$/.test(rel),
});
it("reads the whole tree (floor)", () => {
expect(files.length).toBeGreaterThan(1000);
});
it("finds no raw history write outside the helper", () => {
const offenders = files
.filter(([rel, text]) => !ALLOWED[rel] && RAW_HISTORY_WRITE.test(text))
.map(([rel]) => rel);
expect(offenders).toEqual([]);
});
it("finds no router write built from the rendered search params", () => {
const offenders = files
.filter(
([rel, text]) =>
!ALLOWED[rel] &&
RENDERED_SEARCH.test(text) &&
ROUTER_WRITE.test(text),
)
.map(([rel]) => rel);
expect(offenders).toEqual([]);
});
it("still sees the helper's own writes (the matcher is alive)", () => {
const helper = files.find(([rel]) => rel === "lib/url-state.ts");
expect(helper && RAW_HISTORY_WRITE.test(helper[1])).toBe(true);
for (const rel of Object.keys(ALLOWED)) {
expect(
files.some(([path]) => path === rel),
`${rel} is still in the tree`,
).toBe(true);
}
});
});