Skip to content

armgit / ARES

Public Imported from GitHub
0

ARES - authorized red-team engagement automation with dashboard, campaign scope, module orchestration, OPSEC controls, encrypted vault, and reporting.

61.8 KB
Raw Blame
"""
Round 3 tests - covers the 5 priority features:
  1. CLI end-to-end (campaign CRUD, pause/resume, target import)
  2. Reporting (ReportGenerator JSON/HTML/MD, context builder)
  3. Attack Chain (DependencyResolver, CapabilityResolver, ChainAdvisor)
  4. Dashboard (all API routes, WebSocket, HTML render)
  5. Goal Planning (GoalEngine.plan, AttackPlanner, AdaptiveStrategy)
"""
from __future__ import annotations

import asyncio
import json
import tempfile
from contextlib import contextmanager
from pathlib import Path
from types import SimpleNamespace
from typing import Any
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

# ─────────────────────────────────────────────────────────────
# HELPERS
# ─────────────────────────────────────────────────────────────

def _make_campaign(name="Test Campaign", client="Acme", n_findings=3):
    from ares.core.campaign import Campaign, Finding, NoiseProfile, ScopeEntry, Severity

    campaign = Campaign(
        name=name, client=client, operator="tester",
        scope=[ScopeEntry(cidr="10.0.0.0/8")],
        noise_profile=NoiseProfile.NORMAL,
        domain="CORP.LOCAL",
    )
    severities = [Severity.CRITICAL, Severity.HIGH, Severity.MEDIUM, Severity.LOW, Severity.INFO]
    for i in range(n_findings):
        campaign.add_finding(Finding(
            title           = f"Test Finding {i+1}",
            description     = f"Description for finding {i+1}",
            severity        = severities[i % len(severities)],
            confidence      = 0.9,
            module_id       = f"ad.module_{i}",
            host            = f"10.0.0.{i+1}",
            evidence        = "Evidence data",
            remediation     = "Fix this ASAP",
            mitre_technique = "T1558.003",
            mitre_tactic    = "Credential Access",
        ))
    return campaign


def _make_mock_registry(module_ids=None):
    """Return a mock ModuleRegistry with the given module IDs."""
    from ares.core.plugin.loader import ModuleRegistry

    registry = MagicMock(spec=ModuleRegistry)
    module_ids = module_ids or ["ad.enum_users", "ad.kerberoast", "ad.dcsync"]

    class FakeMod:
        MODULE_ID          = "ad.enum_users"
        MODULE_NAME        = "AD User Enum"
        MODULE_CATEGORY    = "ad"
        REQUIRES           = []
        OUTPUTS            = ["user_list", "spn_list"]
        MITRE_TECHNIQUES   = ["T1087.002"]
        OPSEC_LEVEL        = MagicMock(value="low")

    classes = {}
    for mid in module_ids:
        m = type(f"Mod_{mid.replace('.','_')}", (), {
            "MODULE_ID":        mid,
            "MODULE_NAME":      mid,
            "MODULE_CATEGORY":  mid.split(".")[0],
            "REQUIRES":         [],
            "OUTPUTS":          ["user_list"],
            "MITRE_TECHNIQUES": ["T1087.002"],
            "OPSEC_LEVEL":      MagicMock(value="low"),
        })
        classes[mid] = m

    registry.get.side_effect     = lambda mid: classes.get(mid)
    registry.all.return_value    = list(classes.values())
    registry.__contains__        = lambda self, mid: mid in classes
    return registry


# ─────────────────────────────────────────────────────────────
# TARGET 2: REPORTING
# ─────────────────────────────────────────────────────────────

