#!/usr/bin/env node
// @ts-nocheck
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { pathToFileURL } from 'node:url';
import { currentDistFreshness } from './lib/ensure-dist-fresh.js';
const root = process.cwd();
const npmBin = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'sks-command-import-smoke-'));
const cache = path.join(tmp, 'npm-cache');
const consumer = path.join(tmp, 'consumer');
const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
const failures = [];
const rows = [];
fs.mkdirSync(consumer, { recursive: true });
fs.writeFileSync(path.join(consumer, 'package.json'), `${JSON.stringify({ name: 'sks-command-smoke-consumer', private: true }, null, 2)}\n`);
try {
    if (process.env.SKS_ENSURE_DIST_NO_REBUILD === '1' || process.env.SKS_RELEASE_DIST_FRESHNESS_NO_REBUILD === '1') {
        const freshness = currentDistFreshness();
        rows.push({ label: 'dist_freshness', ok: freshness.ok, status: freshness.ok ? 0 : 1, issues: freshness.issues });
        if (!freshness.ok)
            failures.push(`dist_not_fresh:${freshness.issues.join(',')}`);
    }
    else {
        run('build', npmBin, ['run', 'build'], { cwd: root });
    }
    const pack = run('npm_pack', npmBin, ['pack', '--json', '--ignore-scripts', '--pack-destination', tmp, '--registry', 'https://registry.npmjs.org/'], { cwd: root });
    const info = pack.ok ? JSON.parse(pack.stdout || '[]')[0] : null;
    const tarball = info ? path.join(tmp, info.filename) : null;
    if (tarball)
        extractPackedTarball(tarball);
    const pkgRoot = path.join(consumer, 'node_modules', 'sneakoscope');
    if (!fs.existsSync(pkgRoot))
        failures.push('installed_package_missing');
    else
        await smokeCommands(pkgRoot);
}
finally {
    if (failures.length && process.argv.includes('--keep')) {
        // Keep temp root for debugging only when explicitly requested.
    }
    else if (!process.argv.includes('--keep')) {
        fs.rmSync(tmp, { recursive: true, force: true });
    }
}
const result = {
    schema: 'sks.blackbox-command-import-smoke.v1',
    ok: failures.length === 0,
    temp_root: failures.length && process.argv.includes('--keep') ? tmp : null,
    rows,
    failures
};
console.log(JSON.stringify(result, null, 2));
if (!result.ok)
    process.exitCode = 1;
function run(label, cmd, args, options = {}) {
    const result = spawnSync(cmd, args, {
        cwd: options.cwd || root,
        encoding: 'utf8',
        timeout: options.timeout || 120_000,
        env: childNpmEnv(options.env)
    });
    const row = {
        label,
        command: [cmd, ...args].join(' '),
        status: result.status,
        ok: result.status === 0,
        stdout: result.stdout || '',
        stderr_tail: String(result.stderr || '').slice(-800)
    };
    rows.push({ ...row, stdout: undefined });
    if (!row.ok)
        failures.push(`${label}:${row.stderr_tail || result.stdout}`);
    return row;
}
function childNpmEnv(extra = {}) {
    const env = { ...process.env, npm_config_cache: cache, SKS_SKIP_NPM_FRESHNESS_CHECK: '1', CI: 'true', ...extra };
    delete env.npm_config_dry_run;
    delete env.NPM_CONFIG_DRY_RUN;
    return env;
}
function extractPackedTarball(tarball) {
    const pkgRoot = path.join(consumer, 'node_modules', 'sneakoscope');
    fs.mkdirSync(pkgRoot, { recursive: true });
    const stripFlag = process.platform === 'win32' ? '--strip-components=1' : '--strip-components';
    const args = process.platform === 'win32'
        ? ['-xzf', tarball, '-C', pkgRoot, stripFlag]
        : ['-xzf', tarball, '-C', pkgRoot, stripFlag, '1'];
    const extracted = run('extract_pack_tarball', 'tar', args, { cwd: consumer });
    if (!extracted.ok)
        return;
    linkRuntimeDependencies(pkgRoot);
}
function linkRuntimeDependencies(pkgRoot) {
    const deps = Object.keys(pkg.dependencies || {});
    if (!deps.length)
        return;
    const nodeModules = path.join(pkgRoot, 'node_modules');
    fs.mkdirSync(nodeModules, { recursive: true });
    for (const dep of deps) {
        const source = path.join(root, 'node_modules', dep);
        const target = path.join(nodeModules, dep);
        if (!fs.existsSync(source)) {
            failures.push(`dependency_missing:${dep}`);
            continue;
        }
        fs.mkdirSync(path.dirname(target), { recursive: true });
        try {
            fs.symlinkSync(source, target, process.platform === 'win32' ? 'junction' : 'dir');
            rows.push({ label: `link_dependency:${dep}`, ok: true, status: 0 });
        }
        catch (err) {
            failures.push(`dependency_link_failed:${dep}:${err.message}`);
            rows.push({ label: `link_dependency:${dep}`, ok: false, status: 1, stderr_tail: String(err.message).slice(-800) });
        }
    }
}
async function smokeCommands(pkgRoot) {
    const registryPath = path.join(pkgRoot, 'dist', 'cli', 'command-registry.js');
    if (!fs.existsSync(registryPath)) {
        failures.push('dist_cli_command_registry_missing');
        return;
    }
    const registry = await import(pathToFileURL(registryPath));
    const names = registry.commandNames();
    for (const name of names) {
        const entry = registry.COMMANDS[name];
        try {
            const mod = await entry.lazy();
            const runner = mod.run || mod.main || mod.default;
            if (typeof runner !== 'function')
                failures.push(`${name}:runner_missing`);
            rows.push({ label: `lazy_import:${name}`, ok: typeof runner === 'function', status: typeof runner === 'function' ? 0 : 1 });
        }
        catch (err) {
            failures.push(`${name}:${err.stack || err.message}`);
            rows.push({ label: `lazy_import:${name}`, ok: false, status: 1, stderr_tail: String(err.stack || err.message).slice(-800) });
        }
    }
}
//# sourceMappingURL=blackbox-command-import-smoke.js.map