-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_invoke_internal_mcp_tool.py
More file actions
119 lines (93 loc) · 3.56 KB
/
Copy pathtest_invoke_internal_mcp_tool.py
File metadata and controls
119 lines (93 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from __future__ import annotations
import importlib.util
from dataclasses import dataclass
from pathlib import Path
from typing import Sequence
PROJECT_ROOT = Path(__file__).resolve().parents[1]
SCRIPT_PATH = PROJECT_ROOT / "utilities" / "invoke_internal_mcp_tool.py"
def _load_module():
spec = importlib.util.spec_from_file_location(
"invoke_internal_mcp_tool_under_test",
SCRIPT_PATH,
)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def test_plain_python_delegates_internal_mcp_helper_through_pdm():
module = _load_module()
captured: dict[str, object] = {}
@dataclass
class _Completed:
returncode: int
def fake_runner(args: Sequence[str], *, cwd: Path) -> _Completed:
captured["args"] = list(args)
captured["cwd"] = cwd
return _Completed(returncode=7)
returncode = module._maybe_delegate_via_pdm(
["jira_get_myself"],
environ={},
prefix=str(PROJECT_ROOT.parent / "foreign-python"),
which=lambda name: "pdm.exe" if name == "pdm" else None,
runner=fake_runner,
)
assert returncode == 7
assert captured["args"] == [
"pdm.exe",
"run",
"python",
str(SCRIPT_PATH),
"jira_get_myself",
]
assert captured["cwd"] == module.PROJECT_ROOT
def test_internal_mcp_helper_does_not_delegate_inside_project_environment():
module = _load_module()
def fail_runner(args: Sequence[str], *, cwd: Path) -> object: # pragma: no cover
raise AssertionError(f"unexpected runner: {list(args)} {cwd}")
returncode = module._maybe_delegate_via_pdm(
["jira_get_myself"],
environ={"PDM_PROJECT_ROOT": str(PROJECT_ROOT)},
prefix="C:/Python311",
which=lambda name: "pdm.exe",
runner=fail_runner,
)
assert returncode is None
def test_internal_mcp_helper_pdm_delegation_can_be_disabled():
module = _load_module()
def fail_runner(args: Sequence[str], *, cwd: Path) -> object: # pragma: no cover
raise AssertionError(f"unexpected runner: {list(args)} {cwd}")
returncode = module._maybe_delegate_via_pdm(
["jira_get_myself"],
environ={"VON_INTERNAL_MCP_TOOL_NO_PDM_DELEGATE": "1"},
prefix="C:/Python311",
which=lambda name: "pdm.exe",
runner=fail_runner,
)
assert returncode is None
def test_internal_mcp_helper_applies_external_model_dotenv_override_keys():
module = _load_module()
captured: dict[str, object] = {}
def fake_apply(keys: Sequence[str]) -> dict[str, str]:
captured["keys"] = tuple(keys)
return {"OPENAI_API_KEY": "sk-test"}
applied = module._apply_internal_mcp_dotenv_overrides(fake_apply)
assert applied == {"OPENAI_API_KEY": "sk-test"}
keys = captured["keys"]
assert isinstance(keys, tuple)
assert "OPENAI_API_KEY" in keys
assert "OPENAI_API_KEY_FILE" in keys
assert "OPENAI_API_BACKUP_KEY" in keys
assert "OPENAI_API_BACKUP_KEY_FILE" in keys
assert "VON_DEFAULT_OPENAI_MODEL" in keys
assert "GEMINI_API_KEY" in keys
assert "GEMINI_API_KEY_FILE" in keys
assert "GEMINI_API_BACKUP_KEY" in keys
assert "GEMINI_API_BACKUP_KEY_FILE" in keys
assert "GOOGLE_API_KEY" in keys
assert "GOOGLE_API_KEY_FILE" in keys
assert "GOOGLE_API_BACKUP_KEY" in keys
assert "GOOGLE_API_BACKUP_KEY_FILE" in keys
assert "VON_DEFAULT_GEMINI_MODEL" in keys
assert "META_API_KEY" in keys
assert "META_API_KEY_FILE" in keys