-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_validator.py
More file actions
247 lines (206 loc) · 6.34 KB
/
test_validator.py
File metadata and controls
247 lines (206 loc) · 6.34 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""Tests for core validator."""
import json
import pytest
from pathlib import Path
from jsonagents.validator import Validator, validate_manifest, ValidationResult
def test_validator_init():
"""Test validator initialization."""
validator = Validator()
assert validator is not None
assert validator.uri_validator is not None
assert validator.policy_validator is not None
def test_validate_minimal_manifest():
"""Test validation of minimal valid manifest."""
manifest = {
"manifest_version": "1.0",
"profiles": ["core"],
"agent": {
"id": "ajson://example.com/agents/test",
"name": "Test Agent",
"version": "1.0.0"
},
"capabilities": [
{
"id": "echo",
"description": "Echo service"
}
],
"modalities": {
"input": ["text"],
"output": ["text"]
}
}
validator = Validator()
result = validator.validate(manifest)
assert result.is_valid
assert len(result.errors) == 0
def test_validate_missing_version():
"""Test validation fails without manifest_version."""
manifest = {
"profiles": ["core"],
"agent": {
"id": "ajson://example.com/agents/test",
"name": "Test Agent"
}
}
validator = Validator()
result = validator.validate(manifest)
assert not result.is_valid
assert any("manifest_version" in error.lower() for error in result.errors)
def test_validate_invalid_uri():
"""Test validation catches invalid ajson:// URI."""
manifest = {
"manifest_version": "1.0",
"profiles": ["core"],
"agent": {
"id": "ajson:invalid-uri",
"name": "Test Agent"
}
}
validator = Validator()
result = validator.validate(manifest)
assert not result.is_valid
assert any("uri" in error.lower() for error in result.errors)
def test_validate_invalid_policy():
"""Test validation catches invalid policy expression."""
manifest = {
"manifest_version": "1.0",
"profiles": ["core", "gov"],
"agent": {
"id": "ajson://example.com/agents/test",
"name": "Test Agent"
},
"capabilities": [
{
"id": "echo",
"description": "Echo service"
}
],
"modalities": {
"input": ["text"],
"output": ["text"]
},
"policies": [
{
"id": "test-policy",
"effect": "deny",
"action": "tool.call",
"where": "tool.type === 'http'" # Invalid operator
}
]
}
validator = Validator()
result = validator.validate(manifest)
assert not result.is_valid
assert any("===" in error for error in result.errors)
def test_validate_with_capabilities():
"""Test validation with capabilities."""
manifest = {
"manifest_version": "1.0",
"profiles": ["core"],
"agent": {
"id": "ajson://example.com/agents/test",
"name": "Test Agent",
"version": "1.0.0"
},
"capabilities": [
{
"id": "summarization",
"description": "Summarize text"
}
],
"modalities": {
"input": ["text"],
"output": ["text"]
}
}
validator = Validator()
result = validator.validate(manifest)
assert result.is_valid
assert len(result.warnings) == 0 # Should have capabilities now
def test_validate_warns_no_capabilities():
"""Test validation warns when no capabilities declared."""
manifest = {
"manifest_version": "1.0",
"profiles": ["core"],
"agent": {
"id": "ajson://example.com/agents/test",
"name": "Test Agent",
"version": "1.0.0"
},
"modalities": {
"input": ["text"],
"output": ["text"]
}
}
validator = Validator()
result = validator.validate(manifest)
assert result.is_valid
assert any("capabilities" in warning.lower() for warning in result.warnings)
def test_validate_strict_mode():
"""Test strict mode treats warnings as errors."""
manifest = {
"manifest_version": "1.0",
"profiles": ["core"],
"agent": {
"id": "ajson://example.com/agents/test",
"name": "Test Agent",
"version": "1.0.0"
},
"modalities": {
"input": ["text"],
"output": ["text"]
}
}
validator = Validator()
result = validator.validate(manifest, strict=True)
assert not result.is_valid
assert any("capabilities" in error.lower() for error in result.errors)
def test_validate_manifest_function():
"""Test convenience function."""
manifest = {
"manifest_version": "1.0",
"profiles": ["core"],
"agent": {
"id": "ajson://example.com/agents/test",
"name": "Test Agent",
"version": "1.0.0"
},
"capabilities": [
{
"id": "echo",
"description": "Echo service"
}
],
"modalities": {
"input": ["text"],
"output": ["text"]
}
}
result = validate_manifest(manifest)
assert result.is_valid
def test_validate_invalid_json_string():
"""Test validation of malformed JSON string."""
validator = Validator()
# Create temp file with invalid JSON
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
f.write("{invalid json")
temp_path = f.name
try:
result = validator.validate(temp_path)
assert not result.is_valid
assert any("json" in error.lower() for error in result.errors)
finally:
Path(temp_path).unlink()
def test_validation_result_str():
"""Test ValidationResult string representation."""
result = ValidationResult(
is_valid=False,
errors=["Error 1", "Error 2"],
warnings=["Warning 1"]
)
result_str = str(result)
assert "❌" in result_str
assert "Error 1" in result_str
assert "Warning 1" in result_str