import test from 'node:test' import assert from 'node:assert/strict' import { TouchQueue } from '../src/lib/touch.ts' const event = (action, x=1) => ({ deviceId:'phone', action, x, y:2, width:576, height:1280 }) const tick = () => new Promise(resolve => setImmediate(resolve)) test('sends down immediately, coalesces moves and preserves up and subsequent down', async () => { const sent=[], releases=[] const q=new TouchQueue(e=>{sent.push(e);return new Promise(resolve=>releases.push(resolve))},assert.fail) q.push(event(0));assert.equal(sent[0].action,0) for(let x=1;x<=100;x++)q.push(event(2,x)) q.push(event(1,100));q.push(event(0,5));q.push(event(3,5)) for(let i=0;i<5;i++){releases.shift()?.();await tick()} assert.deepEqual(sent.map(e=>[e.action,e.x]),[[0,1],[2,100],[1,100],[0,5],[3,5]]) }) test('attempts cancel and clears pending gestures after a failed send', async () => { const sent=[],errors=[] const q=new TouchQueue(async e=>{sent.push(e.action);if(e.action===0)throw Error('closed')},e=>errors.push(e)) q.push(event(0));q.push(event(2));q.push(event(1));await tick() assert.deepEqual(sent,[0,3]);assert.equal(errors.length,1) })