"use strict";
/**
 * test_workflow_parser — Story 4.1 regression suite.
 *
 * Validates the WorkflowSchema + WorkflowParser contract via 8 scenarios:
 *   1. Happy path — minimal valid workflow round-trips
 *   2. Multi-phase fixture — greenfield-like document parses
 *   3. Missing root `workflow:` key fails with MISSING_ROOT
 *   4. Invalid type fails with INVALID_TYPE
 *   5. Step missing both agent and squad fails with NEITHER_AGENT_NOR_SQUAD
 *   6. Duplicate step id fails with DUPLICATE_STEP_ID
 *   7. Unknown requires (step-id-shaped) fails with UNKNOWN_REQUIRES
 *   8. Circular requires chain detected with CIRCULAR_REQUIRES
 *
 * Prints TEST_WORKFLOW_PARSER_OK on full pass.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const WorkflowParser_1 = require("./orchestrator/workflow/WorkflowParser");
const WorkflowSchema_1 = require("./orchestrator/workflow/WorkflowSchema");
function assert(cond, msg) {
    if (!cond)
        throw new Error(`assertion failed: ${msg}`);
}
function assertHasError(result, expectedCode, scenario) {
    assert(!result.ok, `${scenario}: expected ok=false, got ok=true`);
    const match = result.errors.find((e) => e.code === expectedCode);
    assert(match !== undefined, `${scenario}: expected error code '${expectedCode}', got [${result.errors.map((e) => e.code).join(', ')}]`);
}
// ─────────────────────────────────────────────────────────────
// Scenario 1 — Happy path
// ─────────────────────────────────────────────────────────────
function scenarioHappyPath() {
    const yaml = `
workflow:
  id: minimal-test
  name: Minimal Test
  description: A minimal valid workflow
  type: custom
  version: "1.0"
  sequence:
    - id: step-1
      agent: dev
      action: do-thing
      creates: [out.md]
`;
    const r = (0, WorkflowParser_1.parseWorkflow)(yaml);
    assert(r.ok, `happy path must succeed, errors: ${JSON.stringify(r.errors)}`);
    assert(r.workflow?.id === 'minimal-test', 'id round-trip');
    assert(r.workflow?.type === 'custom', 'type round-trip');
    assert(r.workflow?.sequence.length === 1, 'sequence length');
    console.log('✅ scenario 1: happy path');
}
// ─────────────────────────────────────────────────────────────
// Scenario 2 — Multi-phase fixture (squad+agent mix)
// ─────────────────────────────────────────────────────────────
function scenarioMultiPhaseShape() {
    const yaml = `
workflow:
  id: greenfield-like
  name: Greenfield-Like
  description: Mirrors the greenfield-fullstack structure
  type: greenfield
  version: "1.0"
  project_types: [web-app, saas]
  metadata:
    elicit: true
    confirmation_required: true
  phases:
    - phase_0: Bootstrap
    - phase_1: Discovery
  sequence:
    - phase: 0
      name: Bootstrap
      description: Setup
    - id: env-bootstrap
      agent: devops
      action: environment_bootstrap
      duration_estimate: "15-30 min"
      creates: [.openlife/config.yaml, README.md]
      task: environment-bootstrap.md

    - phase: 1
      name: Discovery
      description: Plan everything

    - id: project-brief
      agent: analyst
      creates: project-brief.md
      duration_estimate: "30-60 min"
      requires: env-bootstrap

    - id: design-review
      squad: design-squad
      action: review-design
      requires: project-brief
      idempotent: true
`;
    const r = (0, WorkflowParser_1.parseWorkflow)(yaml);
    assert(r.ok, `multi-phase shape must succeed, errors: ${JSON.stringify(r.errors)}`);
    assert(r.workflow?.sequence.length === 5, `expected 5 sequence entries, got ${r.workflow?.sequence.length}`);
    assert(r.workflow?.project_types?.includes('saas'), 'project_types round-trip');
    assert(r.workflow?.metadata?.elicit === true, 'metadata.elicit round-trip');
    console.log('✅ scenario 2: multi-phase (squad+agent mix)');
}
// ─────────────────────────────────────────────────────────────
// Scenario 3 — Missing `workflow:` root
// ─────────────────────────────────────────────────────────────
function scenarioMissingRoot() {
    const yaml = `
id: orphan
name: Orphan
type: custom
version: "1.0"
sequence: []
`;
    const r = (0, WorkflowParser_1.parseWorkflow)(yaml);
    assertHasError(r, WorkflowSchema_1.WORKFLOW_ERRORS.MISSING_ROOT, 'missing root');
    console.log('✅ scenario 3: missing root (no `workflow:` key)');
}
// ─────────────────────────────────────────────────────────────
// Scenario 4 — Invalid type value
// ─────────────────────────────────────────────────────────────
function scenarioInvalidType() {
    const yaml = `
workflow:
  id: bad-type
  name: Bad Type
  type: nonsense
  version: "1.0"
  sequence:
    - id: step-1
      agent: dev
`;
    const r = (0, WorkflowParser_1.parseWorkflow)(yaml);
    assertHasError(r, WorkflowSchema_1.WORKFLOW_ERRORS.INVALID_TYPE, 'invalid type');
    console.log('✅ scenario 4: invalid type value');
}
// ─────────────────────────────────────────────────────────────
// Scenario 5 — Step with neither agent nor squad
// ─────────────────────────────────────────────────────────────
function scenarioNoAgentOrSquad() {
    const yaml = `
workflow:
  id: no-actor
  name: No Actor
  type: custom
  version: "1.0"
  sequence:
    - id: orphan-step
      action: do-thing
`;
    const r = (0, WorkflowParser_1.parseWorkflow)(yaml);
    assertHasError(r, WorkflowSchema_1.WORKFLOW_ERRORS.NEITHER_AGENT_NOR_SQUAD, 'no agent or squad');
    console.log('✅ scenario 5: step without agent or squad');
}
// ─────────────────────────────────────────────────────────────
// Scenario 6 — Duplicate step id
// ─────────────────────────────────────────────────────────────
function scenarioDuplicateStepId() {
    const yaml = `
workflow:
  id: dup-test
  name: Dup Test
  type: custom
  version: "1.0"
  sequence:
    - id: step-a
      agent: dev
    - id: step-a
      agent: qa
`;
    const r = (0, WorkflowParser_1.parseWorkflow)(yaml);
    assertHasError(r, WorkflowSchema_1.WORKFLOW_ERRORS.DUPLICATE_STEP_ID, 'duplicate step id');
    console.log('✅ scenario 6: duplicate step id');
}
// ─────────────────────────────────────────────────────────────
// Scenario 7 — Unknown requires (step-id-shaped)
// ─────────────────────────────────────────────────────────────
function scenarioUnknownRequires() {
    const yaml = `
workflow:
  id: req-test
  name: Req Test
  type: custom
  version: "1.0"
  sequence:
    - id: step-a
      agent: dev
      requires: ghost-step
`;
    const r = (0, WorkflowParser_1.parseWorkflow)(yaml);
    assertHasError(r, WorkflowSchema_1.WORKFLOW_ERRORS.UNKNOWN_REQUIRES, 'unknown requires');
    console.log('✅ scenario 7: unknown requires (step-id-shaped)');
}
// ─────────────────────────────────────────────────────────────
// Scenario 8 — Circular requires chain
// ─────────────────────────────────────────────────────────────
function scenarioCircularRequires() {
    const yaml = `
workflow:
  id: cycle-test
  name: Cycle Test
  type: custom
  version: "1.0"
  sequence:
    - id: step-a
      agent: dev
      requires: step-b
    - id: step-b
      agent: dev
      requires: step-c
    - id: step-c
      agent: dev
      requires: step-a
`;
    const r = (0, WorkflowParser_1.parseWorkflow)(yaml);
    assertHasError(r, WorkflowSchema_1.WORKFLOW_ERRORS.CIRCULAR_REQUIRES, 'circular requires');
    console.log('✅ scenario 8: circular requires chain');
}
// ─────────────────────────────────────────────────────────────
// Driver
// ─────────────────────────────────────────────────────────────
function main() {
    console.log('🧪 test_workflow_parser — Story 4.1 regression suite');
    scenarioHappyPath();
    scenarioMultiPhaseShape();
    scenarioMissingRoot();
    scenarioInvalidType();
    scenarioNoAgentOrSquad();
    scenarioDuplicateStepId();
    scenarioUnknownRequires();
    scenarioCircularRequires();
    // Smoke-check the file loader exists and reports missing files cleanly
    const r = (0, WorkflowParser_1.parseWorkflowFile)('/nonexistent/path/to/workflow.yaml');
    assert(!r.ok, 'parseWorkflowFile must return ok=false for missing path');
    console.log('');
    console.log('TEST_WORKFLOW_PARSER_OK');
}
try {
    main();
}
catch (err) {
    console.error('❌ test_workflow_parser FAILED:', err instanceof Error ? err.message : err);
    process.exit(1);
}
