"""Contract tests for api_error helper — Story 2.4 (AC: 5)."""
import json
import pytest
from app.utils.errors import api_error


class TestApiError:
    def test_content_type_is_json(self, app):
        with app.app_context():
            response = api_error("something went wrong", 400)
            assert response.content_type == 'application/json'

    def test_body_has_error_key(self, app):
        with app.app_context():
            response = api_error("not found", 404)
            data = json.loads(response.data)
            assert 'error' in data
            assert data['error'] == 'not found'

    def test_body_has_code_key(self, app):
        with app.app_context():
            response = api_error("not found", 404)
            data = json.loads(response.data)
            assert 'code' in data
            assert data['code'] == 404

    @pytest.mark.parametrize("code", [400, 404, 500])
    def test_http_status_matches_code_argument(self, app, code):
        with app.app_context():
            response = api_error("error", code)
            assert response.status_code == code
