-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconftest.py
More file actions
418 lines (336 loc) · 12.7 KB
/
Copy pathconftest.py
File metadata and controls
418 lines (336 loc) · 12.7 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""
Test configuration and fixtures for the GNN Processing Pipeline.
Phase 7: conftest was reduced from 545 lines to a focused set of project-
specific fixtures. Removed:
- pathlib._local PurePosixPath patch (unused, speculative)
- safe_to_fail marker auto-apply (Phase 7.1)
- Unused fixtures: full_pipeline_environment, simulate_failures,
capture_logs, pipeline_arguments
- RealRenderModule recovery fallback (in-tree imports always succeed)
"""
from __future__ import annotations
import sys
import tempfile
# Make "tests.*" an importable alias for the src/tests/ directory so that
# tests which do `from tests.conftest import X` continue to resolve.
import types as _types
from pathlib import Path
from typing import Any, Dict, Generator, cast
import pytest
_pkg = _types.ModuleType("tests")
_pkg.__path__ = [str(Path(__file__).parent)]
sys.modules.setdefault("tests", _pkg)
sys.modules["tests.conftest"] = sys.modules[__name__]
# -----------------------------------------------------------------------------
# Marker configuration
# -----------------------------------------------------------------------------
PYTEST_MARKERS: Dict[str, str] = {
"unit": "Unit tests for individual components",
"integration": "Integration tests for component interactions",
"performance": "Performance and resource usage tests",
"slow": "Tests that take significant time to complete",
"fast": "Quick tests for rapid feedback",
"destructive": "Tests that may modify system state",
"external": "Tests requiring external dependencies",
"core": "Core module tests",
"pipeline": "Pipeline infrastructure tests",
"recovery": "Pipeline recovery tests",
"utilities": "Utility function tests",
"environment": "Environment validation tests",
"render": "Rendering and code generation tests",
"export": "Export functionality tests",
"parsers": "Parser and format tests",
"main_orchestrator": "Main orchestrator tests",
"type_checking": "Type checking tests",
"mcp": "Model Context Protocol tests",
"sapf": "SAPF audio generation tests",
"visualization": "Visualization tests",
}
def pytest_configure(config: Any) -> None:
"""Register project-specific pytest markers."""
for name, description in PYTEST_MARKERS.items():
config.addinivalue_line("markers", f"{name}: {description}")
def pytest_collection_modifyitems(config: Any, items: list) -> None:
"""Tag slow tests with the performance marker for dashboarding."""
for item in items:
if any(m.name == "slow" for m in item.iter_markers()):
item.add_marker(pytest.mark.performance)
# -----------------------------------------------------------------------------
# Session-level fixtures
# -----------------------------------------------------------------------------
@pytest.fixture(scope="session")
def test_config() -> Dict[str, Any]:
"""Session-wide test configuration."""
return {
"test_mode": True,
"safe_mode": True,
"temp_dir": tempfile.mkdtemp(),
"max_test_duration": 300,
"memory_limit_mb": 1024,
}
@pytest.fixture(scope="session")
def project_root() -> Path:
"""Absolute path to the project root."""
return Path(__file__).parent.parent.parent
@pytest.fixture(scope="session")
def src_dir() -> Path:
"""Absolute path to the src/ directory."""
return Path(__file__).parent.parent
@pytest.fixture(scope="session")
def test_dir() -> Path:
"""Absolute path to the src/tests/ directory."""
return Path(__file__).parent
# -----------------------------------------------------------------------------
# Filesystem fixtures
# -----------------------------------------------------------------------------
@pytest.fixture
def safe_filesystem() -> Generator[Any, None, None]:
"""A scratch filesystem under a fresh tempdir that cleans itself up."""
temp_dir = Path(tempfile.mkdtemp())
class SafeFileSystem:
def __init__(self, base: Path) -> None:
self.temp_dir = base
self.created_files: list[Path] = []
self.created_dirs: list[Path] = []
def create_file(self, path: Any, content: str = "") -> Path:
full = self.temp_dir / path
full.parent.mkdir(parents=True, exist_ok=True)
full.write_text(content)
self.created_files.append(full)
return cast("Path", full)
def create_dir(self, path: Any) -> Path:
full = self.temp_dir / path
full.mkdir(parents=True, exist_ok=True)
self.created_dirs.append(full)
return cast("Path", full)
def cleanup(self) -> None:
import shutil
if self.temp_dir.exists():
try:
shutil.rmtree(self.temp_dir)
except OSError:
pass
fs = SafeFileSystem(temp_dir)
yield fs
fs.cleanup()
@pytest.fixture
def isolated_temp_dir() -> Generator[Path, None, None]:
"""A throwaway temp directory with automatic cleanup."""
temp_dir = Path(tempfile.mkdtemp())
try:
yield temp_dir
finally:
import shutil
shutil.rmtree(temp_dir, ignore_errors=True)
@pytest.fixture
def temp_directories(tmp_path: Path) -> Dict[str, Path]:
"""Standard input/output/temp directory set anchored under tmp_path."""
input_dir = tmp_path / "input"
output_dir = tmp_path / "output"
temp_dir = tmp_path / "temp"
for d in (input_dir, output_dir, temp_dir):
d.mkdir(parents=True, exist_ok=True)
return {
"input_dir": input_dir,
"output_dir": output_dir,
"temp_dir": temp_dir,
"root": tmp_path,
}
@pytest.fixture
def temp_output_dir() -> Generator[Path, None, None]:
"""Temporary output directory for visualization tests."""
base = Path(tempfile.mkdtemp())
directory = base / "viz_output"
directory.mkdir(parents=True, exist_ok=True)
try:
yield directory
finally:
import shutil
shutil.rmtree(base, ignore_errors=True)
# -----------------------------------------------------------------------------
# GNN sample content fixtures
# -----------------------------------------------------------------------------
_SAMPLE_GNN_CONTENT = """
# Test GNN Model
## ModelName
test_model
## StateSpaceBlock
s[3,1,type=int]
o[3,1,type=int]
## Connections
s -> o
## InitialParameterization
A = [[0.7, 0.2, 0.1], [0.2, 0.7, 0.1], [0.1, 0.2, 0.7]]
B = [[[0.9, 0.05, 0.05], [0.05, 0.9, 0.05], [0.05, 0.05, 0.9]], [[0.9, 0.05, 0.05], [0.05, 0.9, 0.05], [0.05, 0.05, 0.9]], [[0.9, 0.05, 0.05], [0.05, 0.9, 0.05], [0.05, 0.05, 0.9]]]
C = [0.0, 0.0, 1.0]
D = [0.34, 0.33, 0.33]
"""
@pytest.fixture
def sample_gnn_files(safe_filesystem: Any) -> Dict[str, Path]:
"""Pair of on-disk GNN files sharing a minimal coherent POMDP schema."""
files: dict[str, Any] = {
"simple": safe_filesystem.create_file("simple.gnn", _SAMPLE_GNN_CONTENT),
"second": safe_filesystem.create_file(
"second.gnn",
_SAMPLE_GNN_CONTENT.replace("test_model", "second_model"),
),
}
return files
def _write_sample_gnn_markdown(target: Path) -> None:
"""Write a minimal GNN markdown with ontology annotations to ``target``."""
content = (
"# Active Inference Model\n\n"
"## ActInfOntologyAnnotation\n"
"s = HiddenState\n"
"s_prime = NextHiddenState\n"
"o = Observation\n"
"π = PolicyVector\n"
"u = Action\n"
"t = Time\n"
"A = LikelihoodMatrix\n"
"B = TransitionMatrix\n"
"C = LogPreferenceVector\n"
"D = PriorOverHiddenStates\n"
"E = Habit\n"
"F = VariationalFreeEnergy\n"
"G = ExpectedFreeEnergy\n\n"
"## Connections\n"
"s -> o\n"
)
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(content)
@pytest.fixture
def test_data_dir() -> Generator[Path, None, None]:
"""Directory containing a sample GNN file at samples/actinf_pomdp_agent.md."""
base = Path(tempfile.mkdtemp())
sample = base / "samples" / "actinf_pomdp_agent.md"
_write_sample_gnn_markdown(sample)
try:
yield sample.parent
finally:
import shutil
shutil.rmtree(base, ignore_errors=True)
@pytest.fixture
def sample_gnn_file() -> Generator[Path, None, None]:
"""Path to a single on-disk sample GNN markdown file."""
tmp = Path(tempfile.mkdtemp())
path = tmp / "actinf_pomdp_agent.md"
_write_sample_gnn_markdown(path)
try:
yield path
finally:
import shutil
shutil.rmtree(tmp, ignore_errors=True)
@pytest.fixture
def sample_gnn_spec() -> Dict[str, Any]:
"""In-memory GNN spec dict used by render tests."""
return {
"model_name": "actinf_pomdp_agent",
"name": "actinf_pomdp_agent",
"states": ["s0", "s1"],
"observations": ["o0", "o1"],
"actions": ["stay"],
"num_states": 2,
"num_observations": 2,
"num_actions": 1,
"time_horizon": 4,
"seed": 42,
"b_tensor_order": "next_state_previous_state_action",
"initialparameterization": {
"A": [[0.9, 0.1], [0.1, 0.9]],
"B": [[0.9, 0.2], [0.1, 0.8]],
"C": [0.0, 1.0],
"D": [1.0, 0.0],
"E": [1.0],
},
"model_parameters": {
"num_hidden_states": 2,
"num_obs": 2,
"num_actions": 1,
"b_tensor_order": "next_state_previous_state_action",
},
"parameters": {
"A": [[0.9, 0.1], [0.1, 0.9]],
"B": [[0.9, 0.2], [0.1, 0.8]],
"C": [0.0, 1.0],
"D": [1.0, 0.0],
"E": [1.0],
},
}
@pytest.fixture
def sample_markdown() -> str:
"""Minimal GNN markdown source for parser tests."""
return (
"# TestModel\n\n"
"## ModelName\nTestModel\n\n"
"## StateSpaceBlock\ns[2,1,type=int]\n\n"
"## Connections\ns->o\n\n"
"## InitialParameterization\nA={(1,0),(0,1)}\n"
)
@pytest.fixture
def sample_scala() -> str:
"""Minimal Scala source snippet for parser tests."""
return (
"object MyModel {\n"
" val a_m: Matrix(Fin 2, Fin 3) = ???\n"
" val b_f: Matrix(Fin 2, Fin 2) = ???\n"
" // EFE = G + F\n"
"}\n"
)
@pytest.fixture
def comprehensive_test_data(isolated_temp_dir: Path) -> Dict[str, Any]:
"""Consolidated test-data bundle used by integration tests."""
return {
"temp_dir": isolated_temp_dir,
"gnn_files": {
"simple": isolated_temp_dir / "simple.gnn",
"complex": isolated_temp_dir / "complex.gnn",
},
"output_dir": isolated_temp_dir / "output",
"config": {"test_mode": True, "safe_mode": True},
}
# -----------------------------------------------------------------------------
# Render + MCP test helpers
# -----------------------------------------------------------------------------
class _RealRenderModule:
"""Thin adapter used by render integration tests — delegates to
``render.processor.render_gnn_spec``. Phase 7: fallback chain removed."""
def render_gnn_spec(self, spec: Any, target: str, outdir: Any) -> Any:
from render.processor import render_gnn_spec
return render_gnn_spec(spec, target, outdir)
@pytest.fixture
def test_render_module() -> _RealRenderModule:
return _RealRenderModule()
class _MCPTools:
"""Lightweight in-memory MCP registry used by MCP wiring tests."""
def __init__(self) -> None:
self.tools: Dict[str, Any] = {}
self.resources: Dict[str, Any] = {}
def register_tool(self, name: str, *args: Any, **kwargs: Any) -> None:
function = kwargs.get("function")
schema = kwargs.get("schema")
description = kwargs.get("description", "")
if function is None and args:
function = args[0]
if len(args) >= 2 and schema is None:
schema = args[1]
if len(args) >= 3 and not description:
description = args[2]
self.tools[name] = {
"function": function,
"func": function,
"schema": schema or {},
"description": description,
}
def register_resource(
self, pattern: str, handler: Any, description: str = ""
) -> None:
self.resources[pattern] = {"handler": handler, "description": description}
def execute_tool(self, name: str, **kwargs: Any) -> Any:
if name not in self.tools:
return {"error": "tool_not_found", "name": name}
func = self.tools[name].get("function") or self.tools[name].get("func")
return func(**kwargs)
@pytest.fixture
def test_mcp_tools() -> _MCPTools:
return _MCPTools()