Files
faunosteo/apps/app/tests/unit/editor-frame.test.ts
T
bchanot 8cd1bda6c2 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.
2026-11-17 18:20:58 +01:00

436 lines
13 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** 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 }, defaultRadius(frame)),
).toEqual({
kind: 'circle',
circle: { cx: 20, cy: 15.01, r: 13.2 },
shrunk: false,
})
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 }, defaultRadius(frame)),
).toEqual({
kind: 'circle',
circle: { cx: 13.19, cy: 15, r: 13.19 },
shrunk: true,
})
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 }, 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',
})
})
})
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 },
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 },
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 },
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 }, defaultRadius(frame)),
).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), defaultRadius(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'],
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,
defaultRadius(frame),
)?.circle.cx,
).toBe(13.2)
expect(
correctionFor(
frame,
{ cx: 20, cy: 15, r: 13.2 },
['annotation.geometry.too_small'],
13.2,
defaultRadius(frame),
),
).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'],
13.2,
defaultRadius(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, defaultRadius(frame))
expect(proposal.kind).not.toBe('outside')
if (proposal.kind === 'outside') continue
const insert = circleInserter(b)
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)
}
})
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 },
defaultRadius(frame),
)
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 },
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)
})
})
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 }, defaultRadius(frame))
if (inside.kind !== 'circle') throw new Error('proposal')
const insert = circleInserter(b, 'view-det')
expect(
attemptPoint(frame, inside.circle, insert, {
proposed: inside.circle.r,
wanted: defaultRadius(frame),
}),
).toMatchObject({
status: 'created',
})
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)
})
})
describe('a radius the user typed (H5)', () => {
const frame = f(0, 0, 40, 30)
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, 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', () => {
const b = bed('0 0 5000 5000')
const big = frameOf(b)!
const attempt = attemptPoint(
big,
{ cx: 2500, cy: 2500, r: 1 },
circleInserter(b),
{ proposed: defaultRadius(big), wanted: defaultRadius(big) },
)
expect(attempt).toMatchObject({ status: 'refused' })
expect(b.preview.document).toBe(b.text)
})
})
describe('the keyboard point (H4)', () => {
it.each([
'0.003 0.004 20 20',
'0 0 20 20',
'-50 20 40 30',
'0.003 0.004 1000 30',
])('fits the frame %s by construction, with no correction', (viewBox) => {
const b = bed(viewBox)
const frame = frameOf(b)!
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), {
proposed: circle.r,
wanted: defaultRadius(frame),
})
expect(attempt.status).not.toBe('correction')
if (viewBox.endsWith('1000 30')) return
expect(attempt).toMatchObject({ status: 'created' })
})
})
describe.each(['0 0 1000 30', '0 0 4000 200'])(
'the elongated frame %s (H8)',
(viewBox) => {
it('gives the core the verdict on every edge and corner click, keeping nothing invalid', () => {
const frame = frameOf(bed(viewBox))!
for (const click of clicksOf(frame)) {
const b = bed(viewBox)
const proposal = proposePoint(frame, click, defaultRadius(frame))
if (proposal.kind === 'outside') continue
const attempt = attemptPoint(
frame,
proposal.circle,
circleInserter(b),
{ proposed: proposal.circle.r, wanted: defaultRadius(frame) },
)
expect(['created', 'refused', 'correction']).toContain(attempt.status)
expect(currentErrors(b).size).toBe(0)
if (attempt.status !== 'created')
expect(b.preview.document).toBe(b.text)
}
})
},
)