class TestReportGenerator:

    def test_build_report_context_empty_campaign(self):
        from ares.modules.reporting.report_gen import build_report_context
        campaign = _make_campaign(n_findings=0)
        ctx = build_report_context(campaign)
        assert "findings" in ctx
        assert "mitre_map" in ctx
        assert "exec_summary" in ctx
        assert "generated_at" in ctx
        assert ctx["total_findings"] == 0

    def test_build_report_context_with_findings(self):
        from ares.modules.reporting.report_gen import build_report_context
        campaign = _make_campaign(n_findings=4)
        ctx = build_report_context(campaign)
        assert ctx["total_findings"] == 4
        assert isinstance(ctx["findings"], list)
        assert isinstance(ctx["timeline"], list)
        assert isinstance(ctx["mitre_map"], dict)

    def test_build_report_context_mitre_map_structure(self):
        from ares.modules.reporting.report_gen import build_report_context
        campaign = _make_campaign(n_findings=2)
        ctx = build_report_context(campaign)
        # MITRE map should be {tactic: [{technique, count, ...}]}
        for tactic, items in ctx["mitre_map"].items():
            assert isinstance(items, list)

    def test_build_report_context_exec_summary_contains_name(self):
        from ares.modules.reporting.report_gen import build_report_context
        campaign = _make_campaign(name="Q1 Engagement")
        ctx = build_report_context(campaign)
        assert "Q1 Engagement" in ctx["exec_summary"] or isinstance(ctx["exec_summary"], str)

    def test_generate_json_report(self):
        from ares.modules.reporting.report_gen import ReportGenerator
        campaign = _make_campaign(n_findings=3)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="json")
            assert path.exists()
            data = json.loads(path.read_text())
            assert "findings" in data
            assert "campaign" in data
            assert "summary" in data
            assert len(data["findings"]) == 3

    def test_generate_json_report_finding_schema(self):
        from ares.modules.reporting.report_gen import ReportGenerator
        campaign = _make_campaign(n_findings=2)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="json")
            data = json.loads(path.read_text())
            f = data["findings"][0]
            for key in ("id", "title", "severity", "confidence", "mitre_technique"):
                assert key in f, f"Missing key: {key}"
            assert f["confidence"] == campaign.findings[0].confidence

    def test_generate_html_report(self):
        from ares.modules.reporting.report_gen import ReportGenerator
        campaign = _make_campaign(n_findings=2)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="html")
            assert path.exists()
            html = path.read_text()
            assert "<!DOCTYPE html" in html or "<html" in html
            assert campaign.name in html or campaign.client in html

    def test_generate_html_report_uses_readable_evidence_table(self):
        from ares.modules.reporting.report_gen import ReportGenerator

        campaign = _make_campaign(n_findings=1)
        campaign.findings[0].evidence = {
            "host": "10.0.0.1",
            "port": 8888,
            "tech": "Apache",
            "version": "2.4.49",
            "compliance_mapping": {"PCI-DSS": ["8.2.3", "8.3.6"]},
        }
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="html")
            html = path.read_text()
            assert 'class="evidence-table"' in html
            assert "<th>Host</th><td>10.0.0.1</td>" in html
            assert "<th>Compliance Mapping</th><td>PCI-DSS:" in html
            assert '"host": "10.0.0.1"' not in html
            assert '"PCI-DSS":' not in html

    def test_report_footer_reserves_print_safe_space(self):
        from ares.modules.reporting.report_gen import ReportGenerator

        campaign = _make_campaign(n_findings=0)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="html")
            html = path.read_text()
            assert "padding-bottom: 18mm" in html
            assert "width: 54px" in html

    def test_generate_markdown_report(self):
        from ares.modules.reporting.report_gen import ReportGenerator
        campaign = _make_campaign(n_findings=2)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="md")
            assert path.exists()
            md = path.read_text()
            assert "# ARES Report" in md
            assert "## Executive Summary" in md
            assert "## Findings" in md

    def test_generate_markdown_contains_all_findings(self):
        from ares.modules.reporting.report_gen import ReportGenerator
        campaign = _make_campaign(n_findings=3)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="md")
            md = path.read_text()
            for i in range(1, 4):
                assert f"Test Finding {i}" in md

    def test_generate_all_formats(self, monkeypatch):
        from ares.modules.reporting.report_gen import ReportGenerator

        def fake_pdf(self, campaign, graph_json):
            path = self._out(campaign, "pdf")
            path.write_bytes(b"%PDF-1.4\n% unit fake\n")
            return path

        monkeypatch.setattr(ReportGenerator, "_gen_pdf", fake_pdf)
        campaign = _make_campaign(n_findings=1)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            paths = gen.generate_all(campaign)
            # JSON, HTML, MD should all succeed (PDF requires weasyprint)
            for fmt in ("json", "html", "md"):
                assert fmt in paths
                assert paths[fmt].exists()

    def test_generate_pdf_requires_pdf_backend(self, monkeypatch):
        import builtins

        from ares.modules.reporting.report_gen import (
            ReportDependencyError,
            ReportGenerator,
        )

        real_import = builtins.__import__

        def fail_weasyprint_import(name, *args, **kwargs):
            if name == "weasyprint":
                raise ImportError("missing test pdf backend")
            return real_import(name, *args, **kwargs)

        monkeypatch.setattr(builtins, "__import__", fail_weasyprint_import)
        monkeypatch.setattr(
            ReportGenerator,
            "_write_pdf_with_browser",
            lambda self, html, pdf_path: False,
        )
        campaign = _make_campaign(n_findings=1)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            with pytest.raises(ReportDependencyError, match="PDF generation requires"):
                gen.generate(campaign, fmt="pdf")
            assert not list(Path(tmpdir).glob("*.pdf"))
            assert not list(Path(tmpdir).glob("*.html"))

    def test_generate_pdf_uses_browser_fallback(self, monkeypatch):
        import builtins

        from ares.modules.reporting.report_gen import ReportGenerator

        real_import = builtins.__import__

        def fail_weasyprint_import(name, *args, **kwargs):
            if name == "weasyprint":
                raise OSError("missing native test pdf backend")
            return real_import(name, *args, **kwargs)

        def fake_browser_pdf(self, html, pdf_path):
            assert "<!DOCTYPE html>" in html
            pdf_path.write_bytes(b"%PDF-1.4\n% test pdf\n")
            return True

        monkeypatch.setattr(builtins, "__import__", fail_weasyprint_import)
        monkeypatch.setattr(ReportGenerator, "_write_pdf_with_browser", fake_browser_pdf)
        campaign = _make_campaign(n_findings=1)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="pdf")
            assert path.suffix == ".pdf"
            assert path.exists()
            assert not list(Path(tmpdir).glob("*.html"))

    def test_generate_unknown_format_raises(self):
        from ares.modules.reporting.report_gen import ReportGenerator
        campaign = _make_campaign()
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            with pytest.raises(ValueError, match="Unknown format"):
                gen.generate(campaign, fmt="docx")

    def test_report_sla_values(self):
        from ares.modules.reporting.report_gen import REMEDIATION_SLA
        assert REMEDIATION_SLA["critical"] <= 7
        assert REMEDIATION_SLA["high"] <= 30
        assert REMEDIATION_SLA["critical"] < REMEDIATION_SLA["high"]

    def test_report_json_severity_ordering(self):
        """Findings should be sorted by severity (critical first)."""
        from ares.modules.reporting.report_gen import ReportGenerator
        campaign = _make_campaign(n_findings=5)
        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="json")
            data = json.loads(path.read_text())
            sevs = [f["severity"] for f in data["findings"]]
            sev_order = {"critical": 0, "high": 1, "medium": 2, "low": 3, "info": 4}
            # First finding should be most severe
            for i in range(len(sevs) - 1):
                assert sev_order.get(sevs[i], 99) <= sev_order.get(sevs[i+1], 99)

    def test_report_output_dir_created(self):
        from ares.modules.reporting.report_gen import ReportGenerator
        with tempfile.TemporaryDirectory() as tmpdir:
            new_dir = Path(tmpdir) / "reports" / "nested"
            campaign = _make_campaign()  # noqa: F841
            gen = ReportGenerator(output_dir=str(new_dir))  # noqa: F841
            assert new_dir.exists()

    def test_report_mitre_tactic_order(self):
        from ares.modules.reporting.report_gen import MITRE_TACTIC_ORDER
        assert "Reconnaissance" in MITRE_TACTIC_ORDER
        assert "Credential Access" in MITRE_TACTIC_ORDER
        assert "Lateral Movement" in MITRE_TACTIC_ORDER
        assert MITRE_TACTIC_ORDER.index("Reconnaissance") < MITRE_TACTIC_ORDER.index("Lateral Movement")


# ─────────────────────────────────────────────────────────────
# TARGET 3: ATTACK CHAIN
# ─────────────────────────────────────────────────────────────

class TestDependencyResolver:

    def _resolver(self):
        from ares.core.chain.chain import DependencyResolver
        return DependencyResolver()

    def test_single_node_no_deps(self):
        from ares.core.chain.chain import ChainNode, DependencyResolver
        r = DependencyResolver()
        stages = r.resolve([ChainNode("a")])
        assert stages == [["a"]]

    def test_linear_chain(self):
        from ares.core.chain.chain import ChainNode, DependencyResolver
        r = DependencyResolver()
        nodes = [
            ChainNode("a"),
            ChainNode("b", depends_on=["a"]),
            ChainNode("c", depends_on=["b"]),
        ]
        stages = r.resolve(nodes)
        assert stages[0] == ["a"]
        assert stages[1] == ["b"]
        assert stages[2] == ["c"]

    def test_parallel_modules_in_same_stage(self):
        from ares.core.chain.chain import ChainNode, DependencyResolver
        r = DependencyResolver()
        nodes = [
            ChainNode("enum_users"),
            ChainNode("enum_computers"),
            ChainNode("kerberoast", depends_on=["enum_users"]),
        ]
        stages = r.resolve(nodes)
        # enum_users and enum_computers should be in the same stage
        assert len(stages[0]) == 2
        assert set(stages[0]) == {"enum_users", "enum_computers"}

    def test_cyclic_dependency_raises(self):
        from ares.core.chain.chain import ChainNode, CyclicDependencyError, DependencyResolver
        r = DependencyResolver()
        nodes = [
            ChainNode("a", depends_on=["b"]),
            ChainNode("b", depends_on=["a"]),
        ]
        with pytest.raises(CyclicDependencyError):
            r.resolve(nodes)

    def test_empty_nodes(self):
        from ares.core.chain.chain import DependencyResolver
        r = DependencyResolver()
        assert r.resolve([]) == []

    def test_diamond_dependency(self):
        from ares.core.chain.chain import ChainNode, DependencyResolver
        r = DependencyResolver()
        # a → b, a → c, b+c → d
        nodes = [
            ChainNode("a"),
            ChainNode("b", depends_on=["a"]),
            ChainNode("c", depends_on=["a"]),
            ChainNode("d", depends_on=["b", "c"]),
        ]
        stages = r.resolve(nodes)
        assert stages[0] == ["a"]
        assert set(stages[1]) == {"b", "c"}
        assert stages[2] == ["d"]


