"""Tests for the extracted prompts package. The render() functions are pure (they take docs as a parameter, no module globals), so they're trivially unit-testable. We verify each one renders without raising both with and without a docs index. """ import json from pathlib import Path import pytest from mcp_cucm_axl import prompts from mcp_cucm_axl.docs_loader import DocsIndex @pytest.fixture def fake_docs(tmp_path: Path) -> DocsIndex: chunks = [ { "id": "cucm::v15::admin::Route-Plan::0", "text": "Route plan defines call routing.", "heading_path": ["Route Plan"], "source_path": str(tmp_path / "fake.md"), "product": "cucm", "version": "v15", "doc": "system-config-guide", }, { "id": "cucm::v15::admin::SIP-Trunk::0", "text": "SIP trunks connect CUCM to other call control entities.", "heading_path": ["SIP Trunk Configuration"], "source_path": str(tmp_path / "fake.md"), "product": "cucm", "version": "v15", "doc": "system-config-guide", }, ] (tmp_path / "chunks.jsonl").write_text( "\n".join(json.dumps(c) for c in chunks) ) (tmp_path / "index_meta.json").write_text( json.dumps({"model_name": "test", "embedding_dim": 384, "products": ["cucm"]}) ) idx = DocsIndex.load(tmp_path) assert idx is not None return idx # ---- route_plan_overview ---------------------------------------------------- def test_route_plan_overview_renders_with_docs(fake_docs): text = prompts.route_plan_overview.render(fake_docs) assert "Route Plan Overview" in text assert "axl_version" in text assert "route_partitions" in text def test_route_plan_overview_renders_without_docs(): # Graceful degradation — no docs index should still produce a usable prompt text = prompts.route_plan_overview.render(None) assert "Route Plan Overview" in text assert "cisco-docs index is not loaded" in text # ---- investigate_pattern --------------------------------------------------- def test_investigate_pattern_includes_pattern_in_output(fake_docs): text = prompts.investigate_pattern.render(fake_docs, "10.911", "CER911-PT") assert "10.911" in text assert "CER911-PT" in text def test_investigate_pattern_no_partition(fake_docs): text = prompts.investigate_pattern.render(fake_docs, "9.@") assert "9.@" in text # The partition clause should not appear when no partition is given assert "in partition" not in text # ---- audit_routing ---------------------------------------------------------- def test_audit_routing_default_focus(fake_docs): text = prompts.audit_routing.render(fake_docs) assert "focus: `full`" in text def test_audit_routing_custom_focus(fake_docs): text = prompts.audit_routing.render(fake_docs, "translations") assert "focus: `translations`" in text # ---- cucm_sql_help ---------------------------------------------------------- def test_cucm_sql_help_includes_question(fake_docs): q = "How do I find phones with no associated user?" text = prompts.cucm_sql_help.render(fake_docs, q) assert q in text assert "axl_describe_table" in text def test_cucm_sql_help_handles_empty_question(fake_docs): # No substantive keywords — must still render something useful text = prompts.cucm_sql_help.render(fake_docs, "x y z") assert "axl_list_tables" in text # ---- sip_trunk_report (NEW) ------------------------------------------------ def test_sip_trunk_report_includes_query_1(fake_docs): text = prompts.sip_trunk_report.render(fake_docs) # Query 1 essentials: device JOIN sipdevice, the trunk_name column, # and the WHERE filter on Trunk class assert "FROM device d" in text assert "JOIN sipdevice sd" in text assert "tc.name = 'Trunk'" in text def test_sip_trunk_report_includes_query_2_and_tools(fake_docs): text = prompts.sip_trunk_report.render(fake_docs) assert "siptrunkdestination" in text # And references the existing MCP tool for route group traversal assert "route_lists_and_groups()" in text def test_sip_trunk_report_with_name_filter_injects_safe_like(fake_docs): text = prompts.sip_trunk_report.render(fake_docs, "PSTN") # The filter should appear as a LIKE clause and the scope note should # reflect the narrowing assert "%PSTN%" in text assert "narrowed to trunks" in text def test_sip_trunk_report_name_filter_escaped(fake_docs): # SQL injection defense: a single-quote in the name_filter must be # doubled before going into the LIKE pattern text = prompts.sip_trunk_report.render(fake_docs, "O'Reilly") assert "O''Reilly" in text def test_sip_trunk_report_renders_without_docs(): text = prompts.sip_trunk_report.render(None) assert "SIP Trunk Report" in text assert "cisco-docs index is not loaded" in text # ---- registration smoke test ----------------------------------------------- def test_all_prompts_registered_in_server(): """Confirm each prompt module's render() is wired through a FastMCP shim in server.py. Catches the case where a new module is added but the shim wasn't (the prompt would be invisible to the LLM).""" import asyncio from mcp_cucm_axl import server async def _list(): registered = await server.mcp.list_prompts() return {p.name for p in registered} names = asyncio.run(_list()) assert names == { "route_plan_overview", "investigate_pattern", "audit_routing", "cucm_sql_help", "sip_trunk_report", }, f"unexpected prompt set: {names}"