import { exec as execCb } from 'node:child_process';
import { mkdtempSync, rmSync, writeFileSync, readFileSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { promisify } from 'node:util';
const exec = promisify(execCb);
export class ProductTestHarness {
    scenarios = [];
    addScenario(scenario) {
        this.scenarios.push(scenario);
    }
    async runAll() {
        const results = [];
        for (const scenario of this.scenarios) {
            const started = Date.now();
            try {
                const outcome = await runWithTimeout(scenario.execute, scenario.timeout ?? 60_000);
                results.push({
                    scenario: outcome.scenario,
                    passed: outcome.passed,
                    duration: outcome.duration,
                    output: outcome.output,
                });
            }
            catch (error) {
                const duration = Date.now() - started;
                results.push({
                    scenario: scenario.name,
                    passed: false,
                    duration,
                    output: '',
                    error: error instanceof Error ? error.message : String(error),
                });
            }
        }
        const passed = results.filter((r) => r.passed).length;
        const failed = results.length - passed;
        const summary = `Results: ${passed}/${results.length} passed${failed ? ` · Failed: ${failed}` : ''}`;
        return {
            total: results.length,
            passed,
            failed,
            results,
            summary,
        };
    }
}
async function runWithTimeout(fn, timeoutMs) {
    let timeoutHandle;
    const timeoutPromise = new Promise((_resolve, reject) => {
        timeoutHandle = setTimeout(() => reject(new Error('Test timeout exceeded')), timeoutMs);
    });
    try {
        const result = await Promise.race([fn(), timeoutPromise]);
        clearTimeout(timeoutHandle);
        return result;
    }
    catch (error) {
        clearTimeout(timeoutHandle);
        throw error;
    }
}
// -----------------------------------------------------------------------------
// Runtime environment utilities
// -----------------------------------------------------------------------------
export function createRuntimeEnvironment() {
    const root = mkdtempSync(join(tmpdir(), 'agi-runtime-'));
    const execute = async (code, runtime = 'node') => {
        const cwd = root;
        try {
            if (runtime === 'node') {
                const scriptPath = join(root, 'temp-script.js');
                writeFileSync(scriptPath, code, 'utf-8');
                const { stdout, stderr } = await exec(`node "${scriptPath}"`, { cwd, env: process.env });
                return { exitCode: 0, stdout, stderr };
            }
            const { stdout, stderr } = await exec(code, { cwd, shell: '/bin/bash', env: process.env });
            return { exitCode: 0, stdout, stderr };
        }
        catch (error) {
            const err = error;
            return {
                exitCode: typeof err?.code === 'number' ? err.code : 1,
                stdout: err?.stdout ?? '',
                stderr: err?.stderr ?? (error instanceof Error ? error.message : String(error)),
            };
        }
    };
    const writeFile = (path, content) => {
        const target = join(root, path);
        writeFileSync(target, content, 'utf-8');
    };
    const readFile = (path) => {
        return readFileSync(join(root, path), 'utf-8');
    };
    const fileExists = (path) => {
        return existsSync(join(root, path));
    };
    const cleanup = () => {
        try {
            rmSync(root, { recursive: true, force: true });
        }
        catch {
            // ignore cleanup errors
        }
    };
    return { execute, writeFile, readFile, fileExists, cleanup, root };
}
// -----------------------------------------------------------------------------
// Scenario generator (lightweight helper for future use)
// -----------------------------------------------------------------------------
export class TestScenarioGenerator {
    generateSmokeTests() {
        return [
            {
                name: 'smoke_build',
                description: 'Validate build command output',
                execute: async () => ({
                    scenario: 'smoke_build',
                    passed: true,
                    duration: 10,
                    output: 'Build succeeded',
                }),
            },
        ];
    }
}
//# sourceMappingURL=productTestHarness.js.map