|
| 1 | +import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test' |
| 2 | + |
| 3 | +let nativePrewarmCalls = 0 |
| 4 | +let nativeReturnValue = false |
| 5 | +let nativeShouldThrow = false |
| 6 | + |
| 7 | +const nativeIsModifierPressed = mock((modifier: string) => { |
| 8 | + if (nativeShouldThrow) { |
| 9 | + throw new Error('native modifier failure') |
| 10 | + } |
| 11 | + return nativeReturnValue |
| 12 | +}) |
| 13 | + |
| 14 | +mock.module('modifiers-napi', () => ({ |
| 15 | + prewarm: async () => { |
| 16 | + nativePrewarmCalls++ |
| 17 | + }, |
| 18 | + isModifierPressed: nativeIsModifierPressed, |
| 19 | +})) |
| 20 | + |
| 21 | +const originalPlatform = process.platform |
| 22 | + |
| 23 | +async function loadModule() { |
| 24 | + return import(`../modifiers.ts?case=${Math.random()}`) |
| 25 | +} |
| 26 | + |
| 27 | +beforeEach(() => { |
| 28 | + nativePrewarmCalls = 0 |
| 29 | + nativeReturnValue = false |
| 30 | + nativeShouldThrow = false |
| 31 | + nativeIsModifierPressed.mockClear() |
| 32 | + Object.defineProperty(process, 'platform', { |
| 33 | + value: originalPlatform, |
| 34 | + configurable: true, |
| 35 | + }) |
| 36 | +}) |
| 37 | + |
| 38 | +afterEach(() => { |
| 39 | + Object.defineProperty(process, 'platform', { |
| 40 | + value: originalPlatform, |
| 41 | + configurable: true, |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +describe('src/utils/modifiers', () => { |
| 46 | + test('does not touch the native module on non-darwin', async () => { |
| 47 | + Object.defineProperty(process, 'platform', { |
| 48 | + value: 'win32', |
| 49 | + configurable: true, |
| 50 | + }) |
| 51 | + const mod = await loadModule() |
| 52 | + |
| 53 | + mod.prewarmModifiers() |
| 54 | + expect(nativePrewarmCalls).toBe(0) |
| 55 | + expect(mod.isModifierPressed('shift')).toBe(false) |
| 56 | + expect(nativeIsModifierPressed).not.toHaveBeenCalled() |
| 57 | + }) |
| 58 | + |
| 59 | + test('caches native prewarm after the first darwin call', async () => { |
| 60 | + Object.defineProperty(process, 'platform', { |
| 61 | + value: 'darwin', |
| 62 | + configurable: true, |
| 63 | + }) |
| 64 | + const mod = await loadModule() |
| 65 | + |
| 66 | + mod.prewarmModifiers() |
| 67 | + mod.prewarmModifiers() |
| 68 | + |
| 69 | + // prewarm is fire-and-forget async — flush microtasks |
| 70 | + await new Promise(resolve => setTimeout(resolve, 0)) |
| 71 | + expect(nativePrewarmCalls).toBe(1) |
| 72 | + }) |
| 73 | + |
| 74 | + test('forwards modifier checks to the native module on darwin', async () => { |
| 75 | + Object.defineProperty(process, 'platform', { |
| 76 | + value: 'darwin', |
| 77 | + configurable: true, |
| 78 | + }) |
| 79 | + nativeReturnValue = true |
| 80 | + const mod = await loadModule() |
| 81 | + |
| 82 | + expect(mod.isModifierPressed('shift')).toBe(true) |
| 83 | + expect(nativeIsModifierPressed).toHaveBeenCalledWith('shift') |
| 84 | + }) |
| 85 | + |
| 86 | + test('returns false when native modifier checks throw on darwin', async () => { |
| 87 | + Object.defineProperty(process, 'platform', { |
| 88 | + value: 'darwin', |
| 89 | + configurable: true, |
| 90 | + }) |
| 91 | + nativeShouldThrow = true |
| 92 | + const mod = await loadModule() |
| 93 | + |
| 94 | + expect(mod.isModifierPressed('shift')).toBe(false) |
| 95 | + }) |
| 96 | +}) |
0 commit comments