codive_core/
verification.rs1use std::time::Duration;
16
17#[derive(Debug, Clone)]
19pub struct VerificationResult {
20 pub name: String,
22
23 pub command: String,
25
26 pub passed: bool,
28
29 pub output: String,
31
32 pub exit_code: Option<i32>,
34
35 pub duration: Duration,
37}
38
39impl VerificationResult {
40 pub fn passed(name: impl Into<String>, command: impl Into<String>, output: String, duration: Duration) -> Self {
42 Self {
43 name: name.into(),
44 command: command.into(),
45 passed: true,
46 output,
47 exit_code: Some(0),
48 duration,
49 }
50 }
51
52 pub fn failed(
54 name: impl Into<String>,
55 command: impl Into<String>,
56 output: String,
57 exit_code: Option<i32>,
58 duration: Duration,
59 ) -> Self {
60 Self {
61 name: name.into(),
62 command: command.into(),
63 passed: false,
64 output,
65 exit_code,
66 duration,
67 }
68 }
69}
70
71pub fn format_verification_errors(results: &[VerificationResult]) -> String {
73 let failed: Vec<_> = results.iter().filter(|r| !r.passed).collect();
74
75 if failed.is_empty() {
76 return String::new();
77 }
78
79 let mut message = String::new();
80
81 for result in failed {
82 message.push_str(&format!(
83 "VERIFICATION FAILED: {}\n\n",
84 result.name.to_uppercase()
85 ));
86 message.push_str(&format!("Command: $ {}\n", result.command));
87
88 if let Some(code) = result.exit_code {
89 message.push_str(&format!("Exit code: {}\n", code));
90 }
91
92 message.push_str("\nOutput:\n```\n");
93 message.push_str(&result.output);
94 message.push_str("\n```\n\n");
95 }
96
97 message.push_str("Please fix the errors above and try again.");
98
99 message
100}
101
102pub fn format_verification_status(attempt: u32, max_attempts: u32, results: &[VerificationResult]) -> String {
104 let failed_count = results.iter().filter(|r| !r.passed).count();
105
106 if failed_count == 0 {
107 "All verification checks passed".to_string()
108 } else {
109 format!(
110 "Verification failed (attempt {}/{}): {} check(s) failed",
111 attempt, max_attempts, failed_count
112 )
113 }
114}
115
116#[cfg(test)]
121mod tests {
122 use super::*;
123
124 #[test]
125 fn test_verification_result_creation() {
126 let passed = VerificationResult::passed(
127 "build",
128 "cargo build",
129 "Compiling...".to_string(),
130 Duration::from_secs(5),
131 );
132 assert!(passed.passed);
133 assert_eq!(passed.exit_code, Some(0));
134
135 let failed = VerificationResult::failed(
136 "test",
137 "cargo test",
138 "test failed".to_string(),
139 Some(1),
140 Duration::from_secs(10),
141 );
142 assert!(!failed.passed);
143 assert_eq!(failed.exit_code, Some(1));
144 }
145
146 #[test]
147 fn test_format_verification_errors() {
148 let results = vec![
149 VerificationResult::passed("build", "cargo build", "ok".to_string(), Duration::from_secs(1)),
150 VerificationResult::failed(
151 "test",
152 "cargo test",
153 "error: test failed".to_string(),
154 Some(1),
155 Duration::from_secs(2),
156 ),
157 ];
158
159 let message = format_verification_errors(&results);
160
161 assert!(message.contains("VERIFICATION FAILED: TEST"));
162 assert!(message.contains("cargo test"));
163 assert!(message.contains("error: test failed"));
164 assert!(message.contains("Exit code: 1"));
165 assert!(message.contains("Please fix the errors"));
166 }
167
168 #[test]
169 fn test_format_verification_errors_empty() {
170 let results = vec![
171 VerificationResult::passed("build", "cargo build", "ok".to_string(), Duration::from_secs(1)),
172 ];
173
174 let message = format_verification_errors(&results);
175 assert!(message.is_empty());
176 }
177
178 #[test]
179 fn test_format_verification_status() {
180 let failed_results = vec![
181 VerificationResult::failed("build", "cargo build", "error".to_string(), Some(1), Duration::from_secs(1)),
182 ];
183
184 let status = format_verification_status(1, 3, &failed_results);
185 assert!(status.contains("attempt 1/3"));
186 assert!(status.contains("1 check(s) failed"));
187
188 let passed_results = vec![
189 VerificationResult::passed("build", "cargo build", "ok".to_string(), Duration::from_secs(1)),
190 ];
191
192 let status = format_verification_status(1, 3, &passed_results);
193 assert!(status.contains("passed"));
194 }
195}