"""Tests for lapis_spec.sections.flows."""

from lapis_spec.sections.flows import convert_flows


class TestConvertFlows:
    def test_no_flows(self):
        doc = {"paths": {}}
        flows = convert_flows(doc)
        assert flows == []

    def test_x_flows_list(self):
        doc = {
            "x-flows": [
                {
                    "name": "onboarding",
                    "description": "Customer onboarding",
                    "chain": "create_customer -> create_invoice -> send_invoice",
                    "narrative": "Creates a customer and sends first invoice.",
                }
            ],
            "paths": {},
        }
        flows = convert_flows(doc)
        assert len(flows) == 1
        assert flows[0].name == "onboarding"
        assert "create_customer" in flows[0].chain
        assert flows[0].narrative is not None

    def test_x_flows_dict(self):
        doc = {
            "x-flows": {
                "lifecycle": {
                    "description": "Invoice lifecycle",
                    "chain": "create -> send -> pay",
                }
            },
            "paths": {},
        }
        flows = convert_flows(doc)
        assert len(flows) == 1
        assert flows[0].name == "lifecycle"

    def test_from_links(self):
        doc = {
            "paths": {
                "/users": {
                    "post": {
                        "operationId": "create_user",
                        "responses": {
                            "201": {
                                "description": "Created",
                                "links": {
                                    "GetUser": {
                                        "operationId": "get_user",
                                    }
                                },
                            }
                        },
                    }
                },
                "/users/{id}": {
                    "get": {
                        "operationId": "get_user",
                        "responses": {"200": {"description": "OK"}},
                    }
                },
            }
        }
        flows = convert_flows(doc)
        assert len(flows) >= 1
        assert "create_user" in flows[0].chain
        assert "get_user" in flows[0].chain

    def test_empty_x_flows(self):
        doc = {"x-flows": [], "paths": {}}
        flows = convert_flows(doc)
        assert flows == []