class TestAttackChain:

    def test_manual_chain_add_and_resolve(self):
        from ares.core.chain.chain import AttackChain
        chain = AttackChain("test")
        chain.add("ad.enum_users")
        chain.add("ad.kerberoast", after=["ad.enum_users"])
        stages = chain.resolve()
        assert stages[0] == ["ad.enum_users"]
        assert stages[1] == ["ad.kerberoast"]

    def test_chain_summary(self):
        from ares.core.chain.chain import AttackChain
        chain = AttackChain("summary_test")
        chain.add("a")
        chain.add("b", after=["a"])
        summary = chain.summary()
        assert summary["total_modules"] == 2
        assert summary["stage_count"] == 2
        assert summary["name"] == "summary_test"

    def test_chain_node_params(self):
        from ares.core.chain.chain import AttackChain
        chain = AttackChain("params_test")
        chain.add("mod_a", params={"dc": "10.0.0.1", "domain": "CORP"})
        params = chain.node_params()
        assert params["mod_a"]["dc"] == "10.0.0.1"

    def test_from_template_ad_full(self):
        registry = _make_mock_registry(["ad.enum_users", "ad.kerberoast"])
        from ares.core.chain.chain import AttackChain
        chain = AttackChain.from_template("ad_recon", registry)
        assert chain.name == "ad_recon"
        assert len(chain._nodes) >= 1

    def test_from_template_unknown_raises(self):
        registry = _make_mock_registry()
        from ares.core.chain.chain import AttackChain
        with pytest.raises(ValueError, match="Unknown template"):
            AttackChain.from_template("nonexistent_template", registry)

    def test_templates_keys(self):
        from ares.core.chain.chain import AttackChain
        assert "ad_full" in AttackChain.TEMPLATES
        assert "ad_recon" in AttackChain.TEMPLATES
        assert "linux_privesc" in AttackChain.TEMPLATES
        assert "full_engagement" in AttackChain.TEMPLATES

    def test_auto_chain_from_registry(self):
        registry = _make_mock_registry(["ad.enum_users", "ad.kerberoast"])
        from ares.core.chain.chain import AttackChain
        chain = AttackChain.auto(registry, ["ad.enum_users", "ad.kerberoast"], name="auto_test")
        assert chain.name == "auto_test"
        assert len(chain._nodes) >= 1

    def test_chain_fluent_api(self):
        from ares.core.chain.chain import AttackChain
        chain = (
            AttackChain("fluent")
            .add("a")
            .add("b", after=["a"])
            .add("c", after=["b"])
        )
        stages = chain.resolve()
        assert len(stages) == 3


class TestCapabilityResolver:

    def test_builds_nodes_from_registry(self):
        from ares.core.chain.chain import CapabilityResolver

        # Mod A produces user_list, Mod B requires user_list
        class ModA:
            MODULE_ID = "mod.a"
            REQUIRES  = []
            OUTPUTS   = ["user_list"]

        class ModB:
            MODULE_ID = "mod.b"
            REQUIRES  = ["user_list"]
            OUTPUTS   = []

        registry = MagicMock()
        registry.get.side_effect = lambda mid: {"mod.a": ModA, "mod.b": ModB}.get(mid)

        resolver = CapabilityResolver(registry)
        nodes = resolver.build_nodes(["mod.a", "mod.b"])

        assert len(nodes) == 2
        b_node = next(n for n in nodes if n.module_id == "mod.b")
        assert "mod.a" in b_node.depends_on


class TestChainAdvisor:

    def _make_finding(self, title, severity="medium", mitre="T1087.002"):
        f = MagicMock()
        f.title          = title
        f.severity       = MagicMock(value=severity)
        f.mitre_technique = mitre
        return f

    def test_suggests_kerberoast_for_spn_finding(self):
        from ares.core.chain.chain import ChainAdvisor
        registry = MagicMock()
        registry.__contains__ = lambda self, mid: True
        advisor = ChainAdvisor(registry)
        findings = [self._make_finding("Found SPN accounts for kerberoasting")]
        suggestions = advisor.suggest(findings)
        module_ids = [s.module_id for s in suggestions]
        assert "ad.kerberoast" in module_ids

    def test_suggests_asreproast_for_pre_auth_disabled(self):
        from ares.core.chain.chain import ChainAdvisor
        registry = MagicMock()
        registry.__contains__ = lambda self, mid: True
        advisor = ChainAdvisor(registry)
        findings = [self._make_finding("Pre-auth disabled on user john@corp")]
        suggestions = advisor.suggest(findings)
        module_ids = [s.module_id for s in suggestions]
        assert "ad.asreproast" in module_ids

    def test_no_suggestions_for_empty_findings(self):
        from ares.core.chain.chain import ChainAdvisor
        registry = MagicMock()
        registry.__contains__ = lambda self, mid: False
        advisor = ChainAdvisor(registry)
        suggestions = advisor.suggest([])
        assert suggestions == []

    def test_suggestions_sorted_by_priority(self):
        from ares.core.chain.chain import ChainAdvisor
        registry = MagicMock()
        registry.__contains__ = lambda self, mid: True
        advisor = ChainAdvisor(registry)
        findings = [
            self._make_finding("SPN accounts", "critical"),
            self._make_finding("Critical ACL path found", "critical"),
        ]
        suggestions = advisor.suggest(findings)
        # Verify suggestions have priorities set
        priorities = [s.priority for s in suggestions]
        assert priorities == sorted(priorities)


# ─────────────────────────────────────────────────────────────
# TARGET 4: DASHBOARD
# ─────────────────────────────────────────────────────────────

