// Check that fbjs/lib/UserAgent reports the names its API documents, whatever
// naming scheme the installed ua-parser-js uses.
'use strict';

const assert = require('assert');

const CASES = [
  {
    ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 ' +
        '(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    browser: 'Chrome',
    platform: 'Mac OS X',
  },
  {
    ua: 'Mozilla/5.0 (Linux; Android 13; SM-S908B) AppleWebKit/537.36 ' +
        '(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36',
    browser: 'Chrome',
    platform: 'Android',
  },
  {
    ua: 'Mozilla/5.0 (Android 13; Mobile; rv:121.0) Gecko/121.0 Firefox/121.0',
    browser: 'Firefox',
    platform: 'Android',
  },
  {
    ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 ' +
        'Firefox/121.0',
    browser: 'Firefox',
    platform: 'Windows',
  },
];

// Both UserAgentData and ua-parser-js read the user agent once, when they are
// first loaded, so each case needs fresh globals and a fresh module registry.
function load(ua) {
  for (const key of Object.keys(require.cache)) {
    delete require.cache[key];
  }
  // navigator is a getter-only global since node 21
  Object.defineProperty(global, 'navigator', {
    value: {userAgent: ua},
    configurable: true,
  });
  global.window = {navigator: {userAgent: ua}};
  return require('fbjs/lib/UserAgent');
}

for (const c of CASES) {
  const UserAgent = load(c.ua);
  assert.ok(
    UserAgent.isBrowser(c.browser),
    'isBrowser(' + JSON.stringify(c.browser) + ') for ' + c.ua
  );
  assert.ok(
    UserAgent.isPlatform(c.platform),
    'isPlatform(' + JSON.stringify(c.platform) + ') for ' + c.ua
  );
}

console.log('ok - ' + CASES.length + ' user agents');
