Skip to main content

fetchium_core/validate/
output.rs

1//! V6 Output integrity validation — citation reachability, content hash (PRD §19).
2
3use crate::validate::types::*;
4
5/// Citation reachability check input.
6#[derive(Debug, Clone)]
7pub struct CitationCheck {
8    pub citation_id: String,
9    pub url: String,
10    pub url_reachable: bool,
11    pub content_hash_matches: bool,
12}
13
14/// V6 Output Integrity Validator.
15pub struct OutputValidator;
16
17impl OutputValidator {
18    /// V6: Verify citation links are valid and format is well-formed.
19    pub fn validate(citations: &[CitationCheck], format_valid: bool) -> LayerResult {
20        let start = std::time::Instant::now();
21        let mut issues = Vec::new();
22        let mut score = 1.0;
23        let mut broken = 0usize;
24
25        for c in citations {
26            if !c.url_reachable {
27                broken += 1;
28                issues.push(ValidationIssue {
29                    severity: IssueSeverity::Warning,
30                    code: "V6_BROKEN_CITATION".into(),
31                    message: format!("Citation [{}] URL unreachable: {}", c.citation_id, c.url),
32                    source_url: Some(c.url.clone()),
33                });
34            }
35            if !c.content_hash_matches {
36                issues.push(ValidationIssue {
37                    severity: IssueSeverity::Warning,
38                    code: "V6_CONTENT_CHANGED".into(),
39                    message: format!("Citation [{}] content changed since fetch", c.citation_id),
40                    source_url: Some(c.url.clone()),
41                });
42            }
43        }
44
45        if !citations.is_empty() {
46            let broken_ratio = broken as f64 / citations.len() as f64;
47            score *= 1.0 - broken_ratio;
48        }
49        if !format_valid {
50            score *= 0.5;
51        }
52
53        LayerResult {
54            layer: ValidationLayerId::V6OutputIntegrity,
55            passed: score >= 0.5,
56            score,
57            issues,
58            duration_ms: start.elapsed().as_millis() as u64,
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn all_reachable_passes() {
69        let r = OutputValidator::validate(
70            &[CitationCheck {
71                citation_id: "1".into(),
72                url: "https://example.com".into(),
73                url_reachable: true,
74                content_hash_matches: true,
75            }],
76            true,
77        );
78        assert!(r.passed);
79        assert_eq!(r.score, 1.0);
80    }
81
82    #[test]
83    fn broken_citation_penalizes() {
84        let r = OutputValidator::validate(
85            &[CitationCheck {
86                citation_id: "1".into(),
87                url: "https://broken.example.com".into(),
88                url_reachable: false,
89                content_hash_matches: true,
90            }],
91            true,
92        );
93        assert!(r.issues.iter().any(|i| i.code == "V6_BROKEN_CITATION"));
94        assert!(!r.passed);
95    }
96
97    #[test]
98    fn content_changed_flagged() {
99        let r = OutputValidator::validate(
100            &[CitationCheck {
101                citation_id: "1".into(),
102                url: "https://example.com".into(),
103                url_reachable: true,
104                content_hash_matches: false,
105            }],
106            true,
107        );
108        assert!(r.issues.iter().any(|i| i.code == "V6_CONTENT_CHANGED"));
109    }
110}