|
| 1 | +// Binary to verify ValidationScript GREEN phase implementation |
| 2 | +// This demonstrates our enhanced data structure works correctly |
| 3 | + |
| 4 | +use mandrel_mcp_th::spec::{ExecutionPhase, ScriptLanguage, ValidationScript}; |
| 5 | +use serde_json; |
| 6 | + |
| 7 | +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)] |
| 8 | +pub struct TestSpec { |
| 9 | + pub name: String, |
| 10 | + pub version: String, |
| 11 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 12 | + pub validation_scripts: Option<Vec<ValidationScript>>, |
| 13 | +} |
| 14 | + |
| 15 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 16 | + println!("🧪 Testing ValidationScript Enhanced Data Structure (GREEN Phase)"); |
| 17 | + |
| 18 | + // Test 1: Direct struct creation with new enums |
| 19 | + println!("\n✅ Test 1: Direct ValidationScript Creation"); |
| 20 | + let script = ValidationScript { |
| 21 | + name: "test_script".to_string(), |
| 22 | + language: ScriptLanguage::Lua, |
| 23 | + execution_phase: ExecutionPhase::Before, |
| 24 | + required: true, |
| 25 | + source: "result = { success = true }".to_string(), |
| 26 | + timeout_ms: Some(5000), |
| 27 | + }; |
| 28 | + |
| 29 | + println!(" ✓ Created ValidationScript with enhanced structure"); |
| 30 | + println!(" ✓ Name: {}", script.name); |
| 31 | + println!(" ✓ Language: {:?}", script.language); |
| 32 | + println!(" ✓ Execution Phase: {:?}", script.execution_phase); |
| 33 | + println!(" ✓ Required: {}", script.required); |
| 34 | + println!(" ✓ Timeout: {:?}", script.timeout_ms); |
| 35 | + |
| 36 | + // Test 2: Test all enum variants |
| 37 | + println!("\n✅ Test 2: Testing All Enum Variants"); |
| 38 | + |
| 39 | + let languages = vec![ |
| 40 | + ScriptLanguage::JavaScript, |
| 41 | + ScriptLanguage::Python, |
| 42 | + ScriptLanguage::Lua, |
| 43 | + ]; |
| 44 | + |
| 45 | + let phases = vec![ |
| 46 | + ExecutionPhase::Before, |
| 47 | + ExecutionPhase::After, |
| 48 | + ExecutionPhase::Both, |
| 49 | + ]; |
| 50 | + |
| 51 | + for language in &languages { |
| 52 | + for phase in &phases { |
| 53 | + let test_script = ValidationScript { |
| 54 | + name: format!("test_{:?}_{:?}", language, phase), |
| 55 | + language: language.clone(), |
| 56 | + execution_phase: phase.clone(), |
| 57 | + required: false, |
| 58 | + source: "test script".to_string(), |
| 59 | + timeout_ms: None, |
| 60 | + }; |
| 61 | + println!(" ✓ Created script: {:?} + {:?}", language, phase); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Test 3: JSON serialization (instead of YAML to avoid dependency issues) |
| 66 | + println!("\n✅ Test 3: JSON Serialization Test"); |
| 67 | + let script = ValidationScript { |
| 68 | + name: "serialize_test".to_string(), |
| 69 | + language: ScriptLanguage::JavaScript, |
| 70 | + execution_phase: ExecutionPhase::Both, |
| 71 | + required: false, |
| 72 | + source: "console.log('test')".to_string(), |
| 73 | + timeout_ms: Some(3000), |
| 74 | + }; |
| 75 | + |
| 76 | + let json = serde_json::to_string_pretty(&script)?; |
| 77 | + println!(" ✓ Successfully serialized ValidationScript to JSON:"); |
| 78 | + println!("{}", json); |
| 79 | + |
| 80 | + // Test 4: JSON deserialization |
| 81 | + println!("\n✅ Test 4: JSON Deserialization Test"); |
| 82 | + let json_input = r#"{ |
| 83 | + "name": "deserialize_test", |
| 84 | + "language": "python", |
| 85 | + "execution_phase": "after", |
| 86 | + "required": true, |
| 87 | + "source": "print('hello world')", |
| 88 | + "timeout_ms": 2000 |
| 89 | + }"#; |
| 90 | + |
| 91 | + let parsed: ValidationScript = serde_json::from_str(json_input)?; |
| 92 | + println!(" ✓ Successfully deserialized ValidationScript from JSON"); |
| 93 | + println!(" ✓ Name: {}", parsed.name); |
| 94 | + println!(" ✓ Language: {:?}", parsed.language); |
| 95 | + println!(" ✓ Execution Phase: {:?}", parsed.execution_phase); |
| 96 | + println!(" ✓ Required: {}", parsed.required); |
| 97 | + println!(" ✓ Timeout: {:?}", parsed.timeout_ms); |
| 98 | + |
| 99 | + // Verify the parsed values |
| 100 | + assert_eq!(parsed.name, "deserialize_test"); |
| 101 | + assert_eq!(parsed.language, ScriptLanguage::Python); |
| 102 | + assert_eq!(parsed.execution_phase, ExecutionPhase::After); |
| 103 | + assert_eq!(parsed.required, true); |
| 104 | + assert_eq!(parsed.timeout_ms, Some(2000)); |
| 105 | + |
| 106 | + // Test 5: Test specification with multiple scripts |
| 107 | + println!("\n✅ Test 5: Complex Multi-Script JSON Specification"); |
| 108 | + let complex_json = r#"{ |
| 109 | + "name": "Enhanced Test Server", |
| 110 | + "version": "1.0.0", |
| 111 | + "validation_scripts": [ |
| 112 | + { |
| 113 | + "name": "lua_validator", |
| 114 | + "language": "lua", |
| 115 | + "execution_phase": "after", |
| 116 | + "required": true, |
| 117 | + "source": "result = { success = true }" |
| 118 | + }, |
| 119 | + { |
| 120 | + "name": "js_validator", |
| 121 | + "language": "javascript", |
| 122 | + "execution_phase": "both", |
| 123 | + "required": false, |
| 124 | + "source": "result = { success: true };", |
| 125 | + "timeout_ms": 1500 |
| 126 | + }, |
| 127 | + { |
| 128 | + "name": "py_validator", |
| 129 | + "language": "python", |
| 130 | + "execution_phase": "before", |
| 131 | + "required": true, |
| 132 | + "source": "result = {'success': True}", |
| 133 | + "timeout_ms": 3000 |
| 134 | + } |
| 135 | + ] |
| 136 | + }"#; |
| 137 | + |
| 138 | + let spec: TestSpec = serde_json::from_str(complex_json)?; |
| 139 | + println!(" ✓ Successfully parsed complex specification"); |
| 140 | + |
| 141 | + let scripts = spec.validation_scripts.unwrap(); |
| 142 | + println!(" ✓ Found {} validation scripts", scripts.len()); |
| 143 | + |
| 144 | + // Verify each script |
| 145 | + assert_eq!(scripts[0].language, ScriptLanguage::Lua); |
| 146 | + assert_eq!(scripts[0].execution_phase, ExecutionPhase::After); |
| 147 | + assert_eq!(scripts[0].required, true); |
| 148 | + |
| 149 | + assert_eq!(scripts[1].language, ScriptLanguage::JavaScript); |
| 150 | + assert_eq!(scripts[1].execution_phase, ExecutionPhase::Both); |
| 151 | + assert_eq!(scripts[1].required, false); |
| 152 | + assert_eq!(scripts[1].timeout_ms, Some(1500)); |
| 153 | + |
| 154 | + assert_eq!(scripts[2].language, ScriptLanguage::Python); |
| 155 | + assert_eq!(scripts[2].execution_phase, ExecutionPhase::Before); |
| 156 | + assert_eq!(scripts[2].required, true); |
| 157 | + assert_eq!(scripts[2].timeout_ms, Some(3000)); |
| 158 | + |
| 159 | + println!(" ✓ All assertions passed for complex specification!"); |
| 160 | + |
| 161 | + println!("\n🎉 SUCCESS! ValidationScript Enhanced Data Structure is working correctly!"); |
| 162 | + println!("\n📋 GREEN Phase Implementation Summary:"); |
| 163 | + println!(" ✅ ScriptLanguage enum (JavaScript, Python, Lua)"); |
| 164 | + println!(" ✅ ExecutionPhase enum (Before, After, Both)"); |
| 165 | + println!(" ✅ Enhanced ValidationScript struct with proper types"); |
| 166 | + println!(" ✅ Non-optional required: bool field"); |
| 167 | + println!(" ✅ Non-optional source: String field"); |
| 168 | + println!(" ✅ Optional timeout_ms: Option<u64> field"); |
| 169 | + println!(" ✅ JSON serialization/deserialization working"); |
| 170 | + println!(" ✅ Complex multi-script specifications supported"); |
| 171 | + println!(" ✅ Type safety enforced through enum usage"); |
| 172 | + |
| 173 | + println!("\n🔄 Next Steps (REFACTOR Phase):"); |
| 174 | + println!(" • Complete script execution integration"); |
| 175 | + println!(" • Add ScriptContext generation"); |
| 176 | + println!(" • Wire into ValidationEngine"); |
| 177 | + println!(" • Add comprehensive error handling"); |
| 178 | + println!(" • Performance optimization"); |
| 179 | + |
| 180 | + Ok(()) |
| 181 | +} |
0 commit comments