107ac75b1Sopenharmony_ci/* 207ac75b1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 307ac75b1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 407ac75b1Sopenharmony_ci * you may not use rollupObject file except in compliance with the License. 507ac75b1Sopenharmony_ci * You may obtain a copy of the License at 607ac75b1Sopenharmony_ci * 707ac75b1Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 807ac75b1Sopenharmony_ci * 907ac75b1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1007ac75b1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1107ac75b1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1207ac75b1Sopenharmony_ci * See the License for the specific language governing permissions and 1307ac75b1Sopenharmony_ci * limitations under the License. 1407ac75b1Sopenharmony_ci */ 1507ac75b1Sopenharmony_ci 1607ac75b1Sopenharmony_ci// Execute this file first to avoid circular dependency problems 1707ac75b1Sopenharmony_ci 1807ac75b1Sopenharmony_ciimport { expect } from 'chai'; 1907ac75b1Sopenharmony_ciimport mocha from 'mocha'; 2007ac75b1Sopenharmony_ciimport fs from "fs"; 2107ac75b1Sopenharmony_ciimport path from "path"; 2207ac75b1Sopenharmony_ciimport MagicString from 'magic-string'; 2307ac75b1Sopenharmony_ciimport sinon from 'sinon'; 2407ac75b1Sopenharmony_ci 2507ac75b1Sopenharmony_ciimport { 2607ac75b1Sopenharmony_ci getBuildModeInLowerCase, 2707ac75b1Sopenharmony_ci getPackageInfo, 2807ac75b1Sopenharmony_ci genSourceMapFileName, 2907ac75b1Sopenharmony_ci isOhModules, 3007ac75b1Sopenharmony_ci isEs2Abc, 3107ac75b1Sopenharmony_ci writeArkguardObfuscatedSourceCode, 3207ac75b1Sopenharmony_ci writeMinimizedSourceCode, 3307ac75b1Sopenharmony_ci tryMangleFileName 3407ac75b1Sopenharmony_ci} from '../../lib/ark_utils'; 3507ac75b1Sopenharmony_ciimport { 3607ac75b1Sopenharmony_ci DEBUG, 3707ac75b1Sopenharmony_ci RELEASE, 3807ac75b1Sopenharmony_ci OH_MODULES, 3907ac75b1Sopenharmony_ci EXTNAME_TS, 4007ac75b1Sopenharmony_ci EXTNAME_JS, 4107ac75b1Sopenharmony_ci EXTNAME_ETS, 4207ac75b1Sopenharmony_ci OBFUSCATION_TOOL 4307ac75b1Sopenharmony_ci} from '../../lib/fast_build/ark_compiler/common/ark_define'; 4407ac75b1Sopenharmony_ciimport RollUpPluginMock from './mock/rollup_mock/rollup_plugin_mock'; 4507ac75b1Sopenharmony_ciimport { 4607ac75b1Sopenharmony_ci BUNDLE_NAME_DEFAULT, 4707ac75b1Sopenharmony_ci ENTRY_MODULE_NAME_DEFAULT, 4807ac75b1Sopenharmony_ci EXTNAME_MAP, 4907ac75b1Sopenharmony_ci ENTRYABILITY_JS 5007ac75b1Sopenharmony_ci} from './mock/rollup_mock/common'; 5107ac75b1Sopenharmony_ciimport projectConfig from './utils/processProjectConfig'; 5207ac75b1Sopenharmony_ciimport { 5307ac75b1Sopenharmony_ci ES2ABC, 5407ac75b1Sopenharmony_ci TS2ABC 5507ac75b1Sopenharmony_ci} from '../../lib/pre_define'; 5607ac75b1Sopenharmony_ciimport { changeFileExtension } from '../../lib/fast_build/ark_compiler/utils'; 5707ac75b1Sopenharmony_ciimport ModuleSourceFileMock from './mock/class_mock/module_source_files_mock'; 5807ac75b1Sopenharmony_ciimport { 5907ac75b1Sopenharmony_ci genTemporaryPath, 6007ac75b1Sopenharmony_ci toUnixPath 6107ac75b1Sopenharmony_ci} from '../../lib/utils'; 6207ac75b1Sopenharmony_ciimport { 6307ac75b1Sopenharmony_ci ObConfigResolver, 6407ac75b1Sopenharmony_ci MergedConfig 6507ac75b1Sopenharmony_ci} from '../../lib/fast_build/ark_compiler/common/ob_config_resolver'; 6607ac75b1Sopenharmony_ciimport { 6707ac75b1Sopenharmony_ci utProcessArkConfig 6807ac75b1Sopenharmony_ci} from '../../lib/fast_build/ark_compiler/common/process_ark_config'; 6907ac75b1Sopenharmony_ciimport { ModuleSourceFile } from '../../lib/fast_build/ark_compiler/module/module_source_file'; 7007ac75b1Sopenharmony_ciimport { PROJECT_ROOT, TERSER_PROCESSED_EXPECTED_CODE } from './mock/rollup_mock/path_config'; 7107ac75b1Sopenharmony_ciimport { GEN_ABC_PLUGIN_NAME } from '../../lib/fast_build/ark_compiler/common/ark_define'; 7207ac75b1Sopenharmony_ciimport { SourceMapGenerator } from '../../lib/fast_build/ark_compiler/generate_sourcemap'; 7307ac75b1Sopenharmony_ciimport { ArkObfuscator } from 'arkguard'; 7407ac75b1Sopenharmony_ci 7507ac75b1Sopenharmony_cimocha.describe('test ark_utils file api', function () { 7607ac75b1Sopenharmony_ci mocha.before(function () { 7707ac75b1Sopenharmony_ci this.rollup = new RollUpPluginMock(); 7807ac75b1Sopenharmony_ci }); 7907ac75b1Sopenharmony_ci 8007ac75b1Sopenharmony_ci mocha.after(() => { 8107ac75b1Sopenharmony_ci delete this.rollup; 8207ac75b1Sopenharmony_ci }); 8307ac75b1Sopenharmony_ci 8407ac75b1Sopenharmony_ci mocha.it('1-1: test getBuildModeInLowerCase under build debug', function () { 8507ac75b1Sopenharmony_ci this.rollup.build(); 8607ac75b1Sopenharmony_ci const buildMode = getBuildModeInLowerCase(this.rollup.share.projectConfig); 8707ac75b1Sopenharmony_ci expect(buildMode === DEBUG).to.be.true; 8807ac75b1Sopenharmony_ci }); 8907ac75b1Sopenharmony_ci 9007ac75b1Sopenharmony_ci mocha.it('1-2: test getBuildModeInLowerCase under build release', function () { 9107ac75b1Sopenharmony_ci this.rollup.build(RELEASE); 9207ac75b1Sopenharmony_ci const buildMode = getBuildModeInLowerCase(this.rollup.share.projectConfig); 9307ac75b1Sopenharmony_ci expect(buildMode === RELEASE).to.be.true; 9407ac75b1Sopenharmony_ci }); 9507ac75b1Sopenharmony_ci 9607ac75b1Sopenharmony_ci mocha.it('1-3: test getBuildModeInLowerCase under preview debug', function () { 9707ac75b1Sopenharmony_ci this.rollup.preview(); 9807ac75b1Sopenharmony_ci const buildMode = getBuildModeInLowerCase(this.rollup.share.projectConfig); 9907ac75b1Sopenharmony_ci expect(buildMode === DEBUG).to.be.true; 10007ac75b1Sopenharmony_ci }); 10107ac75b1Sopenharmony_ci 10207ac75b1Sopenharmony_ci mocha.it('1-4: test getBuildModeInLowerCase under hot reload debug', function () { 10307ac75b1Sopenharmony_ci this.rollup.hotReload(); 10407ac75b1Sopenharmony_ci const buildMode = getBuildModeInLowerCase(this.rollup.share.projectConfig); 10507ac75b1Sopenharmony_ci expect(buildMode === DEBUG).to.be.true; 10607ac75b1Sopenharmony_ci }); 10707ac75b1Sopenharmony_ci 10807ac75b1Sopenharmony_ci mocha.it('2-1: test getPackageInfo under build debug', function () { 10907ac75b1Sopenharmony_ci this.rollup.build(); 11007ac75b1Sopenharmony_ci const returnInfo = getPackageInfo(this.rollup.share.projectConfig.aceModuleJsonPath); 11107ac75b1Sopenharmony_ci expect(returnInfo[0] === BUNDLE_NAME_DEFAULT).to.be.true; 11207ac75b1Sopenharmony_ci expect(returnInfo[1] === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 11307ac75b1Sopenharmony_ci }); 11407ac75b1Sopenharmony_ci 11507ac75b1Sopenharmony_ci mocha.it('2-2: test getPackageInfo under build release', function () { 11607ac75b1Sopenharmony_ci this.rollup.build(RELEASE); 11707ac75b1Sopenharmony_ci const returnInfo = getPackageInfo(this.rollup.share.projectConfig.aceModuleJsonPath); 11807ac75b1Sopenharmony_ci expect(returnInfo[0] === BUNDLE_NAME_DEFAULT).to.be.true; 11907ac75b1Sopenharmony_ci expect(returnInfo[1] === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 12007ac75b1Sopenharmony_ci }); 12107ac75b1Sopenharmony_ci 12207ac75b1Sopenharmony_ci mocha.it('2-3: test getPackageInfo under preview debug', function () { 12307ac75b1Sopenharmony_ci this.rollup.preview(); 12407ac75b1Sopenharmony_ci const returnInfo = getPackageInfo(this.rollup.share.projectConfig.aceModuleJsonPath); 12507ac75b1Sopenharmony_ci expect(returnInfo[0] === BUNDLE_NAME_DEFAULT).to.be.true; 12607ac75b1Sopenharmony_ci expect(returnInfo[1] === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 12707ac75b1Sopenharmony_ci }); 12807ac75b1Sopenharmony_ci 12907ac75b1Sopenharmony_ci mocha.it('2-4: test getPackageInfo under hot reload debug', function () { 13007ac75b1Sopenharmony_ci this.rollup.hotReload(); 13107ac75b1Sopenharmony_ci const returnInfo = getPackageInfo(this.rollup.share.projectConfig.aceModuleJsonPath); 13207ac75b1Sopenharmony_ci expect(returnInfo[0] === BUNDLE_NAME_DEFAULT).to.be.true; 13307ac75b1Sopenharmony_ci expect(returnInfo[1] === ENTRY_MODULE_NAME_DEFAULT).to.be.true; 13407ac75b1Sopenharmony_ci }); 13507ac75b1Sopenharmony_ci 13607ac75b1Sopenharmony_ci mocha.it('3-1: test genSourceMapFileName under build debug', function () { 13707ac75b1Sopenharmony_ci this.rollup.build(); 13807ac75b1Sopenharmony_ci for (const filePath of this.rollup.share.allFiles) { 13907ac75b1Sopenharmony_ci if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_JS)) { 14007ac75b1Sopenharmony_ci const originPath = genSourceMapFileName(filePath); 14107ac75b1Sopenharmony_ci const expectedPath = `${filePath}${EXTNAME_MAP}`; 14207ac75b1Sopenharmony_ci expect(originPath === expectedPath).to.be.true; 14307ac75b1Sopenharmony_ci } else if (filePath.endsWith(EXTNAME_ETS)) { 14407ac75b1Sopenharmony_ci const originPath = genSourceMapFileName(filePath); 14507ac75b1Sopenharmony_ci expect(originPath === filePath).to.be.true; 14607ac75b1Sopenharmony_ci } 14707ac75b1Sopenharmony_ci } 14807ac75b1Sopenharmony_ci }); 14907ac75b1Sopenharmony_ci 15007ac75b1Sopenharmony_ci mocha.it('3-2: test genSourceMapFileName under build release', function () { 15107ac75b1Sopenharmony_ci this.rollup.build(RELEASE); 15207ac75b1Sopenharmony_ci for (const filePath of this.rollup.share.allFiles) { 15307ac75b1Sopenharmony_ci if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_JS)) { 15407ac75b1Sopenharmony_ci const originPath = genSourceMapFileName(filePath); 15507ac75b1Sopenharmony_ci const expectedPath = `${filePath}${EXTNAME_MAP}`; 15607ac75b1Sopenharmony_ci expect(originPath === expectedPath).to.be.true; 15707ac75b1Sopenharmony_ci } 15807ac75b1Sopenharmony_ci } 15907ac75b1Sopenharmony_ci }); 16007ac75b1Sopenharmony_ci 16107ac75b1Sopenharmony_ci mocha.it('3-3: test genSourceMapFileName under preview debug', function () { 16207ac75b1Sopenharmony_ci this.rollup.preview(); 16307ac75b1Sopenharmony_ci for (const filePath of this.rollup.share.allFiles) { 16407ac75b1Sopenharmony_ci if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_JS)) { 16507ac75b1Sopenharmony_ci const originPath = genSourceMapFileName(filePath); 16607ac75b1Sopenharmony_ci const expectedPath = `${filePath}${EXTNAME_MAP}`; 16707ac75b1Sopenharmony_ci expect(originPath === expectedPath).to.be.true; 16807ac75b1Sopenharmony_ci } 16907ac75b1Sopenharmony_ci } 17007ac75b1Sopenharmony_ci }); 17107ac75b1Sopenharmony_ci 17207ac75b1Sopenharmony_ci mocha.it('3-4: test genSourceMapFileName under hot reload debug', function () { 17307ac75b1Sopenharmony_ci this.rollup.hotReload(); 17407ac75b1Sopenharmony_ci for (const filePath of this.rollup.share.allFiles) { 17507ac75b1Sopenharmony_ci if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_JS)) { 17607ac75b1Sopenharmony_ci const originPath = genSourceMapFileName(filePath); 17707ac75b1Sopenharmony_ci const expectedPath = `${filePath}${EXTNAME_MAP}`; 17807ac75b1Sopenharmony_ci expect(originPath === expectedPath).to.be.true; 17907ac75b1Sopenharmony_ci } 18007ac75b1Sopenharmony_ci } 18107ac75b1Sopenharmony_ci }); 18207ac75b1Sopenharmony_ci 18307ac75b1Sopenharmony_ci mocha.it('4-1: test isOhModules under build debug', function () { 18407ac75b1Sopenharmony_ci this.rollup.build(); 18507ac75b1Sopenharmony_ci const returnInfo = isOhModules(this.rollup.share.projectConfig); 18607ac75b1Sopenharmony_ci expect(returnInfo === false).to.be.true; 18707ac75b1Sopenharmony_ci this.rollup.share.projectConfig.packageDir = OH_MODULES; 18807ac75b1Sopenharmony_ci const returnInfoOh = isOhModules(this.rollup.share.projectConfig); 18907ac75b1Sopenharmony_ci expect(returnInfoOh).to.be.true; 19007ac75b1Sopenharmony_ci }); 19107ac75b1Sopenharmony_ci 19207ac75b1Sopenharmony_ci mocha.it('4-2: test isOhModules under build release', function () { 19307ac75b1Sopenharmony_ci this.rollup.build(RELEASE); 19407ac75b1Sopenharmony_ci const returnInfo = isOhModules(this.rollup.share.projectConfig); 19507ac75b1Sopenharmony_ci expect(returnInfo === false).to.be.true; 19607ac75b1Sopenharmony_ci this.rollup.share.projectConfig.packageDir = OH_MODULES; 19707ac75b1Sopenharmony_ci const returnInfoOh = isOhModules(this.rollup.share.projectConfig); 19807ac75b1Sopenharmony_ci expect(returnInfoOh).to.be.true; 19907ac75b1Sopenharmony_ci }); 20007ac75b1Sopenharmony_ci 20107ac75b1Sopenharmony_ci mocha.it('4-3: test isOhModules under preview debug', function () { 20207ac75b1Sopenharmony_ci this.rollup.preview(); 20307ac75b1Sopenharmony_ci const returnInfo = isOhModules(this.rollup.share.projectConfig); 20407ac75b1Sopenharmony_ci expect(returnInfo === false).to.be.true; 20507ac75b1Sopenharmony_ci this.rollup.share.projectConfig.packageDir = OH_MODULES; 20607ac75b1Sopenharmony_ci const returnInfoOh = isOhModules(this.rollup.share.projectConfig); 20707ac75b1Sopenharmony_ci expect(returnInfoOh).to.be.true; 20807ac75b1Sopenharmony_ci }); 20907ac75b1Sopenharmony_ci 21007ac75b1Sopenharmony_ci mocha.it('4-4: test isOhModules under hot reload debug', function () { 21107ac75b1Sopenharmony_ci this.rollup.hotReload(); 21207ac75b1Sopenharmony_ci const returnInfo = isOhModules(this.rollup.share.projectConfig); 21307ac75b1Sopenharmony_ci expect(returnInfo === false).to.be.true; 21407ac75b1Sopenharmony_ci this.rollup.share.projectConfig.packageDir = OH_MODULES; 21507ac75b1Sopenharmony_ci const returnInfoOh = isOhModules(this.rollup.share.projectConfig); 21607ac75b1Sopenharmony_ci expect(returnInfoOh).to.be.true; 21707ac75b1Sopenharmony_ci }); 21807ac75b1Sopenharmony_ci 21907ac75b1Sopenharmony_ci mocha.it('4-5: test isOhModules under hot fix debug', function () { 22007ac75b1Sopenharmony_ci projectConfig.buildMode = DEBUG; 22107ac75b1Sopenharmony_ci const returnInfo = isOhModules(projectConfig); 22207ac75b1Sopenharmony_ci expect(returnInfo).to.be.true; 22307ac75b1Sopenharmony_ci }); 22407ac75b1Sopenharmony_ci 22507ac75b1Sopenharmony_ci mocha.it('4-6: test isOhModules under hot fix release', function () { 22607ac75b1Sopenharmony_ci projectConfig.buildMode = RELEASE; 22707ac75b1Sopenharmony_ci const returnInfo = isOhModules(projectConfig); 22807ac75b1Sopenharmony_ci expect(returnInfo).to.be.true; 22907ac75b1Sopenharmony_ci }); 23007ac75b1Sopenharmony_ci 23107ac75b1Sopenharmony_ci mocha.it('5-1: test isEs2Abc under build debug', function () { 23207ac75b1Sopenharmony_ci this.rollup.build(); 23307ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = ES2ABC; 23407ac75b1Sopenharmony_ci const returnInfo = isEs2Abc(this.rollup.share.projectConfig); 23507ac75b1Sopenharmony_ci expect(returnInfo).to.be.true; 23607ac75b1Sopenharmony_ci 23707ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = TS2ABC; 23807ac75b1Sopenharmony_ci const returnInfoTS2ABC = isEs2Abc(this.rollup.share.projectConfig); 23907ac75b1Sopenharmony_ci expect(returnInfoTS2ABC === false).to.be.true; 24007ac75b1Sopenharmony_ci 24107ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = "undefined"; 24207ac75b1Sopenharmony_ci const returnInfoUndef = isEs2Abc(this.rollup.share.projectConfig); 24307ac75b1Sopenharmony_ci expect(returnInfoUndef).to.be.true; 24407ac75b1Sopenharmony_ci 24507ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = undefined; 24607ac75b1Sopenharmony_ci const returnInfoUndefined = isEs2Abc(this.rollup.share.projectConfig); 24707ac75b1Sopenharmony_ci expect(returnInfoUndefined).to.be.true; 24807ac75b1Sopenharmony_ci }); 24907ac75b1Sopenharmony_ci 25007ac75b1Sopenharmony_ci mocha.it('5-2: test isEs2Abc under build release', function () { 25107ac75b1Sopenharmony_ci this.rollup.build(RELEASE); 25207ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = ES2ABC; 25307ac75b1Sopenharmony_ci const returnInfo = isEs2Abc(this.rollup.share.projectConfig); 25407ac75b1Sopenharmony_ci expect(returnInfo).to.be.true; 25507ac75b1Sopenharmony_ci 25607ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = TS2ABC; 25707ac75b1Sopenharmony_ci const returnInfoTS2ABC = isEs2Abc(this.rollup.share.projectConfig); 25807ac75b1Sopenharmony_ci expect(returnInfoTS2ABC).to.be.false; 25907ac75b1Sopenharmony_ci }); 26007ac75b1Sopenharmony_ci 26107ac75b1Sopenharmony_ci mocha.it('5-3: test isEs2Abc under preview debug', function () { 26207ac75b1Sopenharmony_ci this.rollup.preview(); 26307ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = ES2ABC; 26407ac75b1Sopenharmony_ci const returnInfo = isEs2Abc(this.rollup.share.projectConfig); 26507ac75b1Sopenharmony_ci expect(returnInfo).to.be.true; 26607ac75b1Sopenharmony_ci 26707ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = TS2ABC; 26807ac75b1Sopenharmony_ci const returnInfoTS2ABC = isEs2Abc(this.rollup.share.projectConfig); 26907ac75b1Sopenharmony_ci expect(returnInfoTS2ABC).to.be.false; 27007ac75b1Sopenharmony_ci }); 27107ac75b1Sopenharmony_ci 27207ac75b1Sopenharmony_ci mocha.it('5-4: test isEs2Abc under hot reload debug', function () { 27307ac75b1Sopenharmony_ci this.rollup.hotReload(); 27407ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = ES2ABC; 27507ac75b1Sopenharmony_ci const returnInfo = isEs2Abc(this.rollup.share.projectConfig); 27607ac75b1Sopenharmony_ci expect(returnInfo).to.be.true; 27707ac75b1Sopenharmony_ci 27807ac75b1Sopenharmony_ci this.rollup.share.projectConfig.pandaMode = TS2ABC; 27907ac75b1Sopenharmony_ci const returnInfoTS2ABC = isEs2Abc(this.rollup.share.projectConfig); 28007ac75b1Sopenharmony_ci expect(returnInfoTS2ABC).to.be.false; 28107ac75b1Sopenharmony_ci }); 28207ac75b1Sopenharmony_ci 28307ac75b1Sopenharmony_ci mocha.it('5-5: test isEs2Abc under hot fix debug', function () { 28407ac75b1Sopenharmony_ci projectConfig.buildMode = DEBUG; 28507ac75b1Sopenharmony_ci projectConfig.pandaMode = ES2ABC; 28607ac75b1Sopenharmony_ci const returnInfo = isEs2Abc(projectConfig); 28707ac75b1Sopenharmony_ci expect(returnInfo).to.be.true; 28807ac75b1Sopenharmony_ci 28907ac75b1Sopenharmony_ci projectConfig.pandaMode = TS2ABC; 29007ac75b1Sopenharmony_ci const returnInfoTS2ABC = isEs2Abc(projectConfig); 29107ac75b1Sopenharmony_ci expect(returnInfoTS2ABC).to.be.false; 29207ac75b1Sopenharmony_ci }); 29307ac75b1Sopenharmony_ci 29407ac75b1Sopenharmony_ci mocha.it('5-6: test isEs2Abc under hot fix release', function () { 29507ac75b1Sopenharmony_ci projectConfig.buildMode = RELEASE; 29607ac75b1Sopenharmony_ci projectConfig.pandaMode = ES2ABC; 29707ac75b1Sopenharmony_ci const returnInfo = isEs2Abc(projectConfig); 29807ac75b1Sopenharmony_ci expect(returnInfo).to.be.true; 29907ac75b1Sopenharmony_ci 30007ac75b1Sopenharmony_ci projectConfig.pandaMode = TS2ABC; 30107ac75b1Sopenharmony_ci const returnInfoTS2ABC = isEs2Abc(projectConfig); 30207ac75b1Sopenharmony_ci expect(returnInfoTS2ABC).to.be.false; 30307ac75b1Sopenharmony_ci }); 30407ac75b1Sopenharmony_ci 30507ac75b1Sopenharmony_ci mocha.it('6-1: test the error message of writeArkguardObfuscatedSourceCode', async function () { 30607ac75b1Sopenharmony_ci this.rollup.build(RELEASE); 30707ac75b1Sopenharmony_ci SourceMapGenerator.initInstance(this.rollup); 30807ac75b1Sopenharmony_ci const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME); 30907ac75b1Sopenharmony_ci const stub = sinon.stub(logger, 'error'); 31007ac75b1Sopenharmony_ci const red: string = '\x1B[31m'; 31107ac75b1Sopenharmony_ci try { 31207ac75b1Sopenharmony_ci await writeArkguardObfuscatedSourceCode( 31307ac75b1Sopenharmony_ci {content: undefined, buildFilePath: '', relativeSourceFilePath: '', originSourceFilePath: ''}, 31407ac75b1Sopenharmony_ci logger, this.rollup.share.projectConfig, {}); 31507ac75b1Sopenharmony_ci } catch (e) { 31607ac75b1Sopenharmony_ci } 31707ac75b1Sopenharmony_ci expect(stub.calledWith(red, 31807ac75b1Sopenharmony_ci `ArkTS:INTERNAL ERROR: Failed to obfuscate file '' with arkguard. TypeError: Cannot read properties of undefined (reading 'obfuscate')` 31907ac75b1Sopenharmony_ci )).to.be.true; 32007ac75b1Sopenharmony_ci stub.restore(); 32107ac75b1Sopenharmony_ci SourceMapGenerator.cleanSourceMapObject(); 32207ac75b1Sopenharmony_ci }); 32307ac75b1Sopenharmony_ci 32407ac75b1Sopenharmony_ci mocha.it('7-1: test the error message of writeMinimizedSourceCode', async function () { 32507ac75b1Sopenharmony_ci this.rollup.build(RELEASE); 32607ac75b1Sopenharmony_ci const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME); 32707ac75b1Sopenharmony_ci const stub = sinon.stub(logger, 'error'); 32807ac75b1Sopenharmony_ci const red: string = '\x1B[31m'; 32907ac75b1Sopenharmony_ci const reset: string = '\x1B[39m'; 33007ac75b1Sopenharmony_ci try { 33107ac75b1Sopenharmony_ci await writeMinimizedSourceCode(undefined, '', logger); 33207ac75b1Sopenharmony_ci } catch (e) { 33307ac75b1Sopenharmony_ci } 33407ac75b1Sopenharmony_ci expect(stub.calledWith(red, 33507ac75b1Sopenharmony_ci 'ArkTS:INTERNAL ERROR: Failed to obfuscate source code for ', reset 33607ac75b1Sopenharmony_ci )).to.be.true; 33707ac75b1Sopenharmony_ci stub.restore(); 33807ac75b1Sopenharmony_ci }); 33907ac75b1Sopenharmony_ci 34007ac75b1Sopenharmony_ci mocha.it('8-1: test tryMangleFileName when obfuscation is disabled', async function () { 34107ac75b1Sopenharmony_ci const filePath = '/mnt/application/entry/build/default/cache/default/default@CompileArkTS/esmodule/release/src/main/ets/entryability/EntryAbility.ts'; 34207ac75b1Sopenharmony_ci const originalFilePath = '/mnt/application/entry/src/main/ets/entryability/EntryAbility.ets'; 34307ac75b1Sopenharmony_ci const projectConfig = { 34407ac75b1Sopenharmony_ci projectRootPath: '/mnt/application', 34507ac75b1Sopenharmony_ci packageDir: 'oh_modules', 34607ac75b1Sopenharmony_ci modulePathMap: { 34707ac75b1Sopenharmony_ci entry: '/mnt/application/entry' 34807ac75b1Sopenharmony_ci }, 34907ac75b1Sopenharmony_ci obfuscationMergedObConfig: { 35007ac75b1Sopenharmony_ci options: { 35107ac75b1Sopenharmony_ci enableFileNameObfuscation: false 35207ac75b1Sopenharmony_ci } 35307ac75b1Sopenharmony_ci } 35407ac75b1Sopenharmony_ci } 35507ac75b1Sopenharmony_ci const result = tryMangleFileName(filePath, projectConfig, originalFilePath); 35607ac75b1Sopenharmony_ci expect(result === filePath).to.be.true; 35707ac75b1Sopenharmony_ci }); 35807ac75b1Sopenharmony_ci 35907ac75b1Sopenharmony_ci mocha.it('8-2: test tryMangleFileName when obfuscation is enabled', async function () { 36007ac75b1Sopenharmony_ci const filePath = '/mnt/application/entry/build/default/cache/default/default@CompileArkTS/esmodule/release/src/main/ets/entryability/EntryAbility.ts'; 36107ac75b1Sopenharmony_ci const originalFilePath = '/mnt/application/entry/src/main/ets/entryability/EntryAbility.ets'; 36207ac75b1Sopenharmony_ci const newFilePath = '/mnt/application/entry/build/default/cache/default/default@CompileArkTS/esmodule/release/src/main/ets/a/b.ts'; 36307ac75b1Sopenharmony_ci const projectConfig = { 36407ac75b1Sopenharmony_ci projectRootPath: '/mnt/application', 36507ac75b1Sopenharmony_ci packageDir: 'oh_modules', 36607ac75b1Sopenharmony_ci modulePathMap: { 36707ac75b1Sopenharmony_ci entry: '/mnt/application/entry' 36807ac75b1Sopenharmony_ci }, 36907ac75b1Sopenharmony_ci obfuscationMergedObConfig: { 37007ac75b1Sopenharmony_ci options: { 37107ac75b1Sopenharmony_ci enableFileNameObfuscation: true 37207ac75b1Sopenharmony_ci } 37307ac75b1Sopenharmony_ci } 37407ac75b1Sopenharmony_ci } 37507ac75b1Sopenharmony_ci 37607ac75b1Sopenharmony_ci const printerConfig = { 37707ac75b1Sopenharmony_ci //Print obfuscation time&memory usage of all files and obfuscation processes 37807ac75b1Sopenharmony_ci mFilesPrinter: false, 37907ac75b1Sopenharmony_ci //Print time&memory usage of a single file obfuscation in transform processes 38007ac75b1Sopenharmony_ci mSingleFilePrinter: false, 38107ac75b1Sopenharmony_ci //Print sum up time of transform processes during obfuscation 38207ac75b1Sopenharmony_ci mSumPrinter: false, 38307ac75b1Sopenharmony_ci //Output path of printer 38407ac75b1Sopenharmony_ci mOutputPath: "" 38507ac75b1Sopenharmony_ci } 38607ac75b1Sopenharmony_ci 38707ac75b1Sopenharmony_ci const arkguardConfig = { 38807ac75b1Sopenharmony_ci mRenameFileName: { 38907ac75b1Sopenharmony_ci mEnable: true, 39007ac75b1Sopenharmony_ci mNameGeneratorType: 1, 39107ac75b1Sopenharmony_ci mReservedFileNames: [ 39207ac75b1Sopenharmony_ci 'entry', 39307ac75b1Sopenharmony_ci 'mnt', 39407ac75b1Sopenharmony_ci 'application', 39507ac75b1Sopenharmony_ci 'entry', 39607ac75b1Sopenharmony_ci 'src', 39707ac75b1Sopenharmony_ci 'main', 39807ac75b1Sopenharmony_ci 'ets', 39907ac75b1Sopenharmony_ci 'build', 40007ac75b1Sopenharmony_ci 'default', 40107ac75b1Sopenharmony_ci 'cache', 40207ac75b1Sopenharmony_ci 'default', 40307ac75b1Sopenharmony_ci 'default@CompileArkTS', 40407ac75b1Sopenharmony_ci 'esmodule', 40507ac75b1Sopenharmony_ci 'release' 40607ac75b1Sopenharmony_ci ], 40707ac75b1Sopenharmony_ci }, 40807ac75b1Sopenharmony_ci mPerformancePrinter: printerConfig 40907ac75b1Sopenharmony_ci } 41007ac75b1Sopenharmony_ci 41107ac75b1Sopenharmony_ci let arkObfuscator: ArkObfuscator = new ArkObfuscator(); 41207ac75b1Sopenharmony_ci arkObfuscator.init(arkguardConfig); 41307ac75b1Sopenharmony_ci const content = `function foo() {}` 41407ac75b1Sopenharmony_ci const obfuscateResult = arkObfuscator.obfuscate(content, filePath, undefined); 41507ac75b1Sopenharmony_ci obfuscateResult.then(result => { 41607ac75b1Sopenharmony_ci const afterObfuscateFilePath = result.filePath; 41707ac75b1Sopenharmony_ci expect(afterObfuscateFilePath === newFilePath).to.be.true; 41807ac75b1Sopenharmony_ci }) 41907ac75b1Sopenharmony_ci 42007ac75b1Sopenharmony_ci const result = tryMangleFileName(filePath, projectConfig, originalFilePath); 42107ac75b1Sopenharmony_ci expect(result === newFilePath).to.be.true; 42207ac75b1Sopenharmony_ci }); 42307ac75b1Sopenharmony_ci 42407ac75b1Sopenharmony_ci mocha.it('9-1: test writeArkguardObfuscatedSourceCode when obfuscation is enabled', async function () { 42507ac75b1Sopenharmony_ci this.rollup.build(RELEASE); 42607ac75b1Sopenharmony_ci const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME); 42707ac75b1Sopenharmony_ci const arkguardConfig = { 42807ac75b1Sopenharmony_ci mCompact: false, 42907ac75b1Sopenharmony_ci mDisableConsole: false, 43007ac75b1Sopenharmony_ci mSimplify: false, 43107ac75b1Sopenharmony_ci mRemoveComments: true, 43207ac75b1Sopenharmony_ci mNameObfuscation: { 43307ac75b1Sopenharmony_ci mEnable: true, 43407ac75b1Sopenharmony_ci mNameGeneratorType: 1, 43507ac75b1Sopenharmony_ci mReservedNames: [], 43607ac75b1Sopenharmony_ci mRenameProperties: false, 43707ac75b1Sopenharmony_ci mReservedProperties: [], 43807ac75b1Sopenharmony_ci mKeepStringProperty: true, 43907ac75b1Sopenharmony_ci mTopLevel: false, 44007ac75b1Sopenharmony_ci mReservedToplevelNames: [], 44107ac75b1Sopenharmony_ci mUniversalReservedProperties: [], 44207ac75b1Sopenharmony_ci mUniversalReservedToplevelNames: [], 44307ac75b1Sopenharmony_ci }, 44407ac75b1Sopenharmony_ci mRemoveDeclarationComments: { 44507ac75b1Sopenharmony_ci mEnable: true, 44607ac75b1Sopenharmony_ci mReservedComments: [] 44707ac75b1Sopenharmony_ci }, 44807ac75b1Sopenharmony_ci mEnableSourceMap: true, 44907ac75b1Sopenharmony_ci mEnableNameCache: true, 45007ac75b1Sopenharmony_ci mRenameFileName: { 45107ac75b1Sopenharmony_ci mEnable: false, 45207ac75b1Sopenharmony_ci mNameGeneratorType: 1, 45307ac75b1Sopenharmony_ci mReservedFileNames: [], 45407ac75b1Sopenharmony_ci }, 45507ac75b1Sopenharmony_ci mExportObfuscation: false, 45607ac75b1Sopenharmony_ci mPerformancePrinter: { 45707ac75b1Sopenharmony_ci mFilesPrinter: false, 45807ac75b1Sopenharmony_ci mSingleFilePrinter: false, 45907ac75b1Sopenharmony_ci mSumPrinter: false, 46007ac75b1Sopenharmony_ci mOutputPath: "" 46107ac75b1Sopenharmony_ci }, 46207ac75b1Sopenharmony_ci mKeepFileSourceCode: { 46307ac75b1Sopenharmony_ci mKeepSourceOfPaths: new Set(), 46407ac75b1Sopenharmony_ci mkeepFilesAndDependencies: new Set(), 46507ac75b1Sopenharmony_ci }, 46607ac75b1Sopenharmony_ci }; 46707ac75b1Sopenharmony_ci 46807ac75b1Sopenharmony_ci const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 46907ac75b1Sopenharmony_ci sourceMapGenerator.setNewSoureMaps(false); 47007ac75b1Sopenharmony_ci const arkObfuscator: ArkObfuscator = new ArkObfuscator(); 47107ac75b1Sopenharmony_ci arkObfuscator.init(arkguardConfig); 47207ac75b1Sopenharmony_ci 47307ac75b1Sopenharmony_ci const projectConfig = { 47407ac75b1Sopenharmony_ci projectRootPath: PROJECT_ROOT, 47507ac75b1Sopenharmony_ci arkObfuscator: arkObfuscator, 47607ac75b1Sopenharmony_ci packageDir: 'oh_modules', 47707ac75b1Sopenharmony_ci localPackageSet: new Set(), 47807ac75b1Sopenharmony_ci useTsHar: false, 47907ac75b1Sopenharmony_ci useNormalized: false 48007ac75b1Sopenharmony_ci }; 48107ac75b1Sopenharmony_ci 48207ac75b1Sopenharmony_ci const sourceFileName = 'sourceFile.ts'; 48307ac75b1Sopenharmony_ci const originalSourceFilePath = path.join(__dirname, '../../test/ark_compiler_ut/testdata/har_obfuscation', sourceFileName); 48407ac75b1Sopenharmony_ci const moduleInfo = { 48507ac75b1Sopenharmony_ci content: fs.readFileSync(originalSourceFilePath, 'utf-8'), 48607ac75b1Sopenharmony_ci buildFilePath: `${PROJECT_ROOT}/har_obfuscation/build/default/cache/${sourceFileName}`, 48707ac75b1Sopenharmony_ci relativeSourceFilePath: `har_obfuscation/${sourceFileName}`, 48807ac75b1Sopenharmony_ci originSourceFilePath: originalSourceFilePath, 48907ac75b1Sopenharmony_ci rollupModuleId: originalSourceFilePath 49007ac75b1Sopenharmony_ci }; 49107ac75b1Sopenharmony_ci 49207ac75b1Sopenharmony_ci try { 49307ac75b1Sopenharmony_ci await writeArkguardObfuscatedSourceCode(moduleInfo, logger, projectConfig, {}); 49407ac75b1Sopenharmony_ci } catch (e) { 49507ac75b1Sopenharmony_ci } 49607ac75b1Sopenharmony_ci const expectedResult = `export function add(d: number, e: number): number { 49707ac75b1Sopenharmony_ci return d + e; 49807ac75b1Sopenharmony_ci} 49907ac75b1Sopenharmony_ciexport function findElement<a>(b: a[], c: (item: a) => boolean): a | undefined { 50007ac75b1Sopenharmony_ci return b.find(c); 50107ac75b1Sopenharmony_ci} 50207ac75b1Sopenharmony_ci`; 50307ac75b1Sopenharmony_ci const result = fs.readFileSync(moduleInfo.buildFilePath, 'utf-8'); 50407ac75b1Sopenharmony_ci expect(result === expectedResult).to.be.true; 50507ac75b1Sopenharmony_ci 50607ac75b1Sopenharmony_ci const declFileName = 'sourceFile.d.ts'; 50707ac75b1Sopenharmony_ci const originalDeclFilePath = path.join(__dirname, '../../test/ark_compiler_ut/testdata/har_obfuscation/', declFileName); 50807ac75b1Sopenharmony_ci 50907ac75b1Sopenharmony_ci const declModuleInfo = { 51007ac75b1Sopenharmony_ci content: fs.readFileSync(originalDeclFilePath, 'utf-8'), 51107ac75b1Sopenharmony_ci buildFilePath: `${PROJECT_ROOT}/har_obfuscation/build/default/cache/${declFileName}`, 51207ac75b1Sopenharmony_ci relativeSourceFilePath: `har_obfuscation/build/default/cache/${declFileName}`, 51307ac75b1Sopenharmony_ci originSourceFilePath: originalSourceFilePath, 51407ac75b1Sopenharmony_ci rollupModuleId: originalSourceFilePath 51507ac75b1Sopenharmony_ci }; 51607ac75b1Sopenharmony_ci 51707ac75b1Sopenharmony_ci try { 51807ac75b1Sopenharmony_ci await writeArkguardObfuscatedSourceCode(declModuleInfo, logger, projectConfig, {}); 51907ac75b1Sopenharmony_ci } catch (e) { 52007ac75b1Sopenharmony_ci } 52107ac75b1Sopenharmony_ci 52207ac75b1Sopenharmony_ci const expectedDeclResult = `export declare function add(d: number, e: number): number; 52307ac75b1Sopenharmony_ciexport declare function findElement<a>(b: a[], c: (item: a) => boolean): a | undefined; 52407ac75b1Sopenharmony_ci`; 52507ac75b1Sopenharmony_ci const declResult = fs.readFileSync(declModuleInfo.buildFilePath, 'utf-8'); 52607ac75b1Sopenharmony_ci expect(declResult === expectedDeclResult).to.be.true; 52707ac75b1Sopenharmony_ci }); 52807ac75b1Sopenharmony_ci});