/**
 * Shared helper for C28 test suites. Spins up a throwaway project dir
 * with an initialized `.aperant/` skeleton so `apt-tools` CLI calls work
 * without touching the real project.
 */
import { execSync, spawnSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
// __dirname = packages/framework/src/__test-helpers/
// Go up two levels to packages/framework/, then into bin/.
export const APT_TOOLS = resolve(__dirname, '..', '..', 'bin', 'apt-tools.mjs');
export function createTmpProject(opts = {}) {
    const dir = mkdtempSync(join(tmpdir(), 'apt-c28-'));
    if (opts.gitInit !== false) {
        spawnSync('git', ['init', '-q', '-b', 'main'], { cwd: dir });
        spawnSync('git', ['config', 'user.email', 'test@example.com'], { cwd: dir });
        spawnSync('git', ['config', 'user.name', 'test'], { cwd: dir });
        spawnSync('git', ['config', 'commit.gpgsign', 'false'], { cwd: dir });
        writeFileSync(join(dir, 'README.md'), '# test\n');
        spawnSync('git', ['add', 'README.md'], { cwd: dir });
        spawnSync('git', ['commit', '-q', '-m', 'init'], { cwd: dir });
    }
    if (opts.initAperant !== false) {
        mkdirSync(join(dir, '.aperant'), { recursive: true });
        mkdirSync(join(dir, '.aperant', 'tasks'), { recursive: true });
        mkdirSync(join(dir, '.aperant', 'events'), { recursive: true });
        mkdirSync(join(dir, '.aperant', 'locks'), { recursive: true });
        mkdirSync(join(dir, '.aperant', 'roadmap'), { recursive: true });
        const now = new Date().toISOString();
        writeFileSync(join(dir, '.aperant', 'state.json'), `${JSON.stringify({
            version: 1,
            initialized_at: now,
            last_activity: now,
            active_tasks: {},
            completed_tasks: [],
            decisions: {},
            has_tests: false,
        }, null, 2)}\n`);
        // C34: active.json is no longer seeded — per-agent team-status/ is
        // created lazily by the coordination store.
        writeFileSync(join(dir, '.aperant', 'config.json'), `${JSON.stringify({
            version: 3,
            autonomy_level: 2,
            project_type: 'monorepo',
        }, null, 2)}\n`);
        writeFileSync(join(dir, '.aperant', 'tasks', 'INDEX.md'), '# Task Index\n\n');
        mkdirSync(join(dir, '.aperant', 'roadmap', 'project'), { recursive: true });
        writeFileSync(join(dir, '.aperant', 'roadmap', 'project', 'milestones.json'), `${JSON.stringify({ version: 1, project: 'test', scope: 'project', milestones: [] }, null, 2)}\n`);
        // Team mode enabled by default so team-status + INDEX propagation run.
        const teamEnabled = opts.teamMode !== false;
        writeFileSync(join(dir, '.aperant', 'team.json'), `${JSON.stringify({ version: 1, enabled: teamEnabled, members: {} }, null, 2)}\n`);
    }
    return {
        dir,
        cleanup: () => {
            try {
                rmSync(dir, { recursive: true, force: true });
            }
            catch {
                /* best-effort */
            }
        },
    };
}
export function runCli(args, cwd) {
    const result = spawnSync(process.execPath, [APT_TOOLS, ...args], {
        cwd: cwd ?? process.cwd(),
        encoding: 'utf-8',
        env: process.env,
        stdio: ['ignore', 'pipe', 'pipe'],
    });
    const stdout = result.stdout ?? '';
    const stderr = result.stderr ?? '';
    let json = null;
    try {
        json = JSON.parse(stdout);
    }
    catch {
        // Non-JSON output — leave as null.
    }
    return {
        status: result.status ?? -1,
        stdout,
        stderr,
        json,
    };
}
/** Seed a milestone with one phase into .aperant/roadmap/{scope}/milestones.json. */
export function seedMilestone(projectDir, milestoneId, phaseId, scope = 'project') {
    const scopeDir = join(projectDir, '.aperant', 'roadmap', scope);
    mkdirSync(scopeDir, { recursive: true });
    writeFileSync(join(scopeDir, 'milestones.json'), `${JSON.stringify({
        version: 1,
        project: 'test',
        scope,
        milestones: [
            {
                id: milestoneId,
                title: milestoneId,
                goal: 'test',
                scope,
                phases: [
                    {
                        id: phaseId,
                        title: phaseId,
                        description: 'test',
                        status: 'planned',
                        owner: 'test',
                        scope,
                    },
                ],
            },
        ],
    }, null, 2)}\n`);
}
/** Read state.json. */
export function readState(projectDir) {
    const raw = execSync(`cat ${join(projectDir, '.aperant', 'state.json')}`, {
        encoding: 'utf-8',
    });
    return JSON.parse(raw);
}
//# sourceMappingURL=tmp-project.js.map