# -*- coding: utf-8 -*-
"""
test_routes_cache.py — 缓存管理路由测试

覆盖 routes/cache.py：get_cache_stats / clear_cache（均需 admin_required）
"""


class TestCacheStats:
    """GET /api/cache/stats"""

    def test_requires_login(self, client):
        """未登录返回 401"""
        r = client.get("/api/cache/stats")
        assert r.status_code == 401
        assert r.get_json()["error"]["code"] == 1004

    def test_requires_admin(self, auth_client):
        """普通用户返回 403"""
        r = auth_client.get("/api/cache/stats")
        assert r.status_code == 403
        assert r.get_json()["error"]["code"] == 1005

    def test_admin_success(self, admin_client):
        """管理员返回 200 + stats"""
        r = admin_client.get("/api/cache/stats")
        assert r.status_code == 200
        data = r.get_json()
        assert data["success"] is True
        assert "stats" in data
        assert "total_files" in data["stats"]


class TestCacheClear:
    """POST /api/cache/clear"""

    def test_requires_login(self, client):
        r = client.post("/api/cache/clear")
        assert r.status_code == 401
        assert r.get_json()["error"]["code"] == 1004

    def test_requires_admin(self, auth_client):
        r = auth_client.post("/api/cache/clear")
        assert r.status_code == 403
        assert r.get_json()["error"]["code"] == 1005

    def test_admin_success(self, admin_client):
        """管理员清空返回 200 + cleared_count"""
        r = admin_client.post("/api/cache/clear")
        assert r.status_code == 200
        data = r.get_json()
        assert data["success"] is True
        assert "cleared_count" in data
