"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
/*
 * Story 3.2 — dist-templates layout regression test.
 *
 * Asserts:
 * 1. dist-templates/claude-code/{agents,commands/openlife,mcp} all exist
 * 2. The 5 starter agents (MAESTRO/LYRA/FORGE/ATLAS/GENESIS) ship as `openlife-*.md`
 * 3. Each agent file parses: YAML frontmatter with required keys (name, description), and a non-empty body
 * 4. The 4 starter slash commands ship under commands/openlife/
 * 5. Each slash command has a `description` in its frontmatter
 * 6. The MCP manifest is valid JSON with a `name` field
 * 7. dist-templates/ is included in package.json `files` so npm publishes it
 */
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
function assert(cond, msg) {
    if (!cond)
        throw new Error(`ASSERTION_FAILED: ${msg}`);
}
const ROOT = path.resolve(__dirname, '..');
const DIST = path.join(ROOT, 'dist-templates');
const CC = path.join(DIST, 'claude-code');
function parseFrontmatter(content) {
    if (!content.startsWith('---\n')) {
        throw new Error('Missing frontmatter opening "---\\n"');
    }
    const rest = content.slice(4);
    const endIdx = rest.indexOf('\n---\n');
    if (endIdx === -1)
        throw new Error('Missing frontmatter closing "\\n---\\n"');
    const fmRaw = rest.slice(0, endIdx);
    const body = rest.slice(endIdx + 5).trim();
    const fm = {};
    for (const line of fmRaw.split('\n')) {
        const m = line.match(/^([a-zA-Z0-9_-]+):\s*(.*)$/);
        if (m)
            fm[m[1]] = m[2].trim();
    }
    return { fm, body };
}
function testDirectoryLayout() {
    assert(fs.existsSync(DIST), 'dist-templates/ must exist');
    assert(fs.existsSync(CC), 'dist-templates/claude-code/ must exist');
    assert(fs.existsSync(path.join(CC, 'agents')), 'agents/ must exist');
    assert(fs.existsSync(path.join(CC, 'commands', 'openlife')), 'commands/openlife/ must exist');
    assert(fs.existsSync(path.join(CC, 'mcp')), 'mcp/ must exist');
    assert(fs.existsSync(path.join(DIST, 'README.md')), 'dist-templates/README.md must exist');
}
function testStarterAgents() {
    const expected = ['openlife-maestro', 'openlife-lyra', 'openlife-forge', 'openlife-atlas', 'openlife-genesis'];
    for (const name of expected) {
        const file = path.join(CC, 'agents', `${name}.md`);
        assert(fs.existsSync(file), `${file} must exist`);
        const { fm, body } = parseFrontmatter(fs.readFileSync(file, 'utf-8'));
        assert(fm.name === name, `${name}.md frontmatter.name must equal ${name}, got "${fm.name}"`);
        assert(fm.description && fm.description.length > 30, `${name}.md description too short or missing`);
        assert(body.length > 200, `${name}.md body too short (${body.length} chars) — system prompts should be substantive`);
    }
}
function testStarterSlashCommands() {
    const expected = ['status', 'ask', 'doctor', 'dream'];
    for (const name of expected) {
        const file = path.join(CC, 'commands', 'openlife', `${name}.md`);
        assert(fs.existsSync(file), `${file} must exist`);
        const { fm, body } = parseFrontmatter(fs.readFileSync(file, 'utf-8'));
        assert(fm.description && fm.description.length > 10, `${name}.md needs a description`);
        assert(body.length > 50, `${name}.md body too short`);
    }
}
function testMcpManifest() {
    const file = path.join(CC, 'mcp', 'openlife-orchestrator.json');
    assert(fs.existsSync(file), `${file} must exist`);
    const content = fs.readFileSync(file, 'utf-8');
    let parsed;
    try {
        parsed = JSON.parse(content);
    }
    catch (err) {
        throw new Error(`MCP manifest is not valid JSON: ${err.message}`);
    }
    assert(parsed.name === 'openlife-orchestrator', 'MCP manifest.name must be openlife-orchestrator');
    assert(Array.isArray(parsed.tools) && parsed.tools.length > 0, 'MCP manifest.tools must be a non-empty array');
    assert(typeof parsed.command === 'string' && parsed.command.length > 0, 'MCP manifest.command required');
}
function testPackageJsonIncludesTemplates() {
    const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf-8'));
    assert(Array.isArray(pkg.files), 'package.json.files must be an array');
    assert(pkg.files.includes('dist-templates'), `package.json.files must include "dist-templates" so npm distributes it; got: ${JSON.stringify(pkg.files)}`);
}
async function main() {
    testDirectoryLayout();
    testStarterAgents();
    testStarterSlashCommands();
    testMcpManifest();
    testPackageJsonIncludesTemplates();
    console.log('TEST_DIST_TEMPLATES_LAYOUT_OK');
}
main().catch((err) => {
    console.error('TEST_DIST_TEMPLATES_LAYOUT_FAIL:', err?.message || err);
    process.exit(1);
});
