/** * @file wheelReports.test.ts * @description A touchpad scrolls 1:1 with the finger (one report per * LINES_PER_REPORT rows, remainder carried), a wheel notch always sends one. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect } from "vitest"; import { LINES_PER_REPORT, SCROLL_SPEED, wheelReports, type WheelState } from "../wheelReports"; const ROW = 20; const px = (deltaY: number) => ({ deltaY, deltaMode: 0 }); describe("wheelReports", () => { it("adds small touchpad steps up to one report per LINES_PER_REPORT rows", () => { const s: WheelState = { partial: 0 }; let sent = 0; // 110 steps of 10px = 1100px = 55 rows, scrolled at SCROLL_SPEED. for (let i = 0; i < 110; i++) sent += wheelReports(s, px(10), ROW, 40); expect(sent).toBe(Math.trunc((55 * SCROLL_SPEED) / LINES_PER_REPORT)); }); it("drops the carried remainder when the swipe turns around", () => { const s: WheelState = { partial: 0 }; wheelReports(s, px(40), ROW, 40); // 2 rows down, nothing sent yet let sent = 0; // 8 rows up = 5.6 scrolled: one report, the 2 rows down forgotten. for (let i = 0; i < 8; i++) sent += wheelReports(s, px(-20), ROW, 40); expect(sent).toBe(-1); }); it("sends at least one report for a wheel notch, in either delta mode", () => { expect(wheelReports({ partial: 0 }, px(100), ROW, 40)).toBe(1); expect(wheelReports({ partial: 0 }, px(-100), ROW, 40)).toBe(-1); expect(wheelReports({ partial: 0 }, { deltaY: 3, deltaMode: 1 }, ROW, 40)).toBe(1); expect(wheelReports({ partial: 0 }, { deltaY: 1, deltaMode: 2 }, ROW, 40)).toBe(6); // 8 × 0.7 }); it("slows a run of wheel notches to SCROLL_SPEED of a report each", () => { const s: WheelState = { partial: 0 }; let sent = 0; for (let i = 0; i < 10; i++) sent += wheelReports(s, px(100), ROW, 40); expect(sent).toBe(10 * SCROLL_SPEED); // Turning around starts from zero: the first notch up moves at once. expect(wheelReports(s, px(-100), ROW, 40)).toBe(-1); }); it("sends nothing for a horizontal-only event or an unmeasured terminal", () => { expect(wheelReports({ partial: 0 }, px(0), ROW, 40)).toBe(0); expect(wheelReports({ partial: 0 }, px(10), 0, 40)).toBe(0); }); });