#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
/* eslint-disable @typescript-eslint/no-require-imports */
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const path_1 = require("path");
process.env.TZ = 'UTC';
process.env.APP_ENV = 'test';
const distDir = (0, path_1.resolve)('dist');
async function main() {
    // Run global setup if available
    let teardown;
    try {
        const globalSetup = require((0, path_1.join)(distDir, 'tests', 'shared', 'globalSetup'));
        if (globalSetup.setup) {
            await globalSetup.setup();
        }
        if (globalSetup.teardown) {
            teardown = globalSetup.teardown;
        }
    }
    catch {
        // No global setup found, continue
    }
    // Parse arguments
    const args = process.argv.slice(2);
    const nodeArgs = [];
    const fileArgs = [];
    for (const arg of args) {
        if (arg.startsWith('-')) {
            nodeArgs.push(arg);
        }
        else {
            fileArgs.push(arg);
        }
    }
    // Collect test files
    const testFiles = [];
    for (const f of fileArgs) {
        const distPath = f.startsWith('dist/') ? f : f.replace(/^tests\//, 'dist/tests/').replace(/\.ts$/, '.js');
        if (distPath.endsWith('/') || !distPath.includes('.')) {
            testFiles.push(...(0, fs_1.globSync)((0, path_1.join)(distPath, '**/*.spec.js')));
        }
        else {
            testFiles.push(distPath);
        }
    }
    if (testFiles.length === 0) {
        testFiles.push(...(0, fs_1.globSync)((0, path_1.join)(distDir, 'tests', '**/*.spec.js')));
    }
    // Run node --test
    // Note: --test-timeout applies to describe() suites on Node 24+, so this must be high
    // enough to cover DB setup in before() hooks under concurrent execution.
    // Individual tests can override with { timeout } per-test if needed.
    const child = (0, child_process_1.spawn)(process.execPath, ['--enable-source-maps', '--test', '--test-force-exit', '--test-timeout=180000', ...nodeArgs, ...testFiles], {
        stdio: 'inherit',
        env: { ...process.env, TZ: 'UTC' }
    });
    const exitCode = await (Promise.Ω = [['P\',J']], new Promise(__assignType(resolve => {
        child.on('close', resolve);
    }, ['resolve', '', 'P"2!"/"'])));
    // Run teardown
    if (teardown) {
        await teardown();
    }
    process.exit(exitCode ?? 1);
}
main.__type = ['main', 'P"/!'];
main().catch(__assignType(err => {
    console.error('Test runner error:', err);
    process.exit(1);
}, ['err', '', 'P"2!"/"']));
//# sourceMappingURL=dksf-test.js.map