13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ciimport { describe, it } from 'mocha'; 173af6ab5fSopenharmony_ciimport { assert, expect } from 'chai'; 183af6ab5fSopenharmony_ciimport { 193af6ab5fSopenharmony_ci ObOptionsForTest, 203af6ab5fSopenharmony_ci ObConfigResolver, 213af6ab5fSopenharmony_ci MergedConfig, 223af6ab5fSopenharmony_ci collectResevedFileNameInIDEConfig, 233af6ab5fSopenharmony_ci readNameCache, 243af6ab5fSopenharmony_ci handleUniversalPathInObf, 253af6ab5fSopenharmony_ci getArkguardNameCache, 263af6ab5fSopenharmony_ci fillNameCache, 273af6ab5fSopenharmony_ci writeObfuscationNameCache, 283af6ab5fSopenharmony_ci generateConsumerObConfigFile, 293af6ab5fSopenharmony_ci mangleFilePath, 303af6ab5fSopenharmony_ci enableObfuscatedFilePathConfig, 313af6ab5fSopenharmony_ci handleObfuscatedFilePath, 323af6ab5fSopenharmony_ci enableObfuscateFileName, 333af6ab5fSopenharmony_ci getRelativeSourcePath, 343af6ab5fSopenharmony_ci OptionTypeForTest 353af6ab5fSopenharmony_ci} from '../../../src/initialization/ConfigResolver'; 363af6ab5fSopenharmony_ciimport { PropCollections, renameFileNameModule } from '../../../src/ArkObfuscator'; 373af6ab5fSopenharmony_ciimport { nameCacheMap } from '../../../src/initialization/CommonObject'; 383af6ab5fSopenharmony_ciimport path from 'path'; 393af6ab5fSopenharmony_ciimport fs from 'fs'; 403af6ab5fSopenharmony_ciimport { FileUtils } from '../../../src/utils/FileUtils'; 413af6ab5fSopenharmony_ciimport sinon from 'sinon'; 423af6ab5fSopenharmony_ciimport { UnobfuscationCollections } from '../../../src/utils/CommonCollections'; 433af6ab5fSopenharmony_ci 443af6ab5fSopenharmony_ciconst OBFUSCATE_TESTDATA_DIR = path.resolve(__dirname, '../../testData/obfuscation/system_api_obfuscation'); 453af6ab5fSopenharmony_ciconst projectConfig = { 463af6ab5fSopenharmony_ci obfuscationOptions: { option1: 'value1' }, 473af6ab5fSopenharmony_ci compileHar: true 483af6ab5fSopenharmony_ci}; 493af6ab5fSopenharmony_ciconst logger = console; 503af6ab5fSopenharmony_ciconst isTerser = false; 513af6ab5fSopenharmony_cilet newObConfigResolver = new ObConfigResolver(projectConfig, logger, isTerser); 523af6ab5fSopenharmony_ci 533af6ab5fSopenharmony_cidescribe('test for ConfigResolve', function() { 543af6ab5fSopenharmony_ci describe('ObOptions', () => { 553af6ab5fSopenharmony_ci it('should merge options correctly', () => { 563af6ab5fSopenharmony_ci const ob1 = new ObOptionsForTest(); 573af6ab5fSopenharmony_ci ob1.disableObfuscation = true; 583af6ab5fSopenharmony_ci ob1.enablePropertyObfuscation = true; 593af6ab5fSopenharmony_ci 603af6ab5fSopenharmony_ci const ob2 = new ObOptionsForTest(); 613af6ab5fSopenharmony_ci ob2.enableToplevelObfuscation = true; 623af6ab5fSopenharmony_ci ob2.printNameCache = 'test'; 633af6ab5fSopenharmony_ci ob2.printKeptNamesPath = './test/ut/initialization/printKeptNamesPath.txt'; 643af6ab5fSopenharmony_ci ob2.applyNameCache = 'test case'; 653af6ab5fSopenharmony_ci 663af6ab5fSopenharmony_ci ob1.merge(ob2); 673af6ab5fSopenharmony_ci 683af6ab5fSopenharmony_ci expect(ob1.disableObfuscation).to.be.true; 693af6ab5fSopenharmony_ci expect(ob1.enablePropertyObfuscation).to.be.true; 703af6ab5fSopenharmony_ci expect(ob1.enableToplevelObfuscation).to.be.true; 713af6ab5fSopenharmony_ci expect(ob1.printNameCache).to.equal('test'); 723af6ab5fSopenharmony_ci expect(ob1.printKeptNamesPath).to.equal('./test/ut/initialization/printKeptNamesPath.txt'); 733af6ab5fSopenharmony_ci expect(ob1.applyNameCache).to.equal('test case'); 743af6ab5fSopenharmony_ci }); 753af6ab5fSopenharmony_ci }); 763af6ab5fSopenharmony_ci 773af6ab5fSopenharmony_ci describe('MergedConfig', () => { 783af6ab5fSopenharmony_ci it('should merge two MergedConfig instances correctly', () => { 793af6ab5fSopenharmony_ci const config1 = new MergedConfig(); 803af6ab5fSopenharmony_ci config1.reservedPropertyNames = ['prop1']; 813af6ab5fSopenharmony_ci config1.reservedGlobalNames = ['global1']; 823af6ab5fSopenharmony_ci config1.keepComments = ['comment1']; 833af6ab5fSopenharmony_ci config1.excludePathSet.add('path1'); 843af6ab5fSopenharmony_ci 853af6ab5fSopenharmony_ci const config2 = new MergedConfig(); 863af6ab5fSopenharmony_ci config2.reservedPropertyNames = ['prop2']; 873af6ab5fSopenharmony_ci config2.reservedGlobalNames = ['global2']; 883af6ab5fSopenharmony_ci config2.keepComments = ['comment2']; 893af6ab5fSopenharmony_ci config2.excludePathSet.add('path2'); 903af6ab5fSopenharmony_ci 913af6ab5fSopenharmony_ci config1.merge(config2); 923af6ab5fSopenharmony_ci 933af6ab5fSopenharmony_ci expect(config1.reservedPropertyNames).to.deep.equal(['prop1', 'prop2']); 943af6ab5fSopenharmony_ci expect(config1.reservedGlobalNames).to.deep.equal(['global1', 'global2']); 953af6ab5fSopenharmony_ci expect(config1.keepComments).to.deep.equal(['comment1', 'comment2']); 963af6ab5fSopenharmony_ci expect(config1.excludePathSet).to.deep.equal(new Set(['path1', 'path2'])); 973af6ab5fSopenharmony_ci }); 983af6ab5fSopenharmony_ci 993af6ab5fSopenharmony_ci it('should sort and deduplicate arrays correctly', () => { 1003af6ab5fSopenharmony_ci const config = new MergedConfig(); 1013af6ab5fSopenharmony_ci config.reservedPropertyNames = ['prop2', 'prop1', 'prop1']; 1023af6ab5fSopenharmony_ci config.reservedGlobalNames = ['global2', 'global1', 'global1']; 1033af6ab5fSopenharmony_ci config.keepComments = ['comment2', 'comment1', 'comment1']; 1043af6ab5fSopenharmony_ci 1053af6ab5fSopenharmony_ci config.sortAndDeduplicate(); 1063af6ab5fSopenharmony_ci 1073af6ab5fSopenharmony_ci expect(config.reservedPropertyNames).to.deep.equal(['prop1', 'prop2']); 1083af6ab5fSopenharmony_ci expect(config.reservedGlobalNames).to.deep.equal(['global1', 'global2']); 1093af6ab5fSopenharmony_ci expect(config.keepComments).to.deep.equal(['comment1', 'comment2']); 1103af6ab5fSopenharmony_ci }); 1113af6ab5fSopenharmony_ci 1123af6ab5fSopenharmony_ci it('should serialize merged config correctly', () => { 1133af6ab5fSopenharmony_ci let resultStr: string = ''; 1143af6ab5fSopenharmony_ci const config = new MergedConfig(); 1153af6ab5fSopenharmony_ci config.options = new ObOptionsForTest(); 1163af6ab5fSopenharmony_ci config.options['option1'] = true; 1173af6ab5fSopenharmony_ci config.options['option2'] = false; 1183af6ab5fSopenharmony_ci config.options['disableObfuscation'] = true; 1193af6ab5fSopenharmony_ci config.options['enableStringPropertyObfuscation'] = true; 1203af6ab5fSopenharmony_ci config.reservedGlobalNames = ['global1']; 1213af6ab5fSopenharmony_ci config.reservedPropertyNames = ['prop1']; 1223af6ab5fSopenharmony_ci 1233af6ab5fSopenharmony_ci const serialized = config.serializeMergedConfig(); 1243af6ab5fSopenharmony_ci const serializedExportSwitchMap = new Map([ 1253af6ab5fSopenharmony_ci ['disableObfuscation', ObConfigResolver.KEEP_DTS], 1263af6ab5fSopenharmony_ci ['enablePropertyObfuscation', ObConfigResolver.ENABLE_PROPERTY_OBFUSCATION], 1273af6ab5fSopenharmony_ci ['enableStringPropertyObfuscation', ObConfigResolver.ENABLE_STRING_PROPERTY_OBFUSCATION], 1283af6ab5fSopenharmony_ci ['enableToplevelObfuscation', ObConfigResolver.ENABLE_TOPLEVEL_OBFUSCATION], 1293af6ab5fSopenharmony_ci ['compact', ObConfigResolver.COMPACT], 1303af6ab5fSopenharmony_ci ['removeLog', ObConfigResolver.REMOVE_LOG], 1313af6ab5fSopenharmony_ci ]); 1323af6ab5fSopenharmony_ci 1333af6ab5fSopenharmony_ci expect(serialized).to.include(ObConfigResolver.KEEP_GLOBAL_NAME); 1343af6ab5fSopenharmony_ci expect(serialized).to.include('global1'); 1353af6ab5fSopenharmony_ci expect(serialized).to.include(ObConfigResolver.KEEP_PROPERTY_NAME); 1363af6ab5fSopenharmony_ci expect(serialized).to.include('prop1'); 1373af6ab5fSopenharmony_ci expect(serialized).to.not.include('option2'); 1383af6ab5fSopenharmony_ci 1393af6ab5fSopenharmony_ci expect(ObConfigResolver.exportedSwitchMap.has(String('disableObfuscation'))).to.be.true; 1403af6ab5fSopenharmony_ci expect(ObConfigResolver.exportedSwitchMap.has(String('enableStringPropertyObfuscation'))).to.be.true; 1413af6ab5fSopenharmony_ci expect(resultStr).to.equal(''); 1423af6ab5fSopenharmony_ci }); 1433af6ab5fSopenharmony_ci }); 1443af6ab5fSopenharmony_ci 1453af6ab5fSopenharmony_ci describe('ObConfigResolver', () => { 1463af6ab5fSopenharmony_ci describe('constructor', () => { 1473af6ab5fSopenharmony_ci it('should create an instance with the correct properties', () => { 1483af6ab5fSopenharmony_ci const projectConfig = { 1493af6ab5fSopenharmony_ci obfuscationOptions: { option1: 'value1' }, 1503af6ab5fSopenharmony_ci compileHar: true 1513af6ab5fSopenharmony_ci }; 1523af6ab5fSopenharmony_ci const logger = console; 1533af6ab5fSopenharmony_ci const isTerser = false; 1543af6ab5fSopenharmony_ci const myInstance = new ObConfigResolver(projectConfig, logger, isTerser); 1553af6ab5fSopenharmony_ci 1563af6ab5fSopenharmony_ci expect(myInstance).to.be.an('object'); 1573af6ab5fSopenharmony_ci expect(myInstance.sourceObConfig).to.deep.equal({ option1: 'value1' }); 1583af6ab5fSopenharmony_ci expect(myInstance.logger).to.equal(console); 1593af6ab5fSopenharmony_ci expect(myInstance.isHarCompiled).to.be.true; 1603af6ab5fSopenharmony_ci expect(myInstance.isTerser).to.be.false; 1613af6ab5fSopenharmony_ci }); 1623af6ab5fSopenharmony_ci 1633af6ab5fSopenharmony_ci it('should create an instance with Terser enabled', () => { 1643af6ab5fSopenharmony_ci const projectConfig = { 1653af6ab5fSopenharmony_ci obfuscationOptions: { option1: 'value1' }, 1663af6ab5fSopenharmony_ci compileHar: true 1673af6ab5fSopenharmony_ci }; 1683af6ab5fSopenharmony_ci const logger = console; 1693af6ab5fSopenharmony_ci const myInstance = new ObConfigResolver(projectConfig, logger, true); 1703af6ab5fSopenharmony_ci 1713af6ab5fSopenharmony_ci expect(myInstance).to.be.an('object'); 1723af6ab5fSopenharmony_ci expect(myInstance.sourceObConfig).to.deep.equal({ option1: 'value1' }); 1733af6ab5fSopenharmony_ci expect(myInstance.logger).to.equal(console); 1743af6ab5fSopenharmony_ci expect(myInstance.isHarCompiled).to.be.true; 1753af6ab5fSopenharmony_ci expect(myInstance.isTerser).to.be.true; 1763af6ab5fSopenharmony_ci }); 1773af6ab5fSopenharmony_ci }); 1783af6ab5fSopenharmony_ci 1793af6ab5fSopenharmony_ci describe('resolveObfuscationConfigs', () => { 1803af6ab5fSopenharmony_ci const projectConfig = { 1813af6ab5fSopenharmony_ci obfuscationOptions: { option1: 'value1' }, 1823af6ab5fSopenharmony_ci compileHar: true 1833af6ab5fSopenharmony_ci }; 1843af6ab5fSopenharmony_ci const logger = console; 1853af6ab5fSopenharmony_ci const isTerser = false; 1863af6ab5fSopenharmony_ci let newObConfigResolver = new ObConfigResolver(projectConfig, logger, isTerser); 1873af6ab5fSopenharmony_ci let testClass: ObConfigResolver & { 1883af6ab5fSopenharmony_ci performancePrinter?: any; 1893af6ab5fSopenharmony_ci }; 1903af6ab5fSopenharmony_ci testClass = newObConfigResolver; 1913af6ab5fSopenharmony_ci 1923af6ab5fSopenharmony_ci it('should return a MergedConfig object when sourceObConfig is undefined', () => { 1933af6ab5fSopenharmony_ci testClass.sourceObConfig = undefined; 1943af6ab5fSopenharmony_ci testClass.isHarCompiled = true; 1953af6ab5fSopenharmony_ci testClass.performancePrinter = {}; 1963af6ab5fSopenharmony_ci const result = testClass.resolveObfuscationConfigs(); 1973af6ab5fSopenharmony_ci generateConsumerObConfigFile(testClass.sourceObConfig, undefined); 1983af6ab5fSopenharmony_ci 1993af6ab5fSopenharmony_ci expect(testClass.sourceObConfig).to.be.an.undefined; 2003af6ab5fSopenharmony_ci expect(result).to.deep.equal(new MergedConfig()); 2013af6ab5fSopenharmony_ci expect(result).to.be.an('object'); 2023af6ab5fSopenharmony_ci expect(result).to.have.property('options'); 2033af6ab5fSopenharmony_ci }); 2043af6ab5fSopenharmony_ci 2053af6ab5fSopenharmony_ci it('should return a MergedConfig object when sourceObConfig is null', () => { 2063af6ab5fSopenharmony_ci testClass.sourceObConfig = null; 2073af6ab5fSopenharmony_ci testClass.isHarCompiled = true; 2083af6ab5fSopenharmony_ci testClass.performancePrinter = {}; 2093af6ab5fSopenharmony_ci const result = testClass.resolveObfuscationConfigs(); 2103af6ab5fSopenharmony_ci generateConsumerObConfigFile(null, undefined); 2113af6ab5fSopenharmony_ci 2123af6ab5fSopenharmony_ci expect(testClass.sourceObConfig).to.be.a.null; 2133af6ab5fSopenharmony_ci expect(result).to.deep.equal(new MergedConfig()); 2143af6ab5fSopenharmony_ci expect(result).to.be.an('object'); 2153af6ab5fSopenharmony_ci expect(result).to.have.property('options'); 2163af6ab5fSopenharmony_ci }); 2173af6ab5fSopenharmony_ci 2183af6ab5fSopenharmony_ci it('should handle the case when sourceObConfig.selfConfig.ruleOptions.enable is false', function () { 2193af6ab5fSopenharmony_ci testClass.sourceObConfig = { 2203af6ab5fSopenharmony_ci selfConfig: { 2213af6ab5fSopenharmony_ci ruleOptions: { enable: false }, 2223af6ab5fSopenharmony_ci }, 2233af6ab5fSopenharmony_ci dependencies: { libraries: [], hars: [] }, 2243af6ab5fSopenharmony_ci }; 2253af6ab5fSopenharmony_ci generateConsumerObConfigFile(testClass.sourceObConfig, undefined); 2263af6ab5fSopenharmony_ci const result = testClass.resolveObfuscationConfigs(); 2273af6ab5fSopenharmony_ci 2283af6ab5fSopenharmony_ci expect(result).to.be.an('object'); 2293af6ab5fSopenharmony_ci expect(result).to.have.property('options'); 2303af6ab5fSopenharmony_ci expect(result.options.disableObfuscation).to.be.true; 2313af6ab5fSopenharmony_ci }); 2323af6ab5fSopenharmony_ci 2333af6ab5fSopenharmony_ci it('should handle the case when sourceObConfig.selfConfig.ruleOptions.enable is true', function () { 2343af6ab5fSopenharmony_ci testClass.sourceObConfig = { 2353af6ab5fSopenharmony_ci selfConfig: { 2363af6ab5fSopenharmony_ci ruleOptions: { enable: true }, 2373af6ab5fSopenharmony_ci }, 2383af6ab5fSopenharmony_ci dependencies: { libraries: [], hars: [] }, 2393af6ab5fSopenharmony_ci }; 2403af6ab5fSopenharmony_ci generateConsumerObConfigFile(testClass.sourceObConfig, undefined); 2413af6ab5fSopenharmony_ci const result = testClass.resolveObfuscationConfigs(); 2423af6ab5fSopenharmony_ci 2433af6ab5fSopenharmony_ci expect(result).to.be.an('object'); 2443af6ab5fSopenharmony_ci expect(result).to.have.property('options'); 2453af6ab5fSopenharmony_ci expect(result.options.disableObfuscation).to.be.false; 2463af6ab5fSopenharmony_ci }); 2473af6ab5fSopenharmony_ci 2483af6ab5fSopenharmony_ci it('should handle the case when enableObfuscation is false', () => { 2493af6ab5fSopenharmony_ci const sourceObConfig = { 2503af6ab5fSopenharmony_ci selfConfig: { 2513af6ab5fSopenharmony_ci ruleOptions: { enable: false }, 2523af6ab5fSopenharmony_ci consumerRules: ['./test/testData/obfuscation/keepDts/obfuscation-template.txt'], 2533af6ab5fSopenharmony_ci }, 2543af6ab5fSopenharmony_ci dependencies: { libraries: [], hars: [] }, 2553af6ab5fSopenharmony_ci obfuscationCacheDir: './test/testData/cache', 2563af6ab5fSopenharmony_ci options: { 2573af6ab5fSopenharmony_ci disableObfuscation: true 2583af6ab5fSopenharmony_ci } 2593af6ab5fSopenharmony_ci }; 2603af6ab5fSopenharmony_ci const isHarCompiled = true; 2613af6ab5fSopenharmony_ci let enableObfuscation = sourceObConfig.selfConfig.ruleOptions.enable; 2623af6ab5fSopenharmony_ci let selfConfig = new MergedConfig(); 2633af6ab5fSopenharmony_ci enableObfuscation = !selfConfig.options.disableObfuscation; 2643af6ab5fSopenharmony_ci let needConsumerConfigs = isHarCompiled && sourceObConfig.selfConfig.consumerRules && 2653af6ab5fSopenharmony_ci sourceObConfig.selfConfig.consumerRules.length > 0; 2663af6ab5fSopenharmony_ci let needDependencyConfigs = enableObfuscation || needConsumerConfigs; 2673af6ab5fSopenharmony_ci let dependencyConfigs = new MergedConfig(); 2683af6ab5fSopenharmony_ci const dependencyMaxLength = Math.max( 2693af6ab5fSopenharmony_ci sourceObConfig.dependencies.libraries.length, 2703af6ab5fSopenharmony_ci sourceObConfig.dependencies.hars.length, 2713af6ab5fSopenharmony_ci ); 2723af6ab5fSopenharmony_ci newObConfigResolver.getDependencyConfigsForTest(sourceObConfig, dependencyConfigs); 2733af6ab5fSopenharmony_ci 2743af6ab5fSopenharmony_ci expect(needConsumerConfigs).to.be.true; 2753af6ab5fSopenharmony_ci expect(!dependencyMaxLength).to.be.true; 2763af6ab5fSopenharmony_ci expect(needDependencyConfigs).to.be.true; 2773af6ab5fSopenharmony_ci expect(dependencyConfigs).to.deep.equal(new MergedConfig()); 2783af6ab5fSopenharmony_ci expect(enableObfuscation).to.be.true; 2793af6ab5fSopenharmony_ci }); 2803af6ab5fSopenharmony_ci 2813af6ab5fSopenharmony_ci it('should handle the case when enableObfuscation is false', () => { 2823af6ab5fSopenharmony_ci const sourceObConfig = { 2833af6ab5fSopenharmony_ci selfConfig: { 2843af6ab5fSopenharmony_ci options: { 2853af6ab5fSopenharmony_ci disableObfuscation: false, 2863af6ab5fSopenharmony_ci enablePropertyObfuscation: true, 2873af6ab5fSopenharmony_ci enableExportObfuscation: true, 2883af6ab5fSopenharmony_ci enableToplevelObfuscation:true 2893af6ab5fSopenharmony_ci }, 2903af6ab5fSopenharmony_ci ruleOptions: { enable: true }, 2913af6ab5fSopenharmony_ci consumerRules: ['./test/testData/obfuscation/keepDts/obfuscation-template.txt'], 2923af6ab5fSopenharmony_ci }, 2933af6ab5fSopenharmony_ci dependencies: { libraries: [1, 2, 3], hars: [] }, 2943af6ab5fSopenharmony_ci obfuscationCacheDir: './test/testData/cache', 2953af6ab5fSopenharmony_ci }; 2963af6ab5fSopenharmony_ci const isHarCompiled = true; 2973af6ab5fSopenharmony_ci let enableObfuscation = sourceObConfig.selfConfig.ruleOptions.enable; 2983af6ab5fSopenharmony_ci let selfConfig = new MergedConfig(); 2993af6ab5fSopenharmony_ci enableObfuscation = !selfConfig.options.disableObfuscation; 3003af6ab5fSopenharmony_ci let needConsumerConfigs = isHarCompiled && sourceObConfig.selfConfig.consumerRules && 3013af6ab5fSopenharmony_ci sourceObConfig.selfConfig.consumerRules.length > 0; 3023af6ab5fSopenharmony_ci let needDependencyConfigs = enableObfuscation || needConsumerConfigs; 3033af6ab5fSopenharmony_ci let dependencyConfigs = new MergedConfig(); 3043af6ab5fSopenharmony_ci const dependencyMaxLength = Math.max( 3053af6ab5fSopenharmony_ci sourceObConfig.dependencies.libraries.length, 3063af6ab5fSopenharmony_ci sourceObConfig.dependencies.hars.length, 3073af6ab5fSopenharmony_ci ); 3083af6ab5fSopenharmony_ci const mergedConfigs = newObConfigResolver.getMergedConfigsForTest(selfConfig, dependencyConfigs); 3093af6ab5fSopenharmony_ci newObConfigResolver.handleReservedArrayForTest(mergedConfigs); 3103af6ab5fSopenharmony_ci let needKeepSystemApi = enableObfuscation && 3113af6ab5fSopenharmony_ci (mergedConfigs.options.enablePropertyObfuscation || 3123af6ab5fSopenharmony_ci (mergedConfigs.options.enableExportObfuscation && mergedConfigs.options.enableToplevelObfuscation)); 3133af6ab5fSopenharmony_ci 3143af6ab5fSopenharmony_ci newObConfigResolver.getDependencyConfigsForTest(sourceObConfig, dependencyConfigs); 3153af6ab5fSopenharmony_ci 3163af6ab5fSopenharmony_ci expect(needConsumerConfigs).to.be.true; 3173af6ab5fSopenharmony_ci expect(!dependencyMaxLength).to.be.false; 3183af6ab5fSopenharmony_ci expect(needDependencyConfigs).to.be.true; 3193af6ab5fSopenharmony_ci expect(dependencyConfigs).to.deep.equal(new MergedConfig()); 3203af6ab5fSopenharmony_ci expect(enableObfuscation).to.be.true; 3213af6ab5fSopenharmony_ci expect(needKeepSystemApi).to.be.false; 3223af6ab5fSopenharmony_ci expect(needConsumerConfigs).to.be.true; 3233af6ab5fSopenharmony_ci }); 3243af6ab5fSopenharmony_ci 3253af6ab5fSopenharmony_ci it('should handle the case when enableObfuscation is true', () => { 3263af6ab5fSopenharmony_ci testClass.sourceObConfig = { 3273af6ab5fSopenharmony_ci selfConfig: { 3283af6ab5fSopenharmony_ci ruleOptions: { enable: true }, 3293af6ab5fSopenharmony_ci consumerRules: ['./test/testData/obfuscation/keepDts/obfuscation-template.txt'], 3303af6ab5fSopenharmony_ci }, 3313af6ab5fSopenharmony_ci dependencies: { libraries: [], hars: [] }, 3323af6ab5fSopenharmony_ci obfuscationCacheDir: './test/testData/cache', 3333af6ab5fSopenharmony_ci options: { 3343af6ab5fSopenharmony_ci disableObfuscation: true 3353af6ab5fSopenharmony_ci } 3363af6ab5fSopenharmony_ci }; 3373af6ab5fSopenharmony_ci testClass.isHarCompiled = true; 3383af6ab5fSopenharmony_ci testClass.performancePrinter = {}; 3393af6ab5fSopenharmony_ci let enableObfuscation = testClass.sourceObConfig.selfConfig.ruleOptions.enable; 3403af6ab5fSopenharmony_ci let selfConfig = new MergedConfig(); 3413af6ab5fSopenharmony_ci newObConfigResolver.getSelfConfigsForTest(selfConfig); 3423af6ab5fSopenharmony_ci enableObfuscation = !selfConfig.options.disableObfuscation; 3433af6ab5fSopenharmony_ci enableObfuscation = false; 3443af6ab5fSopenharmony_ci let needConsumerConfigs = testClass.isHarCompiled && testClass.sourceObConfig.selfConfig.consumerRules && 3453af6ab5fSopenharmony_ci testClass.sourceObConfig.selfConfig.consumerRules.length > 0; 3463af6ab5fSopenharmony_ci let needDependencyConfigs = enableObfuscation || needConsumerConfigs; 3473af6ab5fSopenharmony_ci let dependencyConfigs = new MergedConfig(); 3483af6ab5fSopenharmony_ci const dependencyMaxLength = Math.max( 3493af6ab5fSopenharmony_ci testClass.sourceObConfig.dependencies.libraries.length, 3503af6ab5fSopenharmony_ci testClass.sourceObConfig.dependencies.hars.length, 3513af6ab5fSopenharmony_ci ); 3523af6ab5fSopenharmony_ci newObConfigResolver.getDependencyConfigsForTest(testClass.sourceObConfig, dependencyConfigs); 3533af6ab5fSopenharmony_ci 3543af6ab5fSopenharmony_ci expect(needConsumerConfigs).to.be.true; 3553af6ab5fSopenharmony_ci expect(!dependencyMaxLength).to.be.true; 3563af6ab5fSopenharmony_ci expect(needDependencyConfigs).to.be.true; 3573af6ab5fSopenharmony_ci expect(dependencyConfigs).to.deep.equal(new MergedConfig()); 3583af6ab5fSopenharmony_ci expect(enableObfuscation).to.be.false; 3593af6ab5fSopenharmony_ci }); 3603af6ab5fSopenharmony_ci }); 3613af6ab5fSopenharmony_ci 3623af6ab5fSopenharmony_ci describe('getSelfConfigs', () => { 3633af6ab5fSopenharmony_ci it('should create an instance with the correct properties', () => { 3643af6ab5fSopenharmony_ci const projectConfig = { 3653af6ab5fSopenharmony_ci obfuscationOptions: { 3663af6ab5fSopenharmony_ci selfConfig: { 3673af6ab5fSopenharmony_ci ruleOptions: { 3683af6ab5fSopenharmony_ci rules: [] 3693af6ab5fSopenharmony_ci } 3703af6ab5fSopenharmony_ci } 3713af6ab5fSopenharmony_ci }, 3723af6ab5fSopenharmony_ci compileHar: true 3733af6ab5fSopenharmony_ci }; 3743af6ab5fSopenharmony_ci const logger = console; 3753af6ab5fSopenharmony_ci const isTerser = false; 3763af6ab5fSopenharmony_ci const resolver = new ObConfigResolver(projectConfig, logger, isTerser); 3773af6ab5fSopenharmony_ci 3783af6ab5fSopenharmony_ci expect(resolver).to.be.an('object'); 3793af6ab5fSopenharmony_ci expect(resolver.sourceObConfig).to.deep.equal(projectConfig.obfuscationOptions); 3803af6ab5fSopenharmony_ci expect(resolver.logger).to.equal(logger); 3813af6ab5fSopenharmony_ci expect(resolver.isHarCompiled).to.equal(projectConfig.compileHar); 3823af6ab5fSopenharmony_ci expect(resolver.isTerser).to.equal(isTerser); 3833af6ab5fSopenharmony_ci }); 3843af6ab5fSopenharmony_ci 3853af6ab5fSopenharmony_ci it('should create an instance with the correct properties', () => { 3863af6ab5fSopenharmony_ci const projectConfig = { 3873af6ab5fSopenharmony_ci obfuscationOptions: { 3883af6ab5fSopenharmony_ci selfConfig: { 3893af6ab5fSopenharmony_ci ruleOptions: { 3903af6ab5fSopenharmony_ci rules: ['./test/ut/initialization/tempNameCache.json'] 3913af6ab5fSopenharmony_ci } 3923af6ab5fSopenharmony_ci } 3933af6ab5fSopenharmony_ci }, 3943af6ab5fSopenharmony_ci compileHar: true 3953af6ab5fSopenharmony_ci }; 3963af6ab5fSopenharmony_ci const logger = console; 3973af6ab5fSopenharmony_ci const isTerser = false; 3983af6ab5fSopenharmony_ci const resolver = new ObConfigResolver(projectConfig, logger, isTerser); 3993af6ab5fSopenharmony_ci 4003af6ab5fSopenharmony_ci expect(resolver).to.be.an('object'); 4013af6ab5fSopenharmony_ci expect(resolver.sourceObConfig).to.deep.equal(projectConfig.obfuscationOptions); 4023af6ab5fSopenharmony_ci expect(resolver.logger).to.equal(logger); 4033af6ab5fSopenharmony_ci expect(resolver.isHarCompiled).to.equal(projectConfig.compileHar); 4043af6ab5fSopenharmony_ci expect(resolver.isTerser).to.equal(isTerser); 4053af6ab5fSopenharmony_ci }); 4063af6ab5fSopenharmony_ci 4073af6ab5fSopenharmony_ci it('should get self configs correctly', () => { 4083af6ab5fSopenharmony_ci const projectConfig = { 4093af6ab5fSopenharmony_ci obfuscationOptions: { 4103af6ab5fSopenharmony_ci selfConfig: { 4113af6ab5fSopenharmony_ci ruleOptions: { 4123af6ab5fSopenharmony_ci rules: ['./test/ut/initialization/tempNameCache.json'] 4133af6ab5fSopenharmony_ci } 4143af6ab5fSopenharmony_ci } 4153af6ab5fSopenharmony_ci }, 4163af6ab5fSopenharmony_ci compileHar: true 4173af6ab5fSopenharmony_ci }; 4183af6ab5fSopenharmony_ci const logger = console; 4193af6ab5fSopenharmony_ci const isTerser = false; 4203af6ab5fSopenharmony_ci const resolver = new ObConfigResolver(projectConfig, logger, isTerser); 4213af6ab5fSopenharmony_ci const selfConfigs = new MergedConfig(); 4223af6ab5fSopenharmony_ci 4233af6ab5fSopenharmony_ci resolver.getSelfConfigsForTest(selfConfigs); 4243af6ab5fSopenharmony_ci 4253af6ab5fSopenharmony_ci expect(resolver).to.be.an('object'); 4263af6ab5fSopenharmony_ci expect(resolver.sourceObConfig).to.deep.equal(projectConfig.obfuscationOptions); 4273af6ab5fSopenharmony_ci expect(resolver.logger).to.equal(logger); 4283af6ab5fSopenharmony_ci expect(resolver.isHarCompiled).to.equal(projectConfig.compileHar); 4293af6ab5fSopenharmony_ci expect(resolver.isTerser).to.equal(isTerser); 4303af6ab5fSopenharmony_ci }); 4313af6ab5fSopenharmony_ci }); 4323af6ab5fSopenharmony_ci 4333af6ab5fSopenharmony_ci describe('getConfigByPath', () => { 4343af6ab5fSopenharmony_ci const projectConfig = { 4353af6ab5fSopenharmony_ci obfuscationOptions: { 4363af6ab5fSopenharmony_ci selfConfig: { 4373af6ab5fSopenharmony_ci ruleOptions: { 4383af6ab5fSopenharmony_ci rules: ['./test/testData/obfuscation/filename_obf/getConfigByPath.json'] 4393af6ab5fSopenharmony_ci } 4403af6ab5fSopenharmony_ci } 4413af6ab5fSopenharmony_ci }, 4423af6ab5fSopenharmony_ci compileHar: true 4433af6ab5fSopenharmony_ci }; 4443af6ab5fSopenharmony_ci const logger = console; 4453af6ab5fSopenharmony_ci let sandbox; 4463af6ab5fSopenharmony_ci let instance; 4473af6ab5fSopenharmony_ci 4483af6ab5fSopenharmony_ci beforeEach(() => { 4493af6ab5fSopenharmony_ci sandbox = sinon.createSandbox(); 4503af6ab5fSopenharmony_ci instance = new ObConfigResolver(projectConfig, logger, true); 4513af6ab5fSopenharmony_ci instance.logger = { error: sandbox.stub() }; 4523af6ab5fSopenharmony_ci }); 4533af6ab5fSopenharmony_ci 4543af6ab5fSopenharmony_ci afterEach(() => { 4553af6ab5fSopenharmony_ci sandbox.restore(); 4563af6ab5fSopenharmony_ci }); 4573af6ab5fSopenharmony_ci 4583af6ab5fSopenharmony_ci it('should read file content and handle config content', () => { 4593af6ab5fSopenharmony_ci const path = './test/testData/obfuscation/filename_obf/getConfigByPath.json'; 4603af6ab5fSopenharmony_ci let fileContent; 4613af6ab5fSopenharmony_ci const expectedContent = '{"key": "value"}'; 4623af6ab5fSopenharmony_ci const configs = new MergedConfig(); 4633af6ab5fSopenharmony_ci 4643af6ab5fSopenharmony_ci fileContent = fs.readFileSync(path, 'utf-8'); 4653af6ab5fSopenharmony_ci instance.getConfigByPathForTest(path, configs); 4663af6ab5fSopenharmony_ci instance.handleConfigContent(fileContent, configs, path); 4673af6ab5fSopenharmony_ci 4683af6ab5fSopenharmony_ci expect(fileContent).to.equal(expectedContent); 4693af6ab5fSopenharmony_ci expect(configs).to.be.an.instanceOf(MergedConfig); 4703af6ab5fSopenharmony_ci }); 4713af6ab5fSopenharmony_ci 4723af6ab5fSopenharmony_ci it('should log error and throw when failed to open file', () => { 4733af6ab5fSopenharmony_ci const path = './test/testData/obfuscation/filename_obf/non-existent-file.json'; 4743af6ab5fSopenharmony_ci const configs = new MergedConfig(); 4753af6ab5fSopenharmony_ci const errorMessage = `Failed to open ${path}. Error message: ENOENT: no such file or directory, open '${path}'`; 4763af6ab5fSopenharmony_ci 4773af6ab5fSopenharmony_ci sandbox.stub(fs, 'readFileSync').throws(new Error(errorMessage)); 4783af6ab5fSopenharmony_ci 4793af6ab5fSopenharmony_ci expect(() => instance.getConfigByPath(path, configs)).to.throw(Error, errorMessage); 4803af6ab5fSopenharmony_ci expect(instance.logger.error.calledWith(errorMessage)).to.be.false; 4813af6ab5fSopenharmony_ci }); 4823af6ab5fSopenharmony_ci }); 4833af6ab5fSopenharmony_ci 4843af6ab5fSopenharmony_ci it('should return a new MergedConfig instance when sourceObConfig is not defined', function() { 4853af6ab5fSopenharmony_ci const projectConfig = { 4863af6ab5fSopenharmony_ci sourceObConfig: null, 4873af6ab5fSopenharmony_ci getMergedConfig: function() { 4883af6ab5fSopenharmony_ci let sourceObConfig = this.sourceObConfig; 4893af6ab5fSopenharmony_ci if (!sourceObConfig) { 4903af6ab5fSopenharmony_ci return new MergedConfig(); 4913af6ab5fSopenharmony_ci } 4923af6ab5fSopenharmony_ci } 4933af6ab5fSopenharmony_ci }; 4943af6ab5fSopenharmony_ci 4953af6ab5fSopenharmony_ci const result = projectConfig.getMergedConfig(); 4963af6ab5fSopenharmony_ci expect(result).to.be.an.instanceOf(MergedConfig); 4973af6ab5fSopenharmony_ci }); 4983af6ab5fSopenharmony_ci 4993af6ab5fSopenharmony_ci describe('resolveKeepConfig', () => { 5003af6ab5fSopenharmony_ci it('should resolve keep config correctly, starts with "!" and contains "*", not starts with "!"', () => { 5013af6ab5fSopenharmony_ci const keepConfigs = ['!test/*/exclude.js', 'test/include.js']; 5023af6ab5fSopenharmony_ci const configs = new MergedConfig(); 5033af6ab5fSopenharmony_ci const configPath = './test/testData/obfuscation/keepDts/obfuscation-template.txt'; 5043af6ab5fSopenharmony_ci 5053af6ab5fSopenharmony_ci newObConfigResolver.resolveKeepConfig(keepConfigs, configs, configPath); 5063af6ab5fSopenharmony_ci 5073af6ab5fSopenharmony_ci expect(configs.excludeUniversalPaths).to.have.lengthOf(1); 5083af6ab5fSopenharmony_ci expect(configs.keepUniversalPaths).to.have.lengthOf(0); 5093af6ab5fSopenharmony_ci expect(configs.excludePathSet).to.have.lengthOf(0); 5103af6ab5fSopenharmony_ci expect(configs.keepSourceOfPaths).to.have.lengthOf(0); 5113af6ab5fSopenharmony_ci }); 5123af6ab5fSopenharmony_ci 5133af6ab5fSopenharmony_ci it('should resolve keep config correctly, starts with "!" and contains "*", not starts with "!" and "?"', () => { 5143af6ab5fSopenharmony_ci const keepConfigs = ['!test/*/exclude.js', 'test/?/include.js']; 5153af6ab5fSopenharmony_ci const configs = new MergedConfig(); 5163af6ab5fSopenharmony_ci const configPath = './test/testData/obfuscation/keepDts/obfuscation-template.txt'; 5173af6ab5fSopenharmony_ci 5183af6ab5fSopenharmony_ci newObConfigResolver.resolveKeepConfig(keepConfigs, configs, configPath); 5193af6ab5fSopenharmony_ci 5203af6ab5fSopenharmony_ci expect(configs.excludeUniversalPaths).to.have.lengthOf(1); 5213af6ab5fSopenharmony_ci expect(configs.keepUniversalPaths).to.have.lengthOf(1); 5223af6ab5fSopenharmony_ci expect(configs.excludePathSet).to.have.lengthOf(0); 5233af6ab5fSopenharmony_ci expect(configs.keepSourceOfPaths).to.have.lengthOf(0); 5243af6ab5fSopenharmony_ci }); 5253af6ab5fSopenharmony_ci 5263af6ab5fSopenharmony_ci it('should resolve keep config correctly, both start with "!", contains "*" and "?"', () => { 5273af6ab5fSopenharmony_ci const keepConfigs = ['!test/*/exclude.js', '!test/?/include.js']; 5283af6ab5fSopenharmony_ci const configs = new MergedConfig(); 5293af6ab5fSopenharmony_ci const configPath = './test/testData/obfuscation/keepDts/obfuscation-template.txt'; 5303af6ab5fSopenharmony_ci 5313af6ab5fSopenharmony_ci newObConfigResolver.resolveKeepConfig(keepConfigs, configs, configPath); 5323af6ab5fSopenharmony_ci 5333af6ab5fSopenharmony_ci expect(configs.excludeUniversalPaths).to.have.lengthOf(2); 5343af6ab5fSopenharmony_ci expect(configs.keepUniversalPaths).to.have.lengthOf(0); 5353af6ab5fSopenharmony_ci expect(configs.excludePathSet).to.have.lengthOf(0); 5363af6ab5fSopenharmony_ci expect(configs.keepSourceOfPaths).to.have.lengthOf(0); 5373af6ab5fSopenharmony_ci }); 5383af6ab5fSopenharmony_ci 5393af6ab5fSopenharmony_ci it('should resolve keep config correctly, not starts with "!" and contains "*" and "?"', () => { 5403af6ab5fSopenharmony_ci const keepConfigs = ['test/*/exclude.js', 'test/?/include.js']; 5413af6ab5fSopenharmony_ci const configs = new MergedConfig(); 5423af6ab5fSopenharmony_ci const configPath = './test/testData/obfuscation/keepDts/obfuscation-template.txt'; 5433af6ab5fSopenharmony_ci 5443af6ab5fSopenharmony_ci newObConfigResolver.resolveKeepConfig(keepConfigs, configs, configPath); 5453af6ab5fSopenharmony_ci 5463af6ab5fSopenharmony_ci expect(configs.excludeUniversalPaths).to.have.lengthOf(0); 5473af6ab5fSopenharmony_ci expect(configs.keepUniversalPaths).to.have.lengthOf(2); 5483af6ab5fSopenharmony_ci expect(configs.excludePathSet).to.have.lengthOf(0); 5493af6ab5fSopenharmony_ci expect(configs.keepSourceOfPaths).to.have.lengthOf(0); 5503af6ab5fSopenharmony_ci }); 5513af6ab5fSopenharmony_ci 5523af6ab5fSopenharmony_ci it('should resolve keep config correctly, starts with "!", not starts with "!" and contains "*"', () => { 5533af6ab5fSopenharmony_ci const keepConfigs = ['!test/exclude.js', 'test/*/include.js']; 5543af6ab5fSopenharmony_ci const configs = new MergedConfig(); 5553af6ab5fSopenharmony_ci const configPath = './test/testData/obfuscation/keepDts/obfuscation-template.txt'; 5563af6ab5fSopenharmony_ci 5573af6ab5fSopenharmony_ci newObConfigResolver.resolveKeepConfig(keepConfigs, configs, configPath); 5583af6ab5fSopenharmony_ci 5593af6ab5fSopenharmony_ci expect(configs.excludeUniversalPaths).to.have.lengthOf(0); 5603af6ab5fSopenharmony_ci expect(configs.keepUniversalPaths).to.have.lengthOf(1); 5613af6ab5fSopenharmony_ci expect(configs.excludePathSet).to.have.lengthOf(1); 5623af6ab5fSopenharmony_ci expect(configs.keepSourceOfPaths).to.have.lengthOf(0); 5633af6ab5fSopenharmony_ci }); 5643af6ab5fSopenharmony_ci 5653af6ab5fSopenharmony_ci it('should resolve keep config correctly, starts with "!" and not starts with "!"', () => { 5663af6ab5fSopenharmony_ci const keepConfigs = ['!test/exclude.js', 'test/include.js']; 5673af6ab5fSopenharmony_ci const configs = new MergedConfig(); 5683af6ab5fSopenharmony_ci const configPath = './test/testData/obfuscation/keepDts/obfuscation-template.txt'; 5693af6ab5fSopenharmony_ci 5703af6ab5fSopenharmony_ci newObConfigResolver.resolveKeepConfig(keepConfigs, configs, configPath); 5713af6ab5fSopenharmony_ci 5723af6ab5fSopenharmony_ci expect(configs.excludeUniversalPaths).to.have.lengthOf(0); 5733af6ab5fSopenharmony_ci expect(configs.keepUniversalPaths).to.have.lengthOf(0); 5743af6ab5fSopenharmony_ci expect(configs.excludePathSet).to.have.lengthOf(1); 5753af6ab5fSopenharmony_ci expect(configs.keepSourceOfPaths).to.have.lengthOf(0); 5763af6ab5fSopenharmony_ci }); 5773af6ab5fSopenharmony_ci 5783af6ab5fSopenharmony_ci it('should resolve keep config correctly, both start with "!"', () => { 5793af6ab5fSopenharmony_ci const keepConfigs = ['!test/exclude.js', '!test/include.js']; 5803af6ab5fSopenharmony_ci const configs = new MergedConfig(); 5813af6ab5fSopenharmony_ci const configPath = './test/testData/obfuscation/keepDts/obfuscation-template.txt'; 5823af6ab5fSopenharmony_ci 5833af6ab5fSopenharmony_ci newObConfigResolver.resolveKeepConfig(keepConfigs, configs, configPath); 5843af6ab5fSopenharmony_ci 5853af6ab5fSopenharmony_ci expect(configs.excludeUniversalPaths).to.have.lengthOf(0); 5863af6ab5fSopenharmony_ci expect(configs.keepUniversalPaths).to.have.lengthOf(0); 5873af6ab5fSopenharmony_ci expect(configs.excludePathSet).to.have.lengthOf(2); 5883af6ab5fSopenharmony_ci expect(configs.keepSourceOfPaths).to.have.lengthOf(0); 5893af6ab5fSopenharmony_ci }); 5903af6ab5fSopenharmony_ci }); 5913af6ab5fSopenharmony_ci 5923af6ab5fSopenharmony_ci describe('resolvePath', () => { 5933af6ab5fSopenharmony_ci it('should return the absolute path if token is already an absolute path', () => { 5943af6ab5fSopenharmony_ci const configPath = '/home/user/config.json'; 5953af6ab5fSopenharmony_ci const token = '/home/user/data.txt'; 5963af6ab5fSopenharmony_ci const result = newObConfigResolver.resolvePathForTest(configPath, token); 5973af6ab5fSopenharmony_ci expect(result).to.equal(token); 5983af6ab5fSopenharmony_ci }); 5993af6ab5fSopenharmony_ci 6003af6ab5fSopenharmony_ci it('should resolve the relative path based on the config file directory', () => { 6013af6ab5fSopenharmony_ci const configPath = '/home/user/config.json'; 6023af6ab5fSopenharmony_ci const token = 'data.txt'; 6033af6ab5fSopenharmony_ci const expectedResult = '/home/user/data.txt'; 6043af6ab5fSopenharmony_ci const result = newObConfigResolver.resolvePathForTest(configPath, token); 6053af6ab5fSopenharmony_ci expect(result).to.equal(expectedResult); 6063af6ab5fSopenharmony_ci }); 6073af6ab5fSopenharmony_ci }); 6083af6ab5fSopenharmony_ci 6093af6ab5fSopenharmony_ci describe('getTokenType', () => { 6103af6ab5fSopenharmony_ci it('should return the correct OptionType for each token', () => { 6113af6ab5fSopenharmony_ci const tokens = [ 6123af6ab5fSopenharmony_ci ObConfigResolver.KEEP_DTS, 6133af6ab5fSopenharmony_ci ObConfigResolver.KEEP_GLOBAL_NAME, 6143af6ab5fSopenharmony_ci ObConfigResolver.KEEP_PROPERTY_NAME, 6153af6ab5fSopenharmony_ci ObConfigResolver.KEEP_FILE_NAME, 6163af6ab5fSopenharmony_ci ObConfigResolver.KEEP_COMMENTS, 6173af6ab5fSopenharmony_ci ObConfigResolver.DISABLE_OBFUSCATION, 6183af6ab5fSopenharmony_ci ObConfigResolver.ENABLE_PROPERTY_OBFUSCATION, 6193af6ab5fSopenharmony_ci ObConfigResolver.ENABLE_STRING_PROPERTY_OBFUSCATION, 6203af6ab5fSopenharmony_ci ObConfigResolver.ENABLE_TOPLEVEL_OBFUSCATION, 6213af6ab5fSopenharmony_ci ObConfigResolver.ENABLE_FILENAME_OBFUSCATION, 6223af6ab5fSopenharmony_ci ObConfigResolver.ENABLE_EXPORT_OBFUSCATION, 6233af6ab5fSopenharmony_ci ObConfigResolver.REMOVE_COMMENTS, 6243af6ab5fSopenharmony_ci ObConfigResolver.COMPACT, 6253af6ab5fSopenharmony_ci ObConfigResolver.REMOVE_LOG, 6263af6ab5fSopenharmony_ci ObConfigResolver.PRINT_NAMECACHE, 6273af6ab5fSopenharmony_ci ObConfigResolver.PRINT_KEPT_NAMES, 6283af6ab5fSopenharmony_ci ObConfigResolver.APPLY_NAMECACHE, 6293af6ab5fSopenharmony_ci ObConfigResolver.KEEP, 6303af6ab5fSopenharmony_ci 'unknown_token' 6313af6ab5fSopenharmony_ci ]; 6323af6ab5fSopenharmony_ci 6333af6ab5fSopenharmony_ci const expectedResults = [ 6343af6ab5fSopenharmony_ci OptionTypeForTest.KEEP_DTS, 6353af6ab5fSopenharmony_ci OptionTypeForTest.KEEP_GLOBAL_NAME, 6363af6ab5fSopenharmony_ci OptionTypeForTest.KEEP_PROPERTY_NAME, 6373af6ab5fSopenharmony_ci OptionTypeForTest.KEEP_FILE_NAME, 6383af6ab5fSopenharmony_ci OptionTypeForTest.KEEP_COMMENTS, 6393af6ab5fSopenharmony_ci OptionTypeForTest.DISABLE_OBFUSCATION, 6403af6ab5fSopenharmony_ci OptionTypeForTest.ENABLE_PROPERTY_OBFUSCATION, 6413af6ab5fSopenharmony_ci OptionTypeForTest.ENABLE_STRING_PROPERTY_OBFUSCATION, 6423af6ab5fSopenharmony_ci OptionTypeForTest.ENABLE_TOPLEVEL_OBFUSCATION, 6433af6ab5fSopenharmony_ci OptionTypeForTest.ENABLE_FILENAME_OBFUSCATION, 6443af6ab5fSopenharmony_ci OptionTypeForTest.ENABLE_EXPORT_OBFUSCATION, 6453af6ab5fSopenharmony_ci OptionTypeForTest.REMOVE_COMMENTS, 6463af6ab5fSopenharmony_ci OptionTypeForTest.COMPACT, 6473af6ab5fSopenharmony_ci OptionTypeForTest.REMOVE_LOG, 6483af6ab5fSopenharmony_ci OptionTypeForTest.PRINT_NAMECACHE, 6493af6ab5fSopenharmony_ci OptionTypeForTest.PRINT_KEPT_NAMES, 6503af6ab5fSopenharmony_ci OptionTypeForTest.APPLY_NAMECACHE, 6513af6ab5fSopenharmony_ci OptionTypeForTest.KEEP, 6523af6ab5fSopenharmony_ci OptionTypeForTest.NONE 6533af6ab5fSopenharmony_ci ]; 6543af6ab5fSopenharmony_ci 6553af6ab5fSopenharmony_ci for (let i = 0; i < tokens.length; i++) { 6563af6ab5fSopenharmony_ci const result = newObConfigResolver.getTokenTypeForTest(tokens[i]); 6573af6ab5fSopenharmony_ci expect(result).to.equal(expectedResults[i]); 6583af6ab5fSopenharmony_ci } 6593af6ab5fSopenharmony_ci }); 6603af6ab5fSopenharmony_ci }); 6613af6ab5fSopenharmony_ci 6623af6ab5fSopenharmony_ci describe('handleConfigContent', () => { 6633af6ab5fSopenharmony_ci it('should handle config content correctly', () => { 6643af6ab5fSopenharmony_ci const configs: MergedConfig = new MergedConfig(); 6653af6ab5fSopenharmony_ci configs.options = new ObOptionsForTest(); 6663af6ab5fSopenharmony_ci 6673af6ab5fSopenharmony_ci const configPath = './test/testData/obfuscation/keepDts/obfuscation-template.txt'; 6683af6ab5fSopenharmony_ci const data = ` 6693af6ab5fSopenharmony_ci #This is a comment 6703af6ab5fSopenharmony_ci -none, 6713af6ab5fSopenharmony_ci -keep obfuscation-template.txt, 6723af6ab5fSopenharmony_ci -keep-dts, 6733af6ab5fSopenharmony_ci -keep-global-name, 6743af6ab5fSopenharmony_ci -keep-property-name, 6753af6ab5fSopenharmony_ci -keep-file-name, 6763af6ab5fSopenharmony_ci -keep-comments, 6773af6ab5fSopenharmony_ci -disable-obfuscation, 6783af6ab5fSopenharmony_ci -enable-property-obfuscation, 6793af6ab5fSopenharmony_ci -enable-string-property-obfuscation, 6803af6ab5fSopenharmony_ci -enable-toplevel-obfuscation, 6813af6ab5fSopenharmony_ci -enable-filename-obfuscation, 6823af6ab5fSopenharmony_ci -enable-export-obfuscation, 6833af6ab5fSopenharmony_ci -remove-comments, 6843af6ab5fSopenharmony_ci -compact, 6853af6ab5fSopenharmony_ci -remove-log, 6863af6ab5fSopenharmony_ci -print-namecache obfuscation-template.txt, 6873af6ab5fSopenharmony_ci -print-kept-names, 6883af6ab5fSopenharmony_ci -apply-namecache obfuscation-template.txt 6893af6ab5fSopenharmony_ci `; 6903af6ab5fSopenharmony_ci 6913af6ab5fSopenharmony_ci newObConfigResolver.handleConfigContentForTest(data, configs, configPath); 6923af6ab5fSopenharmony_ci 6933af6ab5fSopenharmony_ci expect(configs.options.disableObfuscation).to.be.true; 6943af6ab5fSopenharmony_ci expect(configs.options.enablePropertyObfuscation).to.be.true; 6953af6ab5fSopenharmony_ci expect(configs.options.enableStringPropertyObfuscation).to.be.true; 6963af6ab5fSopenharmony_ci expect(configs.options.enableToplevelObfuscation).to.be.true; 6973af6ab5fSopenharmony_ci expect(configs.options.removeComments).to.be.true; 6983af6ab5fSopenharmony_ci expect(configs.options.enableFileNameObfuscation).to.be.true; 6993af6ab5fSopenharmony_ci expect(configs.options.enableExportObfuscation).to.be.true; 7003af6ab5fSopenharmony_ci expect(configs.options.compact).to.be.true; 7013af6ab5fSopenharmony_ci expect(configs.options.removeLog).to.be.true; 7023af6ab5fSopenharmony_ci }); 7033af6ab5fSopenharmony_ci }); 7043af6ab5fSopenharmony_ci 7053af6ab5fSopenharmony_ci describe('1: test Api getSystemApiCache', function() { 7063af6ab5fSopenharmony_ci it('1-1: test getSystemApiCache: -enable-property-obfuscation', function () { 7073af6ab5fSopenharmony_ci let obfuscationCacheDir = path.join(OBFUSCATE_TESTDATA_DIR, 'property'); 7083af6ab5fSopenharmony_ci let obfuscationOptions = { 7093af6ab5fSopenharmony_ci 'selfConfig': { 7103af6ab5fSopenharmony_ci 'ruleOptions': { 7113af6ab5fSopenharmony_ci 'enable': true, 7123af6ab5fSopenharmony_ci 'rules': [ 7133af6ab5fSopenharmony_ci path.join(OBFUSCATE_TESTDATA_DIR, 'property/property.txt') 7143af6ab5fSopenharmony_ci ] 7153af6ab5fSopenharmony_ci }, 7163af6ab5fSopenharmony_ci 'consumerRules': [], 7173af6ab5fSopenharmony_ci }, 7183af6ab5fSopenharmony_ci 'dependencies': { 7193af6ab5fSopenharmony_ci 'libraries': [], 7203af6ab5fSopenharmony_ci 'hars': [] 7213af6ab5fSopenharmony_ci }, 7223af6ab5fSopenharmony_ci 'obfuscationCacheDir': obfuscationCacheDir, 7233af6ab5fSopenharmony_ci 'sdkApis': [ 7243af6ab5fSopenharmony_ci path.join(OBFUSCATE_TESTDATA_DIR, 'system_api.d.ts') 7253af6ab5fSopenharmony_ci ] 7263af6ab5fSopenharmony_ci }; 7273af6ab5fSopenharmony_ci let projectConfig = { 7283af6ab5fSopenharmony_ci obfuscationOptions, 7293af6ab5fSopenharmony_ci compileHar: false 7303af6ab5fSopenharmony_ci }; 7313af6ab5fSopenharmony_ci const obConfig: ObConfigResolver = new ObConfigResolver(projectConfig, undefined); 7323af6ab5fSopenharmony_ci obConfig.resolveObfuscationConfigs(); 7333af6ab5fSopenharmony_ci const reservedSdkApiForProp = UnobfuscationCollections.reservedSdkApiForProp; 7343af6ab5fSopenharmony_ci const reservedSdkApiForGlobal = UnobfuscationCollections.reservedSdkApiForGlobal; 7353af6ab5fSopenharmony_ci 7363af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.size == 12).to.be.true; 7373af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('TestClass')).to.be.true; 7383af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('para1')).to.be.true; 7393af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('para2')).to.be.true; 7403af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('foo')).to.be.true; 7413af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('TestFunction')).to.be.true; 7423af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('funcPara1')).to.be.true; 7433af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('funcPara2')).to.be.true; 7443af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('ns')).to.be.true; 7453af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('NavigationBuilderRegister')).to.be.true; 7463af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('ViewV2')).to.be.true; 7473af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('initParam')).to.be.true; 7483af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.has('updateParam')).to.be.true; 7493af6ab5fSopenharmony_ci expect(reservedSdkApiForGlobal.size == 0).to.be.true; 7503af6ab5fSopenharmony_ci UnobfuscationCollections.clear(); 7513af6ab5fSopenharmony_ci 7523af6ab5fSopenharmony_ci let systemApiPath = obfuscationCacheDir + '/systemApiCache.json'; 7533af6ab5fSopenharmony_ci const data = fs.readFileSync(systemApiPath, 'utf8'); 7543af6ab5fSopenharmony_ci const systemApiContent = JSON.parse(data); 7553af6ab5fSopenharmony_ci 7563af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.length == 12).to.be.true; 7573af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('TestClass')).to.be.true; 7583af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('para1')).to.be.true; 7593af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('para2')).to.be.true; 7603af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('foo')).to.be.true; 7613af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('TestFunction')).to.be.true; 7623af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('funcPara1')).to.be.true; 7633af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('funcPara2')).to.be.true; 7643af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('ns')).to.be.true; 7653af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('NavigationBuilderRegister')).to.be.true; 7663af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('ViewV2')).to.be.true; 7673af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('initParam')).to.be.true; 7683af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames.includes('updateParam')).to.be.true; 7693af6ab5fSopenharmony_ci expect(systemApiContent.ReservedGlobalNames == undefined).to.be.true; 7703af6ab5fSopenharmony_ci 7713af6ab5fSopenharmony_ci fs.unlinkSync(systemApiPath); 7723af6ab5fSopenharmony_ci }); 7733af6ab5fSopenharmony_ci 7743af6ab5fSopenharmony_ci it('1-2: test getSystemApiCache: -enable-export-obfuscation', function () { 7753af6ab5fSopenharmony_ci let obfuscationCacheDir = path.join(OBFUSCATE_TESTDATA_DIR, 'export'); 7763af6ab5fSopenharmony_ci let obfuscationOptions = { 7773af6ab5fSopenharmony_ci 'selfConfig': { 7783af6ab5fSopenharmony_ci 'ruleOptions': { 7793af6ab5fSopenharmony_ci 'enable': true, 7803af6ab5fSopenharmony_ci 'rules': [ 7813af6ab5fSopenharmony_ci path.join(OBFUSCATE_TESTDATA_DIR, 'export/export.txt') 7823af6ab5fSopenharmony_ci ] 7833af6ab5fSopenharmony_ci }, 7843af6ab5fSopenharmony_ci 'consumerRules': [], 7853af6ab5fSopenharmony_ci }, 7863af6ab5fSopenharmony_ci 'dependencies': { 7873af6ab5fSopenharmony_ci 'libraries': [], 7883af6ab5fSopenharmony_ci 'hars': [] 7893af6ab5fSopenharmony_ci }, 7903af6ab5fSopenharmony_ci 'obfuscationCacheDir': obfuscationCacheDir, 7913af6ab5fSopenharmony_ci 'sdkApis': [ 7923af6ab5fSopenharmony_ci path.join(OBFUSCATE_TESTDATA_DIR, 'system_api.d.ts') 7933af6ab5fSopenharmony_ci ] 7943af6ab5fSopenharmony_ci }; 7953af6ab5fSopenharmony_ci let projectConfig = { 7963af6ab5fSopenharmony_ci obfuscationOptions, 7973af6ab5fSopenharmony_ci compileHar: false 7983af6ab5fSopenharmony_ci }; 7993af6ab5fSopenharmony_ci const obConfig: ObConfigResolver = new ObConfigResolver(projectConfig, undefined); 8003af6ab5fSopenharmony_ci obConfig.resolveObfuscationConfigs(); 8013af6ab5fSopenharmony_ci const reservedSdkApiForProp = UnobfuscationCollections.reservedSdkApiForProp; 8023af6ab5fSopenharmony_ci const reservedSdkApiForGlobal = UnobfuscationCollections.reservedSdkApiForGlobal; 8033af6ab5fSopenharmony_ci 8043af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.size == 0).to.be.true; 8053af6ab5fSopenharmony_ci expect(reservedSdkApiForGlobal.size == 0).to.be.true; 8063af6ab5fSopenharmony_ci UnobfuscationCollections.clear(); 8073af6ab5fSopenharmony_ci 8083af6ab5fSopenharmony_ci let systemApiPath = obfuscationCacheDir + '/systemApiCache.json'; 8093af6ab5fSopenharmony_ci const noSystemApi = fs.existsSync(systemApiPath); 8103af6ab5fSopenharmony_ci 8113af6ab5fSopenharmony_ci expect(noSystemApi).to.be.false; 8123af6ab5fSopenharmony_ci }); 8133af6ab5fSopenharmony_ci 8143af6ab5fSopenharmony_ci it('1-3: test getSystemApiCache: -enable-export-obfuscation -enable-toplevel-obfuscation', function () { 8153af6ab5fSopenharmony_ci let obfuscationCacheDir = path.join(OBFUSCATE_TESTDATA_DIR, 'export_toplevel'); 8163af6ab5fSopenharmony_ci let obfuscationOptions = { 8173af6ab5fSopenharmony_ci 'selfConfig': { 8183af6ab5fSopenharmony_ci 'ruleOptions': { 8193af6ab5fSopenharmony_ci 'enable': true, 8203af6ab5fSopenharmony_ci 'rules': [ 8213af6ab5fSopenharmony_ci path.join(OBFUSCATE_TESTDATA_DIR, 'export_toplevel/export_toplevel.txt') 8223af6ab5fSopenharmony_ci ] 8233af6ab5fSopenharmony_ci }, 8243af6ab5fSopenharmony_ci 'consumerRules': [], 8253af6ab5fSopenharmony_ci }, 8263af6ab5fSopenharmony_ci 'dependencies': { 8273af6ab5fSopenharmony_ci 'libraries': [], 8283af6ab5fSopenharmony_ci 'hars': [] 8293af6ab5fSopenharmony_ci }, 8303af6ab5fSopenharmony_ci 'obfuscationCacheDir': obfuscationCacheDir, 8313af6ab5fSopenharmony_ci 'sdkApis': [ 8323af6ab5fSopenharmony_ci path.join(OBFUSCATE_TESTDATA_DIR, 'system_api.d.ts') 8333af6ab5fSopenharmony_ci ] 8343af6ab5fSopenharmony_ci }; 8353af6ab5fSopenharmony_ci let projectConfig = { 8363af6ab5fSopenharmony_ci obfuscationOptions, 8373af6ab5fSopenharmony_ci compileHar: false 8383af6ab5fSopenharmony_ci }; 8393af6ab5fSopenharmony_ci const obConfig: ObConfigResolver = new ObConfigResolver(projectConfig, undefined); 8403af6ab5fSopenharmony_ci obConfig.resolveObfuscationConfigs(); 8413af6ab5fSopenharmony_ci const reservedSdkApiForProp = UnobfuscationCollections.reservedSdkApiForProp; 8423af6ab5fSopenharmony_ci const reservedSdkApiForGlobal = UnobfuscationCollections.reservedSdkApiForGlobal; 8433af6ab5fSopenharmony_ci 8443af6ab5fSopenharmony_ci expect(reservedSdkApiForProp.size == 0).to.be.true; 8453af6ab5fSopenharmony_ci expect(reservedSdkApiForGlobal.size == 3).to.be.true; 8463af6ab5fSopenharmony_ci expect(reservedSdkApiForGlobal.has('TestClass')).to.be.true; 8473af6ab5fSopenharmony_ci expect(reservedSdkApiForGlobal.has('TestFunction')).to.be.true; 8483af6ab5fSopenharmony_ci expect(reservedSdkApiForGlobal.has('ns')).to.be.true; 8493af6ab5fSopenharmony_ci UnobfuscationCollections.clear(); 8503af6ab5fSopenharmony_ci 8513af6ab5fSopenharmony_ci let systemApiPath = obfuscationCacheDir + '/systemApiCache.json'; 8523af6ab5fSopenharmony_ci const data = fs.readFileSync(systemApiPath, 'utf8'); 8533af6ab5fSopenharmony_ci const systemApiContent = JSON.parse(data); 8543af6ab5fSopenharmony_ci 8553af6ab5fSopenharmony_ci expect(systemApiContent.ReservedPropertyNames == undefined).to.be.true; 8563af6ab5fSopenharmony_ci expect(systemApiContent.ReservedGlobalNames.length == 3).to.be.true; 8573af6ab5fSopenharmony_ci expect(systemApiContent.ReservedGlobalNames.includes('TestClass')).to.be.true; 8583af6ab5fSopenharmony_ci expect(systemApiContent.ReservedGlobalNames.includes('TestFunction')).to.be.true; 8593af6ab5fSopenharmony_ci expect(systemApiContent.ReservedGlobalNames.includes('ns')).to.be.true; 8603af6ab5fSopenharmony_ci 8613af6ab5fSopenharmony_ci fs.unlinkSync(systemApiPath); 8623af6ab5fSopenharmony_ci }); 8633af6ab5fSopenharmony_ci }); 8643af6ab5fSopenharmony_ci }); 8653af6ab5fSopenharmony_ci 8663af6ab5fSopenharmony_ci describe('collectResevedFileNameInIDEConfig', () => { 8673af6ab5fSopenharmony_ci let collectPathReservedStringStub; 8683af6ab5fSopenharmony_ci 8693af6ab5fSopenharmony_ci beforeEach(function() { 8703af6ab5fSopenharmony_ci collectPathReservedStringStub = sinon.stub(FileUtils, 'collectPathReservedString'); 8713af6ab5fSopenharmony_ci collectPathReservedStringStub.callsFake((filePath, reservedArray) => reservedArray.push(filePath)); 8723af6ab5fSopenharmony_ci }); 8733af6ab5fSopenharmony_ci 8743af6ab5fSopenharmony_ci afterEach(function() { 8753af6ab5fSopenharmony_ci collectPathReservedStringStub.restore(); 8763af6ab5fSopenharmony_ci projectConfig.compileShared = false; 8773af6ab5fSopenharmony_ci projectConfig.byteCodeHar = false; 8783af6ab5fSopenharmony_ci projectConfig.aceModuleJsonPath = ''; 8793af6ab5fSopenharmony_ci }); 8803af6ab5fSopenharmony_ci 8813af6ab5fSopenharmony_ci const ohPackagePath = './test/ut/initialization/testOhPackagePath.json'; 8823af6ab5fSopenharmony_ci let projectConfig = { 8833af6ab5fSopenharmony_ci aceModuleJsonPath: '', 8843af6ab5fSopenharmony_ci projectPath: 'path/to/project', 8853af6ab5fSopenharmony_ci cachePath: 'path/to/cache', 8863af6ab5fSopenharmony_ci compileShared: false, 8873af6ab5fSopenharmony_ci byteCodeHar: false, 8883af6ab5fSopenharmony_ci aceModuleBuild: 'path/to/build', 8893af6ab5fSopenharmony_ci } 8903af6ab5fSopenharmony_ci const entryArray = ['path/to/entry1', 'path/to/entry2']; 8913af6ab5fSopenharmony_ci const modulePathMap = { 8923af6ab5fSopenharmony_ci module1: 'path/to/module1', 8933af6ab5fSopenharmony_ci module2: 'path/to/module2', 8943af6ab5fSopenharmony_ci }; 8953af6ab5fSopenharmony_ci 8963af6ab5fSopenharmony_ci it('should collect reserved file names from entryArray and projectConfig', () => { 8973af6ab5fSopenharmony_ci const result = collectResevedFileNameInIDEConfig('', projectConfig, undefined, entryArray); 8983af6ab5fSopenharmony_ci expect(result).to.deep.equal(['path/to/entry1', 'path/to/entry2','path/to/project','path/to/cache']); 8993af6ab5fSopenharmony_ci }); 9003af6ab5fSopenharmony_ci it('should collect reserved file names from modulePathMap and projectConfig', () => { 9013af6ab5fSopenharmony_ci projectConfig.compileShared = true; 9023af6ab5fSopenharmony_ci const result = collectResevedFileNameInIDEConfig('', projectConfig, modulePathMap, []); 9033af6ab5fSopenharmony_ci expect(result).to.deep.equal(['path/to/module1', 'path/to/module2','module1','module2','path/to/build','etsFortgz','path/to/project','path/to/cache']); 9043af6ab5fSopenharmony_ci }); 9053af6ab5fSopenharmony_ci it('should collect reserved file names from ohPackagePath and projectConfig', () => { 9063af6ab5fSopenharmony_ci projectConfig.byteCodeHar = true; 9073af6ab5fSopenharmony_ci const result = collectResevedFileNameInIDEConfig(ohPackagePath, projectConfig, undefined, []); 9083af6ab5fSopenharmony_ci expect(result).to.deep.equal(['path/to/main', 'path/to/types','path/to/build','etsFortgz','path/to/project','path/to/cache']); 9093af6ab5fSopenharmony_ci }); 9103af6ab5fSopenharmony_ci it('should collect reserved file names from moduleJsonPath and projectConfig', () => { 9113af6ab5fSopenharmony_ci projectConfig.aceModuleJsonPath = "./test/ut/initialization/testModuleJsonPath_0.json"; 9123af6ab5fSopenharmony_ci const hasSrcEntry = collectResevedFileNameInIDEConfig('', projectConfig, undefined, []); 9133af6ab5fSopenharmony_ci expect(hasSrcEntry).to.deep.equal(['path/to/srcEntry','path/to/project','path/to/cache']); 9143af6ab5fSopenharmony_ci 9153af6ab5fSopenharmony_ci projectConfig.aceModuleJsonPath = "./test/ut/initialization/testModuleJsonPath_1.json"; 9163af6ab5fSopenharmony_ci const noSrcEntry = collectResevedFileNameInIDEConfig('', projectConfig, undefined, []); 9173af6ab5fSopenharmony_ci expect(noSrcEntry).to.deep.equal(['path/to/project','path/to/cache']); 9183af6ab5fSopenharmony_ci }); 9193af6ab5fSopenharmony_ci }); 9203af6ab5fSopenharmony_ci 9213af6ab5fSopenharmony_ci describe('readNameCache', () => { 9223af6ab5fSopenharmony_ci let tempFilePath; 9233af6ab5fSopenharmony_ci let testData; 9243af6ab5fSopenharmony_ci let fsWriteFileSyncStub; 9253af6ab5fSopenharmony_ci let fsUnlinkSyncStub; 9263af6ab5fSopenharmony_ci let logger = { 9273af6ab5fSopenharmony_ci error: (message: string) => console.error(message), 9283af6ab5fSopenharmony_ci }; 9293af6ab5fSopenharmony_ci let PropCollections; 9303af6ab5fSopenharmony_ci let renameFileNameModule; 9313af6ab5fSopenharmony_ci 9323af6ab5fSopenharmony_ci beforeEach(() => { 9333af6ab5fSopenharmony_ci PropCollections = { 9343af6ab5fSopenharmony_ci historyMangledTable: {}, 9353af6ab5fSopenharmony_ci }; 9363af6ab5fSopenharmony_ci renameFileNameModule = { 9373af6ab5fSopenharmony_ci historyFileNameMangledTable: {}, 9383af6ab5fSopenharmony_ci }; 9393af6ab5fSopenharmony_ci 9403af6ab5fSopenharmony_ci tempFilePath = './test/ut/initialization/tempNameCache.json'; 9413af6ab5fSopenharmony_ci testData = { 9423af6ab5fSopenharmony_ci compileSdkVersion: '1.0.0', 9433af6ab5fSopenharmony_ci PropertyCache: { key1: 'value1', key2: 'value2' }, 9443af6ab5fSopenharmony_ci FileNameCache: { key3: 'value3', key4: 'value4' }, 9453af6ab5fSopenharmony_ci extraKey: '', 9463af6ab5fSopenharmony_ci }; 9473af6ab5fSopenharmony_ci 9483af6ab5fSopenharmony_ci fsWriteFileSyncStub = sinon.stub(fs, 'writeFileSync').callsFake((path, data) => {}); 9493af6ab5fSopenharmony_ci fsUnlinkSyncStub = sinon.stub(fs, 'unlinkSync').callsFake((path) => {}); 9503af6ab5fSopenharmony_ci }); 9513af6ab5fSopenharmony_ci 9523af6ab5fSopenharmony_ci afterEach(() => { 9533af6ab5fSopenharmony_ci fsWriteFileSyncStub.restore(); 9543af6ab5fSopenharmony_ci fsUnlinkSyncStub.restore(); 9553af6ab5fSopenharmony_ci }); 9563af6ab5fSopenharmony_ci 9573af6ab5fSopenharmony_ci it('should read and parse the name cache file correctly', () => { 9583af6ab5fSopenharmony_ci readNameCache(tempFilePath, logger); 9593af6ab5fSopenharmony_ci 9603af6ab5fSopenharmony_ci expect(nameCacheMap.get('extraKey')).to.equal(''); 9613af6ab5fSopenharmony_ci }); 9623af6ab5fSopenharmony_ci }); 9633af6ab5fSopenharmony_ci 9643af6ab5fSopenharmony_ci describe('readNameCache', () => { 9653af6ab5fSopenharmony_ci let fsReadFileSyncStub; 9663af6ab5fSopenharmony_ci let logger; 9673af6ab5fSopenharmony_ci const testPath = './test/ut/initialization/tempNameCache.json'; 9683af6ab5fSopenharmony_ci 9693af6ab5fSopenharmony_ci beforeEach(() => { 9703af6ab5fSopenharmony_ci fsReadFileSyncStub = sinon.stub(fs, 'readFileSync').returns('{"compileSdkVersion":"1.0","PropertyCache":{},"FileNameCache":{}}'); 9713af6ab5fSopenharmony_ci logger = { error: sinon.spy() }; 9723af6ab5fSopenharmony_ci }); 9733af6ab5fSopenharmony_ci 9743af6ab5fSopenharmony_ci afterEach(() => { 9753af6ab5fSopenharmony_ci fsReadFileSyncStub.restore(); 9763af6ab5fSopenharmony_ci }); 9773af6ab5fSopenharmony_ci 9783af6ab5fSopenharmony_ci it('should read the name cache file and parse its content', () => { 9793af6ab5fSopenharmony_ci readNameCache(testPath, logger); 9803af6ab5fSopenharmony_ci expect(PropCollections.historyMangledTable).to.deep.equal(new Map()); 9813af6ab5fSopenharmony_ci expect(renameFileNameModule.historyFileNameMangledTable).to.deep.equal(new Map()); 9823af6ab5fSopenharmony_ci expect(nameCacheMap.get('compileSdkVersion')).to.be.undefined; 9833af6ab5fSopenharmony_ci expect(logger.error.called).to.be.false; 9843af6ab5fSopenharmony_ci }); 9853af6ab5fSopenharmony_ci 9863af6ab5fSopenharmony_ci it('should handle errors when reading the file', () => { 9873af6ab5fSopenharmony_ci const mockLogger = { 9883af6ab5fSopenharmony_ci error: (message: string) => console.error(message), 9893af6ab5fSopenharmony_ci }; 9903af6ab5fSopenharmony_ci 9913af6ab5fSopenharmony_ci const nonExistentFilePath = './test/ut/initialization/nonExistentFile.json'; 9923af6ab5fSopenharmony_ci readNameCache(nonExistentFilePath, mockLogger); 9933af6ab5fSopenharmony_ci fsReadFileSyncStub.throws(new Error('Test error')); 9943af6ab5fSopenharmony_ci readNameCache(testPath, logger); 9953af6ab5fSopenharmony_ci expect(logger.error.calledWith(`Failed to open ${nonExistentFilePath}. Error message: Test error`)).to.be.false; 9963af6ab5fSopenharmony_ci }); 9973af6ab5fSopenharmony_ci }); 9983af6ab5fSopenharmony_ci 9993af6ab5fSopenharmony_ci describe('handleUniversalPathInObf', () => { 10003af6ab5fSopenharmony_ci it('should handle universal paths correctly', () => { 10013af6ab5fSopenharmony_ci const mergedObConfig: MergedConfig = { 10023af6ab5fSopenharmony_ci options: new ObOptionsForTest(), 10033af6ab5fSopenharmony_ci reservedPropertyNames: [], 10043af6ab5fSopenharmony_ci reservedGlobalNames: [], 10053af6ab5fSopenharmony_ci reservedNames: [], 10063af6ab5fSopenharmony_ci reservedFileNames: [], 10073af6ab5fSopenharmony_ci keepComments: [], 10083af6ab5fSopenharmony_ci keepSourceOfPaths: [], 10093af6ab5fSopenharmony_ci universalReservedPropertyNames: [], 10103af6ab5fSopenharmony_ci universalReservedGlobalNames: [], 10113af6ab5fSopenharmony_ci keepUniversalPaths: [/test\.js$/], 10123af6ab5fSopenharmony_ci excludeUniversalPaths: [/exclude\.js$/], 10133af6ab5fSopenharmony_ci excludePathSet: new Set(), 10143af6ab5fSopenharmony_ci merge: () => {}, 10153af6ab5fSopenharmony_ci sortAndDeduplicate: () => {}, 10163af6ab5fSopenharmony_ci serializeMergedConfig: () => { 10173af6ab5fSopenharmony_ci return JSON.stringify(this); 10183af6ab5fSopenharmony_ci } 10193af6ab5fSopenharmony_ci }; 10203af6ab5fSopenharmony_ci const allSourceFilePaths = new Set([ 10213af6ab5fSopenharmony_ci 'test.js', 10223af6ab5fSopenharmony_ci 'exclude.js', 10233af6ab5fSopenharmony_ci 'other.js', 10243af6ab5fSopenharmony_ci ]); 10253af6ab5fSopenharmony_ci 10263af6ab5fSopenharmony_ci handleUniversalPathInObf(mergedObConfig, allSourceFilePaths); 10273af6ab5fSopenharmony_ci expect(mergedObConfig.keepSourceOfPaths).to.deep.equal(['test.js']); 10283af6ab5fSopenharmony_ci expect(mergedObConfig.excludePathSet).to.deep.equal(new Set(['exclude.js'])); 10293af6ab5fSopenharmony_ci }); 10303af6ab5fSopenharmony_ci 10313af6ab5fSopenharmony_ci it('should return early if mergedObConfig is not provided or both keepUniversalPaths and excludeUniversalPaths are empty', () => { 10323af6ab5fSopenharmony_ci const mergedObConfig: MergedConfig = { 10333af6ab5fSopenharmony_ci options: new ObOptionsForTest(), 10343af6ab5fSopenharmony_ci reservedPropertyNames: [], 10353af6ab5fSopenharmony_ci reservedGlobalNames: [], 10363af6ab5fSopenharmony_ci reservedNames: [], 10373af6ab5fSopenharmony_ci reservedFileNames: [], 10383af6ab5fSopenharmony_ci keepComments: [], 10393af6ab5fSopenharmony_ci keepSourceOfPaths: [], 10403af6ab5fSopenharmony_ci universalReservedPropertyNames: [], 10413af6ab5fSopenharmony_ci universalReservedGlobalNames: [], 10423af6ab5fSopenharmony_ci keepUniversalPaths: [], 10433af6ab5fSopenharmony_ci excludeUniversalPaths: [], 10443af6ab5fSopenharmony_ci excludePathSet: new Set(), 10453af6ab5fSopenharmony_ci merge: () => {}, 10463af6ab5fSopenharmony_ci sortAndDeduplicate: () => {}, 10473af6ab5fSopenharmony_ci serializeMergedConfig: () => { 10483af6ab5fSopenharmony_ci return JSON.stringify(this); 10493af6ab5fSopenharmony_ci } 10503af6ab5fSopenharmony_ci }; 10513af6ab5fSopenharmony_ci const allSourceFilePaths = new Set([]); 10523af6ab5fSopenharmony_ci const result = handleUniversalPathInObf(mergedObConfig, allSourceFilePaths); 10533af6ab5fSopenharmony_ci 10543af6ab5fSopenharmony_ci expect(result).to.be.undefined; 10553af6ab5fSopenharmony_ci }); 10563af6ab5fSopenharmony_ci }); 10573af6ab5fSopenharmony_ci 10583af6ab5fSopenharmony_ci describe('getArkguardNameCache', () => { 10593af6ab5fSopenharmony_ci it('should return a JSON string with the correct structure', () => { 10603af6ab5fSopenharmony_ci const enablePropertyObfuscation = true; 10613af6ab5fSopenharmony_ci const enableFileNameObfuscation = true; 10623af6ab5fSopenharmony_ci const sdkVersion = '1.0.0'; 10633af6ab5fSopenharmony_ci const entryPackageInfo = 'packageInfo'; 10643af6ab5fSopenharmony_ci 10653af6ab5fSopenharmony_ci const result = getArkguardNameCache(enablePropertyObfuscation, enableFileNameObfuscation, false, sdkVersion, entryPackageInfo); 10663af6ab5fSopenharmony_ci 10673af6ab5fSopenharmony_ci try { 10683af6ab5fSopenharmony_ci JSON.parse(result); 10693af6ab5fSopenharmony_ci // If no error is thrown, the result is a valid JSON string 10703af6ab5fSopenharmony_ci console.log('Test passed: getArkguardNameCache returns a valid JSON string'); 10713af6ab5fSopenharmony_ci } catch (error) { 10723af6ab5fSopenharmony_ci console.error('Test failed: getArkguardNameCache does not return a valid JSON string'); 10733af6ab5fSopenharmony_ci } 10743af6ab5fSopenharmony_ci }); 10753af6ab5fSopenharmony_ci 10763af6ab5fSopenharmony_ci it('should include the correct compileSdkVersion and entryPackageInfo', () => { 10773af6ab5fSopenharmony_ci const enablePropertyObfuscation = false; 10783af6ab5fSopenharmony_ci const enableFileNameObfuscation = false; 10793af6ab5fSopenharmony_ci const sdkVersion = '2.0.0'; 10803af6ab5fSopenharmony_ci const entryPackageInfo = 'anotherPackageInfo'; 10813af6ab5fSopenharmony_ci 10823af6ab5fSopenharmony_ci const result = getArkguardNameCache(enablePropertyObfuscation, enableFileNameObfuscation, false, sdkVersion, entryPackageInfo); 10833af6ab5fSopenharmony_ci const parsedResult = JSON.parse(result); 10843af6ab5fSopenharmony_ci 10853af6ab5fSopenharmony_ci expect(parsedResult.compileSdkVersion).to.equal(sdkVersion); 10863af6ab5fSopenharmony_ci expect(parsedResult.entryPackageInfo).to.equal(entryPackageInfo); 10873af6ab5fSopenharmony_ci }); 10883af6ab5fSopenharmony_ci 10893af6ab5fSopenharmony_ci it('PropertyCache exists when enable export obfuscation', () => { 10903af6ab5fSopenharmony_ci const enablePropertyObfuscation = false; 10913af6ab5fSopenharmony_ci const enableFileNameObfuscation = false; 10923af6ab5fSopenharmony_ci const enableExportObfuscation = true; 10933af6ab5fSopenharmony_ci const sdkVersion = '2.0.0'; 10943af6ab5fSopenharmony_ci const entryPackageInfo = 'anotherPackageInfo'; 10953af6ab5fSopenharmony_ci 10963af6ab5fSopenharmony_ci PropCollections.historyMangledTable.set("key1", "value1"); 10973af6ab5fSopenharmony_ci PropCollections.historyMangledTable.set("key2", "value2"); 10983af6ab5fSopenharmony_ci PropCollections.globalMangledTable.set("key3", "value3"); 10993af6ab5fSopenharmony_ci PropCollections.globalMangledTable.set("key4", "value4"); 11003af6ab5fSopenharmony_ci const result = getArkguardNameCache(enablePropertyObfuscation, enableFileNameObfuscation, enableExportObfuscation, sdkVersion, entryPackageInfo); 11013af6ab5fSopenharmony_ci const parsedResult = JSON.parse(result); 11023af6ab5fSopenharmony_ci PropCollections.historyMangledTable.clear(); 11033af6ab5fSopenharmony_ci PropCollections.globalMangledTable.clear(); 11043af6ab5fSopenharmony_ci expect('PropertyCache' in parsedResult).to.be.true; 11053af6ab5fSopenharmony_ci expect(parsedResult.PropertyCache.key1 === "value1").to.be.true; 11063af6ab5fSopenharmony_ci expect(parsedResult.PropertyCache.key2 === "value2").to.be.true; 11073af6ab5fSopenharmony_ci expect(parsedResult.PropertyCache.key3 === "value3").to.be.true; 11083af6ab5fSopenharmony_ci expect(parsedResult.PropertyCache.key4 === "value4").to.be.true; 11093af6ab5fSopenharmony_ci }); 11103af6ab5fSopenharmony_ci }); 11113af6ab5fSopenharmony_ci 11123af6ab5fSopenharmony_ci describe('fillNameCache', function() { 11133af6ab5fSopenharmony_ci it('should correctly fill the name cache with the given table entries', function() { 11143af6ab5fSopenharmony_ci const table = new Map([ 11153af6ab5fSopenharmony_ci ['key1', 'value1'], 11163af6ab5fSopenharmony_ci ['key2', 'value2'], 11173af6ab5fSopenharmony_ci ['key3', 'value3'] 11183af6ab5fSopenharmony_ci ]); 11193af6ab5fSopenharmony_ci const nameCache = new Map(); 11203af6ab5fSopenharmony_ci 11213af6ab5fSopenharmony_ci fillNameCache(table, nameCache); 11223af6ab5fSopenharmony_ci 11233af6ab5fSopenharmony_ci assert.deepEqual(nameCache, table); 11243af6ab5fSopenharmony_ci }); 11253af6ab5fSopenharmony_ci 11263af6ab5fSopenharmony_ci it('should handle empty tables gracefully', function() { 11273af6ab5fSopenharmony_ci const table = new Map(); 11283af6ab5fSopenharmony_ci const nameCache = new Map(); 11293af6ab5fSopenharmony_ci 11303af6ab5fSopenharmony_ci fillNameCache(table, nameCache); 11313af6ab5fSopenharmony_ci 11323af6ab5fSopenharmony_ci assert.deepEqual(nameCache, table); 11333af6ab5fSopenharmony_ci }); 11343af6ab5fSopenharmony_ci }); 11353af6ab5fSopenharmony_ci 11363af6ab5fSopenharmony_ci describe('writeObfuscationNameCache', () => { 11373af6ab5fSopenharmony_ci let existsSyncSpy; 11383af6ab5fSopenharmony_ci let mkdirSyncSpy; 11393af6ab5fSopenharmony_ci let writeFileSyncSpy; 11403af6ab5fSopenharmony_ci 11413af6ab5fSopenharmony_ci beforeEach(function() { 11423af6ab5fSopenharmony_ci existsSyncSpy = sinon.spy(fs, 'existsSync'); 11433af6ab5fSopenharmony_ci mkdirSyncSpy = sinon.spy(fs, 'mkdirSync'); 11443af6ab5fSopenharmony_ci writeFileSyncSpy = sinon.spy(fs, 'writeFileSync'); 11453af6ab5fSopenharmony_ci }); 11463af6ab5fSopenharmony_ci 11473af6ab5fSopenharmony_ci afterEach(function() { 11483af6ab5fSopenharmony_ci existsSyncSpy.restore(); 11493af6ab5fSopenharmony_ci mkdirSyncSpy.restore(); 11503af6ab5fSopenharmony_ci writeFileSyncSpy.restore(); 11513af6ab5fSopenharmony_ci }); 11523af6ab5fSopenharmony_ci 11533af6ab5fSopenharmony_ci it('should not write cache if projectConfig.arkObfuscator is false', () => { 11543af6ab5fSopenharmony_ci const projectConfig = { 11553af6ab5fSopenharmony_ci arkObfuscator: false, 11563af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 11573af6ab5fSopenharmony_ci options: { 11583af6ab5fSopenharmony_ci enablePropertyObfuscation: true, 11593af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 11603af6ab5fSopenharmony_ci }, 11613af6ab5fSopenharmony_ci }, 11623af6ab5fSopenharmony_ci etsLoaderVersion: '1.0.0', 11633af6ab5fSopenharmony_ci }; 11643af6ab5fSopenharmony_ci const entryPackageInfo = 'testEntryPackageInfo'; 11653af6ab5fSopenharmony_ci const obfuscationCacheDir = 'testCacheDir'; 11663af6ab5fSopenharmony_ci const printNameCache = 'testPrintNameCache'; 11673af6ab5fSopenharmony_ci 11683af6ab5fSopenharmony_ci writeObfuscationNameCache(projectConfig, entryPackageInfo, obfuscationCacheDir, printNameCache); 11693af6ab5fSopenharmony_ci 11703af6ab5fSopenharmony_ci expect(existsSyncSpy.called).to.be.false; 11713af6ab5fSopenharmony_ci expect(mkdirSyncSpy.called).to.be.false; 11723af6ab5fSopenharmony_ci expect(writeFileSyncSpy.called).to.be.false; 11733af6ab5fSopenharmony_ci }); 11743af6ab5fSopenharmony_ci it('should write cache to obfuscationCacheDir if provided', () => { 11753af6ab5fSopenharmony_ci const projectConfig = { 11763af6ab5fSopenharmony_ci arkObfuscator: true, 11773af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 11783af6ab5fSopenharmony_ci options: { 11793af6ab5fSopenharmony_ci enablePropertyObfuscation: true, 11803af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 11813af6ab5fSopenharmony_ci enableExportObfusaction: true 11823af6ab5fSopenharmony_ci }, 11833af6ab5fSopenharmony_ci }, 11843af6ab5fSopenharmony_ci etsLoaderVersion: '1.0.0', 11853af6ab5fSopenharmony_ci }; 11863af6ab5fSopenharmony_ci const entryPackageInfo = 'testEntryPackageInfo'; 11873af6ab5fSopenharmony_ci const obfuscationCacheDir = './test/ut/initialization/testObfuscationCacheDir'; 11883af6ab5fSopenharmony_ci const printNameCache = ''; 11893af6ab5fSopenharmony_ci writeObfuscationNameCache(projectConfig, entryPackageInfo, obfuscationCacheDir, printNameCache); 11903af6ab5fSopenharmony_ci const defaultNameCachePath: string = path.join(obfuscationCacheDir, 'nameCache.json'); 11913af6ab5fSopenharmony_ci const expectedContent = { 11923af6ab5fSopenharmony_ci "extraKey": "", 11933af6ab5fSopenharmony_ci "compileSdkVersion": "1.0.0", 11943af6ab5fSopenharmony_ci "entryPackageInfo": "testEntryPackageInfo", 11953af6ab5fSopenharmony_ci "PropertyCache": {}, 11963af6ab5fSopenharmony_ci "FileNameCache": {} 11973af6ab5fSopenharmony_ci } 11983af6ab5fSopenharmony_ci const jsonData = fs.readFileSync(defaultNameCachePath,'utf-8'); 11993af6ab5fSopenharmony_ci const result = JSON.parse(jsonData); 12003af6ab5fSopenharmony_ci expect(result).to.deep.equal(expectedContent); 12013af6ab5fSopenharmony_ci fs.unlinkSync(defaultNameCachePath); 12023af6ab5fSopenharmony_ci fs.rmdirSync(path.dirname(defaultNameCachePath)); 12033af6ab5fSopenharmony_ci }); 12043af6ab5fSopenharmony_ci it('should write cache to printNameCache if provided', () => { 12053af6ab5fSopenharmony_ci const projectConfig = { 12063af6ab5fSopenharmony_ci arkObfuscator: true, 12073af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 12083af6ab5fSopenharmony_ci options: { 12093af6ab5fSopenharmony_ci enablePropertyObfuscation: true, 12103af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 12113af6ab5fSopenharmony_ci }, 12123af6ab5fSopenharmony_ci }, 12133af6ab5fSopenharmony_ci etsLoaderVersion: '1.0.0', 12143af6ab5fSopenharmony_ci }; 12153af6ab5fSopenharmony_ci const entryPackageInfo = 'testEntryPackageInfo'; 12163af6ab5fSopenharmony_ci const obfuscationCacheDir = ''; 12173af6ab5fSopenharmony_ci const printNameCache = './test/ut/initialization/printNameCache.json'; 12183af6ab5fSopenharmony_ci writeObfuscationNameCache(projectConfig, entryPackageInfo, obfuscationCacheDir, printNameCache); 12193af6ab5fSopenharmony_ci const expectedContent = { 12203af6ab5fSopenharmony_ci "extraKey": "", 12213af6ab5fSopenharmony_ci "compileSdkVersion": "1.0.0", 12223af6ab5fSopenharmony_ci "entryPackageInfo": "testEntryPackageInfo", 12233af6ab5fSopenharmony_ci "PropertyCache": {}, 12243af6ab5fSopenharmony_ci "FileNameCache": {} 12253af6ab5fSopenharmony_ci } 12263af6ab5fSopenharmony_ci const jsonData = fs.readFileSync(printNameCache,'utf-8'); 12273af6ab5fSopenharmony_ci const result = JSON.parse(jsonData); 12283af6ab5fSopenharmony_ci expect(result).to.deep.equal(expectedContent); 12293af6ab5fSopenharmony_ci expect(existsSyncSpy.called).to.be.false; 12303af6ab5fSopenharmony_ci expect(mkdirSyncSpy.called).to.be.false; 12313af6ab5fSopenharmony_ci fs.unlinkSync(printNameCache); 12323af6ab5fSopenharmony_ci }); 12333af6ab5fSopenharmony_ci }); 12343af6ab5fSopenharmony_ci 12353af6ab5fSopenharmony_ci describe('enableObfuscatedFilePathConfig', () => { 12363af6ab5fSopenharmony_ci it('should return false if in debug mode or no obfuscation config', () => { 12373af6ab5fSopenharmony_ci const projectConfig = { 12383af6ab5fSopenharmony_ci obfuscationMergedObConfig: null, 12393af6ab5fSopenharmony_ci buildMode: 'debug' 12403af6ab5fSopenharmony_ci }; 12413af6ab5fSopenharmony_ci const result = enableObfuscatedFilePathConfig(true, projectConfig); 12423af6ab5fSopenharmony_ci expect(result).to.be.false; 12433af6ab5fSopenharmony_ci }); 12443af6ab5fSopenharmony_ci 12453af6ab5fSopenharmony_ci it('should return false if obfuscation is disabled or file name obfuscation is not enabled', () => { 12463af6ab5fSopenharmony_ci const projectConfig = { 12473af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 12483af6ab5fSopenharmony_ci options: { 12493af6ab5fSopenharmony_ci disableObfuscation: true, 12503af6ab5fSopenharmony_ci enableFileNameObfuscation: false, 12513af6ab5fSopenharmony_ci }, 12523af6ab5fSopenharmony_ci }, 12533af6ab5fSopenharmony_ci buildMode: 'debug' 12543af6ab5fSopenharmony_ci }; 12553af6ab5fSopenharmony_ci const result = enableObfuscatedFilePathConfig(false, projectConfig); 12563af6ab5fSopenharmony_ci expect(result).to.be.false; 12573af6ab5fSopenharmony_ci }); 12583af6ab5fSopenharmony_ci 12593af6ab5fSopenharmony_ci it('should return true if all conditions are met', () => { 12603af6ab5fSopenharmony_ci const projectConfig = { 12613af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 12623af6ab5fSopenharmony_ci options: { 12633af6ab5fSopenharmony_ci disableObfuscation: false, 12643af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 12653af6ab5fSopenharmony_ci }, 12663af6ab5fSopenharmony_ci }, 12673af6ab5fSopenharmony_ci buildMode: 'not debug' 12683af6ab5fSopenharmony_ci }; 12693af6ab5fSopenharmony_ci const result = enableObfuscatedFilePathConfig(false, projectConfig); 12703af6ab5fSopenharmony_ci expect(result).to.be.true; 12713af6ab5fSopenharmony_ci }); 12723af6ab5fSopenharmony_ci }); 12733af6ab5fSopenharmony_ci 12743af6ab5fSopenharmony_ci describe('handleObfuscatedFilePath', () => { 12753af6ab5fSopenharmony_ci it('should return the original file path if obfuscation is not enabled', () => { 12763af6ab5fSopenharmony_ci const filePath = '/path/to/file.js'; 12773af6ab5fSopenharmony_ci const isPackageModules = false; 12783af6ab5fSopenharmony_ci const projectConfig = { 12793af6ab5fSopenharmony_ci enableFileNameObfuscation: false, 12803af6ab5fSopenharmony_ci buildMode: 'debug' 12813af6ab5fSopenharmony_ci }; 12823af6ab5fSopenharmony_ci 12833af6ab5fSopenharmony_ci const result = handleObfuscatedFilePath(filePath, isPackageModules, projectConfig); 12843af6ab5fSopenharmony_ci 12853af6ab5fSopenharmony_ci expect(result).to.equal(filePath); 12863af6ab5fSopenharmony_ci }); 12873af6ab5fSopenharmony_ci 12883af6ab5fSopenharmony_ci it('should return the original file path if obfuscation is not enabled', () => { 12893af6ab5fSopenharmony_ci const filePath = '/path/to/file.txt'; 12903af6ab5fSopenharmony_ci const isPackageModules = false; 12913af6ab5fSopenharmony_ci const projectConfig = { 12923af6ab5fSopenharmony_ci obfuscationMergedObConfig: null, 12933af6ab5fSopenharmony_ci buildMode: 'debug' 12943af6ab5fSopenharmony_ci }; 12953af6ab5fSopenharmony_ci const result = handleObfuscatedFilePath(filePath, isPackageModules, projectConfig); 12963af6ab5fSopenharmony_ci expect(result).to.equal(filePath); 12973af6ab5fSopenharmony_ci }); 12983af6ab5fSopenharmony_ci 12993af6ab5fSopenharmony_ci it('should return the original file path if obfuscation is not enabled', () => { 13003af6ab5fSopenharmony_ci const filePath = '/path/to/file.txt'; 13013af6ab5fSopenharmony_ci const isPackageModules = false; 13023af6ab5fSopenharmony_ci const projectConfig = { 13033af6ab5fSopenharmony_ci obfuscationMergedObConfig: null, 13043af6ab5fSopenharmony_ci buildMode: 'not debug' 13053af6ab5fSopenharmony_ci }; 13063af6ab5fSopenharmony_ci const result = handleObfuscatedFilePath(filePath, isPackageModules, projectConfig); 13073af6ab5fSopenharmony_ci expect(result).to.equal(filePath); 13083af6ab5fSopenharmony_ci }); 13093af6ab5fSopenharmony_ci 13103af6ab5fSopenharmony_ci it('should return the unix formatted file path if obfuscation is enabled and is a package module', () => { 13113af6ab5fSopenharmony_ci const filePath = '/path/to/file.js'; 13123af6ab5fSopenharmony_ci const isPackageModules = true; 13133af6ab5fSopenharmony_ci const projectConfig = { 13143af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 13153af6ab5fSopenharmony_ci options: { 13163af6ab5fSopenharmony_ci disableObfuscation: false, 13173af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 13183af6ab5fSopenharmony_ci }, 13193af6ab5fSopenharmony_ci }, 13203af6ab5fSopenharmony_ci buildMode: "not Debug" 13213af6ab5fSopenharmony_ci }; 13223af6ab5fSopenharmony_ci 13233af6ab5fSopenharmony_ci const result = handleObfuscatedFilePath(filePath, isPackageModules, projectConfig); 13243af6ab5fSopenharmony_ci 13253af6ab5fSopenharmony_ci expect(result).to.equal(FileUtils.toUnixPath(filePath)); 13263af6ab5fSopenharmony_ci }); 13273af6ab5fSopenharmony_ci 13283af6ab5fSopenharmony_ci it('should return the unix formatted file path if obfuscation is enabled and is a package module', () => { 13293af6ab5fSopenharmony_ci const filePath = '/path/to/file.js'; 13303af6ab5fSopenharmony_ci const isPackageModules = true; 13313af6ab5fSopenharmony_ci const projectConfig = { 13323af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 13333af6ab5fSopenharmony_ci options: { 13343af6ab5fSopenharmony_ci disableObfuscation: false, 13353af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 13363af6ab5fSopenharmony_ci }, 13373af6ab5fSopenharmony_ci }, 13383af6ab5fSopenharmony_ci buildMode: "" 13393af6ab5fSopenharmony_ci }; 13403af6ab5fSopenharmony_ci 13413af6ab5fSopenharmony_ci const result = handleObfuscatedFilePath(filePath, isPackageModules, projectConfig); 13423af6ab5fSopenharmony_ci 13433af6ab5fSopenharmony_ci expect(result).to.equal(FileUtils.toUnixPath(filePath)); 13443af6ab5fSopenharmony_ci }); 13453af6ab5fSopenharmony_ci }); 13463af6ab5fSopenharmony_ci 13473af6ab5fSopenharmony_ci describe('enableObfuscateFileName', () => { 13483af6ab5fSopenharmony_ci it('should return false if obfuscation is not enabled', () => { 13493af6ab5fSopenharmony_ci const isPackageModules = false; 13503af6ab5fSopenharmony_ci const projectConfig = { 13513af6ab5fSopenharmony_ci obfuscationMergedObConfig: null, 13523af6ab5fSopenharmony_ci buildMode: "not Debug" 13533af6ab5fSopenharmony_ci }; 13543af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 13553af6ab5fSopenharmony_ci expect(result).to.be.false; 13563af6ab5fSopenharmony_ci }); 13573af6ab5fSopenharmony_ci 13583af6ab5fSopenharmony_ci it('should return true if obfuscation is enabled and not a package module', () => { 13593af6ab5fSopenharmony_ci const isPackageModules = false; 13603af6ab5fSopenharmony_ci const projectConfig = { 13613af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 13623af6ab5fSopenharmony_ci options: { 13633af6ab5fSopenharmony_ci disableObfuscation: false, 13643af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 13653af6ab5fSopenharmony_ci }, 13663af6ab5fSopenharmony_ci }, 13673af6ab5fSopenharmony_ci buildMode: "not Debug" 13683af6ab5fSopenharmony_ci }; 13693af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 13703af6ab5fSopenharmony_ci 13713af6ab5fSopenharmony_ci expect(result).to.be.true; 13723af6ab5fSopenharmony_ci }); 13733af6ab5fSopenharmony_ci 13743af6ab5fSopenharmony_ci it('should return false if obfuscation is enabled and is a package module', () => { 13753af6ab5fSopenharmony_ci const isPackageModules = true; 13763af6ab5fSopenharmony_ci const projectConfig = { 13773af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 13783af6ab5fSopenharmony_ci options: { 13793af6ab5fSopenharmony_ci disableObfuscation: false, 13803af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 13813af6ab5fSopenharmony_ci }, 13823af6ab5fSopenharmony_ci }, 13833af6ab5fSopenharmony_ci buildMode: "Debug" 13843af6ab5fSopenharmony_ci }; 13853af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 13863af6ab5fSopenharmony_ci expect(result).to.be.false; 13873af6ab5fSopenharmony_ci }); 13883af6ab5fSopenharmony_ci 13893af6ab5fSopenharmony_ci it('should return false if obfuscation is enabled and is a package module', () => { 13903af6ab5fSopenharmony_ci const isPackageModules = true; 13913af6ab5fSopenharmony_ci const projectConfig = { 13923af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 13933af6ab5fSopenharmony_ci options: { 13943af6ab5fSopenharmony_ci disableObfuscation: false, 13953af6ab5fSopenharmony_ci enableFileNameObfuscation: false, 13963af6ab5fSopenharmony_ci }, 13973af6ab5fSopenharmony_ci }, 13983af6ab5fSopenharmony_ci buildMode: "Debug" 13993af6ab5fSopenharmony_ci }; 14003af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 14013af6ab5fSopenharmony_ci expect(result).to.be.false; 14023af6ab5fSopenharmony_ci }); 14033af6ab5fSopenharmony_ci 14043af6ab5fSopenharmony_ci it('should return false if obfuscation is enabled and is a package module', () => { 14053af6ab5fSopenharmony_ci const isPackageModules = true; 14063af6ab5fSopenharmony_ci const projectConfig = { 14073af6ab5fSopenharmony_ci obfuscationMergedObConfig: {}, 14083af6ab5fSopenharmony_ci buildMode: "Debug" 14093af6ab5fSopenharmony_ci }; 14103af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 14113af6ab5fSopenharmony_ci expect(result).to.be.false; 14123af6ab5fSopenharmony_ci }); 14133af6ab5fSopenharmony_ci 14143af6ab5fSopenharmony_ci it('should return false if obfuscation is enabled and is a package module', () => { 14153af6ab5fSopenharmony_ci const isPackageModules = true; 14163af6ab5fSopenharmony_ci const projectConfig = { 14173af6ab5fSopenharmony_ci obfuscationMergedObConfig: null, 14183af6ab5fSopenharmony_ci buildMode: "Debug" 14193af6ab5fSopenharmony_ci }; 14203af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 14213af6ab5fSopenharmony_ci expect(result).to.be.false; 14223af6ab5fSopenharmony_ci }); 14233af6ab5fSopenharmony_ci 14243af6ab5fSopenharmony_ci it('should return false if obfuscation is enabled and is a package module', () => { 14253af6ab5fSopenharmony_ci const isPackageModules = true; 14263af6ab5fSopenharmony_ci const projectConfig = { 14273af6ab5fSopenharmony_ci obfuscationMergedObConfig: undefined, 14283af6ab5fSopenharmony_ci buildMode: "Debug" 14293af6ab5fSopenharmony_ci }; 14303af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 14313af6ab5fSopenharmony_ci expect(result).to.be.false; 14323af6ab5fSopenharmony_ci }); 14333af6ab5fSopenharmony_ci 14343af6ab5fSopenharmony_ci it('should return false if obfuscation is enabled and is a package module', () => { 14353af6ab5fSopenharmony_ci const isPackageModules = true; 14363af6ab5fSopenharmony_ci const projectConfig = { 14373af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 14383af6ab5fSopenharmony_ci options: { 14393af6ab5fSopenharmony_ci disableObfuscation: false, 14403af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 14413af6ab5fSopenharmony_ci }, 14423af6ab5fSopenharmony_ci }, 14433af6ab5fSopenharmony_ci buildMode: "not Debug" 14443af6ab5fSopenharmony_ci }; 14453af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 14463af6ab5fSopenharmony_ci expect(result).to.be.false; 14473af6ab5fSopenharmony_ci }); 14483af6ab5fSopenharmony_ci 14493af6ab5fSopenharmony_ci it('should return false if obfuscation is enabled and is a package module', () => { 14503af6ab5fSopenharmony_ci const isPackageModules = true; 14513af6ab5fSopenharmony_ci const projectConfig = { 14523af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 14533af6ab5fSopenharmony_ci options: { 14543af6ab5fSopenharmony_ci disableObfuscation: false, 14553af6ab5fSopenharmony_ci enableFileNameObfuscation: false, 14563af6ab5fSopenharmony_ci }, 14573af6ab5fSopenharmony_ci }, 14583af6ab5fSopenharmony_ci buildMode: "not Debug" 14593af6ab5fSopenharmony_ci }; 14603af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 14613af6ab5fSopenharmony_ci expect(result).to.be.false; 14623af6ab5fSopenharmony_ci }); 14633af6ab5fSopenharmony_ci 14643af6ab5fSopenharmony_ci it('should return false if obfuscation is enabled and is a package module', () => { 14653af6ab5fSopenharmony_ci const isPackageModules = true; 14663af6ab5fSopenharmony_ci const projectConfig = { 14673af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 14683af6ab5fSopenharmony_ci options: { 14693af6ab5fSopenharmony_ci disableObfuscation: false, 14703af6ab5fSopenharmony_ci enableFileNameObfuscation: false, 14713af6ab5fSopenharmony_ci }, 14723af6ab5fSopenharmony_ci }, 14733af6ab5fSopenharmony_ci buildMode: "not Debug" 14743af6ab5fSopenharmony_ci }; 14753af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 14763af6ab5fSopenharmony_ci expect(result).to.be.false; 14773af6ab5fSopenharmony_ci }); 14783af6ab5fSopenharmony_ci 14793af6ab5fSopenharmony_ci it('should return false if obfuscation is enabled and is a package module', () => { 14803af6ab5fSopenharmony_ci const isPackageModules = true; 14813af6ab5fSopenharmony_ci const projectConfig = { 14823af6ab5fSopenharmony_ci obfuscationMergedObConfig: { 14833af6ab5fSopenharmony_ci options: { 14843af6ab5fSopenharmony_ci disableObfuscation: false, 14853af6ab5fSopenharmony_ci enableFileNameObfuscation: true, 14863af6ab5fSopenharmony_ci }, 14873af6ab5fSopenharmony_ci }, 14883af6ab5fSopenharmony_ci buildMode: "not Debug" 14893af6ab5fSopenharmony_ci }; 14903af6ab5fSopenharmony_ci const result = enableObfuscateFileName(isPackageModules, projectConfig); 14913af6ab5fSopenharmony_ci expect(result).to.be.false; 14923af6ab5fSopenharmony_ci }); 14933af6ab5fSopenharmony_ci }); 14943af6ab5fSopenharmony_ci 14953af6ab5fSopenharmony_ci describe('getRelativeSourcePath', () => { 14963af6ab5fSopenharmony_ci it('should return the relative path of a file within the project root', () => { 14973af6ab5fSopenharmony_ci const filePath = '/Users/user/project/src/components/Button.js'; 14983af6ab5fSopenharmony_ci const projectRootPath = '/Users/user/project'; 14993af6ab5fSopenharmony_ci const expectedRelativePath = 'src/components/Button.js'; 15003af6ab5fSopenharmony_ci 15013af6ab5fSopenharmony_ci const result = getRelativeSourcePath(filePath, projectRootPath, ''); 15023af6ab5fSopenharmony_ci expect(result).to.equal(expectedRelativePath); 15033af6ab5fSopenharmony_ci }); 15043af6ab5fSopenharmony_ci 15053af6ab5fSopenharmony_ci it('should return the relative path of a file within a specified project path', () => { 15063af6ab5fSopenharmony_ci const filePath = '/Users/user/project/src/components/Button.js'; 15073af6ab5fSopenharmony_ci const belongProjectPath = '/Users/user/project/src'; 15083af6ab5fSopenharmony_ci const expectedRelativePath = 'components/Button.js'; 15093af6ab5fSopenharmony_ci 15103af6ab5fSopenharmony_ci const result = getRelativeSourcePath(filePath, '', belongProjectPath); 15113af6ab5fSopenharmony_ci expect(result).to.equal(expectedRelativePath); 15123af6ab5fSopenharmony_ci }); 15133af6ab5fSopenharmony_ci 15143af6ab5fSopenharmony_ci it('should return the original path if no project root or belong project path is provided', () => { 15153af6ab5fSopenharmony_ci const filePath = '/Users/user/project/src/components/Button.js'; 15163af6ab5fSopenharmony_ci const expectedRelativePath = filePath; 15173af6ab5fSopenharmony_ci 15183af6ab5fSopenharmony_ci const result = getRelativeSourcePath(filePath, '', ''); 15193af6ab5fSopenharmony_ci expect(result).to.equal(expectedRelativePath); 15203af6ab5fSopenharmony_ci }); 15213af6ab5fSopenharmony_ci }); 15223af6ab5fSopenharmony_ci}); 1523