feat(editor): new points take the view's most used size (A5, R-10)

proposePoint, correctedCircle and centreCircle take the wanted radius,
fitted to the frame; pendingForClick, keyboardPending and the C26 test
(correctionFor) use it. The size comes from newPointSize: the strip's
choice, else the most used radius of the view, else defaultRadius.
pointSize.ts (stops, snap, range) and escape.ts (board 32 order) land
with their tests; the repair dialog keeps defaultRadius (D1).

Rewritten assertions: editor-frame.test.ts calls now pass
defaultRadius(frame) explicitly, same expected values.
This commit is contained in:
bchanot committed 2026-11-17 18:20:58 +01:00
1 parent 9d03e0675c
commit 8cd1bda6c2
13 files changed
+513 -100

No files matched your search

@@ -0,0 +1,165 @@
/** 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()
})
})
@@ -1883,6 +1883,7 @@ describe('the strip', () => {
dispatch,
frame: null,
doc,
viewId: 'main',
context: { t: (key) => key, locale: 'fr' },
message: { show: vi.fn() },
}),
+117 -45
View File
@@ -67,33 +67,51 @@ describe('proposePoint', () => {
const frame = f(0, 0, 40, 30)
it('keeps the click with r0 when it is r0 or more from every edge (A)', () => {
expect(proposePoint(frame, { x: 20.004, y: 15.006 })).toEqual({
expect(
proposePoint(frame, { x: 20.004, y: 15.006 }, defaultRadius(frame)),
).toEqual({
kind: 'circle',
circle: { cx: 20, cy: 15.01, r: 13.2 },
shrunk: false,
})
expect(proposePoint(frame, { x: 13.2, y: 15 })).toMatchObject({
expect(
proposePoint(frame, { x: 13.2, y: 15 }, defaultRadius(frame)),
).toMatchObject({
shrunk: false,
})
})
it('keeps the click with a smaller radius when it is nearer an edge (B)', () => {
expect(proposePoint(frame, { x: 13.19, y: 15 })).toEqual({
expect(
proposePoint(frame, { x: 13.19, y: 15 }, defaultRadius(frame)),
).toEqual({
kind: 'circle',
circle: { cx: 13.19, cy: 15, r: 13.19 },
shrunk: true,
})
expect(proposePoint(frame, { x: 5.555, y: 15 })).toMatchObject({
expect(
proposePoint(frame, { x: 5.555, y: 15 }, defaultRadius(frame)),
).toMatchObject({
circle: { cx: 5.56, r: 5.56 },
})
})
it('goes straight to C when B would be under 0.01, and nothing outside F', () => {
expect(proposePoint(frame, { x: 0, y: 0 }).kind).toBe('corrected')
expect(proposePoint(frame, { x: 40, y: 15 }).kind).toBe('corrected')
expect(proposePoint(frame, { x: 0.004, y: 15 }).kind).toBe('corrected')
expect(proposePoint(frame, { x: -0.1, y: 15 })).toEqual({ kind: 'outside' })
expect(proposePoint(frame, { x: 20, y: 30.01 })).toEqual({
expect(proposePoint(frame, { x: 0, y: 0 }, defaultRadius(frame)).kind).toBe(
'corrected',
)
expect(
proposePoint(frame, { x: 40, y: 15 }, defaultRadius(frame)).kind,
).toBe('corrected')
expect(
proposePoint(frame, { x: 0.004, y: 15 }, defaultRadius(frame)).kind,
).toBe('corrected')
expect(
proposePoint(frame, { x: -0.1, y: 15 }, defaultRadius(frame)),
).toEqual({ kind: 'outside' })
expect(
proposePoint(frame, { x: 20, y: 30.01 }, defaultRadius(frame)),
).toEqual({
kind: 'outside',
})
})
@@ -101,24 +119,42 @@ describe('proposePoint', () => {
describe('correctedCircle (C)', () => {
it('clamps per axis with r0, inward on the hundredth grid', () => {
expect(correctedCircle(f(0, 0, 40, 30), { x: 0, y: 0 })).toEqual({
expect(
correctedCircle(
f(0, 0, 40, 30),
{ x: 0, y: 0 },
defaultRadius(f(0, 0, 40, 30)),
),
).toEqual({
cx: 13.2,
cy: 13.2,
r: 13.2,
})
expect(correctedCircle(f(0, 0, 40, 30), { x: 40, y: 30 })).toEqual({
expect(
correctedCircle(
f(0, 0, 40, 30),
{ x: 40, y: 30 },
defaultRadius(f(0, 0, 40, 30)),
),
).toEqual({
cx: 26.8,
cy: 16.8,
r: 13.2,
})
expect(correctedCircle(f(0.003, 0, 40, 30), { x: 0.003, y: 15 }).cx).toBe(
13.21,
)
expect(
correctedCircle(
f(0.003, 0, 40, 30),
{ x: 0.003, y: 15 },
defaultRadius(f(0.003, 0, 40, 30)),
).cx,
).toBe(13.21)
})
it('uses the exact clamped value when the grid leaves an empty interval', () => {
const frame = f(0.003, 0.004, 20, 20)
expect(correctedCircle(frame, { x: 0.003, y: 0.004 })).toEqual({
expect(
correctedCircle(frame, { x: 0.003, y: 0.004 }, defaultRadius(frame)),
).toEqual({
cx: 10.003,
cy: 10.004,
r: 10,
@@ -128,7 +164,9 @@ describe('correctedCircle (C)', () => {
describe('keyboard proposals', () => {
it('holds a point at the centre of F with r0 and a triangle of 10 % of the smaller side', () => {
expect(centreCircle(f(-50, 20, 40, 30))).toEqual({
expect(
centreCircle(f(-50, 20, 40, 30), defaultRadius(f(-50, 20, 40, 30))),
).toEqual({
cx: -30,
cy: 35,
r: 13.2,
@@ -147,12 +185,23 @@ describe('correctionFor', () => {
it('offers C for a refused B or an A refused out_of_frame, never for A too_small', () => {
const reduced = { cx: 13.19, cy: 15, r: 13.19 }
expect(
correctionFor(frame, reduced, ['annotation.geometry.too_small'], 13.19),
correctionFor(
frame,
reduced,
['annotation.geometry.too_small'],
13.19,
defaultRadius(frame),
),
).not.toBeNull()
const edge = { cx: 5, cy: 15, r: 13.2 }
expect(
correctionFor(frame, edge, ['annotation.geometry.out_of_frame'], 13.2)
?.circle.cx,
correctionFor(
frame,
edge,
['annotation.geometry.out_of_frame'],
13.2,
defaultRadius(frame),
)?.circle.cx,
).toBe(13.2)
expect(
correctionFor(
@@ -160,6 +209,7 @@ describe('correctionFor', () => {
{ cx: 20, cy: 15, r: 13.2 },
['annotation.geometry.too_small'],
13.2,
defaultRadius(frame),
),
).toBeNull()
})
@@ -167,7 +217,13 @@ describe('correctionFor', () => {
it('offers nothing when C is the circle that was just refused', () => {
const circle = { cx: 13.2, cy: 13.2, r: 13.2 }
expect(
correctionFor(frame, circle, ['annotation.geometry.out_of_frame'], 13.2),
correctionFor(
frame,
circle,
['annotation.geometry.out_of_frame'],
13.2,
defaultRadius(frame),
),
).toBeNull()
})
})
@@ -210,16 +266,14 @@ describe.each(FRAMES)('a point placed in the frame %s', (viewBox) => {
const frame = frameOf(bed(viewBox))!
for (const click of clicksOf(frame)) {
const b = bed(viewBox)
const proposal = proposePoint(frame, click)
const proposal = proposePoint(frame, click, defaultRadius(frame))
expect(proposal.kind).not.toBe('outside')
if (proposal.kind === 'outside') continue
const insert = circleInserter(b)
const attempt = attemptPoint(
frame,
proposal.circle,
insert,
proposal.circle.r,
)
const attempt = attemptPoint(frame, proposal.circle, insert, {
proposed: proposal.circle.r,
wanted: defaultRadius(frame),
})
expect(attempt).toMatchObject({ status: 'created' })
expect(currentErrors(b).size).toBe(0)
}
@@ -227,7 +281,11 @@ describe.each(FRAMES)('a point placed in the frame %s', (viewBox) => {
it('a corrected proposal is inserted only through the confirmation', () => {
const frame = frameOf(bed(viewBox))!
const proposal = proposePoint(frame, { x: frame.x, y: frame.y })
const proposal = proposePoint(
frame,
{ x: frame.x, y: frame.y },
defaultRadius(frame),
)
if (proposal.kind !== 'corrected')
throw new Error('corner must be corrected')
const b = bed(viewBox)
@@ -253,14 +311,16 @@ describe('a very elongated frame', () => {
it('refuses 1000 × 30 on too_small with the core message and creates nothing', () => {
const b = bed('0 0 1000 30')
const frame = frameOf(b)!
const proposal = proposePoint(frame, { x: 500, y: 15 })
if (proposal.kind !== 'circle') throw new Error('proposal')
const attempt = attemptPoint(
const proposal = proposePoint(
frame,
proposal.circle,
circleInserter(b),
proposal.circle.r,
{ x: 500, y: 15 },
defaultRadius(frame),
)
if (proposal.kind !== 'circle') throw new Error('proposal')
const attempt = attemptPoint(frame, proposal.circle, circleInserter(b), {
proposed: proposal.circle.r,
wanted: defaultRadius(frame),
})
expect(attempt.status).toBe('refused')
expect(b.preview.document).toBe(b.text)
})
@@ -277,16 +337,21 @@ describe('a detail crop', () => {
it('places a point inside the crop and corrects one on its edge, never outside it', () => {
const b = cropBed()
const frame = frameOf(b, 'view-det')!
const inside = proposePoint(frame, { x: 150, y: 350 })
const inside = proposePoint(frame, { x: 150, y: 350 }, defaultRadius(frame))
if (inside.kind !== 'circle') throw new Error('proposal')
const insert = circleInserter(b, 'view-det')
expect(
attemptPoint(frame, inside.circle, insert, inside.circle.r),
attemptPoint(frame, inside.circle, insert, {
proposed: inside.circle.r,
wanted: defaultRadius(frame),
}),
).toMatchObject({
status: 'created',
})
expect(proposePoint(frame, { x: 99, y: 350 })).toEqual({ kind: 'outside' })
const edge = proposePoint(frame, { x: 100, y: 200 })
expect(
proposePoint(frame, { x: 99, y: 350 }, defaultRadius(frame)),
).toEqual({ kind: 'outside' })
const edge = proposePoint(frame, { x: 100, y: 200 }, defaultRadius(frame))
expect(edge.kind).toBe('corrected')
expect(currentErrors(b).size).toBe(0)
})
@@ -298,8 +363,12 @@ describe('a radius the user typed (H5)', () => {
it('is never replaced by a corrected position: the core issue is shown', () => {
const typed = { cx: 20, cy: 15, r: 1 }
const codes = ['annotation.geometry.too_small']
expect(correctionFor(frame, typed, codes, 13.2)).toBeNull()
expect(correctionFor(frame, typed, codes, undefined)).toBeNull()
expect(
correctionFor(frame, typed, codes, 13.2, defaultRadius(frame)),
).toBeNull()
expect(
correctionFor(frame, typed, codes, undefined, defaultRadius(frame)),
).toBeNull()
})
it('on the core: a typed too-small radius creates nothing and keeps the message', () => {
@@ -309,7 +378,7 @@ describe('a radius the user typed (H5)', () => {
big,
{ cx: 2500, cy: 2500, r: 1 },
circleInserter(b),
defaultRadius(big),
{ proposed: defaultRadius(big), wanted: defaultRadius(big) },
)
expect(attempt).toMatchObject({ status: 'refused' })
expect(b.preview.document).toBe(b.text)
@@ -325,13 +394,16 @@ describe('the keyboard point (H4)', () => {
])('fits the frame %s by construction, with no correction', (viewBox) => {
const b = bed(viewBox)
const frame = frameOf(b)!
const circle = centreCircle(frame)
const circle = centreCircle(frame, defaultRadius(frame))
const slack = 1e-9 // float noise of x + w / 2 − r only; the core decides below
expect(circle.cx - circle.r).toBeGreaterThanOrEqual(frame.x - slack)
expect(circle.cy - circle.r).toBeGreaterThanOrEqual(frame.y - slack)
expect(circle.cx + circle.r).toBeLessThanOrEqual(frame.x + frame.w + slack)
expect(circle.cy + circle.r).toBeLessThanOrEqual(frame.y + frame.h + slack)
const attempt = attemptPoint(frame, circle, circleInserter(b), circle.r)
const attempt = attemptPoint(frame, circle, circleInserter(b), {
proposed: circle.r,
wanted: defaultRadius(frame),
})
expect(attempt.status).not.toBe('correction')
if (viewBox.endsWith('1000 30')) return
expect(attempt).toMatchObject({ status: 'created' })
@@ -345,13 +417,13 @@ describe.each(['0 0 1000 30', '0 0 4000 200'])(
const frame = frameOf(bed(viewBox))!
for (const click of clicksOf(frame)) {
const b = bed(viewBox)
const proposal = proposePoint(frame, click)
const proposal = proposePoint(frame, click, defaultRadius(frame))
if (proposal.kind === 'outside') continue
const attempt = attemptPoint(
frame,
proposal.circle,
circleInserter(b),
proposal.circle.r,
{ proposed: proposal.circle.r, wanted: defaultRadius(frame) },
)
expect(['created', 'refused', 'correction']).toContain(attempt.status)
expect(currentErrors(b).size).toBe(0)