fix(02-12.1): fetch a document's original once, and only to show it

The detail sheet loaded the original twice on open — the inline preview
and the download href each fetched and decrypted the whole blob — and
for a non-inline document the old href had cost nothing until clicked.

- one loader, lifted to the sheet, and only for a document that renders
  inline; everything else fetches at click time
- make the download a real disabled control that reflects a failed
  fetch: href="#" with aria-disabled announces unavailable and still
  activates, and it ignored the failure entirely
This commit is contained in:
Marc-André Bombeck committed 2026-08-06 23:04:29 +02:00
1 parent 120323d995
commit 73e73469e3
2 files changed
+222 -42

No files matched your search

@@ -0,0 +1,83 @@
/**
* The detail sheet fetches the original once, and only when it will show it.
*
* Two regressions this pins, both introduced by the fix that moved these
* controls off a bare `/api/…` href and neither visible in any rendered
* assertion:
*
* * **Two eager fetches per open.** `InlinePreview` loaded the original and
* the sheet-level download href loaded it AGAIN, so opening a document
* decrypted and held two copies of it — including for a non-inline
* document, where the old href cost nothing until somebody clicked. The
* vault meets large scans.
* * **A download control that could not be pressed and would not say so.**
* `href="#"` plus `aria-disabled` announces "unavailable" and still
* activates, and it ignored the failure flag entirely, so a permanently
* unfetchable original left a control that silently did nothing.
*
* Asserted on source shape rather than by rendering: the project's component
* convention is SSR-only (no DOM, no testing-library), and both properties are
* about which code paths EXIST — how many loaders there are, and whether the
* disabled state is real. A render could not see either.
*/
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
const SHEET = readFileSync(
join(process.cwd(), "src/components/documents/document-detail-sheet.tsx"),
"utf8",
);
describe("the detail sheet's original-document transport", () => {
it("has exactly one eager loader for the original", () => {
const eager = [...SHEET.matchAll(/useFencedObjectUrl\(/g)];
// Non-zero proof first: a sheet that fetched nothing would satisfy a bare
// "not more than one" check.
expect(eager.length).toBe(1);
});
it("only fetches eagerly for a document it is going to render inline", () => {
// The whole point of the single loader: a non-inline document — a large
// scan, a download-only format — fetches and decrypts nothing on open.
expect(SHEET).toMatch(
/useFencedObjectUrl\(\s*\n?\s*doc && doc\.servingClass === "inline"/,
);
});
it("hands the blob down rather than letting the preview fetch its own", () => {
expect(SHEET).toMatch(/<InlinePreview\s+src=\{inlineSrc\}/);
// And the preview declares it takes bytes, not an id to fetch from.
expect(SHEET).toMatch(/function InlinePreview\(\{\s*\n\s*src,/);
expect(SHEET).not.toMatch(
/function InlinePreview\([\s\S]{0,400}documentId/,
);
});
it("downloads at click time when the bytes are not already in hand", () => {
expect(SHEET).toContain("createFencedBlobLoader()");
expect(SHEET).toMatch(
/const onClick = async \(\) => \{[\s\S]{0,200}readyUrl/,
);
});
it("gives the download a real disabled state that reflects failure", () => {
// `aria-disabled` on an anchor is an announcement, not a behaviour.
expect(SHEET).not.toMatch(/aria-disabled=/);
expect(SHEET).not.toMatch(/href=\{originalHref/);
expect(SHEET).toMatch(/disabled=\{blocked \|\| pending\}/);
expect(SHEET).toMatch(/const blocked = unavailable \|\| failed;/);
// And it says why, with the existing unavailable messaging.
expect(SHEET).toMatch(
/blocked\s*\n?\s*\?\s*t\("documents\.detail\.loadError"\)/,
);
});
it("revokes what it fetched", () => {
// A decrypted health document left in an object URL lives as long as the
// tab does, and nothing in the UI ever shows it.
expect(SHEET).toMatch(
/useEffect\(\(\) => \(\) => loaderRef\.current\?\.dispose\(\), \[\]\)/,
);
});
});