Skip to content

Latest commit

 

History

History
157 lines (123 loc) · 5.4 KB

File metadata and controls

157 lines (123 loc) · 5.4 KB

JSON Validation Test Results

Test Summary

Date: 2026-02-06 Location: /Users/michaelassraf/Documents/GitHub/CodeWiki/codewiki/src/be/cluster_modules.py Lines Tested: 352-376 Result: ✅ 10/10 validation tests passed

Validation Logic Tested

The NEW json.loads() validation code added around line 338 in cluster_modules.py:

# CRITICAL: Validate all component IDs are integers
max_id = len(id_to_fqdn) - 1
for module_name, module_info in module_tree.items():
    if "components" not in module_info:
        continue

    component_ids = module_info["components"]
    invalid_ids = []

    for comp_id in component_ids:
        # Check if ID is an integer
        if not isinstance(comp_id, int):
            invalid_ids.append(f"{comp_id} (type: {type(comp_id).__name__})")
        # Check if ID is in valid range
        elif comp_id < 0 or comp_id > max_id:
            invalid_ids.append(f"{comp_id} (out of range 0-{max_id})")

    if invalid_ids:
        logger.error(f"❌ Module '{module_name}' contains invalid component IDs:")
        logger.error(f"   Invalid IDs: {invalid_ids}")
        logger.error(f"   Expected: Integers in range 0-{max_id}")
        logger.error(f"   LLM ignored instructions and returned non-integer IDs!")
        return {}

Test Cases

✅ TEST 1: Valid response with integer IDs

Input: [0, 1, 2] Expected: PASS Actual: PASS Result: All IDs are integers in valid range

✅ TEST 2: Invalid response with quoted integers

Input: ["0", "1", "2"] Expected: FAIL with type error Actual: FAIL Error: Module 'auth_module' contains invalid component IDs: ['0 (type: str)', '1 (type: str)', '2 (type: str)']. Expected: Integers in range 0-9

✅ TEST 3: Invalid response with string class names

Input: ["AuthService", "UserController"] Expected: FAIL with type error Actual: FAIL Error: Module 'auth_module' contains invalid component IDs: ['AuthService (type: str)', 'UserController (type: str)']. Expected: Integers in range 0-9

✅ TEST 4: Invalid response with out-of-range IDs

Input: [0, 999] Expected: FAIL with range error Actual: FAIL Error: Module 'auth_module' contains invalid component IDs: ['999 (out of range 0-9)']. Expected: Integers in range 0-9

✅ TEST 5: Mixed valid integers and quoted strings

Input: [0, "1", 2] Expected: FAIL with type error Actual: FAIL Error: Module 'auth_module' contains invalid component IDs: ['1 (type: str)']. Expected: Integers in range 0-9

✅ TEST 6: Invalid response with negative ID

Input: [-1, 0, 1] Expected: FAIL with range error Actual: FAIL Error: Module 'auth_module' contains invalid component IDs: ['-1 (out of range 0-9)']. Expected: Integers in range 0-9

✅ TEST 7: Invalid response with float IDs

Input: [0.0, 1.0, 2.0] Expected: FAIL with type error Actual: FAIL Error: Module 'auth_module' contains invalid component IDs: ['0.0 (type: float)', '1.0 (type: float)', '2.0 (type: float)']. Expected: Integers in range 0-9

✅ TEST 8: Valid response with empty components array

Input: [] Expected: PASS Actual: PASS Result: All IDs are integers in valid range

✅ TEST 9: Valid response with missing components key

Input: { "path": "src/auth" } (no components key) Expected: PASS Actual: PASS Result: All IDs are integers in valid range

✅ TEST 10: Invalid JSON syntax

Input: Malformed JSON with missing bracket Expected: FAIL with JSON parse error Actual: FAIL Error: Invalid JSON in LLM response: Expecting ',' delimiter: line 6 column 3 (char 75)

Validation Confirms

Type checking catches quoted integers and strings ✅ Range checking catches out-of-bounds IDs ✅ Error messages clearly state what's wrong and what's expected ✅ Valid cases pass without errors

Key Features Verified

  1. Type Validation: isinstance(comp_id, int) successfully catches:

    • Quoted integers ("0", "1")
    • String class names ("AuthService")
    • Float values (0.0, 1.0)
  2. Range Validation: Successfully catches:

    • Negative IDs (-1)
    • Out-of-bounds IDs (999 when max is 9)
  3. Error Reporting: All error messages include:

    • Invalid value with actual type
    • Expected range (0-max_id)
    • Clear actionable message
  4. Edge Cases: Handles:

    • Empty component arrays
    • Missing component keys
    • Malformed JSON
    • Mixed valid/invalid IDs

Impact

This validation logic prevents the ID-based clustering system from accepting invalid LLM responses, ensuring:

  • No fuzzy string matching needed (replaced 200+ lines of code)
  • LLM responses are strictly validated before normalization
  • Clear error messages help debug prompt issues
  • System fails fast on invalid data instead of producing incorrect results

Test File

Location: /Users/michaelassraf/Documents/GitHub/CodeWiki/tests/test_cluster_validation.py

Run with:

cd /Users/michaelassraf/Documents/GitHub/CodeWiki
python3 tests/test_cluster_validation.py

Conclusion

🎉 All 10 validation tests passed!

The validation logic is working correctly and provides robust protection against invalid LLM responses. The system will now:

  • Only accept integer component IDs
  • Verify all IDs are in valid range
  • Provide clear error messages when validation fails
  • Fail early before attempting normalization