Files
faunosteo/apps/app/tests/unit/scripts.compare.test.ts
T

204 lines
7.3 KiB
TypeScript

/**
* The archive check tool (scripts/compare-reports.ts) on the frozen v2 PDF,
* on a synthetic v2 report with removed and unknown items, and on a v3 report.
*/
import { describe, it, expect, beforeAll } from 'vitest'
import { PDFDocument } from 'pdf-lib'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
import { readReportDocument, type ReportV2 } from '@faunosteo/domain'
import { readAttachedDocument } from '@/pdf/attachment'
import type { SheetIndex } from '@/services/sheetIndex'
import {
compareReport,
diffLines,
parseArgs,
renderComparison,
renderSummary,
UNRESOLVED,
} from '../../scripts/compare/report'
import { loadTestSheet, minimalReport } from '../helpers/sheet'
const HERE = dirname(fileURLToPath(import.meta.url))
const OPTS = { locale: 'fr' as const, identity: false }
let sheet: SheetIndex
function minimalV2(): ReportV2 {
return JSON.parse(
readFileSync(resolve(HERE, '../fixtures/report-v2.minimal.json'), 'utf8'),
) as ReportV2
}
function compareRaw(raw: unknown, identity = false) {
const outcome = readReportDocument(raw, sheet.sheet.migrationMap)
const migrated = (raw as { version: number }).version !== 3
return compareReport(raw, outcome, migrated, sheet, { ...OPTS, identity })
}
beforeAll(async () => {
sheet = await loadTestSheet()
})
describe('compareReport on the frozen v2 archive PDF', () => {
it('shows every legacy zone and finding next to its canonical target', async () => {
const bytes = readFileSync(resolve(HERE, '../fixtures/report-v2.full.pdf'))
const pdf = await PDFDocument.load(new Uint8Array(bytes))
const raw = await readAttachedDocument(pdf)
const c = compareRaw(raw)
const lines = renderComparison('full.pdf', c)
expect(c.source).toBe('v2')
expect(lines[0]).toBe('== full.pdf')
expect(lines).toContain(' v2 → v3 (migrated)')
expect(lines.join('\n')).toContain(
'point-mpbwtst0 left "Hanche (coxo-fémorale)" → hip@main left "Hanche (coxo-fémorale)"',
)
expect(lines.join('\n')).toContain(
'cervical/cervical-observations/stumbling-proprioceptive (!) "Trébuchements / déficit proprioceptif rapporté" → cervical/cervical-consultation-reasons/stumbling-proprioceptive',
)
expect(lines.join('\n')).toContain('severity modérée → moderate')
expect(lines.some((l) => l.includes(UNRESOLVED))).toBe(false)
expect(c.counts).toEqual({
observations: 3,
zones: 5,
unresolvedZones: 0,
findings: 4,
unresolvedFindings: 0,
})
expect(renderSummary('full.pdf', c)).toBe(
'full.pdf · v2→v3 · 3 obs · zones 5/0 unresolved · findings 4/0 unresolved',
)
expect(lines.join('\n')).not.toContain('practitioner')
expect(renderComparison('x', compareRaw(raw, true)).join('\n')).toContain(
'practitioner "Dr. Claire Dupont"',
)
})
})
describe('compareReport on unresolved items', () => {
it('names a removed finding, a finding unknown to the catalog and an unknown zone', () => {
const v2: ReportV2 = {
...minimalV2(),
observations: [
{
id: 'o',
zones: [{ boneId: 'point-gone' }],
findings: [
{
zoneId: 'ssb',
path: ['ssb-dysfunctions', 'sutures', 'sphenozygomatic'],
labelFrSnapshot: 'Suture sphéno-zygomatique',
labelEnSnapshot: 'Sphenozygomatic suture',
requiresVetReferral: true,
},
{
zoneId: 'nowhere',
path: ['x'],
labelFrSnapshot: 'X',
labelEnSnapshot: 'X',
},
],
freeTextFindings: [],
comment: '',
},
],
}
const text = renderComparison('odd.pdf', compareRaw(v2)).join('\n')
expect(text).toContain(
`point-gone ${UNRESOLVED} unknown legacy id, kept as point-gone@main`,
)
expect(text).toContain(
`ssb/ssb-dysfunctions/sutures/sphenozygomatic (!) "Suture sphéno-zygomatique" ${UNRESOLVED} removed from the sheet, kept with its label`,
)
expect(text).toContain(
`nowhere/x "X" ${UNRESOLVED} never in the legacy catalog, kept with its label`,
)
expect(text).toContain('1 zones (1 unresolved) · 2 findings (2 unresolved)')
})
it('reads a v3 report as is and flags what the sheet cannot place', () => {
const report = minimalReport(sheet, {
observations: [
{
id: 'o3',
zones: [{ annotationId: 'hip', viewId: 'main', side: 'left' }],
findings: [
{
findingZoneId: 'hip',
path: ['hip-dysfunctions', 'articular', 'restriction-flexion'],
labelFrSnapshot: 'Restriction en flexion',
labelEnSnapshot: 'Flexion restriction',
},
{
findingZoneId: 'atm',
path: ['atm-observations', 'headshaking'],
labelFrSnapshot: 'Headshaking (secouement de tête)',
labelEnSnapshot: 'Headshaking',
unresolved: true,
},
{
findingZoneId: 'hip',
path: ['hip-dysfunctions'],
labelFrSnapshot: 'Catégorie',
labelEnSnapshot: 'Category',
},
],
freeTextFindings: [],
comment: '',
},
],
})
const c = compareRaw(report)
const text = renderComparison('v3.pdf', c).join('\n')
expect(c.source).toBe('v3')
expect(text).toContain('v3 (read as is)')
expect(text).toContain('hip@main left "Hanche (coxo-fémorale)"')
expect(text).toContain(
'hip/hip-dysfunctions/articular/restriction-flexion "Restriction en flexion"',
)
expect(text).toContain(
`atm/atm-observations/headshaking "Headshaking (secouement de tête)" ${UNRESOLVED} kept with its snapshot`,
)
expect(text).toContain('!! points at a category, not a leaf')
expect(c.counts.unresolvedFindings).toBe(1)
})
it('warns when a v3 report was written on another sheet document', () => {
const report = minimalReport(sheet, {
sheet: { ...sheet.sheet.ref, documentSha256: 'b'.repeat(64) },
})
const text = renderComparison('other.pdf', compareRaw(report)).join('\n')
expect(text).toContain(
'!! written on another sheet document (sha bbbbbbbbbbbb…)',
)
})
})
describe('diff and arguments', () => {
it('diffs the v3 content of a migrated v2 report and its v3 re-reading', () => {
const v2 = minimalV2()
const migrated = readReportDocument(v2, sheet.sheet.migrationMap).report
const a = compareRaw(v2)
const b = compareRaw(migrated)
expect(diffLines(a.normalized, b.normalized)).toEqual([])
const edited = { ...migrated, conclusion: 'changed' }
expect(diffLines(a.normalized, compareRaw(edited).normalized)).toEqual([
'+ conclusion "changed"',
])
})
it('parses files and options, refuses unknown ones', () => {
expect(parseArgs(['a.pdf', '--summary', 'dir', '--locale', 'en'])).toEqual({
files: ['a.pdf', 'dir'],
locale: 'en',
identity: false,
summary: true,
diff: false,
})
expect(parseArgs(['--identity', '--diff', 'a', 'b']).identity).toBe(true)
expect(() => parseArgs(['--locale', 'de'])).toThrow('fr or en')
expect(() => parseArgs(['--json'])).toThrow('unknown option')
})
})