diff --git a/test/unit/helpers.js b/test/unit/helpers.js deleted file mode 100644 index 3fa7925..0000000 --- a/test/unit/helpers.js +++ /dev/null @@ -1,85 +0,0 @@ -const assert = require('assert'); -const { UAParser } = require('../../src/main/ua-parser'); -const { getDeviceVendor, isAppleSilicon, isAIBot, isBot, isChromeFamily } = require('../../src/helpers/ua-parser-helpers'); -const { Bots, Emails } = require('../../src/extensions/ua-parser-extensions'); -const { DeviceVendor } = require('../../src/enums/ua-parser-enums'); - -describe('getDeviceVendor', () => { - it('Can guess the device vendor from a model name', () => { - - const modelSM = 'SM-A605G'; - const modelRedmi = 'Redmi Note 8'; - const modelNexus = 'Nexus 6P'; - const modelAquos = 'AQUOS-TVX19B'; - - assert.equal(getDeviceVendor(modelSM), DeviceVendor.SAMSUNG); - assert.equal(getDeviceVendor(modelRedmi), DeviceVendor.XIAOMI); - assert.equal(getDeviceVendor(modelNexus), DeviceVendor.HUAWEI); - assert.equal(getDeviceVendor(modelAquos), DeviceVendor.SHARP); - }); -}); - -describe('isAppleSilicon', () => { - it('Can detect Apple Silicon device', () => { - - // non-real ua - const macARM = 'Mozilla/5.0 (Macintosh; ARM; Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0'; - const macIntel = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0'; - - assert.equal(isAppleSilicon(UAParser(macIntel)), false); - assert.equal(isAppleSilicon(macIntel), false); - assert.equal(isAppleSilicon(UAParser(macARM)), true); - assert.equal(isAppleSilicon(macARM), true); - }); -}); - -describe('isAIBot', () => { - it('Can detect AI Bots', () => { - - const claudeBot = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)'; - const firefox = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0'; - const searchGPT = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; OAI-SearchBot/1.0; +https://openai.com/searchbot'; - const semrushAI = 'Mozilla/5.0 (compatible; SemrushBot-OCOB/1; +https://www.semrush.com/bot/)'; - - assert.equal(isAIBot(UAParser(claudeBot, Bots)), true); - assert.equal(isAIBot(claudeBot), true); - assert.equal(isAIBot(firefox), false); - assert.equal(isAIBot(searchGPT), true); - assert.equal(isAIBot(semrushAI), true); - }); -}); - -describe('isBot', () => { - it('Can detect Bots', () => { - - // non-real ua - const ahrefsBot = 'Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)'; - const firefox = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0'; - const scrapy = 'Scrapy/1.5.0 (+https://scrapy.org)'; - const thunderbird = 'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0'; - - const botParser = new UAParser(firefox, { Bots, Emails }); - assert.equal(isBot(botParser.getResult()), false); - assert.equal(isBot(botParser.setUA(ahrefsBot).getResult()), true); - assert.equal(isBot(botParser.setUA(scrapy).getResult()), true); - assert.equal(isBot(botParser.setUA(thunderbird).getResult()), false); - - assert.equal(isBot(ahrefsBot), true); - assert.equal(isBot(firefox), false); - assert.equal(isBot(scrapy), true); - assert.equal(isBot(thunderbird), false); - }); -}); - -describe('isChromeFamily', () => { - it('Can detect Chromium-based browser', () => { - - const edge = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.2151.58'; - const firefox = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0'; - - assert.equal(isChromeFamily(UAParser(edge)), true); - assert.equal(isChromeFamily(UAParser(firefox)), false); - assert.equal(isChromeFamily(edge), true); - assert.equal(isChromeFamily(firefox), false); - }); -}); \ No newline at end of file diff --git a/test/unit/submodules/bot-detection.spec.js b/test/unit/submodules/bot-detection.spec.js new file mode 100644 index 0000000..5a0258e --- /dev/null +++ b/test/unit/submodules/bot-detection.spec.js @@ -0,0 +1,42 @@ +const assert = require('assert'); +const { UAParser } = require('../../../src/main/ua-parser'); +const { isAICrawler, isBot } = require('../../../src/bot-detection/bot-detection'); +const { Bots, Emails } = require('../../../src/extensions/ua-parser-extensions'); + +describe('isAICrawler()', () => { + it('Can detect AI Crawlers', () => { + + // AI Crawlers + const claudeBot = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)'; + const searchGPT = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; OAI-SearchBot/1.0; +https://openai.com/searchbot'; + const semrushAI = 'Mozilla/5.0 (compatible; SemrushBot-OCOB/1; +https://www.semrush.com/bot/)'; + + assert.equal(isAICrawler(claudeBot), true); + assert.equal(isAICrawler(searchGPT), true); + assert.equal(isAICrawler(semrushAI), true); + + // Non-AI Crawlers + const firefox = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0'; + + assert.equal(isAICrawler(firefox), false); + }); +}); + +describe('isBot()', () => { + it('Can detect general Bots', () => { + + // Bots + const ahrefsBot = 'Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)'; + const scrapy = 'Scrapy/1.5.0 (+https://scrapy.org)'; + + assert.equal(isBot(ahrefsBot), true); + assert.equal(isBot(scrapy), true); + + // Non-bots + const firefox = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0'; + const thunderbird = 'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0'; + + assert.equal(isBot(firefox), false); + assert.equal(isBot(thunderbird), false); + }); +}); \ No newline at end of file diff --git a/test/unit/submodules/browser-detection.spec.js b/test/unit/submodules/browser-detection.spec.js new file mode 100644 index 0000000..176e369 --- /dev/null +++ b/test/unit/submodules/browser-detection.spec.js @@ -0,0 +1,16 @@ +const assert = require('assert'); +const { UAParser } = require('../../../src/main/ua-parser'); +const { isChromeFamily } = require('../../../src/browser-detection/browser-detection'); + +describe('isChromeFamily()', () => { + it('Can detect Chromium-based browser', () => { + + const edge = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.2151.58'; + const firefox = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0'; + + assert.equal(isChromeFamily(UAParser(edge)), true); + assert.equal(isChromeFamily(edge), true); + assert.equal(isChromeFamily(UAParser(firefox)), false); + assert.equal(isChromeFamily(firefox), false); + }); +}); \ No newline at end of file diff --git a/test/unit/submodules/device-detection.spec.js b/test/unit/submodules/device-detection.spec.js new file mode 100644 index 0000000..40d7ee7 --- /dev/null +++ b/test/unit/submodules/device-detection.spec.js @@ -0,0 +1,32 @@ +const assert = require('assert'); +const { UAParser } = require('../../../src/main/ua-parser'); +const { getDeviceVendor, isAppleSilicon } = require('../../../src/device-detection/device-detection'); +const { DeviceVendor } = require('../../../src/enums/ua-parser-enums'); + +describe('getDeviceVendor()', () => { + it('Can guess the device vendor from a model name', () => { + + const sm = 'SM-A605G'; + const redmi = 'Redmi Note 8'; + const nexus = 'Nexus 6P'; + const aquos = 'AQUOS-TVX19B'; + + assert.equal(getDeviceVendor(sm), DeviceVendor.SAMSUNG); + assert.equal(getDeviceVendor(redmi), DeviceVendor.XIAOMI); + assert.equal(getDeviceVendor(nexus), DeviceVendor.HUAWEI); + assert.equal(getDeviceVendor(aquos), DeviceVendor.SHARP); + }); +}); + +describe('isAppleSilicon()', () => { + it('Can detect Apple Silicon device', () => { + + const macARM = 'Mozilla/5.0 (Macintosh; ARM; Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0'; + const macIntel = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0'; + + assert.equal(isAppleSilicon(UAParser(macIntel)), false); + assert.equal(isAppleSilicon(macIntel), false); + assert.equal(isAppleSilicon(UAParser(macARM)), true); + assert.equal(isAppleSilicon(macARM), true); + }); +}); \ No newline at end of file diff --git a/test/unit/extensions.js b/test/unit/submodules/extensions.spec.js similarity index 96% rename from test/unit/extensions.js rename to test/unit/submodules/extensions.spec.js index c3acfa2..90868e4 100644 --- a/test/unit/extensions.js +++ b/test/unit/submodules/extensions.spec.js @@ -3,9 +3,9 @@ const assert = require('assert'); const parseJS = require('@babel/parser').parse; const traverse = require('@babel/traverse').default; const safe = require('safe-regex'); -const { UAParser } = require('../../src/main/ua-parser'); -const { Bots, CLIs, Crawlers, Emails, Fetchers, InApps, Libraries, Vehicles } = require('../../src/extensions/ua-parser-extensions'); -const { BrowserType, OSName, Extension } = require('../../src/enums/ua-parser-enums'); +const { UAParser } = require('../../../src/main/ua-parser'); +const { Bots, CLIs, Crawlers, Emails, Fetchers, InApps, Libraries, Vehicles } = require('../../../src/extensions/ua-parser-extensions'); +const { BrowserType, OSName, Extension } = require('../../../src/enums/ua-parser-enums'); const { CLI, Crawler, Email, Fetcher, Library } = Extension.BrowserName; describe('Extensions', () => { diff --git a/test/unit/submodules/helpers.spec.js b/test/unit/submodules/helpers.spec.js new file mode 100644 index 0000000..6281c17 --- /dev/null +++ b/test/unit/submodules/helpers.spec.js @@ -0,0 +1,13 @@ +const assert = require('assert'); +const { isFrozenUA } = require('../../../src/helpers/ua-parser-helpers'); + +describe('isFrozenUA()', () => { + it('matches supplied user-agent string with known frozen user-agent pattern', () => { + + const regularMobileUA = "Mozilla/5.0 (Linux; Android 9; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.1234.56 Mobile Safari/537.36"; + const frozenMobileUA = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.0.0 Mobile Safari/537.36"; + + assert.equal(isFrozenUA(regularMobileUA), false); + assert.equal(isFrozenUA(frozenMobileUA), true); + }); +}); \ No newline at end of file