import json
import os
import sys
import unittest

sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
import apimart_mcp as mcp


class McpTests(unittest.TestCase):
    def test_initialize(self):
        response = mcp.handle({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}})
        self.assertEqual(response["result"]["serverInfo"]["name"], "apimart-agent-toolkit")

    def test_lists_six_tools(self):
        response = mcp.handle({"jsonrpc": "2.0", "id": 2, "method": "tools/list"})
        self.assertEqual(len(response["result"]["tools"]), 6)

    def test_lists_full_discovery_catalog(self):
        result = mcp.call_tool("apimart_list_models", {})
        payload = json.loads(result["content"][0]["text"])
        self.assertEqual(payload["models"], 58)
        self.assertEqual(len(payload["items"]), 58)
        self.assertEqual(payload["guides"], 174)

    def test_finds_single_intent_guide(self):
        result = mcp.call_tool("apimart_find_guides", {"model": "wan-3-0", "intent": "pricing"})
        payload = json.loads(result["content"][0]["text"])
        self.assertEqual(payload["count"], 1)

    def test_generation_defaults_to_dry_run(self):
        result = mcp.call_tool("apimart_generate_video", {"prompt": "test"})
        payload = json.loads(result["content"][0]["text"])
        self.assertTrue(payload["dry_run"])


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