"""Fast provider-boundary tests that do not require Django or Mollie access."""

import unittest

from .provider import (
    MollieConfigurationError,
    MollieResponseError,
    checkout_url,
    format_amount,
    format_component_amount,
    payment_status,
    validate_api_key,
)
from .webhooks import event_fingerprint, notification_reference


class MollieProviderContractTests(unittest.TestCase):
    def test_amount_is_exact_and_currency_is_normalised(self):
        self.assertEqual(
            format_amount("10.005", "eur"), {"currency": "EUR", "value": "10.01"}
        )

    def test_invalid_amount_and_currency_are_rejected(self):
        with self.assertRaises(MollieConfigurationError):
            format_amount("0", "EUR")
        with self.assertRaises(MollieConfigurationError):
            format_amount("1", "EURO")
        self.assertEqual(
            format_component_amount("0", "EUR"),
            {"currency": "EUR", "value": "0.00"},
        )

    def test_classic_webhook_reference_and_fingerprint_are_stable(self):
        payment = {
            "id": "tr_example",
            "status": "paid",
            "amountRefunded": {"value": "0.00"},
        }
        self.assertEqual(notification_reference({"id": "tr_example"}), "tr_example")
        self.assertEqual(event_fingerprint(payment), event_fingerprint(payment))
        with self.assertRaises(MollieResponseError):
            notification_reference({"id": "unknown"})

    def test_checkout_url_and_status_require_documented_response_data(self):
        self.assertEqual(payment_status({"status": "paid"}), "paid")
        self.assertEqual(
            checkout_url(
                {"_links": {"checkout": {"href": "https://checkout.example"}}}
            ),
            "https://checkout.example",
        )
        with self.assertRaises(MollieResponseError):
            checkout_url({"_links": {}})

    def test_key_shape_is_validated_without_exposing_the_key(self):
        self.assertEqual(validate_api_key("test_example"), "test_example")
        with self.assertRaises(MollieConfigurationError):
            validate_api_key("CHANGE_ME")