class TestDashboardApp:
    # Dashboard auth now goes through ares.api.rbac.resolve_bearer_principal so
    # the dashboard enforces the same family/epoch/is_active checks as the main
    # API. The test fixture mints a canonical access token (with sid/ver) and
    # wires a DB mock that returns an authoritative principal row.
    _TEST_USERNAME = "dashboard-tester"
    _TEST_USER_ID = "00000000-0000-0000-0000-00000000da54"

    @classmethod
    def _auth_headers(cls) -> dict[str, str]:
        from ares.core.config import get_settings
        from ares.core.security import create_access_token
        from ares.core.token_sessions import generate_family_id

        settings = get_settings()
        token = create_access_token(
            data={
                "sub": cls._TEST_USERNAME,
                "sid": generate_family_id(),
                "ver": 1,
            },
            secret_key=settings.secret_key_value,
            algorithm=settings.ares_jwt_algorithm,
            expires_minutes=60,
        )
        return {"Authorization": f"Bearer {token}"}

    @classmethod
    def _principal_row(cls) -> dict[str, object]:
        return {
            "id": cls._TEST_USER_ID,
            "username": cls._TEST_USERNAME,
            "role": "operator",
            "auth_epoch": 1,
        }

    @classmethod
    def _auth_db(cls) -> AsyncMock:
        mock_db = AsyncMock()
        mock_db.resolve_access_token_principal = AsyncMock(
            return_value=cls._principal_row()
        )
        mock_db.is_access_token_revoked = AsyncMock(return_value=False)
        mock_db.__aenter__ = AsyncMock(return_value=mock_db)
        mock_db.__aexit__ = AsyncMock(return_value=None)
        return mock_db

    @contextmanager
    def _authenticated_dashboard(self, db: AsyncMock | None = None):
        auth_db = db or self._auth_db()
        # Always pre-wire the authoritative bearer check regardless of what the
        # caller has already configured on the mock: tests here care about the
        # resource endpoints (list_campaigns / list_findings / ...), not about
        # authentication, so this seam is always what we want the mock to
        # answer.
        auth_db.resolve_access_token_principal = AsyncMock(
            return_value=self._principal_row()
        )
        auth_db.is_access_token_revoked = AsyncMock(return_value=False)
        auth_db.__aenter__ = AsyncMock(return_value=auth_db)
        auth_db.__aexit__ = AsyncMock(return_value=None)
        # The dashboard now prefers the shared db from dashboard_app.state; set
        # it so auth doesn't have to open a per-request connection.
        from ares.api.dashboard.app import dashboard_app
        prior_state_db = getattr(dashboard_app.state, "db", None)
        dashboard_app.state.db = auth_db
        try:
            with patch(
                "ares.db.database.AresDatabase.create",
                new=AsyncMock(return_value=auth_db),
            ):
                yield self._auth_headers()
        finally:
            if prior_state_db is None:
                try:
                    del dashboard_app.state.db
                except AttributeError:
                    pass
            else:
                dashboard_app.state.db = prior_state_db

    @pytest.fixture
    def client(self):
        from fastapi.testclient import TestClient

        from ares.api.dashboard.app import dashboard_app
        return TestClient(dashboard_app)

    def test_root_returns_html(self, client):
        r = client.get("/")
        assert r.status_code == 200
        assert "text/html" in r.headers["content-type"]
        assert "<html" in r.text.lower() or "<!doctype" in r.text.lower()

    def test_html_contains_ares_title(self, client):
        r = client.get("/")
        assert "ARES" in r.text

    def test_api_status_online(self, client):
        with self._authenticated_dashboard() as headers:
            r = client.get("/api/status", headers=headers)
            assert r.status_code == 200
            data = r.json()
            assert data["status"] == "online"
            assert "version" in data

    def test_api_workers_returns_structure(self, client):
        with self._authenticated_dashboard() as headers:
            r = client.get("/api/workers", headers=headers)
            assert r.status_code == 200
            data = r.json()
            assert "workers" in data
            assert "queue" in data
            assert "pending" in data["queue"]
            # AUDIT run-1 H21: the endpoint reports whether a worker controller
            # is actually attached instead of silently presenting a 0/0/0/0
            # queue as if it were a healthy, idle controller.
            assert data["controller_available"] is False
            assert data["controller_status"] == "not_running"

    def test_api_campaigns_with_db_error_returns_500(self, client):
        """When DB is unavailable, returns 500 gracefully."""
        with patch("ares.api.dashboard.app.dashboard_app") as mock_app:  # noqa: F841
            # Test that the route handles errors correctly
            pass  # Route already has try/except → HTTPException

    def test_api_campaigns_db_mocked(self, client):
        """Mock DB so campaigns route works without real DB."""
        mock_db = AsyncMock()
        mock_db.list_campaigns = AsyncMock(return_value=([{"id": "c-001", "name": "Test"}], 1))
        mock_db.__aenter__ = AsyncMock(return_value=mock_db)
        mock_db.__aexit__  = AsyncMock(return_value=None)

        mock_state = MagicMock()
        mock_state.db = mock_db

        with self._authenticated_dashboard(mock_db) as headers, \
             patch("ares.api.dashboard.app.dashboard_app.state", mock_state):
            r = client.get("/api/campaigns", headers=headers)
            # Either 200 with data or 500 if DB init fails - both are valid
            assert r.status_code in (200, 500)

    def test_api_findings_with_campaign_id(self, client):
        mock_db = AsyncMock()
        marker = "SYNTHETIC-LEGACY-EVIDENCE-MARKER"
        row = {
            "id": "legacy-finding",
            "title": "Legacy metadata remains visible",
            "evidence_json": f'{{"nested":"{marker}"}}',
        }
        original = dict(row)
        mock_db.list_findings = AsyncMock(return_value=([row], 1))
        mock_db.get_campaign = AsyncMock(return_value={"id": "c-001", "operator": self._TEST_USERNAME})
        mock_db.__aenter__ = AsyncMock(return_value=mock_db)
        mock_db.__aexit__  = AsyncMock(return_value=None)

        mock_state = MagicMock()
        mock_state.db = mock_db

        with self._authenticated_dashboard(mock_db) as headers, \
             patch("ares.api.dashboard.app.dashboard_app.state", mock_state):
            r = client.get("/api/campaigns/c-001/findings", headers=headers)
            assert r.status_code == 200
            assert r.json()[0]["evidence_json"] == '{"redacted":true}'
            assert r.json()[0]["title"] == row["title"]
            assert marker not in r.text
            assert row == original

    def test_api_findings_standalone_db_branch_uses_same_redaction(self, client):
        marker = "SYNTHETIC-LEGACY-STANDALONE-MARKER"
        row = {
            "id": "legacy-standalone-finding",
            "title": "Legacy standalone metadata",
            "evidence_json": marker,
        }
        original = dict(row)
        mock_db = AsyncMock()
        mock_db.list_findings = AsyncMock(return_value=([row], 1))
        mock_db.get_campaign = AsyncMock(return_value={"id": "c-001", "operator": self._TEST_USERNAME})
        mock_db.__aenter__ = AsyncMock(return_value=mock_db)
        mock_db.__aexit__ = AsyncMock(return_value=None)
        mock_state = MagicMock()
        mock_state.db = None

        # This slice contains evidence only; legacy campaign ownership remains
        # intentionally unchanged and must be handled by a separate policy.
        with self._authenticated_dashboard(mock_db) as headers, \
             patch("ares.api.dashboard.app.dashboard_app.state", mock_state), \
             patch("ares.api.dashboard.app._get_db", return_value=mock_db):
            r = client.get("/api/campaigns/c-001/findings", headers=headers)

        assert r.status_code == 200
        assert r.json()[0]["evidence_json"] == '{"redacted":true}'
        assert r.json()[0]["title"] == row["title"]
        assert marker not in r.text
        assert row == original
        mock_db.__aenter__.assert_awaited()

    def test_api_summary_with_mock_db(self, client):
        mock_db = AsyncMock()
        mock_db.campaign_summary = AsyncMock(return_value={"total": 5, "critical": 1})
        mock_db.list_findings     = AsyncMock(return_value=([], 0))
        mock_db.get_campaign      = AsyncMock(return_value={"id": "c-001", "operator": self._TEST_USERNAME})
        mock_db.__aenter__        = AsyncMock(return_value=mock_db)
        mock_db.__aexit__         = AsyncMock(return_value=None)

        mock_state = MagicMock()
        mock_state.db = mock_db

        with self._authenticated_dashboard(mock_db) as headers, \
             patch("ares.api.dashboard.app.dashboard_app.state", mock_state):
            r = client.get("/api/campaigns/c-001/summary", headers=headers)
            assert r.status_code in (200, 500)

    def test_api_findings_rejects_other_operators_campaign(self, client):
        """AUDIT run-1 H6: an operator must not read a campaign owned by another
        operator through the dashboard sub-app (404, not the data)."""
        mock_db = AsyncMock()
        mock_db.get_campaign = AsyncMock(return_value={"id": "c-001", "operator": "someone-else"})
        mock_db.list_findings = AsyncMock(return_value=([{"id": "secret-finding"}], 1))
        mock_db.__aenter__ = AsyncMock(return_value=mock_db)
        mock_db.__aexit__ = AsyncMock(return_value=None)

        mock_state = MagicMock()
        mock_state.db = mock_db

        with self._authenticated_dashboard(mock_db) as headers, \
             patch("ares.api.dashboard.app.dashboard_app.state", mock_state):
            r = client.get("/api/campaigns/c-001/findings", headers=headers)
            assert r.status_code == 404
            assert "secret-finding" not in r.text
            mock_db.list_findings.assert_not_awaited()

    def test_api_campaigns_filters_to_owner_for_operator(self, client):
        """AUDIT run-1 H6: the campaign list only returns the operator's own
        campaigns (team_leads see all; this actor is an operator)."""
        mock_db = AsyncMock()
        mine = {"id": "c-mine", "operator": self._TEST_USERNAME}
        theirs = {"id": "c-theirs", "operator": "someone-else"}
        mock_db.list_campaigns = AsyncMock(return_value=([mine, theirs], 2))
        mock_db.__aenter__ = AsyncMock(return_value=mock_db)
        mock_db.__aexit__ = AsyncMock(return_value=None)

        mock_state = MagicMock()
        mock_state.db = mock_db

        with self._authenticated_dashboard(mock_db) as headers, \
             patch("ares.api.dashboard.app.dashboard_app.state", mock_state):
            r = client.get("/api/campaigns", headers=headers)
            assert r.status_code == 200
            ids = [c["id"] for c in r.json()]
            assert ids == ["c-mine"]

    def test_broadcast_finding_sends_to_connections(self):
        """broadcast_finding sends JSON to all active WS connections."""
        import asyncio

        from ares.api.dashboard.app import _live_connections, broadcast_finding

        mock_ws1 = AsyncMock()
        mock_ws2 = AsyncMock()
        original = list(_live_connections)
        _live_connections.clear()
        _live_connections.extend([mock_ws1, mock_ws2])

        asyncio.run(
            broadcast_finding({"type": "finding", "severity": "critical"})
        )

        mock_ws1.send_text.assert_called_once()
        mock_ws2.send_text.assert_called_once()
        sent_data = json.loads(mock_ws1.send_text.call_args[0][0])
        assert sent_data["type"] == "finding"

        _live_connections.clear()
        _live_connections.extend(original)

    def test_broadcast_removes_dead_connections(self):
        """Dead WebSocket connections are removed from _live_connections."""
        import asyncio

        from ares.api.dashboard.app import _live_connections, broadcast_finding

        mock_dead = AsyncMock()
        mock_dead.send_text.side_effect = RuntimeError("connection closed")
        mock_alive = AsyncMock()

        original = list(_live_connections)
        _live_connections.clear()
        _live_connections.extend([mock_dead, mock_alive])

        asyncio.run(
            broadcast_finding({"type": "finding"})
        )

        assert mock_dead not in _live_connections
        assert mock_alive in _live_connections

        _live_connections.clear()
        _live_connections.extend(original)

    def test_dashboard_html_has_stat_cards(self, client):
        r = client.get("/")
        assert "stat-card" in r.text or "CRITICAL" in r.text

    def test_dashboard_html_has_nav_tabs(self, client):
        r = client.get("/")
        assert "nav" in r.text.lower()

    def test_dashboard_html_has_websocket_script(self, client):
        r = client.get("/")
        assert "WebSocket" in r.text or "ws://" in r.text


