import sys as _sys
from pathlib import Path as _Path
_sys.path.insert(0, str(_Path(__file__).resolve().parent))
from mutation_profile_fixture import shared_mutation_env  # noqa: E402
"""Regression tests for non-list API payloads on PR/issue comment listing (#485)."""
import os
import sys
import unittest
from unittest.mock import patch

sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent.parent))

from mcp_server import (  # noqa: E402
    _list_pr_lease_comments,
    gitea_list_issue_comments,
)

FAKE_AUTH = "Basic dGVzdDp0ZXN0"
AUTHOR_ENV = {
    "GITEA_PROFILE_NAME": "gitea-author",
    "GITEA_ALLOWED_OPERATIONS": "gitea.read,gitea.issue.comment",
}


class TestPrLeaseCommentsNonListGuard(unittest.TestCase):
    @patch("mcp_server.api_request")
    @patch("mcp_server.get_auth_header", return_value=FAKE_AUTH)
    def test_list_pr_lease_comments_non_list_payload_returns_empty(self, _auth, mock_api):
        mock_api.return_value = {"message": "Unauthorized"}
        result = _list_pr_lease_comments(
            12, remote="prgs", host=None, org=None, repo=None)
        self.assertEqual(result, [])
        # Single comments GET only — no pre-flight PR lookup (#519 isolation).
        mock_api.assert_called_once()
        _method, url, _auth_arg = mock_api.call_args[0][:3]
        self.assertIn("/issues/12/comments", url)
        self.assertNotIn("/pulls/", url)

    @patch("mcp_server.api_request")
    @patch("mcp_server.get_auth_header", return_value=FAKE_AUTH)
    def test_list_pr_lease_comments_none_returns_empty(self, _auth, mock_api):
        mock_api.return_value = None
        result = _list_pr_lease_comments(
            12, remote="prgs", host=None, org=None, repo=None)
        self.assertEqual(result, [])

    @patch("mcp_server.api_request")
    @patch("mcp_server.get_auth_header", return_value=FAKE_AUTH)
    def test_list_pr_lease_comments_list_payload_unchanged(self, _auth, mock_api):
        comment = {"id": 7, "body": "<!-- mcp-review-lease:v1 -->"}
        mock_api.return_value = [comment]
        result = _list_pr_lease_comments(
            12, remote="prgs", host=None, org=None, repo=None, limit=5)
        self.assertEqual(result, [comment])

    @patch("mcp_server.api_request")
    @patch("mcp_server.get_auth_header", return_value=FAKE_AUTH)
    def test_list_issue_comments_non_list_payload_returns_empty(self, _auth, mock_api):
        mock_api.return_value = {"message": "Unauthorized"}
        with patch.dict(os.environ, AUTHOR_ENV, clear=True):
            result = gitea_list_issue_comments(issue_number=9, remote="prgs")
        self.assertTrue(result["success"])
        self.assertEqual(result["comments"], [])

    @patch("mcp_server.api_request")
    @patch("mcp_server.get_auth_header", return_value=FAKE_AUTH)
    def test_list_issue_comments_list_payload_unchanged(self, _auth, mock_api):
        mock_api.return_value = [
            {
                "id": 101,
                "user": {"login": "alice"},
                "body": "hello",
                "created_at": "2026-07-03T00:00:00Z",
                "updated_at": "2026-07-03T01:00:00Z",
            }
        ]
        with patch.dict(os.environ, AUTHOR_ENV, clear=True):
            result = gitea_list_issue_comments(issue_number=9, remote="prgs")
        self.assertTrue(result["success"])
        self.assertEqual(len(result["comments"]), 1)
        self.assertEqual(result["comments"][0]["author"], "alice")


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