import subprocess
import sys
import unittest
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
SKILL = ROOT / "skills" / "redmine-communicator"
SETUP = SKILL / "scripts" / "setup_redmcp.py"


class RedmineCommunicatorSkillTest(unittest.TestCase):
    def test_skill_files_exist_and_reference_redmcp_safety_rules(self):
        skill_md = (SKILL / "SKILL.md").read_text()
        reference = (SKILL / "references" / "redmcp-tools.md").read_text()

        self.assertIn("redmine-communicator", skill_md)
        self.assertIn("redMCP", skill_md)
        self.assertIn("send_helpdesk_email=true", skill_md)
        self.assertIn("redmine_send_helpdesk_response", reference)
        self.assertIn("customer-visible", reference)

    def test_setup_script_dry_run_prints_stdio_config(self):
        result = subprocess.run(
            [
                sys.executable,
                str(SETUP),
                "--redmine-url",
                "http://redmine.example.test",
                "--redmine-api-key",
                "secret-key",
                "--transport",
                "stdio",
            ],
            cwd=ROOT,
            universal_newlines=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=False,
        )

        self.assertEqual(0, result.returncode, result.stderr)
        self.assertIn("mode=dry-run", result.stdout)
        self.assertIn("redmcp-server.php", result.stdout)
        self.assertNotIn("secret-key", result.stdout)


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