import chalk from "chalk";
import { emptydirSync } from "fs-extra";
import { Parser } from "./parser/index.js";
import { compile } from "./core/compile.js";
import { precompile } from "./core/precompile.js";
import { instrument } from "./core/instrument.js";
import { execWasmBinarys } from "./core/execute.js";
import { generateReport, reportConfig } from "./generator/index.js";
function logAssertResult(trace) {
    const render = (failed, total) => (trace.fail === 0 ? chalk.greenBright(total) : chalk.redBright(total - failed)) + "/" + trace.total.toString();
    console.log(`\ntest case: ${render(trace.fail, trace.total)} (success/total)\n`);
    if (trace.fail !== 0) {
        console.log(chalk.red("Error Message: "));
        for (const [k, errMsgs] of trace.failed_info.entries()) {
            console.log(`\t${k}: `);
            for (const v of errMsgs) {
                console.log("\t\t" + chalk.yellow(v));
            }
        }
    }
}
export function validatArgument(includes, excludes) {
    if (!Array.isArray(includes)) {
        throw new TypeError("include section illegal");
    }
    if (!Array.isArray(excludes)) {
        throw new TypeError("exclude section illegal");
    }
    for (const includePath of includes) {
        if (typeof includePath !== "string") {
            throw new TypeError("include section illegal");
        }
    }
    for (const excludePath of excludes) {
        if (typeof excludePath !== "string") {
            throw new TypeError("exclude section illegal");
        }
    }
}
export class ImportsArgument {
    module = null;
    instance = null;
    exports = null;
}
/**
 * main function of unit-test, will throw Exception in most condition except job carsh
 */
export async function start_unit_test(fo, to, oo) {
    emptydirSync(oo.outputFolder);
    emptydirSync(oo.tempFolder);
    const unittestPackage = await precompile(fo.includes, fo.excludes, fo.testcases, to.flags);
    console.log(chalk.blueBright("code analysis: ") + chalk.bold.greenBright("OK"));
    const wasmPaths = await compile(unittestPackage.testCodePaths, oo.tempFolder, to.flags);
    console.log(chalk.blueBright("compile testcases: ") + chalk.bold.greenBright("OK"));
    const instrumentResult = await instrument(wasmPaths, Array.from(unittestPackage.sourceFunctions.keys()));
    console.log(chalk.blueBright("instrument: ") + chalk.bold.greenBright("OK"));
    const executedResult = await execWasmBinarys(oo.tempFolder, instrumentResult, to.imports);
    console.log(chalk.blueBright("execute testcases: ") + chalk.bold.greenBright("OK"));
    logAssertResult(executedResult);
    const debugInfoFiles = instrumentResult.map((res) => res.debugInfo);
    const parser = new Parser();
    const fileCoverageInfo = await parser.parse(debugInfoFiles, unittestPackage.sourceFunctions);
    reportConfig.warningLimit = oo.warnLimit ?? reportConfig.warningLimit;
    reportConfig.errorLimit = oo.errorLimit ?? reportConfig.errorLimit;
    generateReport(oo.mode, oo.outputFolder, fileCoverageInfo);
    return executedResult.fail === 0;
}
//# sourceMappingURL=index.js.map