Files
HealthLog/src/components/documents/__tests__/episode-documents-card.test.tsx
T
Marc-André Bombeck ee76672798 feat(documents): preview thumbnails in the vault overview
Render a small JPEG preview for each stored document and show it at the
card's leading edge, falling back to the kind icon when none exists.

- Generate previews in the background on upload (new document-thumbnail
  pg-boss queue) plus a boot-time corpus backfill for existing documents;
  upload never blocks on or fails because of it. Images are downscaled via
  @napi-rs/canvas, a PDF's first page is rasterised then downscaled, every
  other type is skipped. Generation never throws.
- Store the preview encrypted at rest in a 1:1 DocumentThumbnail side table
  (migration 0236) so the list/detail queries that omit the original blob
  never drag the preview either. Same AES-256-GCM string codec + fail-closed
  decrypt as the content index; wired into the key-rotation walk.
- Serve it through an owner-scoped GET /api/documents/inbound/[id]/thumbnail
  (auth + module gate + userId-scoped query + fail-closed decrypt), always
  image/jpeg + nosniff + private, no-store; 404 on miss. The card renders an
  authed lazy <img>, onError falls back to the icon.
- Add hasThumbnail to the document DTO so the card only fetches a preview
  that exists.

Also drop the "search looks inside your documents" caption from the overview
(the index-all prompt stays) and the per-card searchable marker; both were
noise. Remove the now-unused i18n keys across all six locales.
2026-07-10 01:15:32 +02:00

107 lines
3.3 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
/**
* The illness page's "Dokumente" section, pinned via static renders:
* module-gated (renders NOTHING when the account has not opted in), a
* quiet one-line affordance on an episode without documents (never a
* teaching empty state), and compact deep-linking rows when links exist.
*/
let moduleEnabled = true;
vi.mock("@/hooks/use-auth", () => ({
useAuth: () => ({
user: { id: "u1", modules: { inboundDocuments: moduleEnabled } },
isLoading: false,
isAuthenticated: true,
}),
}));
vi.mock("@/lib/api/api-fetch", async () => {
const actual = await vi.importActual<typeof import("@/lib/api/api-fetch")>(
"@/lib/api/api-fetch",
);
return {
...actual,
apiGet: () => new Promise(() => {}),
apiPost: vi.fn(),
};
});
import { I18nProvider } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import type { InboundDocumentDto } from "@/lib/validations/inbound-documents";
import { EpisodeDocumentsCard } from "../episode-documents-card";
function doc(id: string, title: string): InboundDocumentDto {
return {
id,
kind: "IMAGING",
title,
filename: `${id}.pdf`,
mimeType: "application/pdf",
byteSize: 2048,
status: "STORED",
providerType: null,
reportDate: null,
documentDate: "2025-10-04",
errorReason: null,
factCount: 0,
pendingCount: 0,
conditionLinks: [{ episodeId: "ep1", name: "Knee" }],
servingClass: "inline",
hasContentIndex: false,
contentIndexSource: null,
hasThumbnail: false,
createdAt: "2025-10-05T08:00:00.000Z",
updatedAt: "2025-10-05T08:00:00.000Z",
};
}
function render(seed?: InboundDocumentDto[]) {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
if (seed !== undefined) {
queryClient.setQueryData(queryKeys.inboundDocumentEpisodePreview("ep1"), {
documents: seed,
nextCursor: null,
});
}
return renderToStaticMarkup(
<QueryClientProvider client={queryClient}>
<I18nProvider initialLocale="en">
<EpisodeDocumentsCard episodeId="ep1" />
</I18nProvider>
</QueryClientProvider>,
);
}
describe("<EpisodeDocumentsCard>", () => {
it("renders nothing when the documents module is off", () => {
moduleEnabled = false;
expect(render([])).toBe("");
moduleEnabled = true;
});
it("shows a quiet one-line affordance on an episode without documents", () => {
const html = render([]);
expect(html).toContain('data-slot="episode-documents-card"');
expect(html).toContain("No documents linked to this condition yet.");
expect(html).toContain("Link");
expect(html).toContain("Upload");
// Not the vault's teaching empty state.
expect(html).not.toContain('data-slot="empty-state"');
});
it("lists linked documents as rows deep-linking into the vault", () => {
const html = render([doc("d1", "MRT Knie"), doc("d2", "Röntgen")]);
expect(html).toContain("MRT Knie");
expect(html).toContain("Röntgen");
expect(html).toContain("/documents?episode=ep1&amp;doc=d1");
// The pre-linked upload deep link carries the episode filter.
expect(html).toContain('href="/documents?episode=ep1"');
});
});