|
| 1 | +import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test' |
| 2 | + |
| 3 | +let ffiShouldThrow = false |
| 4 | +let nativeFlags = 0 |
| 5 | +let dlopenCalls = 0 |
| 6 | + |
| 7 | +mock.module('bun:ffi', () => ({ |
| 8 | + FFIType: { |
| 9 | + i32: 0, |
| 10 | + u64: 0, |
| 11 | + }, |
| 12 | + dlopen: () => { |
| 13 | + dlopenCalls++ |
| 14 | + if (ffiShouldThrow) { |
| 15 | + throw new Error('ffi load failed') |
| 16 | + } |
| 17 | + return { |
| 18 | + symbols: { |
| 19 | + CGEventSourceFlagsState: () => nativeFlags, |
| 20 | + }, |
| 21 | + } |
| 22 | + }, |
| 23 | +})) |
| 24 | + |
| 25 | +const originalPlatform = process.platform |
| 26 | + |
| 27 | +async function loadModule() { |
| 28 | + return import(`../index.ts?case=${Math.random()}`) |
| 29 | +} |
| 30 | + |
| 31 | +beforeEach(() => { |
| 32 | + ffiShouldThrow = false |
| 33 | + nativeFlags = 0 |
| 34 | + dlopenCalls = 0 |
| 35 | + Object.defineProperty(process, 'platform', { |
| 36 | + value: originalPlatform, |
| 37 | + configurable: true, |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +afterEach(() => { |
| 42 | + Object.defineProperty(process, 'platform', { |
| 43 | + value: originalPlatform, |
| 44 | + configurable: true, |
| 45 | + }) |
| 46 | +}) |
| 47 | + |
| 48 | +describe('modifiers-napi', () => { |
| 49 | + test('returns false for non-darwin platforms', async () => { |
| 50 | + Object.defineProperty(process, 'platform', { |
| 51 | + value: 'win32', |
| 52 | + configurable: true, |
| 53 | + }) |
| 54 | + const mod = await loadModule() |
| 55 | + |
| 56 | + await mod.prewarm() |
| 57 | + expect(dlopenCalls).toBe(0) |
| 58 | + expect(mod.isModifierPressed('shift')).toBe(false) |
| 59 | + expect(mod.isModifierPressed('command')).toBe(false) |
| 60 | + }) |
| 61 | + |
| 62 | + test('prewarm is idempotent on darwin', async () => { |
| 63 | + Object.defineProperty(process, 'platform', { |
| 64 | + value: 'darwin', |
| 65 | + configurable: true, |
| 66 | + }) |
| 67 | + const mod = await loadModule() |
| 68 | + |
| 69 | + await mod.prewarm() |
| 70 | + await mod.prewarm() |
| 71 | + |
| 72 | + expect(dlopenCalls).toBe(1) |
| 73 | + }) |
| 74 | + |
| 75 | + test('returns false when ffi loading fails on darwin', async () => { |
| 76 | + Object.defineProperty(process, 'platform', { |
| 77 | + value: 'darwin', |
| 78 | + configurable: true, |
| 79 | + }) |
| 80 | + ffiShouldThrow = true |
| 81 | + const mod = await loadModule() |
| 82 | + |
| 83 | + await mod.prewarm() |
| 84 | + expect(mod.isModifierPressed('shift')).toBe(false) |
| 85 | + }) |
| 86 | + |
| 87 | + test('returns false for unknown modifier names on darwin', async () => { |
| 88 | + Object.defineProperty(process, 'platform', { |
| 89 | + value: 'darwin', |
| 90 | + configurable: true, |
| 91 | + }) |
| 92 | + nativeFlags = 0x20000 |
| 93 | + const mod = await loadModule() |
| 94 | + |
| 95 | + await mod.prewarm() |
| 96 | + expect(mod.isModifierPressed('unknown')).toBe(false) |
| 97 | + }) |
| 98 | + |
| 99 | + test('uses native flag bits for known modifiers on darwin', async () => { |
| 100 | + Object.defineProperty(process, 'platform', { |
| 101 | + value: 'darwin', |
| 102 | + configurable: true, |
| 103 | + }) |
| 104 | + nativeFlags = 0x20000 | 0x40000 |
| 105 | + const mod = await loadModule() |
| 106 | + |
| 107 | + await mod.prewarm() |
| 108 | + expect(mod.isModifierPressed('shift')).toBe(true) |
| 109 | + expect(mod.isModifierPressed('control')).toBe(true) |
| 110 | + expect(mod.isModifierPressed('option')).toBe(false) |
| 111 | + }) |
| 112 | +}) |
0 commit comments