test_garden_layer.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import os
  2. import pytest
  3. import requests
  4. from typing import Any, Dict
  5. from garden_layer import GardenLayer
  6. from garden_layer.config import CLONE_OF, GRAPH, KEROSENE_ROOT, MCP_URL
  7. CYCLE_2026_3 = "http://world.eu.org/example1#ProductionCycle_21d96b6c-179"
  8. FIXTURE_FILES = ["ganja_test_fixture.ttl"]
  9. def pytest_report_header(config):
  10. return "Garden layer tests: requires virtuoso_mcp running at GardenLayer.MCP_URL"
  11. @pytest.fixture
  12. def garden_layer():
  13. layer = GardenLayer()
  14. try:
  15. layer.traverse(KEROSENE_ROOT, CLONE_OF, direction="incoming", limit=1)
  16. except requests.RequestException as exc:
  17. pytest.skip(f"Cannot reach MCP server: {exc}")
  18. if os.getenv("MCP_ALLOW_EXAMPLE_LOAD", "false").lower() == "true":
  19. try:
  20. layer.load_examples(files=FIXTURE_FILES, graph=GRAPH)
  21. except Exception as exc:
  22. pytest.skip(f"Cannot load garden fixtures: {exc}")
  23. result = layer.traverse(KEROSENE_ROOT, CLONE_OF, direction="incoming", limit=1)
  24. bindings = result.get("results", {}).get("bindings", [])
  25. if not bindings:
  26. pytest.skip("No garden fixture data available; set MCP_ALLOW_EXAMPLE_LOAD=true")
  27. return layer
  28. def test_traverse_clone_tree(garden_layer):
  29. """Walk `cloneOf` incoming edges and ensure the root has clones to inspect."""
  30. result = garden_layer.traverse(KEROSENE_ROOT, CLONE_OF, direction="incoming", limit=5)
  31. assert isinstance(result, dict)
  32. bindings = result.get("results", {}).get("bindings", [])
  33. assert isinstance(bindings, list)
  34. assert bindings, "Expected at least one clone binding"
  35. print(f"Traverse of {KEROSENE_ROOT} returned {len(bindings)} clone candidates.")
  36. def test_describe_subject(garden_layer):
  37. """Describe the root plant and surface the top predicates the garden helpers can reuse."""
  38. summary = garden_layer.describe_subject(KEROSENE_ROOT, limit=5)
  39. assert isinstance(summary, dict)
  40. bindings = summary.get("results", {}).get("bindings", [])
  41. assert bindings, "Expected describe_subject to return bindings"
  42. triples = [f"{b['predicate']['value']} -> {b['object'].get('value', '<literal>')}" for b in bindings]
  43. print("describe_subject returned:")
  44. for triple in triples:
  45. print(" ", triple)
  46. def test_path_traverse_lineage(garden_layer):
  47. """Use the path helper to follow `cloneOf` and observe the lineage step."""
  48. path = garden_layer.path_traverse(KEROSENE_ROOT, [CLONE_OF], direction="incoming", limit=5)
  49. assert isinstance(path, dict)
  50. bindings = path.get("result", {}).get("results", {}).get("bindings", [])
  51. assert isinstance(bindings, list)
  52. assert bindings, "Path traverse should find at least one step"
  53. print(f"Property path {CLONE_OF} produced {len(bindings)} step bindings.")
  54. def test_property_usage_statistics(garden_layer):
  55. """Summarize how the `cloneOf` property is used so future helpers can rely on frequency data."""
  56. stats = garden_layer.property_usage_statistics(CLONE_OF, examples_limit=3)
  57. assert isinstance(stats, dict)
  58. count_bindings = stats.get("count", {}).get("results", {}).get("bindings", [])
  59. assert count_bindings, "Expected usage count bindings"
  60. usage_count = count_bindings[0].get("usageCount", {}).get("value")
  61. example_bindings = stats.get("examples", {}).get("results", {}).get("bindings", [])
  62. assert isinstance(example_bindings, list)
  63. print(f"cloneOf usage count: {usage_count}")
  64. print("example bindings:")
  65. for binding in example_bindings:
  66. subject = binding.get("subjectLabel", {}).get("value") or binding.get("subject", {}).get("value")
  67. object_ = binding.get("objectLabel", {}).get("value") or binding.get("object", {}).get("value")
  68. print(f" - {subject} -> {object_}")
  69. def test_batch_insert(garden_layer):
  70. """Batch-insert a TTL snippet and verify the query shape"""
  71. ttl = '<http://world.eu.org/example1#batch_test_subject> <http://www.w3.org/2000/01/rdf-schema#label> "garden batch" .'
  72. result = garden_layer.batch_insert(ttl=ttl, graph=GRAPH)
  73. assert isinstance(result, dict)
  74. assert "query" in result
  75. assert "INSERT DATA" in result.get("query", "").upper()
  76. print("batch_insert generated:")
  77. print(result.get("query", ""))
  78. def call_mcp_tool(tool_name: str, payload: Dict[str, Any]) -> Any:
  79. # Some ontology/traversal calls can be a bit slow depending on Virtuoso load.
  80. response = requests.post(MCP_URL, json={"tool": tool_name, "input": payload}, timeout=20)
  81. response.raise_for_status()
  82. body = response.json()
  83. assert body.get("status") == "ok", f"{tool_name} failed: {body.get('detail') or 'unknown reason'}"
  84. return body["result"]
  85. def test_mcp_garden_describe_subject():
  86. result = call_mcp_tool(
  87. "garden_describe_subject",
  88. {"subject_uri": KEROSENE_ROOT, "limit": 5},
  89. )
  90. bindings = result.get("results", {}).get("bindings", [])
  91. assert bindings, "garden_describe_subject should return bindings"
  92. print("garden_describe_subject returned", len(bindings), "bindings via /mcp")
  93. def test_mcp_garden_property_usage_statistics():
  94. result = call_mcp_tool(
  95. "garden_property_usage_statistics",
  96. {"property_uri": CLONE_OF, "examples_limit": 2},
  97. )
  98. count_bindings = result.get("count", {}).get("results", {}).get("bindings", [])
  99. assert count_bindings, "Expected usage count bindings from the garden tool"
  100. examples = result.get("examples", {}).get("results", {}).get("bindings", [])
  101. assert isinstance(examples, list)
  102. print("garden_property_usage_statistics sample bindings:", len(examples))
  103. def test_mcp_garden_cycle_plants():
  104. result = call_mcp_tool(
  105. "garden_cycle_plants",
  106. {"cycle_uri": CYCLE_2026_3, "limit": 5},
  107. )
  108. bindings = result.get("results", {}).get("bindings", [])
  109. assert bindings, "Expected at least one plant binding from the cycle"
  110. print("garden_cycle_plants returned", len(bindings), "plants for cycle", CYCLE_2026_3)
  111. for binding in bindings:
  112. plant_uri = binding.get("plant", {}).get("value")
  113. assert plant_uri, "Each binding should include a plant URI"
  114. def test_mcp_garden_cycle_plants_latest_when_cycle_uri_missing():
  115. """MCP compliance: cycle_uri should be optional and default to latest cycle."""
  116. result = call_mcp_tool(
  117. "garden_cycle_plants",
  118. {"limit": 3},
  119. )
  120. bindings = result.get("results", {}).get("bindings", [])
  121. assert bindings, "Expected plant bindings from the latest cycle when cycle_uri is omitted"
  122. def test_mcp_garden_cycle_list_detailed_has_strain_info():
  123. """Ensure the detailed cycle list includes strain labels/descriptions and clone siblings."""
  124. result = call_mcp_tool(
  125. "garden_cycle_list_detailed",
  126. {"limit": 10},
  127. )
  128. bindings = result.get("results", {}).get("bindings", [])
  129. assert bindings, "Expected at least one binding from garden_cycle_list_detailed"
  130. # Check that at least one binding has strainDesc or cloneSiblings populated.
  131. any_strain_desc = any('strainDesc' in b and b.get('strainDesc', {}).get('value') for b in bindings)
  132. any_siblings = any('cloneSiblings' in b and b.get('cloneSiblings', {}).get('value') for b in bindings)
  133. assert any_strain_desc or any_siblings, "Expected strainDesc and/or cloneSiblings to be present"
  134. def test_mcp_input_schema_announced_for_garden_tools():
  135. """OpenClaw MCP compliance: garden_* tools should expose inputSchema via tools/list."""
  136. response = requests.post(
  137. MCP_URL,
  138. json={"jsonrpc": "2.0", "id": 999, "method": "tools/list", "params": {}},
  139. timeout=10,
  140. )
  141. response.raise_for_status()
  142. body = response.json()
  143. assert body.get("result"), f"Expected tools/list result, got: {body}"
  144. tools = body["result"].get("tools") or []
  145. tool_by_name = {t.get("name"): t for t in tools if isinstance(t, dict)}
  146. # At least validate a couple of the “new” tools.
  147. for name in [
  148. "garden_clone_to",
  149. "garden_cycle_plants",
  150. "garden_cycle_list_detailed",
  151. ]:
  152. assert name in tool_by_name, f"Missing tool {name} in tools/list"
  153. input_schema = tool_by_name[name].get("inputSchema")
  154. assert isinstance(input_schema, dict), f"{name}.inputSchema missing/invalid"
  155. # Specifically: garden_cycle_plants should allow missing cycle_uri.
  156. cycle_plants_schema = tool_by_name["garden_cycle_plants"].get("inputSchema", {})
  157. props = cycle_plants_schema.get("properties", {})
  158. assert "cycle_uri" in props, "garden_cycle_plants inputSchema must define cycle_uri"
  159. def test_load_examples_fixture(garden_layer):
  160. if os.getenv("MCP_ALLOW_EXAMPLE_LOAD", "false").lower() != "true":
  161. pytest.skip("MCP_ALLOW_EXAMPLE_LOAD must be true to load example fixtures")
  162. result = garden_layer.load_examples(files=["test_fixture.ttl"], graph=GRAPH)
  163. loaded = result.get("loaded", [])
  164. assert any(item.get("file") == "test_fixture.ttl" for item in loaded), "Expected fixture file to be loaded"