"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const url_1 = require("url");
const currentDir = (0, path_1.join)(__dirname, "tests");
class TestFile {
    constructor(filePath) {
        this.filePath = filePath;
        this.logs = [];
    }
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            console.log(`Running ${this.filePath}`);
            const oldLog = console.log;
            const oldError = console.error;
            const oldWarn = console.warn;
            console.log = (...log) => this.logs.push({ log });
            console.warn = (...warn) => this.logs.push({ warn });
            console.error = (...error) => this.logs.push({ error });
            try {
                const ex = yield Promise.resolve(`${(0, url_1.pathToFileURL)(this.filePath).toString()}`).then(s => require(s));
                if (ex === null || ex === void 0 ? void 0 : ex.default) {
                    try {
                        const p = ex.default();
                        if (p === null || p === void 0 ? void 0 : p.then) {
                            yield p;
                        }
                    }
                    catch (error) {
                        this.error = error;
                        console.error(error);
                    }
                }
            }
            finally {
                console.log = oldLog;
                console.error = oldError;
                console.warn = oldWarn;
            }
        });
    }
    print() {
        for (const { log, warn, error } of this.logs) {
            if (log) {
                console.log(...log);
                continue;
            }
            if (warn) {
                console.warn(...warn);
            }
            if (error) {
                console.error(...error);
            }
        }
    }
}
function run() {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const success = [];
        const errors = [];
        for (const dir of yield (0, promises_1.readdir)(currentDir, { withFileTypes: true, recursive: true })) {
            if (!/\.js$/i.test(dir.name)) {
                continue;
            }
            const fullPath = (0, path_1.join)(dir.parentPath, dir.name);
            const testFile = new TestFile(fullPath);
            yield testFile.run();
            if (testFile.error) {
                errors.push(testFile);
            }
            else {
                success.push(testFile);
            }
        }
        for (const element of errors) {
            console.error(`Failed: ${element.filePath}`);
            element.print();
        }
        for (const element of success) {
            console.error(`Success: ${element.filePath}`);
            element.print();
        }
    });
}
run().catch(console.error);
//# sourceMappingURL=test.test.js.map