|
| 1 | +import importlib |
| 2 | + |
| 3 | + |
| 4 | +def _get_decorator_module(): |
| 5 | + # Import the telemetry_decorator module from the Unity MCP server src |
| 6 | + mod = importlib.import_module("UnityMcpBridge.UnityMcpServer~.src.telemetry_decorator") |
| 7 | + return mod |
| 8 | + |
| 9 | + |
| 10 | +def test_subaction_extracted_from_keyword(monkeypatch): |
| 11 | + td = _get_decorator_module() |
| 12 | + |
| 13 | + captured = {} |
| 14 | + |
| 15 | + def fake_record_tool_usage(tool_name, success, duration_ms, error, sub_action=None): |
| 16 | + captured["tool_name"] = tool_name |
| 17 | + captured["success"] = success |
| 18 | + captured["error"] = error |
| 19 | + captured["sub_action"] = sub_action |
| 20 | + |
| 21 | + # Silence milestones/logging in test |
| 22 | + monkeypatch.setattr(td, "record_tool_usage", fake_record_tool_usage) |
| 23 | + monkeypatch.setattr(td, "record_milestone", lambda *a, **k: None) |
| 24 | + monkeypatch.setattr(td, "_decorator_log_count", 999) |
| 25 | + |
| 26 | + def dummy_tool(ctx, action: str, name: str = ""): |
| 27 | + return {"success": True, "name": name} |
| 28 | + |
| 29 | + wrapped = td.telemetry_tool("manage_scene")(dummy_tool) |
| 30 | + |
| 31 | + resp = wrapped(None, action="get_hierarchy", name="Sample") |
| 32 | + assert resp["success"] is True |
| 33 | + assert captured["tool_name"] == "manage_scene" |
| 34 | + assert captured["success"] is True |
| 35 | + assert captured["error"] is None |
| 36 | + assert captured["sub_action"] == "get_hierarchy" |
| 37 | + |
| 38 | + |
| 39 | +def test_subaction_extracted_from_positionals(monkeypatch): |
| 40 | + td = _get_decorator_module() |
| 41 | + |
| 42 | + captured = {} |
| 43 | + |
| 44 | + def fake_record_tool_usage(tool_name, success, duration_ms, error, sub_action=None): |
| 45 | + captured["tool_name"] = tool_name |
| 46 | + captured["sub_action"] = sub_action |
| 47 | + |
| 48 | + monkeypatch.setattr(td, "record_tool_usage", fake_record_tool_usage) |
| 49 | + monkeypatch.setattr(td, "record_milestone", lambda *a, **k: None) |
| 50 | + monkeypatch.setattr(td, "_decorator_log_count", 999) |
| 51 | + |
| 52 | + def dummy_tool(ctx, action: str, name: str = ""): |
| 53 | + return True |
| 54 | + |
| 55 | + wrapped = td.telemetry_tool("manage_scene")(dummy_tool) |
| 56 | + |
| 57 | + _ = wrapped(None, "save", "MyScene") |
| 58 | + assert captured["tool_name"] == "manage_scene" |
| 59 | + assert captured["sub_action"] == "save" |
| 60 | + |
| 61 | + |
| 62 | +def test_subaction_none_when_not_present(monkeypatch): |
| 63 | + td = _get_decorator_module() |
| 64 | + |
| 65 | + captured = {} |
| 66 | + |
| 67 | + def fake_record_tool_usage(tool_name, success, duration_ms, error, sub_action=None): |
| 68 | + captured["tool_name"] = tool_name |
| 69 | + captured["sub_action"] = sub_action |
| 70 | + |
| 71 | + monkeypatch.setattr(td, "record_tool_usage", fake_record_tool_usage) |
| 72 | + monkeypatch.setattr(td, "record_milestone", lambda *a, **k: None) |
| 73 | + monkeypatch.setattr(td, "_decorator_log_count", 999) |
| 74 | + |
| 75 | + def dummy_tool_without_action(ctx, name: str): |
| 76 | + return 123 |
| 77 | + |
| 78 | + wrapped = td.telemetry_tool("apply_text_edits")(dummy_tool_without_action) |
| 79 | + _ = wrapped(None, name="X") |
| 80 | + assert captured["tool_name"] == "apply_text_edits" |
| 81 | + assert captured["sub_action"] is None |
| 82 | + |
| 83 | + |
0 commit comments