#!/bin/bash
# test-ros-output.sh — Validate ros-analysis-agent output structure
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/lib/e2e-helpers.sh"

FIXTURE="$SCRIPT_DIR/fixtures/ros-analysis/fixture.md"

if [ ! -f "$FIXTURE" ]; then
  echo "ERROR: Fixture not found: $FIXTURE"
  echo "Run: bash tests/capture-fixture.sh to generate fixtures"
  exit 1
fi

init_suite "ROS Analysis Agent"

# Structure checks
assert_has_section "$FIXTURE" "ROS-analyse" "Has ROS analysis header"
assert_has_section "$FIXTURE" "Risikoregister" "Has risk register section"
assert_has_section "$FIXTURE" "Risikomatrise" "Has risk matrix section"
assert_has_section "$FIXTURE" "Tiltaksplan" "Has measures plan section"
assert_has_section "$FIXTURE" "Restrisiko" "Has residual risk section"
assert_has_section "$FIXTURE" "Dimensjonsvurdering" "Has dimension assessment section"

# Content quality
assert_min_lines "$FIXTURE" 60 "Minimum 60 lines"
assert_min_tables "$FIXTURE" 4 "Minimum 4 tables (register, matrix, dimensions, measures)"
assert_scores_in_range "$FIXTURE" "ROS scores in valid X/5 range"
assert_has_dimensions "$FIXTURE" 6 "At least 6 risk dimensions scored"

# Encoding
assert_encoding_ok "$FIXTURE" "UTF-8 encoding valid"
assert_no_ascii_approximation "$FIXTURE" "No ASCII approximation of Norwegian chars"

# Domain-specific: methodology references
assert_matches_pattern "$FIXTURE" "(NS 5814|ISO 31000)" "References NS 5814 or ISO 31000 methodology"

# Domain-specific: threat and risk IDs
assert_matches_pattern "$FIXTURE" "T-[A-Z]{3}-[0-9]{2}" "Contains threat IDs (T-xxx-NN format)"
assert_matches_pattern "$FIXTURE" "R-[0-9]" "Contains risk IDs (R-NN format)"

# Domain-specific: risk dimensions
assert_matches_pattern "$FIXTURE" "(Modellsikkerhet|Dataintegritet|Bias|Tilgjengelighet|Forklarbarhet|Juridisk|Organisatorisk)" "Covers ROS risk dimensions"

# Domain-specific: regulatory references
assert_matches_pattern "$FIXTURE" "(AI Act|GDPR|OWASP)" "References key regulations/standards"

# Structure: check all 8 phases (Full ROS)
assert_has_section "$FIXTURE" "Fase 1" "Has Phase 1 header"
assert_has_section "$FIXTURE" "Fase 2" "Has Phase 2 header"
assert_has_section "$FIXTURE" "Fase 3" "Has Phase 3 header"
assert_has_section "$FIXTURE" "Fase 4" "Has Phase 4 header"
assert_has_section "$FIXTURE" "Fase 5" "Has Phase 5 header"
assert_has_section "$FIXTURE" "Fase 6" "Has Phase 6 header"
assert_has_section "$FIXTURE" "Fase 7" "Has Phase 7 header"
assert_has_section "$FIXTURE" "Fase 8" "Has Phase 8 header"
assert_has_section "$FIXTURE" "Ledelsessammendrag" "Has executive summary"

# Measure IDs (M-xxx)
assert_matches_pattern "$FIXTURE" "M-[0-9]" "Contains measure IDs (M-NN format)"

# Minimum threat count for full ROS
threat_count=$(grep -cE "T-[A-Z]{3}-[0-9]{2}" "$FIXTURE" || echo 0)
if [ "$threat_count" -ge 8 ]; then
  pass "Minimum 8 threats identified ($threat_count found)"
else
  fail "Minimum 8 threats — only $threat_count found"
fi

# Vedlegg O coverage (for systems with agents/MCP)
assert_matches_pattern "$FIXTURE" "(MAESTRO|forsyningskjede|MCP|supply chain)" "References supply chain/MAESTRO (Vedlegg O coverage)"

print_summary
