/** U2 Task 2: the press machine, the editor's one decider for clicks, drags and taps. */ import { describe, expect, it } from 'vitest' import { clickAction, dragAction, isDoubleClick, nearestHandle, pressTarget, stepPress, type DragContext, type Hit, type Press, type PressEvent, type PressTarget, type Selection, } from '@/editor/annotations/press' const NONE: Selection = { selectedId: null, chosenVertex: null } const ZONE: Selection = { selectedId: 'zone', chosenVertex: null } describe('nearestHandle (R-9)', () => { it('takes the nearest of three overlapping hit circles (an 11 px segment)', () => { const hits: Hit[] = [ { kind: 'handle', handle: { kind: 'vertex', index: 0 }, centre: { x: 0, y: 0 }, }, { kind: 'handle', handle: { kind: 'insert', after: 0 }, centre: { x: 5.5, y: 0 }, }, { kind: 'handle', handle: { kind: 'vertex', index: 1 }, centre: { x: 11, y: 0 }, }, ] expect(nearestHandle(hits, { x: 9, y: 0 })).toEqual({ kind: 'vertex', index: 1, }) expect(nearestHandle(hits, { x: 5, y: 1 })).toEqual({ kind: 'insert', after: 0, }) expect(nearestHandle(hits, { x: 1, y: 0 })).toEqual({ kind: 'vertex', index: 0, }) }) it('at a tie a vertex beats « − », which beats « + »', () => { const at = { x: 0, y: 0 } const remove: Hit = { kind: 'handle', handle: { kind: 'remove', index: 2 }, centre: { x: 3, y: 0 }, } const insert: Hit = { kind: 'handle', handle: { kind: 'insert', after: 2 }, centre: { x: -3, y: 0 }, } const vertex: Hit = { kind: 'handle', handle: { kind: 'vertex', index: 2 }, centre: { x: 0, y: 3 }, } expect(nearestHandle([insert, remove, vertex], at)).toEqual(vertex.handle) expect(nearestHandle([insert, remove], at)).toEqual(remove.handle) expect(nearestHandle([remove, insert], at)).toEqual(remove.handle) }) it('answers null without a handle', () => { expect( nearestHandle([{ kind: 'annotation', id: 'a' }], { x: 0, y: 0 }), ).toBeNull() }) }) describe('pressTarget (board 32)', () => { const at = { x: 0, y: 0 } it('a selected zone hit first, a point hitbox under it: the interior', () => { const hits: Hit[] = [ { kind: 'annotation', id: 'zone' }, { kind: 'annotation', id: 'point' }, ] expect(pressTarget(hits, at, ZONE)).toEqual({ kind: 'interior', id: 'zone', }) }) it('a point twin hit first, the selected zone under it: the other one', () => { const hits: Hit[] = [ { kind: 'annotation', id: 'point' }, { kind: 'annotation', id: 'zone' }, ] expect(pressTarget(hits, at, ZONE)).toEqual({ kind: 'other', id: 'point' }) }) it('a handle wins over every annotation; nothing is empty', () => { const hits: Hit[] = [ { kind: 'annotation', id: 'point' }, { kind: 'handle', handle: { kind: 'vertex', index: 1 }, centre: at }, ] expect(pressTarget(hits, at, ZONE)).toEqual({ kind: 'handle', handle: { kind: 'vertex', index: 1 }, }) expect(pressTarget([], at, ZONE)).toEqual({ kind: 'empty' }) }) }) describe('clickAction (A1)', () => { const handle = (h: PressTarget & { kind: 'handle' }) => h it('selects another, deselects on empty', () => { expect(clickAction({ kind: 'other', id: 'b' }, ZONE)).toEqual({ type: 'select', id: 'b', }) expect(clickAction({ kind: 'empty' }, ZONE)).toEqual({ type: 'deselect' }) }) it('the interior opens without a chosen vertex, unchooses with one', () => { const interior: PressTarget = { kind: 'interior', id: 'zone' } expect(clickAction(interior, ZONE)).toEqual({ type: 'open', id: 'zone' }) expect(clickAction(interior, { ...ZONE, chosenVertex: 2 })).toEqual({ type: 'unchoose', }) }) it('maps each handle, and a point handle opens the selection', () => { const on = (h: Parameters[0]['handle']) => clickAction(handle({ kind: 'handle', handle: h }), ZONE) expect(on({ kind: 'vertex', index: 3 })).toEqual({ type: 'choose_vertex', index: 3, }) expect(on({ kind: 'insert', after: 1 })).toEqual({ type: 'insert', after: 1, }) expect(on({ kind: 'remove', index: 2 })).toEqual({ type: 'remove', index: 2, }) expect(on({ kind: 'point' })).toEqual({ type: 'open', id: 'zone' }) expect( clickAction({ kind: 'handle', handle: { kind: 'point' } }, NONE), ).toEqual({ type: 'deselect', }) }) }) describe('isDoubleClick (R-8)', () => { const last = { at: { x: 0, y: 0 }, time: 1000 } it('holds within 400 ms and 8 px, edges included', () => { expect(isDoubleClick(last, { x: 8, y: 0 }, 1400)).toBe(true) expect(isDoubleClick(last, { x: 8.01, y: 0 }, 1400)).toBe(false) expect(isDoubleClick(last, { x: 0, y: 0 }, 1400.5)).toBe(false) expect(isDoubleClick(null, { x: 0, y: 0 }, 1000)).toBe(false) }) }) describe('dragAction (A13)', () => { const ctx = (over: Partial = {}): DragContext => ({ kind: 'mouse', movable: true, moveMode: false, ...over, }) const interior: PressTarget = { kind: 'interior', id: 'zone' } const point: PressTarget = { kind: 'handle', handle: { kind: 'point' } } it('a move mode drags whatever the target or the pointer', () => { for (const kind of ['mouse', 'pen', 'touch'] as const) { for (const target of [ interior, point, { kind: 'empty' } as PressTarget, ]) { expect(dragAction(target, ctx({ kind, moveMode: true }))).toEqual({ type: 'drag_mode', }) } } }) it('a finger moves only what it holds: off a handle it is futile', () => { const touch = ctx({ kind: 'touch' }) expect(dragAction(point, touch)).toEqual({ type: 'drag_point' }) expect( dragAction( { kind: 'handle', handle: { kind: 'vertex', index: 4 } }, touch, ), ).toEqual({ type: 'drag_vertex', index: 4, }) expect(dragAction(interior, touch)).toEqual({ type: 'none', futile: true }) expect(dragAction({ kind: 'empty' }, touch)).toEqual({ type: 'none', futile: true, }) }) it('a mouse or pen drags the movable interior, pans otherwise', () => { expect(dragAction(interior, ctx())).toEqual({ type: 'drag_shape', id: 'zone', }) expect(dragAction(interior, ctx({ kind: 'pen' }))).toEqual({ type: 'drag_shape', id: 'zone', }) expect(dragAction(interior, ctx({ movable: false }))).toEqual({ type: 'pan', }) expect(dragAction({ kind: 'other', id: 'b' }, ctx())).toEqual({ type: 'pan', }) expect(dragAction({ kind: 'empty' }, ctx())).toEqual({ type: 'pan' }) }) it('« + » and « − » never drag; an unmovable handle moves nothing', () => { for (const kind of ['mouse', 'touch'] as const) { expect( dragAction( { kind: 'handle', handle: { kind: 'insert', after: 0 } }, ctx({ kind }), ), ).toEqual({ type: 'none', futile: false }) expect( dragAction( { kind: 'handle', handle: { kind: 'remove', index: 0 } }, ctx({ kind }), ), ).toEqual({ type: 'none', futile: false }) } expect(dragAction(point, ctx({ movable: false }))).toEqual({ type: 'none', futile: false, }) }) }) describe('stepPress (R-8)', () => { const ctx = { movable: true, moveMode: false } const target: PressTarget = { kind: 'interior', id: 'zone' } const down = ( pointerId: number, pointers: number, kind = 'mouse' as const, ): PressEvent => ({ type: 'down', pointerId, kind, at: { x: 0, y: 0 }, target, pointers, }) const move = (pointerId: number, x: number): PressEvent => ({ type: 'move', pointerId, at: { x, y: 0 }, }) const up = (pointerId: number, pointers: number): PressEvent => ({ type: 'up', pointerId, pointers, }) /** Runs the events from idle; answers the effects and the last press. */ function run(events: PressEvent[]) { let press: Press = { phase: 'idle' } const effects = events.map((event) => { const step = stepPress(press, event, ctx) press = step.press return step.effect }) return { press, effects } } it('a single press within the slop is a click', () => { const { press, effects } = run([down(1, 1), move(1, 4), up(1, 0)]) expect(effects).toEqual([null, null, { type: 'click', target }]) expect(press).toEqual({ phase: 'idle' }) }) it('past the slop it starts a drag, then ends it', () => { const action = { type: 'drag_shape', id: 'zone' } const { press, effects } = run([ down(1, 1), move(1, 5), move(1, 30), up(1, 0), ]) expect(effects).toEqual([ null, { type: 'drag_start', action }, null, { type: 'drag_end' }, ]) expect(press).toEqual({ phase: 'idle' }) }) it('the slop follows the pointer kind', () => { const finger = run([down(1, 1, 'touch' as never), move(1, 10), up(1, 0)]) expect(finger.effects[2]).toEqual({ type: 'click', target }) }) it('a second down while down is a gesture: no click on any up, idle at zero pointers', () => { const { press, effects } = run([down(1, 1), down(2, 2), up(1, 1), up(2, 0)]) expect(effects).toEqual([null, null, null, null]) expect(press).toEqual({ phase: 'idle' }) const held = run([down(1, 1), down(2, 2), up(2, 1)]) expect(held.press).toEqual({ phase: 'multi' }) }) it('a second down while dragging cancels the drag', () => { const { press, effects } = run([down(1, 1), move(1, 20), down(2, 2)]) expect(effects[2]).toEqual({ type: 'drag_cancel' }) expect(press).toEqual({ phase: 'multi' }) }) it('cancel while dragging cancels the drag; while down it is silent', () => { expect( run([down(1, 1), move(1, 20), { type: 'cancel' }]).effects[2], ).toEqual({ type: 'drag_cancel', }) const quiet = run([down(1, 1), { type: 'cancel' }, up(1, 0)]) expect(quiet.effects).toEqual([null, null, null]) }) it('ignores a move from another pointer id', () => { const { press, effects } = run([down(1, 1), move(2, 50), up(1, 0)]) expect(effects).toEqual([null, null, { type: 'click', target }]) expect(press).toEqual({ phase: 'idle' }) }) })