import path from "node:path";
import { execa } from "execa";
import { TEST_OUT_FOLDER } from "../common/constants.js";
import { logger } from "../common/logger.js";
class MissingNodeExecutableError extends Error {
    constructor() {
        super("Missing executable: Cannot locate node executable");
        Object.setPrototypeOf(this, MissingNodeExecutableError.prototype);
    }
}
export async function runTests(argv) {
    const nodeExecutable = process.platform === "win32" ? "node.exe" : "node";
    if (nodeExecutable) {
        const args = ["--test", "--test-reporter=spec", "--enable-source-maps"];
        if (argv.filter) {
            args.push("--test-name-pattern", argv.filter);
        }
        const nodeVersion = parseInt(process.versions.node.split(".")[0], 10);
        const globPattern = nodeVersion >= 21
            ? path.join(argv.dir, TEST_OUT_FOLDER, "**", "*.test.mjs")
            : path.join(argv.dir, TEST_OUT_FOLDER);
        args.push(globPattern);
        const nodeProcess = execa(nodeExecutable, args);
        nodeProcess.stdout?.pipe(process.stdout);
        nodeProcess.stderr?.pipe(process.stderr);
        try {
            const result = await nodeProcess;
            return result;
        }
        catch (err) {
            logger.debug(err);
            return { exitCode: err.exitCode };
        }
    }
    else {
        throw new MissingNodeExecutableError();
    }
}
//# sourceMappingURL=invoke-test.js.map