"""Automotive convergence stress test — the SAME capability from many sources (Phase Ω #2).

Pins the convergence property: a realistic multi-certified supplier (ISO 9001 + IATF 16949 + TISAX +
ASPICE + CSMS + SUMS) developing an ECU for OEM X feeds the SAME capability from many overlapping
Requirement Sources, and the model stays stable (0 runtime). Checks the three new measurements:
Capability Convergence (sources x distinct types), Existing-vs-New reuse, and Business Leverage.
"""

from __future__ import annotations

import os
import subprocess
import sys


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


def test_runs_end_to_end_multi_source():
    out = _run()
    assert "Automotive Convergence Stress Test" in out
    assert "27 distinct geforderte Capabilities" in out
    assert "0 neue Runtime-Klassen" in out


def test_capability_convergence_ranks_the_most_shared_cap_first():
    out = _run()
    # the most convergent capability is fed by 4 sources across 3 distinct source types
    assert "| `technical_vulnerability_management` | 4 | 3 |" in out
    assert "| `secure_signed_update_distribution` | 4 | 2 |" in out


def test_existing_vs_new_reuse_signal():
    out = _run()
    # 13 of 27 required caps reuse existing cyber/environmental MCAPs (registry converging)
    assert "Reuse aus Cyber/Umwelt): 13/27 = 48%" in out
    assert "Registry zu konvergieren" in out


def test_business_leverage_is_markets_plus_regulation():
    out = _run()
    assert "Business Leverage" in out
    assert "öffnet den **OEM-Markt**" in out


def test_recommends_registry_analysis_not_next_domain():
    out = _run()
    assert "innehalten und die Registry analysieren" in out
    assert "Plattformkern" in out


def test_no_real_company_names():
    out = _run().lower()
    for name in ["eto", "owis", "winterhalter", "bmw", "volkswagen", "bosch"]:
        assert name not in out
