107ac75b1Sopenharmony_ci/*
207ac75b1Sopenharmony_ci * Copyright (c) 2024 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_ciimport mocha from 'mocha';
1707ac75b1Sopenharmony_ciimport fs from 'fs';
1807ac75b1Sopenharmony_ciimport path from 'path';
1907ac75b1Sopenharmony_ciimport { expect } from 'chai';
2007ac75b1Sopenharmony_ciimport * as ts from 'typescript';
2107ac75b1Sopenharmony_ci
2207ac75b1Sopenharmony_ciimport { EXPECT_INDEX_ETS } from './mock/rollup_mock/path_config';
2307ac75b1Sopenharmony_ciimport RollUpPluginMock from './mock/rollup_mock/rollup_plugin_mock';
2407ac75b1Sopenharmony_ciimport {
2507ac75b1Sopenharmony_ci  addLocalPackageSet,
2607ac75b1Sopenharmony_ci  compilerOptions,
2707ac75b1Sopenharmony_ci  localPackageSet,
2807ac75b1Sopenharmony_ci  needReCheckForChangedDepUsers,
2907ac75b1Sopenharmony_ci  resetEtsCheck,
3007ac75b1Sopenharmony_ci  serviceChecker,
3107ac75b1Sopenharmony_ci} from '../../lib/ets_checker';
3207ac75b1Sopenharmony_ciimport { TS_BUILD_INFO_SUFFIX } from '../../lib/pre_define'
3307ac75b1Sopenharmony_ciimport {
3407ac75b1Sopenharmony_ci  globalProgram,
3507ac75b1Sopenharmony_ci  projectConfig
3607ac75b1Sopenharmony_ci} from '../../main';
3707ac75b1Sopenharmony_ci
3807ac75b1Sopenharmony_cimocha.describe('test ets_checker file api', function () {
3907ac75b1Sopenharmony_ci    mocha.before(function () {
4007ac75b1Sopenharmony_ci        this.rollup = new RollUpPluginMock();
4107ac75b1Sopenharmony_ci    });
4207ac75b1Sopenharmony_ci
4307ac75b1Sopenharmony_ci    mocha.after(() => {
4407ac75b1Sopenharmony_ci        delete this.rollup;
4507ac75b1Sopenharmony_ci        const cacheFile: string = path.resolve(projectConfig.cachePath, '../.ts_checker_cache');
4607ac75b1Sopenharmony_ci        if (fs.existsSync(cacheFile)) {
4707ac75b1Sopenharmony_ci            fs.unlinkSync(cacheFile);
4807ac75b1Sopenharmony_ci        }
4907ac75b1Sopenharmony_ci        const tsBuildInfoFilePath: string = path.resolve(projectConfig.cachePath, '..', TS_BUILD_INFO_SUFFIX);
5007ac75b1Sopenharmony_ci        if (fs.existsSync(tsBuildInfoFilePath)) {
5107ac75b1Sopenharmony_ci            fs.unlinkSync(tsBuildInfoFilePath);
5207ac75b1Sopenharmony_ci        }
5307ac75b1Sopenharmony_ci        const tsBuildInfoLinterFilePath: string = tsBuildInfoFilePath + '.linter';
5407ac75b1Sopenharmony_ci        if (fs.existsSync(tsBuildInfoLinterFilePath)) {
5507ac75b1Sopenharmony_ci            fs.unlinkSync(tsBuildInfoLinterFilePath);
5607ac75b1Sopenharmony_ci        }
5707ac75b1Sopenharmony_ci    });
5807ac75b1Sopenharmony_ci
5907ac75b1Sopenharmony_ci    mocha.it('1-1: test addLocalPackageSet for original ohmurl', function () {
6007ac75b1Sopenharmony_ci        this.rollup.build();
6107ac75b1Sopenharmony_ci        const rollupObject = this.rollup;
6207ac75b1Sopenharmony_ci        const rollupFileList = rollupObject.getModuleIds();
6307ac75b1Sopenharmony_ci        projectConfig.useNormalizedOHMUrl = false;
6407ac75b1Sopenharmony_ci        for (const moduleId of rollupFileList) {
6507ac75b1Sopenharmony_ci            if (fs.existsSync(moduleId)) {
6607ac75b1Sopenharmony_ci                addLocalPackageSet(moduleId, rollupObject);
6707ac75b1Sopenharmony_ci            }
6807ac75b1Sopenharmony_ci        }
6907ac75b1Sopenharmony_ci        expect(localPackageSet.has('entry')).to.be.true;
7007ac75b1Sopenharmony_ci        localPackageSet.clear();
7107ac75b1Sopenharmony_ci    });
7207ac75b1Sopenharmony_ci
7307ac75b1Sopenharmony_ci    mocha.it('1-2: test addLocalPackageSet for normalized ohmurl', function () {
7407ac75b1Sopenharmony_ci        this.rollup.build();
7507ac75b1Sopenharmony_ci        const rollupObject = this.rollup;
7607ac75b1Sopenharmony_ci        const rollupFileList = rollupObject.getModuleIds();
7707ac75b1Sopenharmony_ci        projectConfig.useNormalizedOHMUrl = true;
7807ac75b1Sopenharmony_ci        for (const moduleId of rollupFileList) {
7907ac75b1Sopenharmony_ci            const moduleInfo: Object = rollupObject.getModuleInfo(moduleId);
8007ac75b1Sopenharmony_ci            if (moduleInfo) {
8107ac75b1Sopenharmony_ci                const metaInfo: Object = moduleInfo.meta;
8207ac75b1Sopenharmony_ci                metaInfo.pkgName = 'pkgname';
8307ac75b1Sopenharmony_ci            }
8407ac75b1Sopenharmony_ci            if (fs.existsSync(moduleId)) {
8507ac75b1Sopenharmony_ci                addLocalPackageSet(moduleId, rollupObject);
8607ac75b1Sopenharmony_ci            }
8707ac75b1Sopenharmony_ci        }
8807ac75b1Sopenharmony_ci        expect(localPackageSet.has('pkgname')).to.be.true;
8907ac75b1Sopenharmony_ci    });
9007ac75b1Sopenharmony_ci
9107ac75b1Sopenharmony_ci    mocha.it('1-3: test getOrCreateLanguageService when dependency in oh-package.json change', function () {
9207ac75b1Sopenharmony_ci        this.timeout(10000);
9307ac75b1Sopenharmony_ci        this.rollup.build();
9407ac75b1Sopenharmony_ci        let rollupObject = this.rollup;
9507ac75b1Sopenharmony_ci        expect(needReCheckForChangedDepUsers).to.be.false;
9607ac75b1Sopenharmony_ci
9707ac75b1Sopenharmony_ci        interface MockCacheStore {
9807ac75b1Sopenharmony_ci            service: Object | undefined;
9907ac75b1Sopenharmony_ci            pkgJsonFileHash: string;
10007ac75b1Sopenharmony_ci            targetESVersion: number;
10107ac75b1Sopenharmony_ci        }
10207ac75b1Sopenharmony_ci        const mockServiceCache = {
10307ac75b1Sopenharmony_ci            service: undefined,
10407ac75b1Sopenharmony_ci            pkgJsonFileHash: '9f07917d395682c73a90af8f5796a2c6',
10507ac75b1Sopenharmony_ci            targetESVersion: 8
10607ac75b1Sopenharmony_ci        }
10707ac75b1Sopenharmony_ci        let mockCache = new Map<string, MockCacheStore>();
10807ac75b1Sopenharmony_ci        mockCache.set('service', mockServiceCache);
10907ac75b1Sopenharmony_ci        rollupObject.share.cache = mockCache;
11007ac75b1Sopenharmony_ci        rollupObject.share.depInfo = {enableIncre: true};
11107ac75b1Sopenharmony_ci        rollupObject.share.projectConfig.pkgJsonFileHash = '26bde0f30dda53b0afcbf39428ec9851';
11207ac75b1Sopenharmony_ci        Object.assign(projectConfig, rollupObject.share.projectConfig);
11307ac75b1Sopenharmony_ci
11407ac75b1Sopenharmony_ci        // The current hash and the hash in cache of the dependency differs, should recheck
11507ac75b1Sopenharmony_ci        serviceChecker([EXPECT_INDEX_ETS], null, null, null, rollupObject.share);
11607ac75b1Sopenharmony_ci        expect(needReCheckForChangedDepUsers).to.be.true;
11707ac75b1Sopenharmony_ci        expect(globalProgram.program != null).to.be.true;
11807ac75b1Sopenharmony_ci
11907ac75b1Sopenharmony_ci        resetEtsCheck();
12007ac75b1Sopenharmony_ci        expect(needReCheckForChangedDepUsers).to.be.false;
12107ac75b1Sopenharmony_ci        expect(globalProgram.program == null).to.be.true;
12207ac75b1Sopenharmony_ci
12307ac75b1Sopenharmony_ci        // The current hash and the hash in cache of the dependency are the same, no need to recheck
12407ac75b1Sopenharmony_ci        serviceChecker([EXPECT_INDEX_ETS], null, null, null, rollupObject.share);
12507ac75b1Sopenharmony_ci        expect(needReCheckForChangedDepUsers).to.be.false;
12607ac75b1Sopenharmony_ci        expect(globalProgram.program != null).to.be.true;
12707ac75b1Sopenharmony_ci
12807ac75b1Sopenharmony_ci        resetEtsCheck();
12907ac75b1Sopenharmony_ci        expect(needReCheckForChangedDepUsers).to.be.false;
13007ac75b1Sopenharmony_ci        expect(globalProgram.program == null).to.be.true;
13107ac75b1Sopenharmony_ci    });
13207ac75b1Sopenharmony_ci
13307ac75b1Sopenharmony_ci    mocha.it('1-4: test getOrCreateLanguageService when paths in compilerOptions change', function () {
13407ac75b1Sopenharmony_ci        this.timeout(10000);
13507ac75b1Sopenharmony_ci        this.rollup.build();
13607ac75b1Sopenharmony_ci        let rollupObject = this.rollup;
13707ac75b1Sopenharmony_ci        process.env.compileTool = 'rollup';
13807ac75b1Sopenharmony_ci
13907ac75b1Sopenharmony_ci        Object.assign(projectConfig, rollupObject.share.projectConfig);
14007ac75b1Sopenharmony_ci        serviceChecker([EXPECT_INDEX_ETS], null, null, null, rollupObject.share);
14107ac75b1Sopenharmony_ci        expect(JSON.stringify(compilerOptions.paths) === '{"*":["*","../../../../*","../*"]}').to.be.true;
14207ac75b1Sopenharmony_ci        expect(needReCheckForChangedDepUsers).to.be.false;
14307ac75b1Sopenharmony_ci        expect(globalProgram.program != null).to.be.true;
14407ac75b1Sopenharmony_ci        expect(compilerOptions.skipPathsInKeyForCompilationSettings).to.be.true;
14507ac75b1Sopenharmony_ci
14607ac75b1Sopenharmony_ci        resetEtsCheck();
14707ac75b1Sopenharmony_ci        expect(needReCheckForChangedDepUsers).to.be.false;
14807ac75b1Sopenharmony_ci        expect(globalProgram.program == null).to.be.true;
14907ac75b1Sopenharmony_ci        expect(compilerOptions.skipPathsInKeyForCompilationSettings).to.be.true;
15007ac75b1Sopenharmony_ci
15107ac75b1Sopenharmony_ci        interface MockCacheStore {
15207ac75b1Sopenharmony_ci            service: Object | undefined;
15307ac75b1Sopenharmony_ci            pkgJsonFileHash: string;
15407ac75b1Sopenharmony_ci            targetESVersion: number;
15507ac75b1Sopenharmony_ci        }
15607ac75b1Sopenharmony_ci        const mockServiceCache = {
15707ac75b1Sopenharmony_ci            service: undefined,
15807ac75b1Sopenharmony_ci            pkgJsonFileHash: '9f07917d395682c73a90af8f5796a2c6',
15907ac75b1Sopenharmony_ci            targetESVersion: 8
16007ac75b1Sopenharmony_ci        }
16107ac75b1Sopenharmony_ci        let mockCache = new Map<string, MockCacheStore>();
16207ac75b1Sopenharmony_ci        mockCache.set('service', mockServiceCache);
16307ac75b1Sopenharmony_ci        rollupObject.share.cache = mockCache;
16407ac75b1Sopenharmony_ci        rollupObject.share.depInfo = {enableIncre: true};
16507ac75b1Sopenharmony_ci        rollupObject.share.projectConfig.pkgJsonFileHash = '26bde0f30dda53b0afcbf39428ec9851';
16607ac75b1Sopenharmony_ci
16707ac75b1Sopenharmony_ci        // The current hash of the dependency differs, and the paths in compilerOptions will change since resolveModulePaths change
16807ac75b1Sopenharmony_ci        const resolveModulePaths = ['../testdata/expect'];
16907ac75b1Sopenharmony_ci        serviceChecker([EXPECT_INDEX_ETS], null, resolveModulePaths, null, rollupObject.share);
17007ac75b1Sopenharmony_ci        expect(JSON.stringify(compilerOptions.paths) === '{"*":["*","../../../../../../../testdata/expect/*"]}').to.be.true;
17107ac75b1Sopenharmony_ci        expect(needReCheckForChangedDepUsers).to.be.true;
17207ac75b1Sopenharmony_ci        expect(globalProgram.program != null).to.be.true;
17307ac75b1Sopenharmony_ci        expect(compilerOptions.skipPathsInKeyForCompilationSettings).to.be.true;
17407ac75b1Sopenharmony_ci
17507ac75b1Sopenharmony_ci        resetEtsCheck();
17607ac75b1Sopenharmony_ci        expect(needReCheckForChangedDepUsers).to.be.false;
17707ac75b1Sopenharmony_ci        expect(globalProgram.program == null).to.be.true;
17807ac75b1Sopenharmony_ci        expect(compilerOptions.skipPathsInKeyForCompilationSettings).to.be.true;
17907ac75b1Sopenharmony_ci    });
18007ac75b1Sopenharmony_ci    mocha.it('1-5: test GetEmitHost of program', function () {
18107ac75b1Sopenharmony_ci        const compilerOptions: ts.CompilerOptions = {
18207ac75b1Sopenharmony_ci            target: ts.ScriptTarget.ES2021
18307ac75b1Sopenharmony_ci        };
18407ac75b1Sopenharmony_ci        const fileNames: string[] = ['../testdata/testfiles/testGetEmitHost.ts'];
18507ac75b1Sopenharmony_ci        let program: ts.Program = ts.createProgram(fileNames, compilerOptions);
18607ac75b1Sopenharmony_ci        expect(program.getEmitHost()).to.not.be.undefined;
18707ac75b1Sopenharmony_ci    });
18807ac75b1Sopenharmony_ci});