Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchezzzhak committed Sep 20, 2024
1 parent 9701ca9 commit b4a46b0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 69 deletions.
100 changes: 45 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,27 @@ IndexerDevice.init();
IndexerClient.init();

class DeviceDetector {
/**
* @typedef DeviceDetectorOptions
* @param {boolean} skipBotDetection
* @param {number|null} osVersionTruncate
* @param {number|null} clientVersionTruncate
* @param {number|null} maxUserAgentSize
* @param {boolean} deviceIndexes
* @param {boolean} clientIndexes
* @param {boolean} deviceAliasCode
* @param {boolean} deviceInfo
* @param {boolean} deviceTrusted
*/

vendorParserList = {};
osParserList = {};
botParserList = {};
deviceParserList = {};
clientParserList = {};

#skipBotDetection = false;
#deviceIndexes = false;
#clientIndexes = false;
#deviceAliasCode = false;
#deviceTrusted = false;
#deviceInfo = false;
#clientVersionTruncate = null;
#osVersionTruncate = null;
#maxUserAgentSize = null;

/**
* @param {DeviceDetectorOptions} options
**/
constructor(options) {
this.vendorParserList = {};
this.osParserList = {};
this.botParserList = {};
this.deviceParserList = {};
this.clientParserList = {};

this.__skipBotDetection = false;
this.__deviceIndexes = false;
this.__clientIndexes = false;
this.__deviceAliasCode = false;
this.__deviceTrusted = false;
this.__deviceInfo = false;
this.__clientVersionTruncate = null;
this.__osVersionTruncate = null;
this.__maxUserAgentSize = null;

this.init();

this.skipBotDetection = attr(options, 'skipBotDetection', false);
Expand Down Expand Up @@ -129,27 +117,27 @@ class DeviceDetector {
}

set deviceTrusted(check) {
this.__deviceTrusted = check;
this.#deviceTrusted = check;
}

get deviceTrusted() {
return this.__deviceTrusted;
return this.#deviceTrusted;
}

set deviceInfo(stage) {
this.__deviceInfo = stage;
this.#deviceInfo = stage;
}

get deviceInfo() {
return this.__deviceInfo;
return this.#deviceInfo;
}

/**
* Set string size limit for the useragent
* @param {number} size
*/
set maxUserAgentSize(size) {
this.__maxUserAgentSize = size;
this.#maxUserAgentSize = size;
for (let name in this.clientParserList) {
this.clientParserList[name].setMaxUserAgentSize(size);
}
Expand All @@ -166,68 +154,72 @@ class DeviceDetector {
* @returns {null|number}
*/
get maxUserAgentSize() {
return this.__maxUserAgentSize;
return this.#maxUserAgentSize;
}

get skipBotDetection() {
return this.__skipBotDetection;
return this.#skipBotDetection;
}

set skipBotDetection(discard) {
this.__skipBotDetection = discard;
this.#skipBotDetection = discard;
}

/**
* @param {boolean} status - true use indexes, false not use indexes
*/
set deviceIndexes(status) {
this.__deviceIndexes = status;
this.#deviceIndexes = status;
}

/**
* @return {boolean} - true use indexes, false not use indexes
*/
get deviceIndexes() {
return this.__deviceIndexes;
return this.#deviceIndexes;
}

/**
* @param {boolean} status - true use indexes, false not use indexes
* true use indexes, false not use indexes
* @param {boolean} status
*/
set clientIndexes(status) {
this.__clientIndexes = status;
this.#clientIndexes = status;
for (let name in this.clientParserList) {
this.clientParserList[name].clientIndexes = status;
}
}

/**
* @return {boolean} - true use indexes, false not use indexes
* true use indexes, false not use indexes
* @return {boolean}
*/
get clientIndexes() {
return this.__clientIndexes;
return this.#clientIndexes;
}

/**
* @param {boolean} status - true use deviceAliasCode, false not use deviceAliasCode
* true use deviceAliasCode, false not use deviceAliasCode
* @param {boolean} status
*/
set deviceAliasCode(status) {
this.__deviceAliasCode = status;
this.#deviceAliasCode = status;
}

/**
* @return {boolean} - true use deviceAliasCode, false not use deviceAliasCode
* true use deviceAliasCode, false not use deviceAliasCode
* @return {boolean}
*/
get deviceAliasCode() {
return this.__deviceAliasCode;
return this.#deviceAliasCode;
}

/**
* set truncate client version (default null - all)
* @param value
*/
set clientVersionTruncate(value) {
this.__clientVersionTruncate = value;
this.#clientVersionTruncate = value;
for (let name in this.clientParserList) {
this.clientParserList[name].setVersionTruncation(value);
}
Expand All @@ -238,15 +230,15 @@ class DeviceDetector {
* @return int|null
*/
get clientVersionTruncate() {
return this.__clientVersionTruncate;
return this.#clientVersionTruncate;
}

/**
* set truncate os version (default null - all)
* @param value
*/
set osVersionTruncate(value) {
this.__osVersionTruncate = value;
this.#osVersionTruncate = value;
for (let name in this.osParserList) {
this.osParserList[name].setVersionTruncation(value);
}
Expand All @@ -257,7 +249,7 @@ class DeviceDetector {
* @return {null|number}
*/
get osVersionTruncate() {
return this.__osVersionTruncate;
return this.#osVersionTruncate;
}

/**
Expand Down Expand Up @@ -288,7 +280,7 @@ class DeviceDetector {

/**
* get all brands
* @returns {string[]}
* @return {string[]}
*/
getAvailableBrands() {
return this.getParseDevice(DEVICE_PARSER_LIST.MOBILE).getAvailableBrands();
Expand All @@ -297,7 +289,7 @@ class DeviceDetector {
/**
* has device brand
* @param brand
* @returns {boolean}
* @return {boolean}
*/
hasBrand(brand) {
return this.getParseDevice(DEVICE_PARSER_LIST.MOBILE).getCollectionBrands()[brand] !== void 0;
Expand Down Expand Up @@ -730,7 +722,6 @@ class DeviceDetector {
parseDevice(userAgent, clientHints) {
let ua = this.restoreUserAgentFromClientHints(userAgent, clientHints);
let brandIndexes = [];
let deviceCode = '';

let result = {
id: '',
Expand All @@ -746,8 +737,7 @@ class DeviceDetector {
return Object.assign({}, result);
}

const hasResultCode = this.deviceIndexes || this.deviceAliasCode || this.deviceInfo || this.deviceTrusted;
if (hasResultCode) {
if (this.deviceIndexes || this.deviceAliasCode || this.deviceInfo || this.deviceTrusted) {
if (helper.hasDeviceModelByClientHints(clientHints)) {
result.code = clientHints.device.model;
} else {
Expand Down
8 changes: 5 additions & 3 deletions parser/client-abstract-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ const ParserAbstract = require('./abstract-parser');
const IndexerClient = require('./client/indexer-client');

class ClientAbstractParser extends ParserAbstract {

#clientIndexes = true;

constructor() {
super();
this.collectionLength = 0;
this.type = '';
this.__clientIndexes = true;
}

get clientIndexes() {
return this.__clientIndexes;
return this.#clientIndexes;
}

set clientIndexes(stage) {
this.__clientIndexes = stage;
this.#clientIndexes = stage;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions parser/client/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ class Browser extends ClientAbstractParser {
engine = data.engine;
engineVersion = data.engine_version;
}

// If client hints report Chromium, but user agent detects a Chromium based browser, we favor this instead
if (
('Chromium' === name || 'Chrome Webview' === name) &&
data.name !== ''&&
['CR', 'CV', 'AN'].indexOf(data.short_name) !== -1
['CR', 'CV', 'AN'].indexOf(data.short_name) === -1
) {
name = data.name;
short = data.short_name;
Expand Down
4 changes: 0 additions & 4 deletions parser/device-abstract-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ class DeviceParserAbstract extends ParserAbstract {
*/
#parse(userAgent, canBreak = true, brandIndexes = []) {

/*if (helper.hasDesktopFragment(userAgent)) {
return [];
}*/

const output = [];
if (brandIndexes.length) {
for (let cursorId of brandIndexes) {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/oss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5215,7 +5215,7 @@
platform: ""
family: Android
-
ser_agent: AlMosafer/iPhone/17.0.3/8.10.0/1094/BDD0D3CE-EA37-4825-9E01-D26E91F38570/iPhone
user_agent: AlMosafer/iPhone/17.0.3/8.10.0/1094/BDD0D3CE-EA37-4825-9E01-D26E91F38570/iPhone
os:
name: iOS
short_name: IOS
Expand Down
3 changes: 0 additions & 3 deletions tests/os.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ function testVersionAndSkip(resultVersion, fixtureVersion, messageError) {
const runTest= (fixture, result) => {

let messageError = 'fixture data\n' + perryJSON(fixture);

console.log(fixture)

if (isObjNotEmpty(fixture.os.name)) {
expect(fixture.os.name, messageError).to.equal(result.name);
}
Expand Down
1 change: 0 additions & 1 deletion tests/other.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ describe('tests single parse nootebook', function() {
for (let i = 0, total = fixtureData.length; i < total; i++) {
it(i + '/' + total, function() {
let fixture = fixtureData[i];
console.log(fixture);
let result = parser.parse(fixture.user_agent);
delete result.id;
expect(fixture.device).to.deep.equal(result);
Expand Down

0 comments on commit b4a46b0

Please sign in to comment.