"""Tests for lapis_spec.sections.webhooks."""

from lapis_spec.sections.webhooks import convert_webhooks


class TestConvertWebhooks:
    def test_openapi_31_webhooks(self):
        doc = {
            "webhooks": {
                "orderPaid": {
                    "post": {
                        "operationId": "order_paid",
                        "summary": "Order payment received.",
                        "requestBody": {
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "order_id": {"type": "string"},
                                            "amount": {
                                                "type": "number",
                                                "format": "float",
                                            },
                                        },
                                    }
                                }
                            }
                        },
                        "responses": {"200": {"description": "OK"}},
                    }
                }
            }
        }
        webhooks = convert_webhooks(doc, {})
        assert len(webhooks) == 1
        wh = webhooks[0]
        assert wh.name == "order_paid"
        assert wh.method == "POST"
        assert "order_id" in [f.name for f in wh.outputs]

    def test_x_webhooks_30(self):
        doc = {
            "x-webhooks": {
                "itemCreated": {
                    "post": {
                        "summary": "Item created.",
                        "requestBody": {
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "item_id": {"type": "string"},
                                        },
                                    }
                                }
                            }
                        },
                        "responses": {"200": {"description": "OK"}},
                    }
                }
            }
        }
        webhooks = convert_webhooks(doc, {})
        assert len(webhooks) == 1
        assert webhooks[0].name == "item_created"

    def test_no_webhooks(self):
        doc = {"paths": {}}
        webhooks = convert_webhooks(doc, {})
        assert webhooks == []

    def test_webhook_description(self):
        doc = {
            "webhooks": {
                "test": {
                    "post": {
                        "summary": "A test webhook.",
                        "requestBody": {
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {},
                                    }
                                }
                            }
                        },
                        "responses": {"200": {"description": "OK"}},
                    }
                }
            }
        }
        webhooks = convert_webhooks(doc, {})
        assert webhooks[0].description == "A test webhook."

    def test_webhook_triggers(self):
        doc = {
            "webhooks": {
                "alert": {
                    "post": {
                        "summary": "Alert.",
                        "x-triggers": ["When threshold exceeded", "When error detected"],
                        "requestBody": {
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "message": {"type": "string"},
                                        },
                                    }
                                }
                            }
                        },
                        "responses": {"200": {"description": "OK"}},
                    }
                }
            }
        }
        webhooks = convert_webhooks(doc, {})
        assert len(webhooks[0].triggers) == 2

    def test_webhook_path_generated(self):
        doc = {
            "webhooks": {
                "invoice_paid": {
                    "post": {
                        "summary": "Paid.",
                        "requestBody": {
                            "content": {
                                "application/json": {
                                    "schema": {"type": "object", "properties": {}}
                                }
                            }
                        },
                        "responses": {"200": {"description": "OK"}},
                    }
                }
            }
        }
        webhooks = convert_webhooks(doc, {})
        assert "/webhooks/" in webhooks[0].path
