// T9 (#72): the drag state machine at the reducer seam (DESIGN 7.5, 5.5, 6.4). Every test drives // the same events the drag render module and the socket produce, then reads what the token renderer // would draw. No DOM, no GPU, no socket. import { describe, expect, it } from 'vitest' import { initialState, reduce } from '../src/reducer.ts' import type { ReducerEvent, RootState } from '../src/reducer.ts' import { parseServerFrame } from '../src/protocol.ts' import { tokenOf, tokensOf } from '../src/slices/tokens.ts' import type { TokenView } from '../src/slices/tokens.ts' import { draggableAt, draggableTokens, dragOf, drawPositionOf, moveOf, } from '../src/slices/move.ts' const frame = (body: Record): ReducerEvent => { const parsed = parseServerFrame(JSON.stringify(body)) if (!parsed) throw new Error(`bad test frame ${JSON.stringify(body)}`) return { type: 'frame', frame: parsed } } const hello = (n = 1): ReducerEvent => frame({ t: 'hello', n, proto: 2, role: 'player', me: { id: 2, name: 'Ann' } }) const token = (n: number, id: string, overrides: Record = {}): ReducerEvent => frame({ t: 'token', n, id, scene: 1, name: 'Ann', pos: [400, 400], radius: 20, mine: true, statuses: [], props: {}, ...overrides, }) const ack = (n: number, reqId: number, ok: boolean, extra: Record = {}): ReducerEvent => frame({ t: 'ack', n, reqId, ok, ...extra }) const run = (...events: ReducerEvent[]): RootState => events.reduce((state, event) => reduce(state, event), initialState()) /** The initial live view: one controlled token at 400,400 and one another player controls. */ const live = (): ReducerEvent[] => [ { type: 'connecting' }, hello(), token(2, 'own'), token(3, 'other', { mine: false, pos: [800, 400] }), ] describe('optimistic drag (stories 38, 39, 41)', () => { it('draws the token under the pointer and ignores authoritative frames while dragging', () => { let state = run(...live()) state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [450, 430] }) expect(drawPositionOf(state, 'own')).toEqual([450, 430]) expect(dragOf(state)?.id).toBe('own') // Story 41: a slow ack/authoritative frame must not move the sprite under the finger. state = reduce(state, token(4, 'own', { pos: [400, 400] })) expect(drawPositionOf(state, 'own')).toEqual([450, 430]) // The reducer still keeps what the server said; only drawing is overridden. expect(tokenOf(state, 'own')?.pos).toEqual([400, 400]) }) it('tracks the last pointer position; the input module decides which tokens may start a drag', () => { let state = run(...live()) // The slice is a mechanical state machine: `draggableAt` (tested below) is the permission gate, // and the server still refuses a move of someone else's token (story 39) with a nack. expect(draggableAt(tokensOf(state), 'player', [800, 400])).toBeNull() state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [410, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [420, 410] }) expect(drawPositionOf(state, 'own')).toEqual([420, 410]) // A drag-move for another id is ignored while this drag is active. state = reduce(state, { type: 'drag-move', id: 'other', pos: [820, 400] }) expect(dragOf(state)?.id).toBe('own') expect(drawPositionOf(state, 'other')).toBeNull() }) }) describe('ack reconciliation and rollback (stories 39, 40, 42)', () => { it('holds the predicted position until the authoritative frame after an ok ack', () => { let state = run(...live()) state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [460, 400] }) state = reduce(state, { type: 'move-sent', reqId: 7, id: 'own', pos: [460, 400] }) state = reduce(state, { type: 'drag-end', id: 'own', pos: [460, 400] }) expect(dragOf(state)).toBeNull() expect(drawPositionOf(state, 'own')).toEqual([460, 400]) // The ack precedes the token frame (settleLocked): keep drawing the confirmed position. state = reduce(state, ack(5, 7, true, { pos: [460, 400] })) expect(moveOf(state).pending).toEqual([]) expect(drawPositionOf(state, 'own')).toEqual([460, 400]) expect(moveOf(state).accepted).toBe(1) expect(moveOf(state).rollbacks).toEqual({}) // The frame lands: the prediction is no longer needed. state = reduce(state, token(6, 'own', { pos: [460, 400] })) expect(drawPositionOf(state, 'own')).toBeNull() }) it('rolls back a uniform nack while idle, and clears the tween when the renderer is done', () => { let state = run(...live()) state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [640, 400] }) state = reduce(state, { type: 'move-sent', reqId: 8, id: 'own', pos: [640, 400] }) state = reduce(state, { type: 'drag-end', id: 'own', pos: [640, 400] }) // The uniform nack (5.5): ok:false, the old position, no code. state = reduce(state, ack(5, 8, false, { pos: [400, 400] })) const move = moveOf(state) expect(move.pending).toEqual([]) expect(move.refused).toBe(1) expect(move.rollbackCount).toBe(1) const rollback = move.rollbacks['own'] expect(rollback).toBeDefined() expect(rollback).toMatchObject({ id: 'own', from: [640, 400], to: [400, 400] }) // No pending intent remains, so the draw falls through to the authoritative frame. expect(drawPositionOf(state, 'own')).toBeNull() state = reduce(state, { type: 'rollback-done', id: 'own', seq: rollback!.seq }) expect(moveOf(state).rollbacks).toEqual({}) // A stale completion cannot clear a newer tween. state = reduce(state, ack(6, 8, false, { pos: [400, 400] })) expect(moveOf(state).rollbacks).toEqual({}) }) it('does not snap under the finger: a nack while dragging rolls back on release', () => { let state = run(...live()) state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [600, 400] }) state = reduce(state, { type: 'move-sent', reqId: 9, id: 'own', pos: [600, 400] }) state = reduce(state, ack(5, 9, false, { pos: [400, 400] })) // Still under the pointer, still at the pointer; the snap is remembered for release. expect(drawPositionOf(state, 'own')).toEqual([600, 400]) expect(moveOf(state).snap).toEqual({ id: 'own', pos: [400, 400] }) expect(moveOf(state).rollbacks).toEqual({}) state = reduce(state, { type: 'drag-move', id: 'own', pos: [610, 410] }) state = reduce(state, { type: 'drag-end', id: 'own', pos: [610, 410] }) // The release sends the final position; its nack starts the tween from where the finger was. state = reduce(state, { type: 'move-sent', reqId: 10, id: 'own', pos: [610, 410] }) state = reduce(state, ack(6, 10, false, { pos: [400, 400] })) expect(moveOf(state).rollbacks['own']).toMatchObject({ from: [610, 410], to: [400, 400], }) expect(moveOf(state).snap).toBeNull() }) it('lets a later accepted intent win over an earlier refusal', () => { let state = run(...live()) state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [800, 400] }) state = reduce(state, { type: 'move-sent', reqId: 11, id: 'own', pos: [800, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [500, 400] }) state = reduce(state, { type: 'move-sent', reqId: 12, id: 'own', pos: [500, 400] }) // The first (over-cap) intent is refused while the second is in flight: no tween yet. state = reduce(state, ack(5, 11, false, { pos: [400, 400] })) expect(moveOf(state).rollbacks).toEqual({}) expect(moveOf(state).snap).toBeNull() expect(drawPositionOf(state, 'own')).toEqual([500, 400]) state = reduce(state, ack(6, 12, true, { pos: [500, 400] })) expect(moveOf(state).accepted).toBe(1) expect(moveOf(state).refused).toBe(0) expect(moveOf(state).rollbacks).toEqual({}) }) it('drops a rate-limited intent quietly and reconciles with the next frame (story 42)', () => { let state = run(...live()) state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [430, 400] }) state = reduce(state, { type: 'move-sent', reqId: 13, id: 'own', pos: [430, 400] }) state = reduce(state, { type: 'drag-end', id: 'own', pos: [430, 400] }) // 6.4: refused by the bucket, no position, no rollback, connection stays open. state = reduce(state, ack(5, 13, false, { code: 'rate-limited' })) expect(moveOf(state).rateLimited).toBe(1) expect(moveOf(state).refused).toBe(0) expect(moveOf(state).rollbacks).toEqual({}) expect(drawPositionOf(state, 'own')).toBeNull() // The next authoritative frame is the reconciliation; a drag continues from it, not from a // corrupted map. state = reduce(state, token(6, 'own', { pos: [420, 400] })) expect(tokenOf(state, 'own')?.pos).toEqual([420, 400]) state = reduce(state, { type: 'drag-start', id: 'own', pos: [420, 400] }) state = reduce(state, { type: 'drag-move', id: 'own', pos: [440, 400] }) expect(drawPositionOf(state, 'own')).toEqual([440, 400]) }) it('ignores acks for intents this slice does not own', () => { let state = run(...live()) state = reduce(state, ack(5, 99, false, { pos: [1, 1] })) expect(moveOf(state).refused).toBe(0) expect(moveOf(state).pending).toEqual([]) }) }) describe('pending map lifecycle (6.3)', () => { it('drops every pending reqId, drag and tween when the connection closes', () => { let state = run(...live()) state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, { type: 'move-sent', reqId: 20, id: 'own', pos: [430, 400] }) state = reduce(state, { type: 'drag-end', id: 'own', pos: [430, 400] }) state = reduce(state, { type: 'move-sent', reqId: 21, id: 'own', pos: [440, 400] }) expect(moveOf(state).pending).toHaveLength(2) state = reduce(state, { type: 'close', reason: null, code: 1006 }) const move = moveOf(state) expect(move.pending).toEqual([]) expect(move.dropped).toBe(2) expect(move.drag).toBeNull() expect(move.rollbacks).toEqual({}) expect(drawPositionOf(state, 'own')).toBeNull() }) it('drops pending intents on hello (the full resync resets every slice)', () => { let state = run(...live()) state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, { type: 'move-sent', reqId: 30, id: 'own', pos: [430, 400] }) state = reduce(state, hello(10)) expect(moveOf(state).pending).toEqual([]) expect(moveOf(state).drag).toBeNull() expect(drawPositionOf(state, 'own')).toBeNull() }) it('stops a drag when its token leaves the view', () => { let state = run(...live()) state = reduce(state, { type: 'drag-start', id: 'own', pos: [400, 400] }) state = reduce(state, frame({ t: 'token_gone', n: 4, id: 'own' })) expect(moveOf(state).drag).toBeNull() expect(drawPositionOf(state, 'own')).toBeNull() }) }) describe('hit test and permission (stories 38, 43)', () => { const view = (id: string, mine: boolean, pos: [number, number]): TokenView => ({ id, scene: 1, name: id, pos, radius: 20, mine, statuses: [], props: {}, gmOnly: false, kind: null, controller: null, characterId: null, }) const views = [view('a', true, [100, 100]), view('b', false, [200, 100])] it('picks the topmost token under the pointer that the role may move', () => { expect(draggableAt(views, 'player', [100, 100])?.id).toBe('a') expect(draggableAt(views, 'player', [200, 100])).toBeNull() expect(draggableAt(views, 'gm', [200, 100])?.id).toBe('b') expect(draggableAt(views, 'player', [150, 150])).toBeNull() expect(draggableAt(views, null, [100, 100])).toBeNull() }) it('lists what the session may drag: its own tokens, or every token for the GM', () => { let state = run(...live()) expect(draggableTokens(state).map(item => item.id)).toEqual(['own']) state = run( { type: 'connecting' }, frame({ t: 'hello', n: 1, proto: 2, role: 'gm', me: { id: 1, name: 'GM' } }), token(2, 'own', { mine: false }), token(3, 'other', { mine: false }), ) expect(tokensOf(state)).toHaveLength(2) expect(draggableTokens(state).map(item => item.id)).toEqual(['own', 'other']) }) })