From d6a89e75c83268f38b3dcc42a4b329dcb452fcea Mon Sep 17 00:00:00 2001 From: spark Date: Fri, 30 Oct 2026 22:40:33 +0000 Subject: [PATCH] spark: add json validator utility with tests --- wizard-work/spark/json_validate_simple.py | 31 +++++++ .../spark/test_json_validate_simple.py | 92 +++++++++++++++++++ wizard-work/spark/verification_result.txt | 14 +++ 3 files changed, 137 insertions(+) create mode 100644 wizard-work/spark/json_validate_simple.py create mode 100644 wizard-work/spark/test_json_validate_simple.py create mode 100644 wizard-work/spark/verification_result.txt diff --git a/wizard-work/spark/json_validate_simple.py b/wizard-work/spark/json_validate_simple.py new file mode 100644 index 0000000..ac4ab92 --- /dev/null +++ b/wizard-work/spark/json_validate_simple.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +"""Simple JSON validator - validates JSON files and reports errors.""" + +import sys +import json + +def validate_json(filepath): + """Validate a JSON file. Returns (is_valid, error_message).""" + try: + with open(filepath, 'r') as f: + json.load(f) + return True, None + except FileNotFoundError: + return False, f"File not found: {filepath}" + except json.JSONDecodeError as e: + return False, f"Invalid JSON: {e}" + except Exception as e: + return False, f"Error: {e}" + +if __name__ == "__main__": + if len(sys.argv) != 2: + print(f"Usage: {sys.argv[0]} ", file=sys.stderr) + sys.exit(1) + + is_valid, error = validate_json(sys.argv[1]) + if is_valid: + print("VALID") + sys.exit(0) + else: + print(error, file=sys.stderr) + sys.exit(1) \ No newline at end of file diff --git a/wizard-work/spark/test_json_validate_simple.py b/wizard-work/spark/test_json_validate_simple.py new file mode 100644 index 0000000..e24d51b --- /dev/null +++ b/wizard-work/spark/test_json_validate_simple.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Test for json_validate_simple.py - verifies the validator works correctly.""" + +import subprocess +import tempfile +import os +import json + +def test_json_validator(): + """Run tests on json_validate_simple.py""" + results = [] + + # Create temp files for testing + temp_dir = tempfile.mkdtemp() + + # Test 1: Valid JSON object + valid_obj = os.path.join(temp_dir, "valid_obj.json") + with open(valid_obj, 'w') as f: + json.dump({"key": "value", "num": 42}, f) + + result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', valid_obj], + capture_output=True, text=True) + if result.returncode == 0 and 'VALID' in result.stdout: + results.append(("Test 1 (valid JSON object)", "PASS")) + else: + results.append(("Test 1 (valid JSON object)", f"FAIL: stdout={result.stdout}, stderr={result.stderr}, code={result.returncode}")) + + # Test 2: Valid JSON array + valid_arr = os.path.join(temp_dir, "valid_arr.json") + with open(valid_arr, 'w') as f: + json.dump([1, 2, 3], f) + + result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', valid_arr], + capture_output=True, text=True) + if result.returncode == 0 and 'VALID' in result.stdout: + results.append(("Test 2 (valid JSON array)", "PASS")) + else: + results.append(("Test 2 (valid JSON array)", f"FAIL: {result.stdout} {result.stderr}")) + + # Test 3: Invalid JSON (missing comma) + invalid_json = os.path.join(temp_dir, "invalid.json") + with open(invalid_json, 'w') as f: + f.write('{"key": "value" "num": 42}') + + result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', invalid_json], + capture_output=True, text=True) + if result.returncode != 0 and 'Invalid JSON' in result.stderr: + results.append(("Test 3 (invalid JSON)", "PASS")) + else: + results.append(("Test 3 (invalid JSON)", f"FAIL: should have stderr with 'Invalid JSON'")) + + # Test 4: Nonexistent file + result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', '/nonexistent.json'], + capture_output=True, text=True) + if result.returncode != 0: + results.append(("Test 4 (nonexistent file)", "PASS")) + else: + results.append(("Test 4 (nonexistent file)", "FAIL: should have exited nonzero")) + + # Test 5: Empty string (valid JSON null) + empty_valid = os.path.join(temp_dir, "empty_val.json") + with open(empty_valid, 'w') as f: + f.write("null") + + result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', empty_valid], + capture_output=True, text=True) + if result.returncode == 0 and 'VALID' in result.stdout: + results.append(("Test 5 (null JSON)", "PASS")) + else: + results.append(("Test 5 (null JSON)", f"FAIL: {result.stdout} {result.stderr}")) + + # Cleanup + for f in os.listdir(temp_dir): + os.unlink(os.path.join(temp_dir, f)) + os.rmdir(temp_dir) + + return results + +if __name__ == "__main__": + results = test_json_validator() + all_pass = True + for name, status in results: + print(f"{status}: {name}") + if status != "PASS": + all_pass = False + + if all_pass: + print("\nALL TESTS PASSED") + exit(0) + else: + print("\nSOME TESTS FAILED") + exit(1) \ No newline at end of file diff --git a/wizard-work/spark/verification_result.txt b/wizard-work/spark/verification_result.txt new file mode 100644 index 0000000..8d8d9f4 --- /dev/null +++ b/wizard-work/spark/verification_result.txt @@ -0,0 +1,14 @@ +VERIFICATION RESULT +=================== +Artifact: json_validate_simple.py +Purpose: Simple JSON validator utility +Status: WORKING + +Tests Executed: +- Test 1 (valid JSON object): PASS +- Test 2 (valid JSON array): PASS +- Test 3 (invalid JSON): PASS +- Test 4 (nonexistent file): PASS +- Test 5 (null JSON): PASS + +All tests passed. \ No newline at end of file