"use strict";
/**
 * test_capability_pack_schema.ts — Story 1.1
 *
 * Regression test for CapabilityPackSchema + Parser.
 *
 * Asserts:
 *   1. A minimal valid pack parses.
 *   2. A complete pack with all sections parses and round-trips fields.
 *   3. Missing required fields surface stable error codes.
 *   4. Invalid status surfaces INVALID_STATUS.
 *   5. Bad YAML surfaces INVALID_YAML.
 *   6. Missing root key surfaces MISSING_ROOT.
 *   7. Duplicate asset ids inside the same kind surface DUPLICATE_ASSET_REF.
 *   8. Asset refs with invalid source surface INVALID_TYPE.
 */
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 });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const CapabilityPackParser_1 = require("./orchestrator/capability/CapabilityPackParser");
const CapabilityPackSchema_1 = require("./orchestrator/capability/CapabilityPackSchema");
function assertTrue(cond, label) {
    if (!cond)
        throw new Error(`ASSERT_FAILED[${label}]`);
}
function assertErrorCode(errors, code, label) {
    if (!errors.some((e) => e.code === code)) {
        throw new Error(`ASSERT_FAILED[${label}]: expected error code ${code}, got ${JSON.stringify(errors.map((e) => e.code))}`);
    }
}
// ─── Scenario 1: minimal valid pack ──────────────────────────────────
const minimal = `
capability:
  id: minimal-pack
  name: Minimal Pack
  description: smallest possible pack
  version: "1.0.0"
  status: draft
  objective: prove the parser accepts a minimal manifest
`;
const r1 = (0, CapabilityPackParser_1.parseCapabilityPack)(minimal);
assertTrue(r1.ok, 'scenario 1: minimal pack ok');
assertTrue(r1.pack?.id === 'minimal-pack', 'scenario 1: id round-trip');
assertTrue(r1.pack?.status === 'draft', 'scenario 1: status round-trip');
console.log('[1.1] scenario 1 (minimal) ✓');
// ─── Scenario 2: complete pack ───────────────────────────────────────
const complete = `
capability:
  id: deep-research
  name: Deep Research
  description: thorough research capability
  version: "1.0.0"
  status: draft
  objective: produce verifiable research reports with cited sources
  success_criteria:
    - "Every claim has a verifiable citation"
    - "At least 3 contradicting sources considered"
  agents:
    - { id: research-lead, source: embedded, label: "Research Lead" }
    - { id: writer, source: referenced, version: "1.0.0" }
  skills:
    - { id: source-credibility-scoring, source: embedded }
  squads:
    - { id: research-squad, source: embedded }
  workflows:
    - { id: deep-research-flow, source: embedded }
  checklists:
    - { id: quality-gate, path: checklists/quality-gate.md }
  templates:
    - { id: report, path: templates/report.md }
  policies:
    - { id: no-uncited-claims, rationale: "All claims must cite a source" }
  metadata:
    author: Genesis Engine
    tags: [research, analysis]
`;
const r2 = (0, CapabilityPackParser_1.parseCapabilityPack)(complete);
assertTrue(r2.ok, 'scenario 2: complete pack ok');
assertTrue((r2.pack?.agents?.length || 0) === 2, 'scenario 2: 2 agents');
assertTrue(r2.pack?.agents?.[0].source === 'embedded', 'scenario 2: first agent embedded');
assertTrue(r2.pack?.agents?.[1].source === 'referenced', 'scenario 2: second agent referenced');
assertTrue((r2.pack?.success_criteria?.length || 0) === 2, 'scenario 2: 2 success criteria');
assertTrue(r2.pack?.policies?.[0].id === 'no-uncited-claims', 'scenario 2: policy round-trip');
console.log('[1.1] scenario 2 (complete) ✓');
// ─── Scenario 3: missing required fields ────────────────────────────
const missingFields = `
capability:
  name: No ID
`;
const r3 = (0, CapabilityPackParser_1.parseCapabilityPack)(missingFields);
assertTrue(!r3.ok, 'scenario 3: missing fields fails');
assertErrorCode(r3.errors, CapabilityPackSchema_1.CAPABILITY_ERRORS.MISSING_REQUIRED_FIELD, 'scenario 3: error code');
console.log('[1.1] scenario 3 (missing fields) ✓');
// ─── Scenario 4: invalid status ─────────────────────────────────────
const invalidStatus = `
capability:
  id: bad-status
  name: Bad Status
  description: x
  version: "1.0.0"
  status: unstable
  objective: x
`;
const r4 = (0, CapabilityPackParser_1.parseCapabilityPack)(invalidStatus);
assertTrue(!r4.ok, 'scenario 4: invalid status fails');
assertErrorCode(r4.errors, CapabilityPackSchema_1.CAPABILITY_ERRORS.INVALID_STATUS, 'scenario 4: error code');
console.log('[1.1] scenario 4 (invalid status) ✓');
// ─── Scenario 5: bad YAML ────────────────────────────────────────────
const r5 = (0, CapabilityPackParser_1.parseCapabilityPack)(':\n  bad: [unclosed');
assertTrue(!r5.ok, 'scenario 5: bad yaml fails');
assertErrorCode(r5.errors, CapabilityPackSchema_1.CAPABILITY_ERRORS.INVALID_YAML, 'scenario 5: error code');
console.log('[1.1] scenario 5 (bad yaml) ✓');
// ─── Scenario 6: missing root key ────────────────────────────────────
const r6 = (0, CapabilityPackParser_1.parseCapabilityPack)('not_capability:\n  id: x');
assertTrue(!r6.ok, 'scenario 6: missing root fails');
assertErrorCode(r6.errors, CapabilityPackSchema_1.CAPABILITY_ERRORS.MISSING_ROOT, 'scenario 6: error code');
console.log('[1.1] scenario 6 (missing root) ✓');
// ─── Scenario 7: duplicate asset ids ─────────────────────────────────
const dupes = `
capability:
  id: dup-pack
  name: Dup
  description: x
  version: "1.0.0"
  status: draft
  objective: x
  skills:
    - { id: same-skill, source: embedded }
    - { id: same-skill, source: referenced }
`;
const r7 = (0, CapabilityPackParser_1.parseCapabilityPack)(dupes);
assertTrue(!r7.ok, 'scenario 7: duplicate refs fail');
assertErrorCode(r7.errors, CapabilityPackSchema_1.CAPABILITY_ERRORS.DUPLICATE_ASSET_REF, 'scenario 7: error code');
console.log('[1.1] scenario 7 (duplicate refs) ✓');
// ─── Scenario 8: invalid asset source ────────────────────────────────
const badSource = `
capability:
  id: bad-source
  name: Bad Source
  description: x
  version: "1.0.0"
  status: draft
  objective: x
  agents:
    - { id: a1, source: cloud }
`;
const r8 = (0, CapabilityPackParser_1.parseCapabilityPack)(badSource);
assertTrue(!r8.ok, 'scenario 8: bad source fails');
assertErrorCode(r8.errors, CapabilityPackSchema_1.CAPABILITY_ERRORS.INVALID_TYPE, 'scenario 8: error code');
console.log('[1.1] scenario 8 (invalid source) ✓');
// ─── Scenario 9: file-mode parse + embedded ref file check ───────────
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'capability-test-'));
try {
    const packDir = path.join(tmp, 'demo-pack');
    fs.mkdirSync(packDir, { recursive: true });
    fs.mkdirSync(path.join(packDir, 'skills'), { recursive: true });
    fs.writeFileSync(path.join(packDir, 'skills', 'real-skill.md'), '# Real Skill\n');
    const yamlPath = path.join(packDir, 'capability.yaml');
    fs.writeFileSync(yamlPath, `
capability:
  id: demo-pack
  name: Demo
  description: file-mode test
  version: "1.0.0"
  status: draft
  objective: prove file-mode parsing finds embedded files
  skills:
    - { id: real-skill, source: embedded }
    - { id: ghost-skill, source: embedded }
`);
    const r9 = (0, CapabilityPackParser_1.parseCapabilityPackFile)(yamlPath);
    // ghost-skill has no file → should error; real-skill is fine
    assertTrue(!r9.ok, 'scenario 9: ghost-skill missing file fails');
    assertErrorCode(r9.errors, CapabilityPackSchema_1.CAPABILITY_ERRORS.EMBEDDED_REF_MISSING_FILE, 'scenario 9: error code');
    console.log('[1.1] scenario 9 (file-mode + embedded check) ✓');
}
finally {
    fs.rmSync(tmp, { recursive: true, force: true });
}
console.log('TEST_CAPABILITY_PACK_SCHEMA_OK');
