Fix #660 - Infer device vendor & type from sec-ch-ua-model

This commit is contained in:
Faisal Salman 2024-11-30 18:55:27 +07:00
parent 48c221b50b
commit aed89f0b41
3 changed files with 197 additions and 7 deletions

View File

@ -1200,11 +1200,16 @@
}
if (uaCH[MODEL]) {
this.set(MODEL, uaCH[MODEL]);
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);
}
}
// Xbox-Specific Detection
if (uaCH[MODEL] == 'Xbox') {
this.set(TYPE, CONSOLE)
.set(VENDOR, MICROSOFT);
}
if (uaCH[FORMFACTORS]) {
var ff;

View File

@ -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');

View File

@ -35,3 +35,188 @@ describe('Browser naming adjustments', () => {
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);
});
});
});