/** * Lot D: geometry errors and the geometry `accept`, read from ONE `PreviewState` and proved * through the real `replaceDocument` rollback on the Node core. */ import { describe, expect, it } from 'vitest' import type { PreviewState } from '@faunosteo/core-wasm/types' import { CataloguePreview } from '@/catalogue/preview' import { coreRuntime } from '@/core/runtime' import { geometryAccept, geometryErrorsById, unmeasuredViews, type GeometryRefusal, } from '@/editor/issues' import { ASSET, drawnText, edit, insert, point, svgBytes } from '../helpers/editorFixtures' function draft(text: string, viewBox = '0 0 100 100') { const drawing = svgBytes(viewBox) const preview = new CataloguePreview(coreRuntime(), { status: 'draft', document: text }, [ { assetId: ASSET, sha256: drawing.sha256, bytes: drawing.bytes }, ]) const state = preview.state() if (!state.ok) throw new Error(state.kind) return { preview, state: state.value } } function moved(text: string, id: string, cx: number, cy: number, r: number) { return edit(text, { op: 'update', key: { kind: 'annotation', id }, element: point(id, cx, cy, r) }) } function refusalOf(outcome: ReturnType) { if (outcome.ok || outcome.reason !== 'refused') throw new Error('not refused') return outcome.refusal as GeometryRefusal } const valid = () => insert(drawnText(), point('anno-a', 50, 50, 5)) describe('geometryErrorsById', () => { it('maps the issue index to the id after an insert before the annotation', () => { const text = insert(valid(), point('anno-b', 1, 1, 5)) expect([...geometryErrorsById(draft(text).state).keys()]).toEqual(['anno-b']) const shifted = edit(text, { op: 'insert', element: point('anno-c', 60, 60, 5), position: 0, }) const { state } = draft(shifted) expect(state.report.issues.some((i) => i.path === 'annotations[2].geometry')).toBe(true) expect([...geometryErrorsById(state).keys()]).toEqual(['anno-b']) }) it('ignores an Error that is not at annotations[i].geometry', () => { const { state } = draft(valid()) const faked: PreviewState = { ...state, report: { ...state.report, issues: [ { code: 'x.y', severity: 'error', path: 'annotations[0].name_fr', message: 'm' }, { code: 'annotation.geometry.out_of_frame', severity: 'warning', path: 'annotations[0].geometry', message: 'm' }, ], }, } expect(geometryErrorsById(faked).size).toBe(0) }) }) describe('unmeasuredViews', () => { it('measures a view through its own drawing or the one it crops', () => { const detail = insert(drawnText(), { kind: 'view', value: { kind: 'detail', id: 'view-det', name: { fr: 'D', en: 'D' }, svg_asset_id: null, view_box: { min_x: 0, min_y: 0, width: 50, height: 50 }, parent_view_id: 'view-main', }, }) expect(unmeasuredViews(draft(detail).state).size).toBe(0) }) it('counts a missing drawing and a frame-less view as unmeasured', () => { const { state } = draft(drawnText()) const without = { ...state, drawings: [] } expect([...unmeasuredViews(without)]).toEqual(['view-main']) const frameless: PreviewState = { ...state, report: { ...state.report, issues: [{ code: 'view.frame.unknown', severity: 'error', path: 'views[0]', message: 'm' }], }, } expect([...unmeasuredViews(frameless)]).toEqual(['view-main']) }) }) describe('geometryAccept through replaceDocument', () => { it('refuses a valid point moved out of the frame and keeps the previous text', () => { const text = valid() const { preview, state } = draft(text) const accept = geometryAccept({ before: state, changedIds: ['anno-a'] }) const outcome = preview.replaceDocument(moved(text, 'anno-a', 1, 1, 5), accept) expect(refusalOf(outcome)).toMatchObject({ reason: 'changed_invalid', annotationIds: ['anno-a'], issues: [{ code: 'annotation.geometry.out_of_frame' }], }) expect(preview.document).toBe(text) }) it('refuses a typed radius the core reports too_small', () => { const big = '0 0 5000 5000' const text = insert(drawnText(), point('anno-a', 2500, 2500, 400)) const { preview, state } = draft(text, big) expect(geometryErrorsById(state).size).toBe(0) const accept = geometryAccept({ before: state, changedIds: ['anno-a'] }) const outcome = preview.replaceDocument(moved(text, 'anno-a', 2500, 2500, 1), accept) expect(refusalOf(outcome).issues[0].code).toBe('annotation.geometry.too_small') }) it('repairs an inherited out-of-frame point only with a coherent set', () => { const text = insert(drawnText(), point('anno-a', 150, 150, 5)) const { preview, state } = draft(text) expect(geometryErrorsById(state).has('anno-a')).toBe(true) const accept = geometryAccept({ before: state, changedIds: ['anno-a'] }) const half = preview.replaceDocument(moved(text, 'anno-a', 50, 150, 5), accept) expect(refusalOf(half).reason).toBe('changed_invalid') const whole = preview.replaceDocument(moved(text, 'anno-a', 50, 50, 5), accept) expect(whole.ok).toBe(true) }) it('never blocks a commit on an unrelated annotation already invalid', () => { const text = insert(valid(), point('anno-b', 1, 1, 5)) const { preview, state } = draft(text) const accept = geometryAccept({ before: state, changedIds: ['anno-a'] }) expect(preview.replaceDocument(moved(text, 'anno-a', 40, 40, 5), accept).ok).toBe(true) }) it('refuses a view_box that invalidates a valid point', () => { const text = valid() const { preview, state } = draft(text) const main = state.document.views[0] const next = edit(text, { op: 'update', key: { kind: 'view', id: 'view-main' }, element: { kind: 'view', value: { ...main, view_box: { min_x: 0, min_y: 0, width: 40, height: 40 } } }, }) const outcome = preview.replaceDocument(next, geometryAccept({ before: state, changedIds: [] })) expect(refusalOf(outcome)).toMatchObject({ reason: 'newly_invalid', annotationIds: ['anno-a'] }) }) it('accepts a view_box that only touches an already invalid point', () => { const text = insert(drawnText(), point('anno-a', 150, 50, 5)) const { preview, state } = draft(text) const main = state.document.views[0] const next = edit(text, { op: 'update', key: { kind: 'view', id: 'view-main' }, element: { kind: 'view', value: { ...main, view_box: { min_x: 0, min_y: 0, width: 90, height: 90 } } }, }) const outcome = preview.replaceDocument(next, geometryAccept({ before: state, changedIds: [] })) expect(outcome.ok).toBe(true) }) it('refuses a frame change that leaves a view with annotations unmeasured', () => { const text = valid() const { preview, state } = draft(text) const main = state.document.views[0] const next = edit(text, { op: 'update', key: { kind: 'view', id: 'view-main' }, element: { kind: 'view', value: { ...main, svg_asset_id: '22222222-2222-4222-8222-222222222222' } }, }) const outcome = preview.replaceDocument(next, geometryAccept({ before: state, changedIds: [] })) expect(refusalOf(outcome)).toMatchObject({ reason: 'view_unmeasured', viewIds: ['view-main'] }) }) })