reputation_types/
display.rs

1use std::fmt;
2use crate::{AgentData, ReputationScore, ConfidenceLevel};
3
4impl fmt::Display for ConfidenceLevel {
5    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6        match self {
7            ConfidenceLevel::Low => write!(f, "Low"),
8            ConfidenceLevel::Medium => write!(f, "Medium"),
9            ConfidenceLevel::High => write!(f, "High"),
10        }
11    }
12}
13
14impl fmt::Display for AgentData {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        write!(
17            f,
18            "Agent {} - Reviews: {}/{}, Rating: {:.1}",
19            self.did,
20            self.total_reviews,
21            self.total_interactions,
22            self.average_rating.unwrap_or(0.0)
23        )
24    }
25}
26
27impl fmt::Display for ReputationScore {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(
30            f,
31            "Score: {:.1}/100 (Confidence: {} - {:.1}%)",
32            self.score,
33            self.level,
34            self.confidence * 100.0
35        )
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use crate::{AgentDataBuilder, ScoreComponents, PriorBreakdown};
43    use chrono::Utc;
44
45    #[test]
46    fn test_confidence_level_display() {
47        assert_eq!(format!("{}", ConfidenceLevel::Low), "Low");
48        assert_eq!(format!("{}", ConfidenceLevel::Medium), "Medium");
49        assert_eq!(format!("{}", ConfidenceLevel::High), "High");
50    }
51
52    #[test]
53    fn test_agent_data_display() {
54        let agent = AgentDataBuilder::new("did:test:123")
55            .with_reviews(50, 4.5)
56            .total_interactions(100)
57            .build()
58            .unwrap();
59        
60        let display = format!("{}", agent);
61        assert_eq!(display, "Agent did:test:123 - Reviews: 50/100, Rating: 4.5");
62        
63        // Test agent with no rating
64        let agent_no_rating = AgentDataBuilder::new("did:test:456")
65            .total_interactions(10)
66            .build()
67            .unwrap();
68        
69        let display = format!("{}", agent_no_rating);
70        assert_eq!(display, "Agent did:test:456 - Reviews: 0/10, Rating: 0.0");
71    }
72
73    #[test]
74    fn test_reputation_score_display() {
75        let components = ScoreComponents {
76            prior_score: 65.0,
77            prior_breakdown: PriorBreakdown {
78                base_score: 50.0,
79                mcp_bonus: 10.0,
80                identity_bonus: 5.0,
81                security_audit_bonus: 0.0,
82                open_source_bonus: 0.0,
83                age_bonus: 0.0,
84                total: 65.0,
85            },
86            empirical_score: 80.0,
87            confidence_value: 0.85,
88            confidence_level: ConfidenceLevel::High,
89            prior_weight: 0.15,
90            empirical_weight: 0.85,
91        };
92        
93        let score = ReputationScore {
94            score: 77.3,
95            confidence: 0.856,
96            level: ConfidenceLevel::High,
97            components,
98            is_provisional: false,
99            data_points: 150,
100            algorithm_version: "1.0.0".to_string(),
101            calculated_at: Utc::now(),
102        };
103        
104        let display = format!("{}", score);
105        assert_eq!(display, "Score: 77.3/100 (Confidence: High - 85.6%)");
106    }
107}