"""Form tests for the MCP token dashboard."""

from django.contrib.auth import get_user_model
from django.test import TestCase

from mcp_server.forms import UserTokenCreateForm, UserTokenEditForm
from mcp_server.models import UserToken

User = get_user_model()


class CreateFormTest(TestCase):
    def test_required_fields(self):
        form = UserTokenCreateForm(data={})
        self.assertFalse(form.is_valid())
        self.assertIn("name", form.errors)

    def test_name_only_is_valid(self):
        form = UserTokenCreateForm(data={"name": "Test"})
        self.assertTrue(form.is_valid(), form.errors)

    def test_tool_choices_match_registered_tools(self):
        form = UserTokenCreateForm()
        choices = {value for value, _ in form.fields["allowed_tools"].choices}
        # These five must always be present per the FastMCP server.
        for expected in {"search", "get_chunk", "list_libraries", "list_collections", "list_items"}:
            self.assertIn(expected, choices)


class EditFormTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username="alice", password="pw")
        self.token, _ = UserToken.objects.create_token(
            user=self.user, name="t", allowed_tools=["search"]
        )

    def test_initial_allowed_tools_populated(self):
        form = UserTokenEditForm(instance=self.token)
        self.assertEqual(form.fields["allowed_tools"].initial, ["search"])

    def test_save_updates_metadata(self):
        form = UserTokenEditForm(
            data={
                "name": "Renamed",
                "is_active": False,
                "expires_at": "",
                "allowed_tools": ["search", "get_chunk"],
            },
            instance=self.token,
        )
        self.assertTrue(form.is_valid(), form.errors)
        instance = form.save(commit=False)
        instance.allowed_tools = form.cleaned_data["allowed_tools"]
        instance.save()
        self.token.refresh_from_db()
        self.assertEqual(self.token.name, "Renamed")
        self.assertFalse(self.token.is_active)
        self.assertEqual(self.token.allowed_tools, ["search", "get_chunk"])
