-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_oxdraw_integration.py
More file actions
406 lines (326 loc) · 13.1 KB
/
test_oxdraw_integration.py
File metadata and controls
406 lines (326 loc) · 13.1 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
"""
Integration tests for oxdraw module
Tests end-to-end workflows:
- GNN file discovery and conversion
- Mermaid generation with metadata
- oxdraw integration (headless mode)
- Round-trip conversion validation
"""
import json
import shutil
import tempfile
from pathlib import Path
import pytest
from gui.oxdraw.mermaid_converter import (
convert_gnn_file_to_mermaid,
generate_mermaid_metadata,
gnn_to_mermaid,
)
from gui.oxdraw.mermaid_parser import (
convert_mermaid_file_to_gnn,
extract_gnn_metadata,
mermaid_to_gnn,
)
from gui.oxdraw.processor import check_oxdraw_installed, get_module_info, process_oxdraw
# Sample GNN content for testing
SAMPLE_GNN_CONTENT = """# GNN Example: Simple Active Inference Agent
# GNN Version: 1.0
## ModelName
Simple Active Inference Agent
## StateSpaceBlock
A[3,3,type=float] # Likelihood matrix
B[3,3,3,type=float] # Transition matrix
C[3,type=float] # Preference vector
D[3,type=float] # Prior vector
s[3,1,type=float] # Hidden state
o[3,1,type=int] # Observation
u[1,type=int] # Action
## Connections
D>s
s-A
A-o
s-B
u>B
## ActInfOntologyAnnotation
A=LikelihoodMatrix
B=TransitionMatrix
C=LogPreferenceVector
D=PriorOverHiddenStates
s=HiddenState
o=Observation
u=Action
## ModelParameters
num_states: 3
num_obs: 3
num_actions: 3
"""
@pytest.fixture
def temp_dir():
"""Create temporary directory for test files."""
tmpdir = tempfile.mkdtemp()
yield Path(tmpdir)
shutil.rmtree(tmpdir)
@pytest.fixture
def sample_gnn_file(temp_dir):
"""Create sample GNN file for testing."""
gnn_file = temp_dir / "test_model.md"
gnn_file.write_text(SAMPLE_GNN_CONTENT)
return gnn_file
@pytest.fixture
def sample_gnn_model():
"""Create sample parsed GNN model."""
return {
"model_name": "Simple Active Inference Agent",
"version": "1.0",
"variables": {
"A": {
"dimensions": [3, 3],
"data_type": "float",
"ontology_mapping": "LikelihoodMatrix",
"description": "Likelihood matrix"
},
"B": {
"dimensions": [3, 3, 3],
"data_type": "float",
"ontology_mapping": "TransitionMatrix",
"description": "Transition matrix"
},
"C": {
"dimensions": [3],
"data_type": "float",
"ontology_mapping": "LogPreferenceVector",
"description": "Preference vector"
},
"D": {
"dimensions": [3],
"data_type": "float",
"ontology_mapping": "PriorOverHiddenStates",
"description": "Prior vector"
},
"s": {
"dimensions": [3, 1],
"data_type": "float",
"ontology_mapping": "HiddenState",
"description": "Hidden state"
},
"o": {
"dimensions": [3, 1],
"data_type": "int",
"ontology_mapping": "Observation",
"description": "Observation"
},
"u": {
"dimensions": [1],
"data_type": "int",
"ontology_mapping": "Action",
"description": "Action"
}
},
"connections": [
{"source": "D", "target": "s", "symbol": ">", "connection_type": "generative"},
{"source": "s", "target": "A", "symbol": "-", "connection_type": "inference"},
{"source": "A", "target": "o", "symbol": "-", "connection_type": "inference"},
{"source": "s", "target": "B", "symbol": "-", "connection_type": "inference"},
{"source": "u", "target": "B", "symbol": ">", "connection_type": "generative"}
],
"parameters": {
"num_states": 3,
"num_obs": 3,
"num_actions": 3
},
"ontology_mappings": [
{"variable": "A", "ontology_term": "LikelihoodMatrix"},
{"variable": "B", "ontology_term": "TransitionMatrix"},
{"variable": "C", "ontology_term": "LogPreferenceVector"},
{"variable": "D", "ontology_term": "PriorOverHiddenStates"},
{"variable": "s", "ontology_term": "HiddenState"},
{"variable": "o", "ontology_term": "Observation"},
{"variable": "u", "ontology_term": "Action"}
]
}
class TestModuleInfo:
"""Test module information and capabilities."""
def test_get_module_info(self):
"""Test module info retrieval."""
info = get_module_info()
assert isinstance(info, dict)
assert "name" in info
assert info["name"] == "oxdraw"
assert "version" in info
assert "capabilities" in info
assert isinstance(info["capabilities"], list)
assert len(info["capabilities"]) > 0
def test_check_oxdraw_installed(self):
"""Test oxdraw CLI availability check."""
result = check_oxdraw_installed()
assert isinstance(result, bool)
# Test should work regardless of whether oxdraw is installed
class TestGNNToMermaidConversion:
"""Test GNN to Mermaid conversion functionality."""
def test_gnn_to_mermaid_basic(self, sample_gnn_model):
"""Test basic GNN to Mermaid conversion."""
mermaid_content = gnn_to_mermaid(sample_gnn_model, include_metadata=True)
assert isinstance(mermaid_content, str)
assert "flowchart TD" in mermaid_content
assert "Simple Active Inference Agent" in mermaid_content
# Check for variables
for var_name in ["A", "B", "C", "D", "s", "o", "u"]:
assert var_name in mermaid_content
# Check for connections
assert "D ==>" in mermaid_content # Generative
assert "s -.->" in mermaid_content # Inference
def test_gnn_to_mermaid_with_metadata(self, sample_gnn_model):
"""Test metadata embedding in Mermaid."""
mermaid_content = gnn_to_mermaid(sample_gnn_model, include_metadata=True)
assert "GNN_METADATA_START" in mermaid_content
assert "GNN_METADATA_END" in mermaid_content
# Extract and parse metadata
metadata = extract_gnn_metadata(mermaid_content)
assert isinstance(metadata, dict)
assert metadata["model_name"] == "Simple Active Inference Agent"
assert "variables" in metadata
assert "connections" in metadata
def test_gnn_to_mermaid_without_metadata(self, sample_gnn_model):
"""Test conversion without metadata."""
mermaid_content = gnn_to_mermaid(sample_gnn_model, include_metadata=False)
assert "flowchart TD" in mermaid_content
assert "GNN_METADATA" not in mermaid_content
def test_convert_gnn_file_to_mermaid(self, sample_gnn_file, temp_dir):
"""Test file-based GNN to Mermaid conversion."""
output_file = temp_dir / "test_model.mmd"
mermaid_content = convert_gnn_file_to_mermaid(
sample_gnn_file,
output_file
)
assert output_file.exists()
assert isinstance(mermaid_content, str)
assert len(mermaid_content) > 0
# Verify file content
file_content = output_file.read_text()
assert file_content == mermaid_content
class TestMermaidToGNNConversion:
"""Test Mermaid to GNN conversion functionality."""
def test_mermaid_to_gnn_basic(self, sample_gnn_model):
"""Test basic Mermaid to GNN conversion."""
# First convert to Mermaid
mermaid_content = gnn_to_mermaid(sample_gnn_model, include_metadata=True)
# Then convert back
gnn_model = mermaid_to_gnn(mermaid_content, validate_ontology=False)
assert isinstance(gnn_model, dict)
assert "model_name" in gnn_model
assert "variables" in gnn_model
assert "connections" in gnn_model
# Check variables preserved
assert len(gnn_model["variables"]) == len(sample_gnn_model["variables"])
# Check connections preserved
assert len(gnn_model["connections"]) == len(sample_gnn_model["connections"])
def test_extract_gnn_metadata(self, sample_gnn_model):
"""Test metadata extraction from Mermaid."""
mermaid_content = gnn_to_mermaid(sample_gnn_model, include_metadata=True)
metadata = extract_gnn_metadata(mermaid_content)
assert isinstance(metadata, dict)
assert metadata["model_name"] == sample_gnn_model["model_name"]
assert "variables" in metadata
assert len(metadata["variables"]) == len(sample_gnn_model["variables"])
def test_convert_mermaid_file_to_gnn(self, sample_gnn_file, temp_dir):
"""Test file-based Mermaid to GNN conversion."""
# First create Mermaid file
mermaid_file = temp_dir / "test_model.mmd"
convert_gnn_file_to_mermaid(sample_gnn_file, mermaid_file)
# Then convert back
output_gnn = temp_dir / "test_model_from_mermaid.md"
gnn_model = convert_mermaid_file_to_gnn(mermaid_file, output_gnn)
assert output_gnn.exists()
assert isinstance(gnn_model, dict)
assert "variables" in gnn_model
assert len(gnn_model["variables"]) > 0
class TestRoundTripConversion:
"""Test round-trip conversion: GNN → Mermaid → GNN."""
def test_round_trip_preserves_structure(self, sample_gnn_model):
"""Test that round-trip conversion preserves model structure."""
# GNN → Mermaid
mermaid_content = gnn_to_mermaid(sample_gnn_model, include_metadata=True)
# Mermaid → GNN
recovered_model = mermaid_to_gnn(mermaid_content, validate_ontology=False)
# Compare structures
assert len(recovered_model["variables"]) == len(sample_gnn_model["variables"])
assert len(recovered_model["connections"]) == len(sample_gnn_model["connections"])
# Check variable names preserved
original_vars = set(sample_gnn_model["variables"].keys())
recovered_vars = set(recovered_model["variables"].keys())
assert original_vars == recovered_vars
def test_round_trip_preserves_ontology(self, sample_gnn_model):
"""Test that ontology mappings are preserved."""
# GNN → Mermaid
mermaid_content = gnn_to_mermaid(sample_gnn_model, include_metadata=True)
# Mermaid → GNN
recovered_model = mermaid_to_gnn(mermaid_content, validate_ontology=False)
# Check ontology mappings
assert "ontology_mappings" in recovered_model
original_ontology = {
m["variable"]: m["ontology_term"]
for m in sample_gnn_model["ontology_mappings"]
}
recovered_ontology = {
m["variable"]: m["ontology_term"]
for m in recovered_model["ontology_mappings"]
}
for var_name in original_ontology:
assert var_name in recovered_ontology
assert original_ontology[var_name] == recovered_ontology[var_name]
class TestProcessOxdraw:
"""Test main processing function."""
def test_process_oxdraw_headless(self, sample_gnn_file, temp_dir, capsys):
"""Test headless processing mode."""
import logging
logger = logging.getLogger(__name__)
output_dir = temp_dir / "output"
success = process_oxdraw(
target_dir=sample_gnn_file.parent,
output_dir=output_dir,
logger=logger,
mode="headless",
auto_convert=True,
validate_on_save=False,
launch_editor=False
)
assert success
assert output_dir.exists()
# Check for results file
results_file = output_dir / "oxdraw_processing_results.json"
assert results_file.exists()
# Parse results
with open(results_file) as f:
results = json.load(f)
assert "gnn_to_mermaid_conversions" in results
assert len(results["gnn_to_mermaid_conversions"]) > 0
def test_process_oxdraw_no_files(self, temp_dir, capsys):
"""Test processing with no GNN files."""
import logging
logger = logging.getLogger(__name__)
empty_dir = temp_dir / "empty"
empty_dir.mkdir()
output_dir = temp_dir / "output"
success = process_oxdraw(
target_dir=empty_dir,
output_dir=output_dir,
logger=logger,
mode="headless"
)
assert not success # Should fail with no files
class TestMetadataGeneration:
"""Test metadata generation utilities."""
def test_generate_mermaid_metadata(self, sample_gnn_model):
"""Test metadata generation."""
metadata = generate_mermaid_metadata(sample_gnn_model)
assert isinstance(metadata, dict)
assert "model_name" in metadata
assert "variables" in metadata
assert "connections" in metadata
assert "ontology_mappings" in metadata
# Verify variables serialized correctly
assert len(metadata["variables"]) == len(sample_gnn_model["variables"])
# Verify connections serialized correctly
assert len(metadata["connections"]) == len(sample_gnn_model["connections"])
if __name__ == "__main__":
pytest.main([__file__, "-v", "--tb=short"])