1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use rollupObject file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { expect } from 'chai';
17import mocha from 'mocha';
18import sinon from 'sinon';
19
20import {
21  RELEASE,
22  OH_MODULES
23} from '../../../lib/fast_build/ark_compiler/common/ark_define';
24import { TS2ABC } from '../../../lib/pre_define';
25import CommonModeMock from '../mock/class_mock/common_mode_mock';
26import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock';
27import {
28  ES2ABC_PATH,
29  TS2ABC_PATH
30} from '../mock/rollup_mock/path_config';
31import {
32  CMD_DEBUG_INFO,
33  NODE,
34  EXPOSE_GC,
35  DEBUG,
36  MODULES
37} from '../mock/rollup_mock/common';
38
39mocha.describe('test common_mode file api', function () {
40  mocha.before(function () {
41    this.rollup = new RollUpPluginMock();
42  });
43
44  mocha.after(() => {
45    delete this.rollup;
46  });
47
48  mocha.it('1-1-1: test initCmdEnv under build debug: projectConfig.pandaMode is Ts2Abc', function () {
49    this.rollup.build();
50    const modeMock = new CommonModeMock(this.rollup);
51    modeMock.projectConfig.pandaMode = TS2ABC;
52    modeMock.projectConfig.packageDir = OH_MODULES;
53    const args: string[] = modeMock.checkInitCmdEnv();
54    expect(args.length === 5).to.be.true;
55    expect(args[0] === NODE).to.be.true;
56    expect(args[1].indexOf(EXPOSE_GC) > 0).to.be.true;
57    expect(args[2].indexOf(TS2ABC_PATH) > 0).to.be.true;
58    expect(args[3].indexOf(DEBUG) > 0).to.be.true;
59    expect(args[4].indexOf(MODULES) > 0).to.be.true;
60  });
61
62  mocha.it('1-1-2: test initCmdEnv under build debug: projectConfig.pandaMode is Es2Abc', function () {
63    this.rollup.build();
64    const modeMock = new CommonModeMock(this.rollup);
65    const args: string[] = modeMock.checkInitCmdEnv();
66    expect(args.length === 2).to.be.true;
67    expect(args[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
68    expect(args[1] === CMD_DEBUG_INFO).to.be.true;
69  });
70
71  mocha.it('1-1-3: test initCmdEnv under build debug: projectConfig.pandaMode is invalid value', function () {
72    this.rollup.build();
73    const modeMock =  new CommonModeMock(this.rollup);
74    const stub = sinon.stub(modeMock, 'throwArkTsCompilerError');
75    modeMock.projectConfig.pandaMode = 'invalid value';
76    try {
77      modeMock.checkInitCmdEnv();
78    } catch (e) {
79    }
80    expect(stub.calledWithMatch(`ArkTS:INTERNAL ERROR: Invalid compilation mode.`)).to.be.true;
81    stub.restore();
82  });
83
84  mocha.it('1-2: test initCmdEnv under build release', function () {
85    this.rollup.build(RELEASE);
86    const modeMock = new CommonModeMock(this.rollup);
87    const args: string[] = modeMock.checkInitCmdEnv();
88    expect(args.length === 1).to.be.true;
89    expect(args[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
90    expect(args[0] === CMD_DEBUG_INFO).to.be.false;
91  });
92
93  mocha.it('1-3: test initCmdEnv under preview debug', function () {
94    this.rollup.preview();
95    const modeMock = new CommonModeMock(this.rollup);
96    const args: string[] = modeMock.checkInitCmdEnv();
97    expect(args.length === 2).to.be.true;
98    expect(args[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
99    expect(args[1] === CMD_DEBUG_INFO).to.be.true;
100  });
101
102  mocha.it('1-4: test initCmdEnv under hot reload debug', function () {
103    this.rollup.hotReload();
104    const modeMock = new CommonModeMock(this.rollup);
105    const args: string[] = modeMock.checkInitCmdEnv();
106    expect(args.length === 2).to.be.true;
107    expect(args[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
108    expect(args[1] === CMD_DEBUG_INFO).to.be.true;
109  });
110});