import { describe, expect, it } from 'vitest' import { REPORT_VERSION, ReportSchema, buildSheetIndex, buildSheetSnapshot, rebaseReportOnSheet, type AnatomicalSheetDocument, type Report, type SheetRef, type SnapshotSource, } from '../src' import { HORSE_MAIN_ASSET_ID, horseSlice } from './fixtures/horse-slice' const SHA_A = 'a'.repeat(64) const SHA_B = 'b'.repeat(64) const REF_A: SheetRef = { sheetId: 'sheet-1', versionId: null, documentSha256: SHA_A, } const REF_B: SheetRef = { sheetId: 'sheet-1', versionId: 'v2', documentSha256: SHA_B, } function source( ref: SheetRef, document: AnatomicalSheetDocument, ): SnapshotSource { return { status: 'draft', ref, speciesId: 'equine', schemaVersion: 2, document, assets: { [HORSE_MAIN_ASSET_ID]: '' }, } } /** Revision B: the TMJ marker and the head trigger are gone, hip lost its articular node. */ function revisionB(): AnatomicalSheetDocument { const doc = horseSlice() doc.annotations = doc.annotations.filter( (a) => a.id !== 'atm' && a.id !== 'head-trigger', ) const hip = doc.finding_zones.find((z) => z.id === 'hip')! hip.categories[0]!.children = hip.categories[0]!.children.filter( (n) => n.id !== 'articular', ) return doc } function reportOnA(): Report { return { version: REPORT_VERSION, id: 'r1', createdAt: '2026-10-10T10:00:00Z', date: '2026-10-10', practitioner: { name: 'Dr. Dupont' }, animal: { speciesId: 'equine', name: 'Mistral' }, owner: { name: 'Jean Martin' }, sheet: REF_A, observations: [ { id: 'obs-1', zones: [ { annotationId: 'hip', viewId: 'view-main', side: 'left', labelFrSnapshot: 'Hanche (coxo-fémorale)', labelEnSnapshot: 'Hip joint', }, { annotationId: 'atm', viewId: 'view-head' }, { annotationId: 'head-trigger', viewId: 'view-main' }, { annotationId: 'hip', viewId: 'ghost-view', labelFrSnapshot: 'Gardé', }, { annotationId: 'gone-long-ago', viewId: 'view-main', labelFrSnapshot: 'Ancien', unresolved: true, }, ], findings: [ { findingZoneId: 'hip', path: ['hip-dysfunctions', 'articular', 'restriction-flexion'], labelFrSnapshot: 'Restriction en flexion', labelEnSnapshot: 'Flexion restriction', }, { findingZoneId: 'cervical', path: ['cervical-observations', 'stumbling-proprioceptive'], labelFrSnapshot: 'Trébuchements', labelEnSnapshot: 'Stumbling', requiresVetReferral: true, }, { findingZoneId: 'atm', path: ['atm-observations', 'headshaking'], labelFrSnapshot: 'Headshaking', labelEnSnapshot: 'Headshaking', unresolved: true, }, ], freeTextFindings: ['note'], severity: 'moderate', comment: '', }, ], conclusion: 'c', recommendations: 'r', localAnnotations: [], } } const indexA = buildSheetIndex(source(REF_A, horseSlice())) const indexB = buildSheetIndex(source(REF_B, revisionB())) describe('rebaseReportOnSheet', () => { it('returns the report untouched when it already sits on the loaded sheet', () => { const report = reportOnA() const outcome = rebaseReportOnSheet(report, indexA, null) expect(outcome).toEqual({ report, unresolvedZones: 0, unresolvedFindings: 0, rebased: false, }) expect(outcome.report).toBe(report) }) it('flags what the new sheet lost, labels it from the archive, keeps the rest', () => { const report = reportOnA() const archived = buildSheetSnapshot(source(REF_A, horseSlice()), report) const outcome = rebaseReportOnSheet(report, indexB, archived) expect(outcome.rebased).toBe(true) expect(outcome.unresolvedZones).toBe(3) expect(outcome.unresolvedFindings).toBe(1) expect(outcome.report.sheet).toEqual(REF_B) expect(() => ReportSchema.parse(outcome.report)).not.toThrow() const [hip, atm, trigger, ghostView, old] = outcome.report.observations[0]!.zones expect(hip).toEqual(report.observations[0]!.zones[0]) expect(atm).toEqual({ annotationId: 'atm', viewId: 'view-head', unresolved: true, labelFrSnapshot: 'Articulation Temporo-Mandibulaire (ATM)', labelEnSnapshot: 'Temporomandibular Joint (TMJ)', }) expect(trigger).toEqual({ annotationId: 'head-trigger', viewId: 'view-main', unresolved: true, labelFrSnapshot: 'Tête', labelEnSnapshot: 'Head', }) expect(ghostView).toEqual({ annotationId: 'hip', viewId: 'ghost-view', unresolved: true, labelFrSnapshot: 'Gardé', labelEnSnapshot: 'Hip joint', }) expect(old).toEqual(report.observations[0]!.zones[4]) const [flexion, stumbling, headshaking] = outcome.report.observations[0]!.findings expect(flexion).toEqual({ ...report.observations[0]!.findings[0], unresolved: true, }) expect(stumbling).toEqual(report.observations[0]!.findings[1]) expect(headshaking).toEqual(report.observations[0]!.findings[2]) expect(outcome.report.observations[0]!.freeTextFindings).toEqual(['note']) expect(report).toEqual(reportOnA()) }) it('fills no label without an archived snapshot', () => { const outcome = rebaseReportOnSheet(reportOnA(), indexB, null) expect(outcome.report.observations[0]!.zones[1]).toEqual({ annotationId: 'atm', viewId: 'view-head', unresolved: true, }) expect(outcome.unresolvedZones).toBe(3) }) it('counts nothing twice: a rebased report rebased again onto the same sheet is stable', () => { const first = rebaseReportOnSheet(reportOnA(), indexB, null) const second = rebaseReportOnSheet( { ...first.report, sheet: REF_A }, indexB, null, ) expect(second.unresolvedZones).toBe(0) expect(second.unresolvedFindings).toBe(0) expect(second.report).toEqual(first.report) }) })