From aed89f0b414fe75d843f983636d389bc1f94147e Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Sat, 30 Nov 2024 18:55:27 +0700 Subject: [PATCH] Fix #660 - Infer device vendor & type from `sec-ch-ua-model` --- src/main/ua-parser.js | 15 ++-- test/unit/main.js | 4 +- test/unit/ua-ch.js | 185 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+), 7 deletions(-) diff --git a/src/main/ua-parser.js b/src/main/ua-parser.js index 468e456..7edc48d 100755 --- a/src/main/ua-parser.js +++ b/src/main/ua-parser.js @@ -1200,11 +1200,16 @@ } if (uaCH[MODEL]) { this.set(MODEL, uaCH[MODEL]); - } - // Xbox-Specific Detection - if (uaCH[MODEL] == 'Xbox') { - this.set(TYPE, CONSOLE) - .set(VENDOR, MICROSOFT); + if (!this.get(TYPE) || !this.get(VENDOR)) { + var reParse = {}; + rgxMapper.call(reParse, 'droid 9; ' + uaCH[MODEL] + ')', rgxMap); + if (!this.get(TYPE) && !!reParse.type) { + this.set(TYPE, reParse.type); + } + if (!this.get(VENDOR) && !!reParse.vendor) { + this.set(VENDOR, reParse.vendor); + } + } } if (uaCH[FORMFACTORS]) { var ff; diff --git a/test/unit/main.js b/test/unit/main.js index 0433882..ea337f9 100644 --- a/test/unit/main.js +++ b/test/unit/main.js @@ -390,10 +390,10 @@ describe('Map UA-CH headers', function () { assert.strictEqual(cpu.architecture, "arm64"); assert.strictEqual(uap.device.type, "mobile"); assert.strictEqual(uap.device.model, "Pixel 99"); - assert.strictEqual(uap.device.vendor, undefined); + assert.strictEqual(uap.device.vendor, "Google"); assert.strictEqual(device.type, "mobile"); assert.strictEqual(device.model, "Pixel 99"); - assert.strictEqual(device.vendor, undefined); + assert.strictEqual(device.vendor, "Google"); assert.strictEqual(uap.engine.name, 'Blink'); assert.strictEqual(uap.engine.version, '93.0.1.2'); assert.strictEqual(engine.name, 'Blink'); diff --git a/test/unit/ua-ch.js b/test/unit/ua-ch.js index 83722de..84dc1fd 100644 --- a/test/unit/ua-ch.js +++ b/test/unit/ua-ch.js @@ -34,4 +34,189 @@ describe('Browser naming adjustments', () => { const { browser } = UAParser(headers).withClientHints(); assert.strictEqual(browser.name, 'Chrome Headless'); }); +}); + +describe('Identify vendor & type of device from given model name', () => { + + [ + { + model: '220733SG', + expect: { + vendor : 'Xiaomi', + type : 'mobile' + } + }, + { + model: '5087Z', + expect: { + vendor : 'TCL', + type : 'mobile' + } + }, + { + model: '9137W', + expect: { + vendor : 'TCL', + type : 'tablet' + } + }, + { + model: 'BE2015', + expect: { + vendor : 'OnePlus', + type : 'mobile' + } + }, + { + model: 'CPH2389', + expect: { + vendor : 'OPPO', + type : 'mobile' + } + }, + { + model: 'Infinix X669C', + expect: { + vendor : 'Infinix', + type : 'mobile' + } + }, + { + model: 'itel L6502', + expect: { + vendor : 'itel', + type : 'mobile' + } + }, + { + model: 'Lenovo TB-X606F', + expect: { + vendor : 'Lenovo', + type : 'tablet' + } + }, + { + model: 'LM-Q720', + expect: { + vendor : 'LG', + type : 'mobile' + } + }, + { + model: 'M2003J15SC', + expect: { + vendor : 'Xiaomi', + type : 'mobile' + } + }, + { + model: 'MAR-LX1A', + expect: { + vendor : 'Huawei', + type : 'mobile' + } + }, + { + model: 'moto g(20)', + expect: { + vendor : 'Motorola', + type : 'mobile' + } + }, + { + model: 'Nokia C210', + expect: { + vendor : 'Nokia', + type : 'mobile' + } + }, + { + model: 'Pixel 8', + expect: { + vendor : 'Google', + type : 'mobile' + } + }, + { + model: 'Redmi Note 9S', + expect: { + vendor : 'Xiaomi', + type : 'mobile' + } + }, + { + model: 'RMX3830', + expect: { + vendor : 'Realme', + type : 'mobile' + } + }, + { + model: 'SM-S536DL', + expect: { + vendor : 'Samsung', + type : 'mobile' + } + }, + { + model: 'SM-S546VL', + expect: { + vendor : 'Samsung', + type : 'mobile' + } + }, + { + model: 'SM-T875', + expect: { + vendor : 'Samsung', + type : 'tablet' + } + }, + { + model: 'STK-L21', + expect: { + vendor : 'Huawei', + type : 'mobile' + } + }, + { + model: 'T430W', + expect: { + vendor : 'TCL', + type : 'mobile' + } + }, + { + model: 'TECNO KI5k', + expect: { + vendor : 'TECNO', + type : 'mobile' + } + }, + { + model: 'vivo 1820', + expect: { + vendor : 'Vivo', + type : 'mobile' + } + }, + { + model: 'Xbox', + expect: { + vendor : 'Microsoft', + type : 'console' + } + } + ] + .forEach((test) => { + it(`Solve "${test.model}"`, () => { + const headers = { + 'sec-ch-ua-model' : test.model, + }; + const { device } = UAParser(headers).withClientHints(); + assert.strictEqual(device.model, test.model); + assert.strictEqual(device.vendor, test.expect.vendor); + assert.strictEqual(device.type, test.expect.type); + }); + }); }); \ No newline at end of file