# ─────────────────────────────────────────────────────────────
# TARGET 5: GOAL PLANNING
# ─────────────────────────────────────────────────────────────

class TestGoalEngine:

    def _make_engine(self, module_ids=None):
        from ares.goal.engine import GoalEngine
        from ares.state.target_state import OperatorSession

        registry = _make_mock_registry(module_ids or ["ad.enum_users", "ad.kerberoast"])
        session  = OperatorSession(campaign_id="test-001")
        engine = GoalEngine(registry=registry, session=session)
        engine._configure_c_live_test_planning()
        return engine

    def test_plan_domain_admin(self):
        from ares.goal.engine import Goal
        engine = self._make_engine(["ad.enum_users", "ad.kerberoast", "ad.dcsync"])
        plan = engine.plan(Goal.DOMAIN_ADMIN, context={"dc": "10.0.0.1", "domain": "CORP"})
        assert plan.goal == Goal.DOMAIN_ADMIN
        assert len(plan.steps) == 3
        assert [s.module_id for s in plan.steps] == ["ad.enum_users", "ad.kerberoast", "ad.dcsync"]
        assert isinstance(plan.estimated_duration_min, int)


    def test_plan_returns_goal_attack_plan(self):
        from ares.goal.engine import Goal, GoalAttackPlan
        engine = self._make_engine()
        plan = engine.plan(Goal.DOMAIN_ADMIN)
        assert isinstance(plan, GoalAttackPlan)

    def test_plan_steps_have_reasons(self):
        from ares.goal.engine import Goal
        engine = self._make_engine(["ad.enum_users", "ad.kerberoast"])
        plan = engine.plan(Goal.DOMAIN_ADMIN)
        for step in plan.steps:
            assert step.reason != ""
            assert step.module_id != ""

    def test_plan_unknown_goal_raises(self):
        engine = self._make_engine()
        with pytest.raises(ValueError):
            engine.plan("nonexistent_goal")  # type: ignore

    def test_check_goal_achieved_empty_session(self):
        from ares.goal.engine import Goal
        engine = self._make_engine()
        # Fresh session - goal not achieved
        assert engine.check_goal_achieved(Goal.DOMAIN_ADMIN) is False

    def test_check_initial_access_with_owned_host(self):
        from ares.goal.engine import Goal, GoalEngine
        from ares.state.target_state import CompromiseLevel, OperatorSession, TargetHost

        registry = _make_mock_registry()
        session  = OperatorSession(campaign_id="test")
        host     = TargetHost(ip="10.0.0.1", hostname="srv01",
                              compromise_level=CompromiseLevel.USER)
        session.update_host(host)
        engine = GoalEngine(registry=registry, session=session)
        assert engine.check_goal_achieved(Goal.INITIAL_ACCESS) is True

    def test_goal_definitions_exist(self):
        from ares.goal.engine import GOAL_DEFINITIONS, Goal
        assert Goal.DOMAIN_ADMIN in GOAL_DEFINITIONS
        assert Goal.DATA_EXFIL in GOAL_DEFINITIONS
        assert Goal.CLOUD_ADMIN in GOAL_DEFINITIONS

    def test_goal_definitions_have_preferred_chain(self):
        from ares.goal.engine import GOAL_DEFINITIONS, Goal
        defn = GOAL_DEFINITIONS[Goal.DOMAIN_ADMIN]
        assert len(defn.preferred_chain) >= 1
        assert all(isinstance(m, str) for m in defn.preferred_chain)

    def test_plan_context_passed_to_steps(self):
        from ares.goal.engine import Goal
        engine = self._make_engine(["ad.kerberoast"])
        plan = engine.plan(Goal.DOMAIN_ADMIN, context={"dc": "10.0.0.5", "domain": "CORP"})
        for step in plan.steps:
            if step.params:
                # Steps derived from context should reference the domain
                assert isinstance(step.params, dict)

    @pytest.mark.asyncio
    async def test_c_live_goal_execute_uses_admitted_plan_dispatch(self):
        from ares.core.execution_admission import DispatchDispositionV1
        from ares.db.execution_lifecycle import FixedResult, TrustedPrincipal
        from ares.goal.engine import (
            GOAL_DEFINITIONS,
            Goal,
            GoalAttackPlan,
            GoalAttackStep,
            GoalEngine,
        )

        engine = self._make_engine(["ad.enum_users", "ad.enum_spn"])
        production_engine = GoalEngine(registry=engine.registry, session=engine.session)
        with pytest.raises(PermissionError, match="non-dispatchable"):
            production_engine.plan(Goal.DOMAIN_ADMIN)

        nested_params = {"hosts": ["dc01.corp.test"]}
        plan = GoalAttackPlan(
            goal=Goal.DOMAIN_ADMIN,
            definition=GOAL_DEFINITIONS[Goal.DOMAIN_ADMIN],
            steps=[
                GoalAttackStep(1, "ad.enum_users", "frozen", {"nested": nested_params}),
                GoalAttackStep(2, "ad.enum_spn", "frozen", {"nested": nested_params}),
            ],
        )
        production_result = await production_engine.execute(
            plan,
            SimpleNamespace(id="00000000-0000-4000-8000-000000000024"),
        )
        assert production_result["status"] == "non_dispatchable"
        assert production_result["steps_run"] == 0

        requests = []

        async def execute_module(principal, request, campaign):
            del principal, campaign
            requests.append(request)
            if len(requests) == 1:
                nested_params["hosts"][0] = "caller-mutated-after-first-effect"
                request.raw_parameters["nested"]["hosts"].append("first-child-mutation")
            return SimpleNamespace(
                disposition=DispatchDispositionV1.TERMINAL,
                identity=None,
                lifecycle_result=FixedResult.APPLIED,
                terminal_committed=True,
            )

        coordinator = SimpleNamespace(execute_module=AsyncMock(side_effect=execute_module))
        engine._configure_c_live_test_dispatch(
            coordinator=coordinator,
            principal=TrustedPrincipal(
                "00000000-0000-4000-8000-000000000022",
                "00000000-0000-4000-8000-000000000022",
            ),
            idempotency_key="00000000-0000-4000-8000-000000000023",
        )

        result = await engine.execute(
            plan,
            SimpleNamespace(id="00000000-0000-4000-8000-000000000024"),
        )

        assert result["status"] == "terminal"
        assert coordinator.execute_module.await_count == 2
        assert requests[0].ingress_code == "goal_api"
        assert requests[0].module_id == "ad.enum_users"
        assert requests[1].module_id == "ad.enum_spn"
        assert requests[0].whole_intent_digest == requests[1].whole_intent_digest
        assert requests[1].raw_parameters == {"nested": {"hosts": ["dc01.corp.test"]}}


