feat(app): editor Annotations tab, core-decided placement, repair rules

This commit is contained in:
bchanot committed 2026-11-04 13:49:41 +01:00
1 parent 92c595caa1
commit 26097852ff
37 files changed
+5001 -6

No files matched your search

+278
View File
@@ -0,0 +1,278 @@
/** Lot H: the proposal arithmetic (pure) and the core's verdict on each proposal (C26). */
import { describe, expect, it } from 'vitest'
import {
attemptPoint,
correctionFor,
confirmCorrection,
} from '@/editor/annotations/correction'
import {
centreCircle,
centreTriangle,
correctedCircle,
defaultRadius,
parseFrame,
proposePoint,
type Frame,
} from '@/editor/annotations/frame'
import {
bed,
circleInserter,
currentErrors,
drawnText,
frameOf,
withCrop,
} from '../helpers/annotationsCore'
const f = (x: number, y: number, w: number, h: number): Frame => ({
x,
y,
w,
h,
})
describe('parseFrame', () => {
it('reads the attribute text with Number, origin offsets kept', () => {
expect(parseFrame('-50 20 40 30')).toEqual(f(-50, 20, 40, 30))
expect(parseFrame('0,0,10.5,10')).toEqual(f(0, 0, 10.5, 10))
expect(parseFrame('743.0451049804688 0 10 10')?.x).toBe(743.0451049804688)
})
it('gives no frame for anything but four finite numbers over a positive span', () => {
for (const text of [
null,
'',
'0 0 100',
'0 0 a 5',
'0 0 0 5',
'0 0 5 -1',
'0 0 Infinity 5',
]) {
expect(parseFrame(text)).toBeNull()
}
})
})
describe('defaultRadius', () => {
it('is min(12 + 0.03 max, min / 2) floored to 0.01', () => {
expect(defaultRadius(f(0, 0, 40, 30))).toBe(13.2)
expect(defaultRadius(f(0, 0, 500, 380))).toBe(27)
expect(defaultRadius(f(0, 0, 20, 20))).toBe(10)
expect(defaultRadius(f(0, 0, 1000, 30))).toBe(15)
expect(defaultRadius(f(0, 0, 1, 1))).toBe(0.5)
expect(defaultRadius(f(0, 0, 0.0299, 5))).toBe(0.01)
})
})
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({
kind: 'circle',
circle: { cx: 20, cy: 15.01, r: 13.2 },
shrunk: false,
})
expect(proposePoint(frame, { x: 13.2, y: 15 })).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({
kind: 'circle',
circle: { cx: 13.19, cy: 15, r: 13.19 },
shrunk: true,
})
expect(proposePoint(frame, { x: 5.555, y: 15 })).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({
kind: 'outside',
})
})
})
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({
cx: 13.2,
cy: 13.2,
r: 13.2,
})
expect(correctedCircle(f(0, 0, 40, 30), { x: 40, y: 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,
)
})
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({
cx: 10.003,
cy: 10.004,
r: 10,
})
})
})
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({
cx: -30,
cy: 35,
r: 13.2,
})
expect(centreTriangle(f(0, 0, 100, 50))).toEqual([
[50, 22.5],
[52.5, 27.5],
[47.5, 27.5],
])
})
})
describe('correctionFor', () => {
const frame = f(0, 0, 40, 30)
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']),
).not.toBeNull()
const edge = { cx: 5, cy: 15, r: 13.2 }
expect(
correctionFor(frame, edge, ['annotation.geometry.out_of_frame'])?.circle
.cx,
).toBe(13.2)
expect(
correctionFor(frame, { cx: 20, cy: 15, r: 13.2 }, [
'annotation.geometry.too_small',
]),
).toBeNull()
})
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']),
).toBeNull()
})
})
const FRAMES = [
'0 0 40 30',
'0 0 20 20',
'0 0 100 300',
'0 0 500 380',
'0 0 640 480',
'0 0 200 150',
'0 0 4000 200',
'-50 20 40 30',
'0.003 0.004 20 20',
'1040000 1040000 8000 6000',
]
function clicksOf(frame: Frame) {
const r0 = defaultRadius(frame)
const mid = { x: frame.x + frame.w / 2, y: frame.y + frame.h / 2 }
const right = frame.x + frame.w
const bottom = frame.y + frame.h
return [
{ x: frame.x, y: frame.y },
{ x: right, y: frame.y },
{ x: frame.x, y: bottom },
{ x: right, y: bottom },
{ x: mid.x, y: frame.y },
{ x: mid.x, y: bottom },
{ x: frame.x, y: mid.y },
{ x: right, y: mid.y },
mid,
{ x: frame.x + r0, y: mid.y },
{ x: frame.x + r0 - 0.005, y: mid.y },
]
}
describe.each(FRAMES)('a point placed in the frame %s', (viewBox) => {
it('is created or corrected for every click, and the core never keeps an invalid one', () => {
const frame = frameOf(bed(viewBox))!
for (const click of clicksOf(frame)) {
const b = bed(viewBox)
const proposal = proposePoint(frame, click)
expect(proposal.kind).not.toBe('outside')
if (proposal.kind === 'outside') continue
const insert = circleInserter(b)
const attempt = attemptPoint(frame, proposal.circle, insert)
expect(attempt).toMatchObject({ status: 'created' })
expect(currentErrors(b).size).toBe(0)
}
})
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 })
if (proposal.kind !== 'corrected')
throw new Error('corner must be corrected')
const b = bed(viewBox)
const correction = {
click: { x: frame.x, y: frame.y },
circle: proposal.circle,
}
expect(b.preview.state()).toMatchObject({ ok: true })
expect(b.state.document.annotations).toHaveLength(0)
expect(confirmCorrection(correction, circleInserter(b))).toMatchObject({
status: 'created',
})
const kept = b.preview.state()
if (!kept.ok) throw new Error('state')
expect(kept.value.document.annotations[0].geometry).toEqual({
type: 'circle',
...proposal.circle,
})
})
})
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(frame, proposal.circle, circleInserter(b))
expect(attempt.status).toBe('refused')
expect(b.preview.document).toBe(b.text)
})
})
describe('a detail crop', () => {
const crop = { min_x: 100, min_y: 200, width: 100, height: 300 }
const cropBed = () => bed('0 0 1000 1000', withCrop(drawnText(), crop))
it('takes F from the composed root of the crop, origin offsets kept', () => {
expect(frameOf(cropBed(), 'view-det')).toEqual(f(100, 200, 100, 300))
})
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 })
if (inside.kind !== 'circle') throw new Error('proposal')
const insert = circleInserter(b, 'view-det')
expect(attemptPoint(frame, inside.circle, insert)).toMatchObject({
status: 'created',
})
expect(proposePoint(frame, { x: 99, y: 350 })).toEqual({ kind: 'outside' })
const edge = proposePoint(frame, { x: 100, y: 200 })
expect(edge.kind).toBe('corrected')
expect(currentErrors(b).size).toBe(0)
})
})