from __future__ import annotations

import unittest

from api_access import required_role


class ApiAccessPolicyTests(unittest.TestCase):
    def test_member_workspaces_are_consistently_protected(self):
        cases = {
            ("GET", "/api/screener/setup"): "member",
            ("GET", "/api/screener/tracking"): "member",
            ("GET", "/api/mentors/messages"): "member",
            ("GET", "/api/heaven/setup"): "member",
            ("GET", "/api/heaven/readings"): "member",
            ("GET", "/api/assistant/messages"): "member",
            ("POST", "/api/screener/run"): "member",
            ("POST", "/api/screener/tracking"): "member",
            ("POST", "/api/screener/tracking/refresh"): "member",
            ("POST", "/api/mentors/chat"): "member",
            ("POST", "/api/mentors/preferences"): "member",
            ("POST", "/api/heaven/interpret"): "member",
            ("POST", "/api/assistant/chat"): "member",
            ("DELETE", "/api/screener/strategies/42"): "member",
            ("DELETE", "/api/screener/tracking/42"): "member",
            ("DELETE", "/api/mentors/messages"): "member",
            ("DELETE", "/api/assistant/messages"): "member",
            ("DELETE", "/api/heaven/readings/42"): "member",
        }
        for (method, path), role in cases.items():
            with self.subTest(method=method, path=path):
                self.assertEqual(required_role(method, path), role)

    def test_shared_knowledge_mutations_require_admin(self):
        cases = (
            ("POST", "/api/reasons"),
            ("POST", "/api/seat-aliases"),
            ("POST", "/api/heaven/sector-phases"),
            ("DELETE", "/api/heaven/sector-phases/油气开采"),
            ("POST", "/api/backfill"),
            ("GET", "/api/admin/settings"),
        )
        for method, path in cases:
            with self.subTest(method=method, path=path):
                self.assertEqual(required_role(method, path), "admin")

    def test_personal_market_data_routes_need_login_only(self):
        cases = (
            ("GET", "/api/dashboard"),
            ("GET", "/api/watchlist"),
            ("POST", "/api/notes"),
            ("DELETE", "/api/notes/3"),
        )
        for method, path in cases:
            with self.subTest(method=method, path=path):
                self.assertEqual(required_role(method, path), "authenticated")


if __name__ == "__main__":
    unittest.main()
