"use strict";
/**
 * test_workflow_schema_backward_compat.ts — Stories 2.1 + 2.2
 *
 * The Workflow schema gained 15 new optional fields in v1.3. This test
 * proves the v1.2 fixtures (the 4 ported workflows shipped in
 * dist-templates/workflows/) still parse cleanly with the v2 parser.
 *
 * Also asserts the new v1.3 error codes fire on invalid values for
 * mode / status / triggers / tools / success_criteria.
 */
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 path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const WorkflowParser_1 = require("./orchestrator/workflow/WorkflowParser");
const WorkflowSchema_1 = require("./orchestrator/workflow/WorkflowSchema");
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 ${code}, got ${JSON.stringify(errors.map((e) => e.code))}`);
    }
}
// ─── v1.2 backward-compat: all 4 ported workflows must still parse ───
const wfDir = path.join(process.cwd(), 'dist-templates', 'workflows');
const yamls = fs.readdirSync(wfDir).filter((f) => f.endsWith('.yaml'));
assertTrue(yamls.length >= 4, `at least 4 fixtures present (found ${yamls.length})`);
for (const f of yamls) {
    const r = (0, WorkflowParser_1.parseWorkflowFile)(path.join(wfDir, f));
    assertTrue(r.ok, `v1.2 fixture parses: ${f}`);
}
console.log(`[2.1/2.2] backward-compat: ${yamls.length}/${yamls.length} v1.2 fixtures parse cleanly ✓`);
// ─── v1.3 minimum field set still works ──────────────────────────────
const minimalV2 = `
workflow:
  id: v2-minimal
  name: v2 Minimal
  type: custom
  version: "1.0"
  sequence:
    - id: only-step
      agent: dev
      action: do-the-thing
`;
const m = (0, WorkflowParser_1.parseWorkflow)(minimalV2);
assertTrue(m.ok, 'minimal v2 parses');
assertTrue(m.workflow?.mode === undefined, 'mode defaults to undefined (engine reads as task)');
console.log('[2.1] minimal v2 parses ✓');
// ─── v1.3 full field set parses ──────────────────────────────────────
const fullV2 = `
workflow:
  id: v2-full
  name: Full v2
  type: custom
  version: "1.0"
  status: tested
  mode: service
  triggers: [manual, cron]
  objective: "demonstrate every v1.3 field round-trip"
  success_criteria:
    - { id: complete-no-error, description: "all steps reach completion" }
  inputs:
    - { id: brief, type: string, required: true, description: "the brief" }
  outputs:
    - { id: report, type: string, description: "final report path" }
  required_context: ["docs/research-policy.md"]
  squads: ["design-squad"]
  skills: ["analysis"]
  tools:
    allowed: [file, web]
    forbidden: [terminal]
  policies: ["no-uncited-claims"]
  validation:
    - { id: report-exists, description: "report must exist", kind: artifact_exists }
  evidence: ["report.md"]
  failure_handling:
    on_step_failure: retry
    max_retries: 2
  audit_events: ["workflow.completed"]
  learning_rules:
    - { id: store-success, trigger: on_success, description: "remember this pattern" }
  sequence:
    - id: step-1
      agent: dev
      action: build
`;
const f2 = (0, WorkflowParser_1.parseWorkflow)(fullV2);
assertTrue(f2.ok, `full v2 parses (errors=${JSON.stringify(f2.errors)})`);
assertTrue(f2.workflow?.mode === 'service', 'mode round-trip');
assertTrue(f2.workflow?.status === 'tested', 'status round-trip');
assertTrue(f2.workflow?.triggers?.length === 2, 'triggers round-trip');
assertTrue(f2.workflow?.tools?.forbidden?.[0] === 'terminal', 'tools.forbidden round-trip');
assertTrue(f2.workflow?.success_criteria?.[0].id === 'complete-no-error', 'success_criteria round-trip');
console.log('[2.1] full v2 round-trip ✓');
// ─── v1.3 error codes ────────────────────────────────────────────────
const badMode = `
workflow:
  id: bad
  name: Bad
  type: custom
  version: "1.0"
  mode: forever
  sequence:
    - id: x
      agent: y
      action: z
`;
const bm = (0, WorkflowParser_1.parseWorkflow)(badMode);
assertTrue(!bm.ok, 'invalid mode fails');
assertErrorCode(bm.errors, WorkflowSchema_1.WORKFLOW_ERRORS.INVALID_MODE, 'INVALID_MODE');
console.log('[2.2] INVALID_MODE ✓');
const badTrig = `
workflow:
  id: bad
  name: Bad
  type: custom
  version: "1.0"
  triggers: [supernova]
  sequence:
    - id: x
      agent: y
      action: z
`;
const bt = (0, WorkflowParser_1.parseWorkflow)(badTrig);
assertTrue(!bt.ok, 'invalid trigger fails');
assertErrorCode(bt.errors, WorkflowSchema_1.WORKFLOW_ERRORS.INVALID_TRIGGERS, 'INVALID_TRIGGERS');
console.log('[2.2] INVALID_TRIGGERS ✓');
const badTools = `
workflow:
  id: bad
  name: Bad
  type: custom
  version: "1.0"
  tools:
    forbidden: "everything"
  sequence:
    - id: x
      agent: y
      action: z
`;
const btools = (0, WorkflowParser_1.parseWorkflow)(badTools);
assertTrue(!btools.ok, 'invalid tools.forbidden fails');
assertErrorCode(btools.errors, WorkflowSchema_1.WORKFLOW_ERRORS.INVALID_TOOLS_POLICY, 'INVALID_TOOLS_POLICY');
console.log('[2.2] INVALID_TOOLS_POLICY ✓');
const badCrit = `
workflow:
  id: bad
  name: Bad
  type: custom
  version: "1.0"
  success_criteria:
    - "just a string, not an object"
  sequence:
    - id: x
      agent: y
      action: z
`;
const bc = (0, WorkflowParser_1.parseWorkflow)(badCrit);
assertTrue(!bc.ok, 'invalid success_criteria fails');
assertErrorCode(bc.errors, WorkflowSchema_1.WORKFLOW_ERRORS.INVALID_SUCCESS_CRITERIA, 'INVALID_SUCCESS_CRITERIA');
console.log('[2.2] INVALID_SUCCESS_CRITERIA ✓');
console.log('TEST_WORKFLOW_SCHEMA_BACKWARD_COMPAT_OK');
