Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inject.test.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Unit test for the input mapping logic — runs without a display or nut-js.
  2. // Verifies keymap returns sane shapes and inject() no-ops gracefully.
  3. const inject = require('./inject');
  4. const assert = require('assert');
  5. let pass = 0;
  6. function check(name, cond) {
  7. assert.ok(cond, name);
  8. console.log(' ok -', name);
  9. pass++;
  10. }
  11. (async () => {
  12. console.log('input layer tests:');
  13. check('module exposes inject/releaseAll/available', typeof inject.inject === 'function' && typeof inject.releaseAll === 'function');
  14. check('available is boolean', typeof inject.available === 'boolean');
  15. // mapKey returns null when nut-js absent (sandbox) — that is expected & safe.
  16. if (!inject.available) {
  17. check('mapKey is null-safe without nut-js', inject.mapKey('a', 'KeyA') === null);
  18. } else {
  19. check('mapKey letter -> array', Array.isArray(inject.mapKey('a', 'KeyA')));
  20. check('mapKey Enter -> array', Array.isArray(inject.mapKey('Enter', 'Enter')));
  21. check('mapKey symbol -> type fallback', inject.mapKey('@', 'Digit2').type === '@');
  22. }
  23. // inject() must not throw on any event kind, even with no backend.
  24. for (const evt of [
  25. { kind: 'mousemove', x: 0.5, y: 0.5 },
  26. { kind: 'mousedown', button: 0, x: 0.1, y: 0.2 },
  27. { kind: 'mouseup', button: 0 },
  28. { kind: 'dblclick', x: 0.3, y: 0.3 },
  29. { kind: 'scroll', dx: 0, dy: 120 },
  30. { kind: 'keydown', key: 'a', code: 'KeyA' },
  31. { kind: 'keyup', key: 'a', code: 'KeyA' },
  32. ]) {
  33. await inject.inject(evt);
  34. }
  35. check('inject handled all event kinds without throwing', true);
  36. await inject.releaseAll();
  37. check('releaseAll clears state', true);
  38. console.log(`\n${pass} checks passed.`);
  39. })();