Files
faunosteo/tests/unit/pdf.roundtrip.test.ts
T
ZenQuality b5b2e163c9 feat: domain core — schemas, store, equine data, PDF roundtrip
Phase A — Domain & schemas (15 tests):
- Zod schemas: ReportSchema, FindingSchema, PractitionerSchema, SeveritySchema
- Types derived via z.infer (single source of truth)
- newId() helper with optional prefix

Phase B — Equine anatomy + pathology DB (7 tests):
- 26 macro zones, axial subzones C1-7/T1-18/L1-6/S1-5
- 31 pathologies, each citing Merck Vet Manual, PubMed, IMAIOS or LCAO
- Placeholder SVGs (lateral L/R + dorsal) with data-zone convention
- Species registry: equine registered, 8 others remain stubs

Phase C — Zustand store (8 tests):
- Single source of truth with persist middleware (localStorage key osteo-report-draft)
- Actions: hydrate, reset, toggleZone, addFinding, updateFinding, removeFinding, setConclusion/Recommendations/NextAppointment

Phase H/I — PDF export + import roundtrip (2 tests):
- A4 portrait, pdf-lib StandardFonts
- Header (practitioner + animal), anatomy pages (per used view), findings
  table, conclusion/recommendations/RDV page, page footer with date+page#
- Attachment: base64 data URI workaround (pdf-lib treats raw string as
  base64 input, and rejects cross-realm Uint8Array in jsdom tests)
- Import: walks Catalog→Names→EmbeddedFiles→Names array→F stream; inflates
  via pako directly (bypasses pdf-lib's reference-equality FlateDecode check
  which fails across module realms in vitest); validates with ReportSchema

Tooling:
- Add pako 2.x + @types/pako (already transitive of pdf-lib)
- Switch vitest 2 → 3 (already done in prior commit)
- Add 8 test files (34 tests total green)
2026-05-17 07:06:59 +02:00

76 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { exportReportBytes, type AnatomyAssets } from '@/pdf/export';
import { importReport } from '@/pdf/import';
import { EQUINE_ANATOMY } from '@/species/equine/anatomy';
// 1×1 transparent PNG as Uint8Array
const TINY_PNG = Uint8Array.from([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4,
0x89, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00,
0x05, 0x00, 0x01, 0x0d, 0x0a, 0x2d, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
0x42, 0x60, 0x82,
]);
const assets: AnatomyAssets = {
anatomy: EQUINE_ANATOMY,
rasters: {
lateral_left: TINY_PNG,
lateral_right: TINY_PNG,
dorsal: TINY_PNG,
},
};
const sampleReport = {
version: 1 as const,
id: 'r_roundtrip',
createdAt: '2026-05-17T10:00:00.000Z',
species: 'equine' as const,
practitioner: {
fullName: 'Dr. Test',
phone: '0102030405',
email: 'test@example.com',
},
animal: { name: 'Pégase', owner: 'Famille Martin' },
sessionDate: '2026-05-17',
findings: [
{
id: 'finding_1',
zones: ['thoracic-back'],
linkedZones: ['lumbar'],
pathologies: [{ id: 'eq_kissing_spines', label: 'Kissing spines', free: false }],
severity: 'moderate' as const,
comment: 'Sensibilité marquée en flexion latérale.',
},
],
conclusion: 'Tableau de dysfonction dorsale.',
recommendations: 'Repos 7 jours, séance de contrôle à 3 semaines.',
nextAppointment: '2026-06-07',
};
describe('PDF roundtrip', () => {
it('exports then imports back equal report', async () => {
const bytes = await exportReportBytes(sampleReport, assets);
expect(bytes.byteLength).toBeGreaterThan(0);
expect(new TextDecoder().decode(bytes.slice(0, 5))).toBe('%PDF-');
const result = await importReport(bytes);
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.report).toEqual(sampleReport);
}
});
it('rejects a PDF without our attachment', async () => {
// Build a tiny PDF with no attachment using pdf-lib
const { PDFDocument } = await import('pdf-lib');
const pdf = await PDFDocument.create();
pdf.addPage();
const bytes = await pdf.save();
const result = await importReport(bytes);
expect(result.ok).toBe(false);
if (!result.ok) expect(result.reason).toBe('NO_ATTACHMENT');
});
});