Files
troupe-standup/wizard-work/spark/validator_test_suite.py

116 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""Test suite for json_validator.py - verifies correctness with actual test cases."""
import json
import subprocess
import sys
import tempfile
import re
from pathlib import Path
SCRIPT_PATH = Path("/root/.hermes/wizard-outputs/spark/json_validator.py")
OUTPUT_PATH = Path("/root/.hermes/wizard-outputs/spark/validation_test_results.json")
def create_test_files(tmpdir: Path):
"""Create test JSON files for validation."""
test_cases = {}
# Valid JSON files
test_cases["valid_object.json"] = {"name": "test", "value": 42, "nested": {"key": "value"}}
test_cases["valid_array.json"] = [1, 2, 3, "four", {"five": 5}]
test_cases["valid_string.json"] = "just a string"
test_cases["valid_number.json"] = 3.14159
test_cases["valid_boolean.json"] = True
test_cases["valid_null.json"] = None
# Invalid JSON files
test_cases["invalid_syntax.json"] = '{"name": "test", "broken": }'
test_cases["invalid_trailing_comma.json"] = '{"a": 1, "b": 2,}'
for filename, content in test_cases.items():
filepath = tmpdir / filename
with open(filepath, 'w') as f:
if isinstance(content, str):
f.write(content) # Write invalid JSON as raw string
else:
json.dump(content, f)
return test_cases
def run_validator(files):
"""Run the validator script on given files."""
cmd = ["python3", str(SCRIPT_PATH)] + [str(f) for f in files]
result = subprocess.run(cmd, capture_output=True, text=True)
return result
def parse_validator_output(output: str):
"""Parse the validator output to extract file statuses."""
results = {}
# Match lines like: "VALID: /path/to/file.json"
for match in re.finditer(r'✗ INVALID: (.+)', output):
results[match.group(1)] = False
for match in re.finditer(r'✓ VALID: (.+)', output):
results[match.group(1)] = True
return results
def main():
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
test_cases = create_test_files(tmpdir)
expected_valid = {
"valid_object.json": True,
"valid_array.json": True,
"valid_string.json": True,
"valid_number.json": True,
"valid_boolean.json": True,
"valid_null.json": True,
"invalid_syntax.json": False,
"invalid_trailing_comma.json": False,
}
# Run validator on all files
all_files = [tmpdir / name for name in test_cases.keys()]
result = run_validator(all_files)
# Parse output
output_valid = parse_validator_output(result.stdout)
# Verify results
all_correct = True
results = {"tests": [], "passed": 0, "failed": 0}
for filename, should_be_valid in expected_valid.items():
filepath = str(tmpdir / filename)
is_valid = output_valid.get(filepath, None)
passed = is_valid == should_be_valid
results["tests"].append({
"file": filename,
"expected_valid": should_be_valid,
"actual_valid": is_valid,
"passed": passed
})
if passed:
results["passed"] += 1
else:
results["failed"] += 1
all_correct = False
results["all_passed"] = all_correct
results["exit_code_correct"] = (result.returncode == 0) == all_correct
results["validator_stdout"] = result.stdout
# Write results
with open(OUTPUT_PATH, 'w') as f:
json.dump(results, f, indent=2)
print(f"Tests run: {len(test_cases)}")
print(f"Passed: {results['passed']}")
print(f"Failed: {results['failed']}")
print(f"All passed: {all_correct}")
print(f"Results written to: {OUTPUT_PATH}")
return 0 if all_correct else 1
if __name__ == "__main__":
sys.exit(main())