class TestAttackPlanner:

    def _make_planner(self, module_ids=None):
        from ares.goal.planner import AttackPlanner
        registry = _make_mock_registry(module_ids or ["ad.enum_users", "ad.kerberoast"])
        return AttackPlanner(registry=registry)

    def test_suggest_returns_list(self):
        from ares.goal.engine import Goal
        from ares.goal.planner import PlannerContext

        planner = self._make_planner()
        ctx = PlannerContext(goal=Goal.DOMAIN_ADMIN, targets=["10.0.0.1"])
        suggestions = planner.suggest(ctx, limit=3)
        assert isinstance(suggestions, list)

    def test_suggest_respects_limit(self):
        from ares.goal.engine import Goal
        from ares.goal.planner import AttackPlanner, PlannerContext

        registry = _make_mock_registry(
            ["ad.enum_users", "ad.kerberoast", "ad.dcsync",
             "ad.asreproast", "linux.privesc", "cloud.aws"]
        )
        planner = AttackPlanner(registry=registry)
        ctx = PlannerContext(goal=Goal.DOMAIN_ADMIN, targets=["10.0.0.1"])
        suggestions = planner.suggest(ctx, limit=3)
        assert len(suggestions) <= 3

    def test_suggestions_sorted_by_score(self):
        from ares.goal.engine import Goal
        from ares.goal.planner import PlannerContext

        planner = self._make_planner()
        ctx = PlannerContext(goal=Goal.DOMAIN_ADMIN, targets=["10.0.0.1"])
        suggestions = planner.suggest(ctx)
        if len(suggestions) > 1:
            scores = [s.score for s in suggestions]
            assert scores == sorted(scores, reverse=True)

    def test_suggestion_has_rationale(self):
        from ares.goal.engine import Goal
        from ares.goal.planner import PlannerContext

        planner = self._make_planner()
        ctx = PlannerContext(goal=Goal.DOMAIN_ADMIN, targets=["10.0.0.1"])
        suggestions = planner.suggest(ctx)
        for s in suggestions:
            assert s.rationale != ""
            assert 0.0 <= s.score <= 1.0

    def test_noisy_modules_filtered_in_stealth(self):
        from ares.goal.engine import Goal
        from ares.goal.planner import AttackPlanner, PlannerContext

        # Create a noisy module
        class NoisyMod:
            MODULE_ID        = "ad.noisy"
            MODULE_CATEGORY  = "ad"
            REQUIRES         = []
            OUTPUTS          = []
            MITRE_TECHNIQUES = []
            OPSEC_LEVEL      = MagicMock(value="high_noise")

        registry = MagicMock()
        registry.all.return_value = [NoisyMod]
        registry.get.return_value = NoisyMod

        planner = AttackPlanner(registry=registry)
        ctx = PlannerContext(
            goal=Goal.DOMAIN_ADMIN,
            targets=["10.0.0.1"],
            opsec_profile="stealth",
        )
        suggestions = planner.suggest(ctx)
        noisy_ids = [s.module_id for s in suggestions]
        assert "ad.noisy" not in noisy_ids

    def test_already_tried_excluded(self):
        from ares.goal.engine import Goal
        from ares.goal.planner import PlannerContext

        planner = self._make_planner(["ad.enum_users"])
        ctx = PlannerContext(
            goal=Goal.DOMAIN_ADMIN,
            targets=["10.0.0.1"],
            already_tried={"ad.enum_users:10.0.0.1"},
        )
        suggestions = planner.suggest(ctx)
        ids = [s.module_id for s in suggestions]
        assert "ad.enum_users" not in ids

    def test_suggestion_to_dict(self):
        from ares.goal.planner import PlanSuggestion as Suggestion
        s = Suggestion(
            module_id="ad.kerberoast",
            module_name="Kerberoast",
            score=0.85,
            rationale="High relevance",
            suggested_target="10.0.0.1",
            prerequisites=[],
            mitre_techniques=["T1558.003"],
            opsec_level="low",
            estimated_noise=0.2,
        )
        d = s.to_dict()
        assert d["module_id"] == "ad.kerberoast"
        assert d["score"] == 0.85
        assert "T1558.003" in d["mitre"]

    def test_score_weights_sum_to_one(self):
        from ares.goal.planner import _SCORE_WEIGHTS
        total = sum(_SCORE_WEIGHTS.values())
        assert abs(total - 1.0) < 0.001


