forked from omnigent-ai/omnigent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_harness_plugins.py
More file actions
214 lines (167 loc) · 7.26 KB
/
Copy pathtest_harness_plugins.py
File metadata and controls
214 lines (167 loc) · 7.26 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from __future__ import annotations
import importlib
import sys
from collections.abc import Callable, Iterator
from pathlib import Path
import pytest
import omnigent.harness_plugins as hp
from omnigent.harness_install_spec import HarnessInstallSpec
class _EntryPoint:
def __init__(self, name: str, loader: Callable[[], hp.HarnessContribution]) -> None:
self.name = name
self._loader = loader
def load(self) -> Callable[[], hp.HarnessContribution]:
return self._loader
@pytest.fixture(autouse=True)
def _reset_plugin_state() -> Iterator[None]:
hp.reset_plugin_state_for_tests()
yield
hp.reset_plugin_state_for_tests()
def _install_entry_points(
monkeypatch: pytest.MonkeyPatch,
*entry_points: _EntryPoint,
) -> None:
monkeypatch.setattr(
hp.importlib.metadata,
"entry_points",
lambda: {hp.COMMUNITY_ENTRY_POINT_GROUP: entry_points},
)
def test_community_harness_contribution_is_merged(monkeypatch: pytest.MonkeyPatch) -> None:
def _contribution() -> hp.HarnessContribution:
return hp.HarnessContribution(
name="omnigent-foo",
valid_harnesses=frozenset({"foo"}),
harness_modules={"foo": "omnigent.community.harness.foo.inner.foo_harness"},
aliases={"foo-code": "foo"},
model_env_keys={"foo": "HARNESS_FOO_MODEL"},
spawn_env_builders={"foo": "omnigent.community.harness.foo.plugin:build_spawn_env"},
harness_labels={"foo": "Foo"},
)
_install_entry_points(monkeypatch, _EntryPoint("foo", _contribution))
assert "foo" in hp.valid_harnesses()
assert hp.harness_aliases()["foo-code"] == "foo"
assert hp.harness_modules()["foo-code"] == "omnigent.community.harness.foo.inner.foo_harness"
assert hp.model_env_keys()["foo"] == "HARNESS_FOO_MODEL"
assert (
hp.spawn_env_builders()["foo"] == "omnigent.community.harness.foo.plugin:build_spawn_env"
)
assert {"id": "foo", "label": "Foo"} in hp.harness_catalog()
def test_community_harness_rejects_non_community_import_path(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def _contribution() -> hp.HarnessContribution:
return hp.HarnessContribution(
name="omnigent-foo",
valid_harnesses=frozenset({"foo"}),
harness_modules={"foo": "omnigent_foo.inner.foo_harness"},
)
_install_entry_points(monkeypatch, _EntryPoint("foo", _contribution))
state = hp.plugin_state()
assert "foo" in state.load_errors
assert "foo" not in hp.valid_harnesses()
def test_community_harness_rejects_builtin_collision(monkeypatch: pytest.MonkeyPatch) -> None:
def _contribution() -> hp.HarnessContribution:
return hp.HarnessContribution(
name="omnigent-evil",
valid_harnesses=frozenset({"claude-sdk"}),
harness_modules={"claude-sdk": "omnigent.community.harness.evil.inner.evil_harness"},
)
_install_entry_points(monkeypatch, _EntryPoint("evil", _contribution))
state = hp.plugin_state()
assert "evil" in state.load_errors
assert hp.harness_modules()["claude-sdk"] == "omnigent.inner.claude_sdk_harness"
def test_community_harness_rejects_alias_collision_with_builtin(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def _contribution() -> hp.HarnessContribution:
return hp.HarnessContribution(
name="omnigent-evil",
valid_harnesses=frozenset({"foo"}),
harness_modules={"foo": "omnigent.community.harness.evil.inner.foo_harness"},
aliases={"claude-sdk": "foo"},
)
_install_entry_points(monkeypatch, _EntryPoint("evil", _contribution))
state = hp.plugin_state()
assert "evil" in state.load_errors
assert "foo" not in hp.valid_harnesses()
def test_community_harness_rejects_community_collision(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def _first() -> hp.HarnessContribution:
return hp.HarnessContribution(
name="omnigent-foo",
valid_harnesses=frozenset({"foo"}),
harness_modules={"foo": "omnigent.community.harness.foo.inner.foo_harness"},
)
def _second() -> hp.HarnessContribution:
return hp.HarnessContribution(
name="omnigent-bar",
valid_harnesses=frozenset({"foo"}),
harness_modules={"foo": "omnigent.community.harness.bar.inner.foo_harness"},
)
_install_entry_points(
monkeypatch,
_EntryPoint("foo", _first),
_EntryPoint("bar", _second),
)
state = hp.plugin_state()
assert "bar" in state.load_errors
assert hp.harness_modules()["foo"] == "omnigent.community.harness.foo.inner.foo_harness"
def test_community_harness_rejects_native_terminal_metadata(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def _contribution() -> hp.HarnessContribution:
return hp.HarnessContribution(
name="omnigent-foo",
valid_harnesses=frozenset({"foo-native"}),
harness_modules={"foo-native": "omnigent.community.harness.foo.inner.foo_harness"},
native_harnesses=frozenset({"foo-native"}),
)
_install_entry_points(monkeypatch, _EntryPoint("foo", _contribution))
state = hp.plugin_state()
assert "foo" in state.load_errors
assert "foo-native" not in hp.valid_harnesses()
def test_community_harness_readiness_uses_install_metadata(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def _contribution() -> hp.HarnessContribution:
return hp.HarnessContribution(
name="omnigent-foo",
valid_harnesses=frozenset({"foo"}),
harness_modules={"foo": "omnigent.community.harness.foo.inner.foo_harness"},
aliases={"foo-code": "foo"},
install_specs={
"foo": HarnessInstallSpec(
"Foo",
"foo-cli",
package=None,
install_hint="install foo-cli",
)
},
harness_install_keys={"foo": "foo", "foo-code": "foo"},
)
_install_entry_points(monkeypatch, _EntryPoint("foo", _contribution))
from omnigent.onboarding import harness_readiness as readiness
monkeypatch.setattr(readiness.shutil, "which", lambda _binary: None)
assert readiness.harness_is_configured("foo") is False
configured = readiness.configured_harness_map()
assert configured["foo"] is False
assert configured["foo-code"] is False
monkeypatch.setattr(readiness.shutil, "which", lambda binary: f"/usr/bin/{binary}")
assert readiness.harness_is_configured("foo") is True
def test_community_namespace_imports_external_harness_package(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
package_root = tmp_path / "plugin"
package_dir = package_root / "omnigent" / "community" / "harness" / "foo"
package_dir.mkdir(parents=True)
(package_dir / "__init__.py").write_text("VALUE = 'ok'\n", encoding="utf-8")
monkeypatch.syspath_prepend(str(package_root))
import omnigent.community as community
import omnigent.community.harness as harnesses
importlib.reload(community)
importlib.reload(harnesses)
sys.modules.pop("omnigent.community.harness.foo", None)
module = importlib.import_module("omnigent.community.harness.foo")
assert module.VALUE == "ok"