const fs = require('fs');
const path = require('path');

const root = path.resolve(__dirname, '..');
const eventCaptureSource = fs.readFileSync(path.join(root, 'event-capture', 'index.js'), 'utf8');
const appSource = fs.readFileSync(path.join(root, 'app.js'), 'utf8');

function assertSourceContains(source, pattern, message) {
  if (!pattern.test(source)) {
    throw new Error(message);
  }
}

assertSourceContains(
  eventCaptureSource,
  /start\(options = \{\}\)/,
  'event capture start must accept focus options'
);

assertSourceContains(
  eventCaptureSource,
  /const shouldFocus = options\.focus !== false;/,
  'event capture must allow callers to opt out of focusing'
);

assertSourceContains(
  eventCaptureSource,
  /showInactive/,
  'event capture should show without activating when focus is disabled'
);

assertSourceContains(
  appSource,
  /const nativeTapStarted = startNativeKeyTap\(\);[\s\S]{0,800}eventCapture\.start\(\{ focus: !nativeTapStarted \}\);/,
  'offscreen control should avoid focusing the capture window when native key tap is running'
);

console.log('event capture focus behavior tests passed');
