/** U2 Task 4: point sizes (A5, R-10), the Escape order (board 32) and the move step (R-11). */ import { readFileSync } from 'node:fs' import { resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { describe, expect, it } from 'vitest' import type { Annotation, AnatomicalSheetDocument, } from '@faunosteo/core-wasm/types' import { escapeAction, type EscapeInput } from '@/editor/annotations/escape' import { centreCircle, correctedCircle, moveStep, proposePoint, type Frame, } from '@/editor/annotations/frame' import { mostUsedSize, sizeRange, sizeStops, snapSize, } from '@/editor/annotations/pointSize' import { correctionFor } from '@/editor/annotations/correction' import { keyboardPending, pendingForClick } from '@/editor/annotations/creation' const DRAFT = resolve( fileURLToPath(import.meta.url), '../../../../../supabase/seed/anatomical-sheets/equine/draft/document.json', ) const draft = JSON.parse(readFileSync(DRAFT, 'utf8')) as AnatomicalSheetDocument const circle = ( id: string, r: number, kind: Annotation['kind'] = 'point', ): Annotation => ({ id, kind, view_id: 'v', geometry: { type: 'circle', cx: 0, cy: 0, r }, }) as Annotation describe('point sizes on the bundled draft (A5, R-10)', () => { it('main view stops 30 and 34, the most used 34; the head 27', () => { expect(sizeStops(draft.annotations, 'main')).toEqual([30, 34]) expect(mostUsedSize(draft.annotations, 'main')).toBe(34) expect(sizeStops(draft.annotations, 'head')).toEqual([27]) expect(mostUsedSize(draft.annotations, 'head')).toBe(27) }) it('the main view range runs from 13.25 to 68 with the core minimum 26.5', () => { const stops = sizeStops(draft.annotations, 'main') expect(sizeRange(stops, 26.5, 74)).toEqual({ low: 13.25, high: 68 }) expect(sizeRange(stops, null, 74)).toEqual({ low: 15, high: 68 }) expect(sizeRange([], null, 20)).toEqual({ low: 10, high: 40 }) }) }) describe('mostUsedSize and sizeStops', () => { it('a tie goes to the smaller; links never count; no point gives null', () => { const tie = [ circle('a', 30), circle('b', 34), circle('c', 34), circle('d', 30), ] expect(mostUsedSize(tie, 'v')).toBe(30) const links = [circle('a', 30), circle('l1', 50, 'detail_trigger')] expect(sizeStops(links, 'v')).toEqual([30]) expect(mostUsedSize([circle('l', 50, 'detail_trigger')], 'v')).toBeNull() expect(mostUsedSize(draft.annotations, 'nowhere')).toBeNull() }) }) describe('snapSize', () => { it('snaps to the nearest stop within the reach, the edge inclusive', () => { expect(snapSize(31, [30, 34], 1)).toBe(30) expect(snapSize(31.01, [30, 34], 1)).toBe(31.01) expect(snapSize(32.5, [30, 34], 2)).toBe(34) expect(snapSize(32, [30, 34], 2)).toBe(30) expect(snapSize(20.123, [], 5)).toBe(20.12) expect(snapSize(20.123, [30], 0)).toBe(20.12) }) }) describe('escapeAction (board 32 rows 3 to 6)', () => { const none: EscapeInput = { previewPending: false, creating: false, inMode: false, chosenVertex: null, selectedId: null, } it('consumes one situation per press, in the board order', () => { const all = { ...none, previewPending: true, creating: true, inMode: true } const full = { ...all, chosenVertex: 2, selectedId: 'a' } expect(escapeAction(full)).toBe('drop_preview') expect(escapeAction({ ...full, previewPending: false })).toBe( 'ask_creation', ) const mode = { ...full, previewPending: false, creating: false } expect(escapeAction(mode)).toBe('drop_mode') expect(escapeAction({ ...mode, inMode: false })).toBe('unchoose') expect(escapeAction({ ...none, selectedId: 'a' })).toBe('deselect') expect(escapeAction(none)).toBeNull() }) }) describe('moveStep (R-11)', () => { const f = (w: number, h: number): Frame => ({ x: 0, y: 0, w, h }) it('0.5 % of the longer side, 0.01 at least, Shift ×10', () => { expect(moveStep(f(1200, 900), false)).toBe(6) expect(moveStep(f(1200, 900), true)).toBe(60) expect(moveStep(f(300, 300), false)).toBe(1.5) expect(moveStep(f(1, 0.5), false)).toBe(0.01) expect(moveStep(f(1, 0.5), true)).toBe(0.1) }) }) describe('A5: every new point takes the given size', () => { const frame: Frame = { x: 0, y: 0, w: 1000, h: 800 } it('a click, the C26 candidate and the keyboard point use it', () => { const clicked = pendingForClick(frame, { x: 500, y: 400 }, 34) expect(clicked).toMatchObject({ kind: 'pending', pending: { geometry: { r: 34 }, proposedRadius: 34, wantedRadius: 34 }, }) const corner = pendingForClick(frame, { x: 0, y: 0 }, 34) expect(corner).toMatchObject({ pending: { correction: { circle: { cx: 34, cy: 34, r: 34 } } }, }) expect(keyboardPending('point', frame, 34).geometry).toEqual({ type: 'circle', cx: 500, cy: 400, r: 34, }) }) it('the arithmetic caps it at half the smaller side', () => { const small: Frame = { x: 0, y: 0, w: 40, h: 30 } expect(centreCircle(small, 34)).toEqual({ cx: 20, cy: 15, r: 15 }) expect(correctedCircle(small, { x: 0, y: 0 }, 34).r).toBe(15) expect(proposePoint(frame, { x: 500, y: 400 }, 34)).toMatchObject({ circle: { r: 34 }, shrunk: false, }) }) it('C26 tests a reduced radius against the given size, not the frame default', () => { const near = { cx: 20, cy: 400, r: 20 } const codes = ['annotation.geometry.too_small'] expect(correctionFor(frame, near, codes, 20, 34)?.circle).toEqual({ cx: 34, cy: 400, r: 34, }) // At the given size: no correction for a too_small verdict. const full = { cx: 500, cy: 400, r: 34 } expect(correctionFor(frame, full, codes, 34, 34)).toBeNull() }) })