"""Tests for infra_diagram.output.console and infra_diagram.demo."""

from __future__ import annotations

from infra_diagram.demo import DEMO_COMPOSE, DEMO_KUBERNETES, DEMO_TERRAFORM, DEMOS
from infra_diagram.models import InfraDiagram, InfraEdge, InfraGroup, InfraNode, NodeType, SourceType
from infra_diagram.output.console import _truncate, render_diagram_info

# ─── Console output ──────────────────────────────────────────────────

class TestTruncate:
    def test_short_string(self):
        assert _truncate("hello", 10) == "hello"

    def test_exact_length(self):
        assert _truncate("hello", 5) == "hello"

    def test_long_string(self):
        result = _truncate("very/long/path/to/file.tf", 15)
        assert len(result) <= 15
        assert result.startswith("…")

    def test_single_char(self):
        result = _truncate("abcdef", 2)
        assert len(result) <= 2


class TestRenderDiagramInfo:
    def test_no_crash_empty_diagram(self):
        d = InfraDiagram()
        render_diagram_info(d)  # should not raise

    def test_no_crash_with_data(self):
        d = InfraDiagram(title="Test", source_type=SourceType.TERRAFORM)
        d.add_node(InfraNode(id="v", name="vpc", node_type=NodeType.NETWORK, provider="aws", source_file="main.tf"))
        d.add_node(InfraNode(id="e", name="ec2", node_type=NodeType.VM, provider="aws"))
        d.add_edge(InfraEdge(source="v", target="e", label="in"))
        d.add_group(InfraGroup(id="g", name="AWS", node_ids=["v", "e"]))
        render_diagram_info(d)  # should not raise

    def test_no_crash_edges_only(self):
        d = InfraDiagram()
        d.add_node(InfraNode(id="a", name="A", node_type=NodeType.VM))
        d.add_node(InfraNode(id="b", name="B", node_type=NodeType.DATABASE))
        d.add_edge(InfraEdge(source="a", target="b"))
        render_diagram_info(d)


# ─── Demo data ───────────────────────────────────────────────────────

class TestDemoData:
    def test_demos_dict_has_three_entries(self):
        assert "terraform" in DEMOS
        assert "kubernetes" in DEMOS
        assert "compose" in DEMOS

    def test_demo_terraform_has_resources(self):
        assert "aws_vpc" in DEMO_TERRAFORM
        assert "aws_ecs_cluster" in DEMO_TERRAFORM
        assert "aws_rds_instance" in DEMO_TERRAFORM

    def test_demo_kubernetes_has_kinds(self):
        assert "Deployment" in DEMO_KUBERNETES
        assert "Service" in DEMO_KUBERNETES
        assert "Ingress" in DEMO_KUBERNETES

    def test_demo_compose_has_services(self):
        assert "services:" in DEMO_COMPOSE
        assert "postgres" in DEMO_COMPOSE
        assert "redis" in DEMO_COMPOSE

    def test_demo_tuples(self):
        for name, (filename, content) in DEMOS.items():
            assert isinstance(filename, str)
            assert isinstance(content, str)
            assert len(content) > 50
