"""Architecture Stability + Knowledge Velocity KPI — Phase Ω guardrail.

This is not a feature test; it is a LIVING GUARDRAIL. The day someone integrates a Requirement Source
that needs a new runtime class or a new pipeline (i.e. NOT data-only), `test_every_source_is_data_only`
fails — surfacing the exact moment the architecture stopped being general. That is the whole point of
Phase Ω: measure where it breaks under real domain knowledge.
"""

from __future__ import annotations

import os
import subprocess
import sys

import yaml


def _ledger():
    p = os.path.join(os.path.dirname(__file__), "..", "knowledge", "architecture_stability", "integration_ledger.yaml")
    return yaml.safe_load(open(p, encoding="utf-8"))


def _run():
    root = os.path.join(os.path.dirname(__file__), "..")
    r = subprocess.run(
        [sys.executable, "reference_scenarios/architecture_stability_kpi.py"],
        cwd=root, env={**os.environ, "PYTHONPATH": "."}, capture_output=True, text=True,
    )
    assert r.returncode == 0, r.stderr
    return r.stdout


def test_every_source_is_data_only_and_zero_runtime():
    # GUARDRAIL: every integrated source must cost 0 runtime classes, no new pipeline, data-only.
    # If a future domain breaks this, fix the architecture or record the break honestly — do not
    # weaken this assertion to make it pass.
    for s in _ledger()["sources"]:
        assert s["new_runtime_classes"] == 0, s["source"]
        assert s["new_pipeline"] is False, s["source"]
        assert s["integration_kind"] == "data_only", s["source"]


def test_kpis_reported_at_full_stability():
    out = _run()
    n = len(_ledger()["sources"])
    assert "Architecture Stability: %d/%d = 100%%" % (n, n) in out
    assert "Knowledge Velocity: %d/%d = 100%%" % (n, n) in out


def test_capability_types_column_and_non_cyber_generality():
    out = _run()
    # the third KPI column (capability-model granularity Frühindikator) is present and populated
    assert "neue Capability-Typen" in out
    assert "Capability-Modell-Frühindikator" in out
    # the first non-cyber domain is recorded and carried the pipeline 0/0
    assert "non_cyber" in out
    assert "Generalität über Cyber hinaus" in out
    # every ledger source carries a capability-type count
    for s in _ledger()["sources"]:
        assert s["new_capability_types"] >= 1, s["source"]


def test_pipeline_functions_are_one_time_infrastructure():
    out = _run()
    assert "EINMALIG (jetzt eingefroren)" in out
    assert "journey_matcher" in out and "letzte architektonische Baustein" in out


def test_three_knowledge_layers_present():
    out = _run()
    for layer in ["Beschreibend", "Transformation", "Produktion"]:
        assert layer in out


def test_three_success_questions_present():
    out = _run()
    assert "Musste für eine neue Domäne Runtime-Code geändert werden?" in out
    assert "Knowledge Velocity" in out
    assert "Architecture Stability" in out
