"""
Tests for run artifacts data structure and template variables.

Bug found 2026-01-12: runs/detail.html template expects artifact.cid
but the route provides artifact.hash, causing UndefinedError.
"""

import pytest
from pathlib import Path


class TestCacheNotFoundTemplate:
    """Tests for cache/not_found.html template."""

    def test_template_uses_cid_not_content_hash(self) -> None:
        """
        Regression test: not_found.html must use 'cid' variable.

        Bug: Template used 'content_hash' but route passes 'cid'.
        Fix: Changed template to use 'cid'.
        """
        template_path = Path('/home/giles/art/art-celery/app/templates/cache/not_found.html')
        content = template_path.read_text()

        assert 'cid' in content, "Template should use 'cid' variable"
        assert 'content_hash' not in content, \
            "Template should not use 'content_hash' (route passes 'cid')"

    def test_route_passes_cid_to_template(self) -> None:
        """Verify route passes 'cid' variable to not_found template."""
        router_path = Path('/home/giles/art/art-celery/app/routers/cache.py')
        content = router_path.read_text()

        # Find the render call for not_found.html
        assert 'cid=cid' in content, \
            "Route should pass cid=cid to not_found.html template"


class TestInputPreviewsDataStructure:
    """Tests for input_previews dict keys matching template expectations."""

    def test_run_card_template_expects_inp_cid(self) -> None:
        """Run card template uses inp.cid for input previews."""
        path = Path('/home/giles/art/art-celery/app/templates/runs/_run_card.html')
        content = path.read_text()

        assert 'inp.cid' in content, \
            "Run card template should use inp.cid for input previews"

    def test_input_previews_use_cid_key(self) -> None:
        """
        Regression test: input_previews must use 'cid' key not 'hash'.

        Bug: Router created input_previews with 'hash' key but template expected 'cid'.
        """
        path = Path('/home/giles/art/art-celery/app/routers/runs.py')
        content = path.read_text()

        # Should have: "cid": input_hash, not "hash": input_hash
        assert '"cid": input_hash' in content, \
            "input_previews should use 'cid' key"
        assert '"hash": input_hash' not in content, \
            "input_previews should not use 'hash' key (template expects 'cid')"


class TestArtifactDataStructure:
    """Tests for artifact dict keys matching template expectations."""

    def test_template_expects_cid_key(self) -> None:
        """Template uses artifact.cid - verify this expectation."""
        template_path = Path('/home/giles/art/art-celery/app/templates/runs/detail.html')
        content = template_path.read_text()

        # Template uses artifact.cid in multiple places
        assert 'artifact.cid' in content, "Template should reference artifact.cid"

    def test_run_service_artifacts_have_cid_key(self) -> None:
        """
        Regression test: get_run_artifacts must return dicts with 'cid' key.

        Bug: Service returned artifacts with 'hash' key but template expected 'cid'.
        Fix: Changed service to use 'cid' key for consistency.
        """
        # Read the run_service.py file and check it uses 'cid' not 'hash'
        service_path = Path('/home/giles/art/art-celery/app/services/run_service.py')
        content = service_path.read_text()

        # Find the get_artifact_info function and check it returns 'cid'
        # The function should have: "cid": cid, not "hash": cid
        assert '"cid": cid' in content or "'cid': cid" in content, \
            "get_run_artifacts should return artifacts with 'cid' key, not 'hash'"

    def test_router_artifacts_have_cid_key(self) -> None:
        """
        Regression test: inline artifact creation in router must use 'cid' key.

        Bug: Router created artifacts with 'hash' key but template expected 'cid'.
        """
        router_path = Path('/home/giles/art/art-celery/app/routers/runs.py')
        content = router_path.read_text()

        # Check that artifacts.append uses 'cid' key
        # Should have: "cid": output_cid, not "hash": output_cid
        # Count occurrences of the patterns
        hash_pattern_count = content.count('"hash": output_cid')
        cid_pattern_count = content.count('"cid": output_cid')

        assert cid_pattern_count > 0, \
            "Router should create artifacts with 'cid' key"
        assert hash_pattern_count == 0, \
            "Router should not use 'hash' key for artifacts (template expects 'cid')"
