feat: 添加 napi 包测试覆盖与 stub 改进

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
unraid
2026-04-22 22:38:10 +08:00
parent fb41513b32
commit eec961352b
4 changed files with 215 additions and 9 deletions

View File

@@ -14,14 +14,16 @@ const modifierFlags: Record<string, number> = {
const kCGEventSourceStateCombinedSessionState = 0;
let cgEventSourceFlagsState: ((stateID: number) => number) | null = null;
let ffiLoadAttempted = false;
function loadFFI(): void {
if (cgEventSourceFlagsState !== null || process.platform !== "darwin") {
async function loadFFI(): Promise<void> {
if (ffiLoadAttempted || process.platform !== "darwin") {
return;
}
ffiLoadAttempted = true;
try {
const ffi = require("bun:ffi") as typeof import("bun:ffi");
const ffi = await import("bun:ffi");
const lib = ffi.dlopen(
`/System/Library/Frameworks/Carbon.framework/Carbon`,
{
@@ -35,13 +37,12 @@ function loadFFI(): void {
return Number(lib.symbols.CGEventSourceFlagsState(stateID));
};
} catch {
// If loading fails, keep the function null so isModifierPressed returns false
cgEventSourceFlagsState = null;
}
}
export function prewarm(): void {
loadFFI();
export async function prewarm(): Promise<void> {
await loadFFI();
}
export function isModifierPressed(modifier: string): boolean {
@@ -49,8 +50,6 @@ export function isModifierPressed(modifier: string): boolean {
return false;
}
loadFFI();
if (cgEventSourceFlagsState === null) {
return false;
}