56 lines
2.1 KiB
JavaScript
56 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import {
|
|
wheelDeltaFromInput,
|
|
containsMouseSequence,
|
|
stripMouseSequences,
|
|
} from '../src/ui/mouse.js';
|
|
|
|
test('decodes SGR wheel direction', () => {
|
|
assert.equal(wheelDeltaFromInput('\x1b[<64;88;20M'), -1);
|
|
assert.equal(wheelDeltaFromInput('[<65;88;20M'), 1);
|
|
// Some terminals add modifier bits to the button code.
|
|
assert.equal(wheelDeltaFromInput('[<96;88;20M'), -1);
|
|
assert.equal(wheelDeltaFromInput('[<97;88;20M'), 1);
|
|
});
|
|
|
|
test('decodes legacy X10 wheel direction', () => {
|
|
const x10Up = `\x1b[M${String.fromCharCode(96)}!#`;
|
|
const x10Down = `\x1b[M${String.fromCharCode(97)}!#`;
|
|
assert.equal(wheelDeltaFromInput(x10Up), -1);
|
|
assert.equal(wheelDeltaFromInput(x10Down), 1);
|
|
assert.equal(wheelDeltaFromInput(x10Up.slice(1)), -1, 'accepts Ink-stripped ESC');
|
|
});
|
|
|
|
test('ignores non-wheel and incomplete mouse input', () => {
|
|
assert.equal(wheelDeltaFromInput('[<0;1;1M'), 0);
|
|
assert.equal(wheelDeltaFromInput('[<64;1;1'), 0);
|
|
assert.equal(wheelDeltaFromInput('hello'), 0);
|
|
});
|
|
|
|
test('removes mouse reports without dropping surrounding text', () => {
|
|
const value = 'before\x1b[<65;88;20Mafter';
|
|
assert.equal(containsMouseSequence(value), true);
|
|
assert.equal(stripMouseSequences(value), 'beforeafter');
|
|
assert.equal(stripMouseSequences('plain text'), 'plain text');
|
|
|
|
const x10 = `\x1b[M${String.fromCharCode(96)}!#`;
|
|
assert.equal(containsMouseSequence(x10), true);
|
|
assert.equal(stripMouseSequences(`before${x10}after`), 'beforeafter');
|
|
});
|
|
|
|
test('reassembles an SGR wheel report split across input events', () => {
|
|
assert.equal(wheelDeltaFromInput('[<64;'), 0);
|
|
assert.equal(wheelDeltaFromInput('88;20M'), -1);
|
|
// A normal key clears any stale partial sequence state.
|
|
assert.equal(wheelDeltaFromInput('x'), 0);
|
|
});
|
|
|
|
test('removes split SGR fragments from a text-input value', () => {
|
|
assert.equal(stripMouseSequences('before[<64;'), 'before');
|
|
assert.equal(stripMouseSequences('before88;20M'), 'before');
|
|
});
|