class TestAdaptiveStrategy:

    def test_record_and_retrieve_failure(self):
        from ares.goal.adaptive import AdaptiveStrategy
        from ares.state.target_state import OperatorSession

        session = OperatorSession(campaign_id="test")
        engine = AdaptiveStrategy(session=session)
        engine.record_failure("ad.kerberoast", "10.0.0.1", "Auth failed")

        summary = engine.failure_summary()
        assert "ad.kerberoast" in summary or isinstance(summary, dict)

    def test_next_alternative_kerberoast_fails(self):
        from ares.goal.adaptive import AdaptiveStrategy
        from ares.state.target_state import OperatorSession

        session = OperatorSession(campaign_id="test")
        engine = AdaptiveStrategy(session=session)
        engine.record_failure("ad.kerberoast", "10.0.0.1", "No SPN accounts found")

        alt = engine.next_alternative("ad.kerberoast", "10.0.0.1", error="No SPN accounts")
        # Should suggest asreproast or password spray as fallback
        assert alt is None or alt.module_id != "ad.kerberoast"

    def test_fallback_graph_has_entries(self):
        from ares.goal.adaptive import FALLBACK_GRAPH
        assert len(FALLBACK_GRAPH) > 0
        assert "ad.kerberoast" in FALLBACK_GRAPH or "lateral.psexec" in FALLBACK_GRAPH

    def test_strategy_hints_for_edr(self):
        from ares.goal.adaptive import AdaptiveStrategy
        from ares.state.target_state import OperatorSession

        session = OperatorSession(campaign_id="test")
        engine = AdaptiveStrategy(session=session)
        hints = engine.strategy_hints("access is denied - EDR blocked service creation")
        assert isinstance(hints, list)

    def test_alternative_chain_for_psexec_failure(self):
        from ares.goal.adaptive import AdaptiveStrategy
        from ares.state.target_state import OperatorSession

        session = OperatorSession(campaign_id="test")
        engine = AdaptiveStrategy(session=session)
        engine.record_failure("lateral.psexec", "10.0.0.1", "EDR blocked")
        chain = engine.alternative_chain("lateral.psexec", "10.0.0.1")
        assert isinstance(chain, list)


# ─────────────────────────────────────────────────────────────
# TARGET 1: CLI STORE (unit)
# ─────────────────────────────────────────────────────────────

class TestCampaignStore:

    def _store(self, tmpdir):
        from ares.cli._store import CampaignStore
        # Patch campaigns_dir to use tmpdir
        with patch("ares.cli._store.campaigns_dir",
                   return_value=Path(tmpdir)):
            yield CampaignStore()

    def test_save_and_load_campaign(self, tmp_path):
        from ares.cli._store import CampaignStore

        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path):
            store = CampaignStore()
            campaign = MagicMock()
            campaign.id   = "camp-001"
            campaign.name = "Test"
            campaign.model_dump_json.return_value = json.dumps({
                "id": "camp-001", "name": "Test", "targets": [],
                "findings": [], "noise_profile": "normal",
            })
            store.save_campaign(campaign)
            loaded = store.get_campaign("camp-001")
            assert loaded is not None
            assert loaded["id"] == "camp-001"

    def test_add_target_dict(self, tmp_path):
        from ares.cli._store import CampaignStore
        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path):
            store = CampaignStore()
            # Create a fake campaign JSON
            (tmp_path / "camp-001.json").write_text(json.dumps({
                "id": "camp-001", "name": "T", "targets": [],
                "findings": [], "noise_profile": "normal",
            }))
            result = store.add_target("camp-001", {"target": "10.0.0.1", "tags": ["dc"]})
            assert result is True

            targets = store.list_targets("camp-001")
            assert any(t.get("target") == "10.0.0.1" for t in targets)

    def test_add_target_string_normalised(self, tmp_path):
        from ares.cli._store import CampaignStore
        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path):
            store = CampaignStore()
            (tmp_path / "camp-001.json").write_text(json.dumps({
                "id": "camp-001", "name": "T", "targets": [],
                "findings": [], "noise_profile": "normal",
            }))
            store.add_target("camp-001", "10.0.0.5")
            targets = store.list_targets("camp-001")
            assert any(t.get("target") == "10.0.0.5" for t in targets)

    def test_add_target_no_duplicates(self, tmp_path):
        from ares.cli._store import CampaignStore
        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path):
            store = CampaignStore()
            (tmp_path / "camp-001.json").write_text(json.dumps({
                "id": "camp-001", "name": "T", "targets": [],
                "findings": [], "noise_profile": "normal",
            }))
            store.add_target("camp-001", "10.0.0.1")
            store.add_target("camp-001", "10.0.0.1")  # duplicate
            targets = store.list_targets("camp-001")
            ips = [t.get("target") for t in targets]
            assert ips.count("10.0.0.1") == 1

    def test_add_target_nonexistent_campaign(self, tmp_path):
        from ares.cli._store import CampaignStore
        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path):
            store = CampaignStore()
            result = store.add_target("nonexistent", "10.0.0.1")
            assert result is False

    def test_save_checkpoint(self, tmp_path):
        from ares.cli._store import CampaignStore
        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path):
            store = CampaignStore()
            (tmp_path / "camp-001.json").write_text(json.dumps({
                "id": "camp-001", "name": "Test", "targets": [],
                "findings": [], "noise_profile": "normal",
            }))
            cp = store.save_checkpoint("camp-001", notes="Before weekend")
            assert cp is not None
            assert "checkpoint_id" in cp
            assert "saved_at" in cp
            assert Path(cp["path"]).exists()

    def test_load_checkpoint_latest(self, tmp_path):
        from ares.cli._store import CampaignStore
        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path):
            store = CampaignStore()
            (tmp_path / "camp-001.json").write_text(json.dumps({
                "id": "camp-001", "name": "Test", "targets": [],
                "findings": [], "noise_profile": "normal",
            }))
            store.save_checkpoint("camp-001", notes="cp1")
            cp = store.load_checkpoint("camp-001", "latest")
            assert cp is not None
            assert cp["notes"] == "cp1"

    def test_load_checkpoint_nonexistent_campaign(self, tmp_path):
        from ares.cli._store import CampaignStore
        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path):
            store = CampaignStore()
            result = store.load_checkpoint("nonexistent")
            assert result is None

    def test_list_reports_empty(self, tmp_path):
        from ares.cli._store import CampaignStore
        reports_dir = tmp_path / "reports"  # noqa: F841
        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path), \
             patch("pathlib.Path.home", return_value=tmp_path):
            store = CampaignStore()
            # No reports dir → empty list
            result = store.list_reports()
            assert isinstance(result, list)

    def test_list_reports_with_files(self, tmp_path):
        from ares.cli._store import CampaignStore
        reports_dir = tmp_path / ".ares" / "reports"
        reports_dir.mkdir(parents=True)
        (reports_dir / "test_report.html").write_text("<html>test</html>")
        (reports_dir / "test_report.json").write_text('{"findings": []}')

        with patch("ares.cli._store.campaigns_dir", return_value=tmp_path), \
             patch("pathlib.Path.home", return_value=tmp_path):
            store = CampaignStore()
            reports = store.list_reports()
            assert len(reports) >= 2
            fmts = {r["format"] for r in reports}
            assert "html" in fmts
            assert "json" in fmts


