-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_resource_kind.py
More file actions
244 lines (209 loc) · 8.78 KB
/
Copy pathtest_resource_kind.py
File metadata and controls
244 lines (209 loc) · 8.78 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
"""Tests for the resource kind (CAP-001 + CAP-002)."""
from pathlib import Path
from capacium.manifest import Manifest
from capacium.models import Kind, Capability
from capacium.commands.init import VALID_KINDS, _validate_kind
FIXTURES_DIR = Path(__file__).parent / "fixtures"
class TestResourceManifestFixtures:
def test_load_resource_minimal(self):
m = Manifest.load(FIXTURES_DIR / "resource-minimal.yaml")
assert m.kind == "resource"
assert m.name == "test-prompt-library"
assert m.version == "1.0.0"
assert m.description == "A collection of prompt templates for code review"
def test_load_resource_full(self):
m = Manifest.load(FIXTURES_DIR / "resource-full.yaml")
assert m.kind == "resource"
assert m.name == "test-dataset"
assert m.version == "2.0.0"
assert m.description == "Training dataset for sentiment analysis"
assert m.author == "test-org"
assert m.license == "MIT"
assert m.keywords == ["dataset", "nlp", "sentiment"]
def test_resource_kind_accepted(self):
m = Manifest(kind="resource", name="my-resource", version="1.0.0", description="A resource")
assert m.kind == "resource"
errors = m.validate()
assert errors == []
def test_resource_no_mcp_required(self):
m = Manifest(kind="resource", name="my-resource", version="1.0.0", description="A resource")
errors = m.validate()
# Should NOT contain any MCP-related errors
assert not any("mcp" in e.lower() for e in errors)
assert not any("transport" in e.lower() for e in errors)
def test_resource_requires_description(self):
m = Manifest(kind="resource", name="my-resource", version="1.0.0", description="")
errors = m.validate()
assert "Resource manifest requires a description" in errors
def test_resource_with_description_passes(self):
m = Manifest(kind="resource", name="my-resource", version="1.0.0", description="Has a description")
errors = m.validate()
assert "Resource manifest requires a description" not in errors
class TestResourceKindEnum:
def test_resource_in_kind_enum(self):
assert Kind.RESOURCE.value == "resource"
def test_all_kinds_include_resource(self):
kinds = {k.value for k in Kind}
assert "resource" in kinds
def test_capability_with_resource_kind(self):
cap = Capability(owner="test", name="my-res", version="1.0.0", kind=Kind.RESOURCE)
assert cap.kind == Kind.RESOURCE
d = cap.to_dict()
assert d["kind"] == "resource"
def test_capability_from_dict_resource(self):
d = {
"owner": "test",
"name": "my-res",
"version": "1.0.0",
"kind": "resource",
"fingerprint": "",
"install_path": "",
"installed_at": "",
}
cap = Capability.from_dict(d)
assert cap.kind == Kind.RESOURCE
class TestResourceInCLIInit:
def test_resource_in_valid_kinds(self):
assert "resource" in VALID_KINDS
def test_validate_kind_accepts_resource(self):
assert _validate_kind("resource") is None
def test_validate_kind_rejects_invalid(self):
assert _validate_kind("invalid-kind") is not None
class TestResourceSchema:
"""CAP-002: Resource kind 5-layer progressive schema."""
def test_load_resource_standard(self):
m = Manifest.load(FIXTURES_DIR / "resource-standard.yaml")
assert m.kind == "resource"
assert m.name == "test-config-templates"
assert m.resource_type == "config-template"
assert m.resource_format == "yaml"
assert m.access == {"method": "file", "path": "templates/"}
errors = m.validate()
assert errors == []
def test_load_resource_full_layers(self):
m = Manifest.load(FIXTURES_DIR / "resource-full.yaml")
assert m.kind == "resource"
assert m.name == "test-dataset"
assert m.resource_type == "dataset"
assert m.resource_format == "parquet"
assert m.size_hint == "medium"
assert m.access == {"method": "git-submodule", "path": "data/sentiment/"}
assert m.compatibility == {
"frameworks": ["claude-code", "cursor"],
"min_version": "1.0.0",
}
errors = m.validate()
assert errors == []
def test_invalid_resource_type_rejected(self):
m = Manifest(
kind="resource",
name="bad-type",
version="1.0.0",
description="Has bad resource_type",
resource_type="magic-beans",
)
errors = m.validate()
assert any("Invalid resource_type: magic-beans" in e for e in errors)
def test_invalid_resource_format_rejected(self):
m = Manifest(
kind="resource",
name="bad-format",
version="1.0.0",
description="Has bad format",
resource_format="xlsx",
)
errors = m.validate()
assert any("Invalid resource format: xlsx" in e for e in errors)
def test_invalid_size_hint_rejected(self):
m = Manifest(
kind="resource",
name="bad-size",
version="1.0.0",
description="Has bad size hint",
size_hint="enormous",
)
errors = m.validate()
assert any("Invalid size_hint: enormous" in e for e in errors)
def test_resource_no_mcp_fields_required(self):
m = Manifest(
kind="resource",
name="no-mcp",
version="1.0.0",
description="Resource without MCP",
resource_type="prompt-library",
resource_format="yaml",
size_hint="small",
)
errors = m.validate()
assert errors == []
assert m.mcp == {}
assert m.entrypoint == ""
def test_valid_resource_types_accepted(self):
for rt in ("prompt-library", "dataset", "config-template",
"model-weights", "tool-index", "embedding"):
m = Manifest(
kind="resource",
name="rt-test",
version="1.0.0",
description="Testing resource type",
resource_type=rt,
)
errors = m.validate()
assert not any("resource_type" in e for e in errors), f"resource_type {rt} should be valid"
def test_valid_formats_accepted(self):
for fmt in ("yaml", "json", "csv", "parquet", "binary", "directory"):
m = Manifest(
kind="resource",
name="fmt-test",
version="1.0.0",
description="Testing format",
resource_format=fmt,
)
errors = m.validate()
assert not any("resource format" in e for e in errors), f"format {fmt} should be valid"
def test_valid_size_hints_accepted(self):
for sz in ("small", "medium", "large"):
m = Manifest(
kind="resource",
name="sz-test",
version="1.0.0",
description="Testing size hint",
size_hint=sz,
)
errors = m.validate()
assert not any("size_hint" in e for e in errors), f"size_hint {sz} should be valid"
def test_resource_fields_none_by_default(self):
m = Manifest(kind="resource", name="defaults", version="1.0.0", description="Defaults")
assert m.resource_type is None
assert m.resource_format is None
assert m.size_hint is None
assert m.access is None
assert m.compatibility is None
def test_resource_validation_skips_for_non_resource(self):
"""Resource-specific validation should not fire for kind=skill."""
m = Manifest(kind="skill", name="a-skill", version="1.0.0", description="")
errors = m.validate()
assert not any("resource_type" in e for e in errors)
assert not any("resource format" in e for e in errors)
assert not any("size_hint" in e for e in errors)
class TestCapInitKindResource:
def test_cap_init_kind_resource_produces_valid_manifest(self, tmp_path):
import subprocess
result = subprocess.run(
[
"python3", "-m", "capacium.cli", "init",
"--name", "test-init-res",
"--kind", "resource",
"--version", "1.0.0",
"--description", "A test resource from cap init",
],
capture_output=True,
text=True,
cwd=str(tmp_path),
)
assert result.returncode == 0, f"cap init failed: {result.stderr}"
m = Manifest.load(tmp_path / "capability.yaml")
assert m.kind == "resource"
assert m.name == "test-init-res"
errors = m.validate()
assert errors == []