# ─────────────────────────────────────────────────────────────
# INTEGRATION: Report + Campaign round-trip
# ─────────────────────────────────────────────────────────────

class TestReportCampaignRoundTrip:

    def test_full_json_report_round_trip(self):
        from ares.modules.reporting.report_gen import ReportGenerator
        campaign = _make_campaign(name="Integration Test", n_findings=5)

        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="json")
            data = json.loads(path.read_text())

            # Verify key report sections
            assert data["campaign"]["name"] == "Integration Test"
            assert data["summary"]["total_confirmed"] == 5
            assert len(data["findings"]) == 5

            # Verify MITRE coverage is populated
            assert "mitre_coverage" in data

            # Verify all findings have required fields
            for f in data["findings"]:
                assert f["severity"] in ("critical","high","medium","low","info")
                assert 0.0 <= f["confidence"] <= 1.0

    def test_markdown_report_severity_counts(self):
        from ares.modules.reporting.report_gen import ReportGenerator
        campaign = _make_campaign(n_findings=5)

        with tempfile.TemporaryDirectory() as tmpdir:
            gen = ReportGenerator(output_dir=tmpdir)
            path = gen.generate(campaign, fmt="md")
            md = path.read_text()

            # MITRE ATT&CK Coverage section should exist
            assert "MITRE" in md

    def test_chain_plan_to_execution_flow(self):
        """AttackChain.resolve() produces a valid stage order for GoalEngine."""
        from ares.core.chain.chain import AttackChain

        chain = AttackChain("full_flow")
        chain.add("ad.enum_users")
        chain.add("ad.enum_spn",   after=["ad.enum_users"])
        chain.add("ad.kerberoast", after=["ad.enum_spn"])
        chain.add("ad.asreproast")  # runs in parallel with enum_users

        stages = chain.resolve()
        all_modules = [m for stage in stages for m in stage]
        assert "ad.enum_users" in all_modules
        assert "ad.kerberoast" in all_modules

        # kerberoast must come after enum_spn
        spn_stage   = next(i for i, s in enumerate(stages) if "ad.enum_spn" in s)
        krbst_stage = next(i for i, s in enumerate(stages) if "ad.kerberoast" in s)
        assert krbst_stage > spn_stage


def test_c_live_cli_dispatch_is_bearer_http_only(monkeypatch):
    import ares.cli.typer_main as cli

    key = "00000000-0000-4000-8000-000000000047"
    captured: dict[str, Any] = {}
    response = MagicMock()
    response.status = 200
    response.read.return_value = b'{"status":"accepted"}'
    response.__enter__.return_value = response
    response.__exit__.return_value = False
    opener = MagicMock()
    opener.open.return_value = response
    monkeypatch.setenv("ARES_API_URL", "https://ares.example.test:8443")
    monkeypatch.setenv("ARES_ACCESS_TOKEN", "opaque-test-bearer")

    def build_opener(*handlers):
        captured["handlers"] = handlers
        return opener

    monkeypatch.setattr(cli.urllib_request, "build_opener", build_opener)
    monkeypatch.setattr(cli.ssl, "create_default_context", MagicMock(return_value=MagicMock()))

    result = cli._c_live_http_post(
        "/modules/ad.test/run",
        {"campaign_id": "campaign-47", "params": {}, "dry_run": False},
        key,
    )

    assert result["status"] == 200
    opener.open.assert_called_once()
    outgoing = opener.open.call_args.args[0]
    assert outgoing.full_url == "https://ares.example.test:8443/modules/ad.test/run"
    assert outgoing.get_method() == "POST"
    assert outgoing.get_header("Authorization") == "Bearer opaque-test-bearer"
    assert outgoing.get_header("Idempotency-key") == key
    assert json.loads(outgoing.data) == {
        "campaign_id": "campaign-47",
        "dry_run": False,
        "params": {},
    }
    assert any(
        isinstance(handler, cli.urllib_request.HTTPSHandler)
        for handler in captured["handlers"]
    )


def test_c_live_cli_preview_executes_no_module_code(monkeypatch):
    import ares.cli.typer_main as cli

    dispatch = MagicMock(side_effect=AssertionError("preview crossed HTTP boundary"))
    monkeypatch.setattr(cli, "_c_live_http_post", dispatch)

    asyncio.run(
        cli._run_chain(
            "domain_admin",
            "10.0.0.1",
            "campaign-47",
            "CORP.LOCAL",
            "normal",
            False,
            True,
            "00000000-0000-4000-8000-000000000047",
        )
    )
    cli.goal_run(
        "domain_admin",
        "campaign-47",
        "10.0.0.1",
        "CORP.LOCAL",
        True,
        "00000000-0000-4000-8000-000000000047",
    )

    dispatch.assert_not_called()


def test_c_live_cli_transport_contract_covers_url_token_campaign_key_tls_and_errors(
    monkeypatch,
):
    import ares.cli.typer_main as cli

    key = "00000000-0000-4000-8000-000000000096"
    monkeypatch.delenv("ARES_API_URL", raising=False)
    with pytest.raises(cli._CLiveCliError, match="api-url"):
        cli._c_live_api_url()

    monkeypatch.setenv("ARES_API_URL", "http://ares.example.test")
    with pytest.raises(cli._CLiveCliError, match="tls-required"):
        cli._c_live_api_url()

    monkeypatch.setenv("ARES_API_URL", "http://127.0.0.1:8000")
    assert cli._c_live_api_url() == ("http://127.0.0.1:8000", False)
    monkeypatch.setenv("ARES_API_URL", "https://ares.example.test")
    assert cli._c_live_api_url() == ("https://ares.example.test", True)

    monkeypatch.delenv("ARES_ACCESS_TOKEN", raising=False)
    monkeypatch.setattr(cli.getpass, "getpass", MagicMock(return_value="prompted-bearer"))
    assert cli._c_live_bearer() == "prompted-bearer"
    with pytest.raises(cli._CLiveCliError, match="idempotency-key"):
        cli._c_live_idempotency_key("not-a-uuid")
    assert cli._c_live_idempotency_key(key) == key

    post = MagicMock(return_value={"status": 200, "body": {}, "idempotency_key": key})
    monkeypatch.setattr(cli, "_c_live_http_post", post)
    asyncio.run(
        cli._run_chain(
            "domain_admin",
            "10.0.0.1",
            "campaign with spaces",
            "CORP.LOCAL",
            "normal",
            False,
            False,
            key,
        )
    )
    post.assert_called_once()
    path, body, supplied_key = post.call_args.args
    assert path == "/campaigns/campaign%20with%20spaces/run"
    assert body["dry_run"] is False
    assert supplied_key == key