reputation-core 0.1.0

Core calculation engine for the KnowThat Reputation System with advanced scoring algorithms
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//! Test vector generation and validation for transparency
//! 
//! Generates comprehensive JSON test vectors documenting expected behavior
//! of the reputation algorithm for third-party verification.

use reputation_core::Calculator;
use reputation_types::{AgentData, AgentDataBuilder, ReputationScore};
use chrono::{Duration, Utc, DateTime};
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// Test vector file format
#[derive(Debug, Serialize, Deserialize)]
pub struct TestVectorFile {
    pub version: String,
    pub algorithm: String,
    pub generated: DateTime<Utc>,
    pub calculator_config: CalculatorConfig,
    pub test_categories: HashMap<String, String>,
    pub test_cases: Vec<TestCase>,
}

/// Calculator configuration used for test vectors
#[derive(Debug, Serialize, Deserialize)]
pub struct CalculatorConfig {
    pub confidence_k: f64,
    pub prior_base: f64,
    pub prior_max: f64,
}

/// Individual test case
#[derive(Debug, Serialize, Deserialize)]
pub struct TestCase {
    pub id: String,
    pub category: String,
    pub description: String,
    pub input: TestInput,
    pub expected: ExpectedResult,
}

/// Test input (subset of AgentData fields)
#[derive(Debug, Serialize, Deserialize)]
pub struct TestInput {
    pub did: String,
    pub created_at: DateTime<Utc>,
    pub mcp_level: Option<u8>,
    pub identity_verified: bool,
    pub security_audit_passed: bool,
    pub open_source: bool,
    pub total_interactions: u32,
    pub total_reviews: u32,
    pub average_rating: Option<f64>,
    pub positive_reviews: u32,
    pub negative_reviews: u32,
}

/// Expected result
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ExpectedResult {
    Success(ExpectedScore),
    Error(ExpectedError),
}

/// Expected score details
#[derive(Debug, Serialize, Deserialize)]
pub struct ExpectedScore {
    pub score: f64,
    pub confidence: f64,
    pub level: String,
    pub components: ScoreComponents,
    pub is_provisional: bool,
    pub data_points: u32,
    pub algorithm_version: String,
}

/// Score component breakdown
#[derive(Debug, Serialize, Deserialize)]
pub struct ScoreComponents {
    pub prior_score: f64,
    pub prior_breakdown: PriorBreakdown,
    pub empirical_score: f64,
    pub confidence_value: f64,
    pub confidence_level: String,
    pub prior_weight: f64,
    pub empirical_weight: f64,
}

/// Prior score breakdown
#[derive(Debug, Serialize, Deserialize)]
pub struct PriorBreakdown {
    pub base_score: f64,
    pub mcp_bonus: f64,
    pub identity_bonus: f64,
    pub security_audit_bonus: f64,
    pub open_source_bonus: f64,
    pub age_bonus: f64,
    pub total: f64,
}

/// Expected error
#[derive(Debug, Serialize, Deserialize)]
pub struct ExpectedError {
    pub error_type: String,
    pub message: String,
}

/// Validation report
#[derive(Debug)]
pub struct ValidationReport {
    pub total: usize,
    pub passed: usize,
    pub failed: usize,
    pub failures: Vec<ValidationFailure>,
}

/// Validation failure details
#[derive(Debug)]
pub struct ValidationFailure {
    pub test_id: String,
    pub reason: String,
    pub expected: String,
    pub actual: String,
}

impl TestVectorFile {
    /// Create a new test vector file
    pub fn new() -> Self {
        Self {
            version: "1.0.0".to_string(),
            algorithm: "bayesian_reputation_v1".to_string(),
            generated: Utc::now(),
            calculator_config: CalculatorConfig {
                confidence_k: 15.0,
                prior_base: 50.0,
                prior_max: 80.0,
            },
            test_categories: HashMap::from([
                ("new_agents".to_string(), "Agents with no interaction history".to_string()),
                ("verified_agents".to_string(), "Agents with identity verification".to_string()),
                ("high_interaction".to_string(), "Agents with significant activity".to_string()),
                ("edge_cases".to_string(), "Boundary conditions and limits".to_string()),
                ("error_cases".to_string(), "Invalid inputs and expected errors".to_string()),
                ("confidence_levels".to_string(), "Different confidence level examples".to_string()),
                ("review_patterns".to_string(), "Various rating distributions".to_string()),
            ]),
            test_cases: Vec::new(),
        }
    }

    /// Add a test case
    pub fn add_case(&mut self, case: TestCase) {
        self.test_cases.push(case);
    }
}

/// Convert AgentData to TestInput
impl From<&AgentData> for TestInput {
    fn from(agent: &AgentData) -> Self {
        Self {
            did: agent.did.clone(),
            created_at: agent.created_at,
            mcp_level: agent.mcp_level,
            identity_verified: agent.identity_verified,
            security_audit_passed: agent.security_audit_passed,
            open_source: agent.open_source,
            total_interactions: agent.total_interactions,
            total_reviews: agent.total_reviews,
            average_rating: agent.average_rating,
            positive_reviews: agent.positive_reviews,
            negative_reviews: agent.negative_reviews,
        }
    }
}

/// Convert ReputationScore to ExpectedScore
fn score_to_expected(score: &ReputationScore) -> ExpectedScore {
    ExpectedScore {
        score: score.score,
        confidence: score.confidence,
        level: format!("{:?}", score.level),
        components: ScoreComponents {
            prior_score: score.components.prior_score,
            prior_breakdown: PriorBreakdown {
                base_score: score.components.prior_breakdown.base_score,
                mcp_bonus: score.components.prior_breakdown.mcp_bonus,
                identity_bonus: score.components.prior_breakdown.identity_bonus,
                security_audit_bonus: score.components.prior_breakdown.security_audit_bonus,
                open_source_bonus: score.components.prior_breakdown.open_source_bonus,
                age_bonus: score.components.prior_breakdown.age_bonus,
                total: score.components.prior_breakdown.total,
            },
            empirical_score: score.components.empirical_score,
            confidence_value: score.components.confidence_value,
            confidence_level: format!("{:?}", score.components.confidence_level),
            prior_weight: score.components.prior_weight,
            empirical_weight: score.components.empirical_weight,
        },
        is_provisional: score.is_provisional,
        data_points: score.data_points,
        algorithm_version: score.algorithm_version.clone(),
    }
}

/// Generate comprehensive test vectors
pub fn generate_test_vectors() -> TestVectorFile {
    let mut vectors = TestVectorFile::new();
    let calc = Calculator::default();
    
    let mut case_id = 1;
    
    // Category 1: New Agents
    // TC001: Brand new agent with no history
    let agent = AgentDataBuilder::new("did:test:new-agent")
        .created_at(Utc::now())
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "new_agents".to_string(),
        description: "Brand new agent with no history".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // TC002: Week-old agent with no activity
    let agent = AgentDataBuilder::new("did:test:week-old")
        .created_at(Utc::now() - Duration::days(7))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "new_agents".to_string(),
        description: "Week-old agent with no activity".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // TC003: Month-old agent with no activity  
    let agent = AgentDataBuilder::new("did:test:month-old")
        .created_at(Utc::now() - Duration::days(30))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "new_agents".to_string(),
        description: "Month-old agent with no activity".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // Category 2: Verified Agents
    // TC004-006: MCP Level 1, 2, 3
    for mcp_level in 1..=3 {
        let agent = AgentDataBuilder::new(&format!("did:test:mcp-{}", mcp_level))
            .mcp_level(mcp_level)
            .created_at(Utc::now() - Duration::days(90))
            .build()
            .unwrap();
        let score = calc.calculate(&agent).unwrap();
        vectors.add_case(TestCase {
            id: format!("TC{:03}", case_id),
            category: "verified_agents".to_string(),
            description: format!("Agent with MCP Level {}", mcp_level),
            input: TestInput::from(&agent),
            expected: ExpectedResult::Success(score_to_expected(&score)),
        });
        case_id += 1;
    }
    
    // TC007: Identity verified agent
    let agent = AgentDataBuilder::new("did:test:identity-verified")
        .identity_verified(true)
        .created_at(Utc::now() - Duration::days(60))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "verified_agents".to_string(),
        description: "Agent with identity verification".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // TC008: Fully verified agent (all credentials)
    let agent = AgentDataBuilder::new("did:test:fully-verified")
        .mcp_level(3)
        .identity_verified(true)
        .security_audit_passed(true)
        .open_source(true)
        .created_at(Utc::now() - Duration::days(180))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "verified_agents".to_string(),
        description: "Fully verified agent with all credentials".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // Category 3: Review Patterns
    // TC009-013: Different ratings (1-5 stars)
    for rating in 1..=5 {
        let agent = AgentDataBuilder::new(&format!("did:test:rating-{}", rating))
            .total_interactions(100)
            .with_reviews(50, rating as f64)
            .created_at(Utc::now() - Duration::days(30))
            .build()
            .unwrap();
        let score = calc.calculate(&agent).unwrap();
        vectors.add_case(TestCase {
            id: format!("TC{:03}", case_id),
            category: "review_patterns".to_string(),
            description: format!("Agent with {} star rating", rating),
            input: TestInput::from(&agent),
            expected: ExpectedResult::Success(score_to_expected(&score)),
        });
        case_id += 1;
    }
    
    // TC014: Mixed reviews (3.5 rating)
    let agent = AgentDataBuilder::new("did:test:mixed-reviews")
        .total_interactions(200)
        .with_reviews(100, 3.5)
        .created_at(Utc::now() - Duration::days(60))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "review_patterns".to_string(),
        description: "Agent with mixed reviews (3.5 stars)".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // Category 4: Confidence Levels
    // TC015-018: Low, Medium, High, Very High confidence
    let confidence_cases = [
        (1, "Low confidence (1 interaction)"),
        (10, "Low-Medium confidence (10 interactions)"),
        (50, "Medium confidence (50 interactions)"),
        (200, "High confidence (200 interactions)"),
        (1000, "Very high confidence (1000 interactions)"),
    ];
    
    for (interactions, desc) in confidence_cases {
        let mut builder = AgentDataBuilder::new(&format!("did:test:conf-{}", interactions))
            .total_interactions(interactions)
            .created_at(Utc::now() - Duration::days(90));
        
        // Only add reviews if there are interactions
        if interactions > 0 {
            let reviews = (interactions / 2).max(1); // At least 1 review if there are interactions
            builder = builder.with_reviews(reviews, 4.0);
        }
        
        let agent = builder.build().unwrap();
        let score = calc.calculate(&agent).unwrap();
        vectors.add_case(TestCase {
            id: format!("TC{:03}", case_id),
            category: "confidence_levels".to_string(),
            description: desc.to_string(),
            input: TestInput::from(&agent),
            expected: ExpectedResult::Success(score_to_expected(&score)),
        });
        case_id += 1;
    }
    
    // Category 5: Edge Cases
    // TC020: Zero reviews but many interactions
    let agent = AgentDataBuilder::new("did:test:no-reviews")
        .total_interactions(500)
        .created_at(Utc::now() - Duration::days(30))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "edge_cases".to_string(),
        description: "Many interactions but no reviews".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // TC021: Maximum allowed values
    let agent = AgentDataBuilder::new("did:test:max-values")
        .total_interactions(1_000_000)
        .with_reviews(500_000, 5.0)
        .mcp_level(3)
        .identity_verified(true)
        .security_audit_passed(true)
        .open_source(true)
        .created_at(Utc::now() - Duration::days(730))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "edge_cases".to_string(),
        description: "Maximum allowed values".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // TC022: One review only
    let agent = AgentDataBuilder::new("did:test:one-review")
        .total_interactions(1)
        .with_reviews(1, 5.0)
        .created_at(Utc::now() - Duration::days(1))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "edge_cases".to_string(),
        description: "Single perfect review".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // TC023: Old agent reaching prior cap
    let agent = AgentDataBuilder::new("did:test:prior-cap")
        .mcp_level(3)
        .identity_verified(true)
        .security_audit_passed(true)
        .open_source(true)
        .created_at(Utc::now() - Duration::days(365))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "edge_cases".to_string(),
        description: "Agent reaching prior score cap".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    case_id += 1;
    
    // TC024: Boundary confidence (exactly 0.2)
    let agent = AgentDataBuilder::new("did:test:boundary-conf")
        .total_interactions(4) // 4/(4+15) ≈ 0.21
        .created_at(Utc::now() - Duration::days(7))
        .build()
        .unwrap();
    let score = calc.calculate(&agent).unwrap();
    vectors.add_case(TestCase {
        id: format!("TC{:03}", case_id),
        category: "edge_cases".to_string(),
        description: "Boundary confidence level".to_string(),
        input: TestInput::from(&agent),
        expected: ExpectedResult::Success(score_to_expected(&score)),
    });
    
    // Additional test cases for comprehensive coverage
    generate_additional_test_cases(&mut vectors, &calc, case_id);
    
    vectors
}

/// Generate additional test cases for comprehensive coverage
fn generate_additional_test_cases(vectors: &mut TestVectorFile, calc: &Calculator, mut case_id: usize) {
    case_id += 1;
    
    // High interaction patterns
    let interaction_levels = [100, 500, 1000, 5000, 10000];
    for interactions in interaction_levels {
        let agent = AgentDataBuilder::new(&format!("did:test:high-int-{}", interactions))
            .total_interactions(interactions)
            .with_reviews((interactions as f64 * 0.3) as u32, 4.2)
            .created_at(Utc::now() - Duration::days(180))
            .build()
            .unwrap();
        let score = calc.calculate(&agent).unwrap();
        vectors.add_case(TestCase {
            id: format!("TC{:03}", case_id),
            category: "high_interaction".to_string(),
            description: format!("Agent with {} interactions", interactions),
            input: TestInput::from(&agent),
            expected: ExpectedResult::Success(score_to_expected(&score)),
        });
        case_id += 1;
    }
    
    // Various credential combinations
    let credential_combos = [
        (Some(1), false, false, false, "MCP Level 1 only"),
        (None, true, false, false, "Identity verified only"),
        (None, false, true, false, "Security audit only"),
        (None, false, false, true, "Open source only"),
        (Some(2), true, false, false, "MCP Level 2 + Identity"),
        (Some(3), true, true, true, "All credentials maxed"),
    ];
    
    for (mcp, id, sec, os, desc) in credential_combos {
        let mut builder = AgentDataBuilder::new(&format!("did:test:cred-{}", case_id))
            .total_interactions(50)
            .with_reviews(25, 4.0)
            .created_at(Utc::now() - Duration::days(90));
        
        if let Some(level) = mcp {
            builder = builder.mcp_level(level);
        }
        builder = builder
            .identity_verified(id)
            .security_audit_passed(sec)
            .open_source(os);
        
        let agent = builder.build().unwrap();
        let score = calc.calculate(&agent).unwrap();
        vectors.add_case(TestCase {
            id: format!("TC{:03}", case_id),
            category: "verified_agents".to_string(),
            description: desc.to_string(),
            input: TestInput::from(&agent),
            expected: ExpectedResult::Success(score_to_expected(&score)),
        });
        case_id += 1;
    }
    
    // Age bonus progression
    let age_days = [0, 30, 60, 90, 180, 365];
    for days in age_days {
        let agent = AgentDataBuilder::new(&format!("did:test:age-{}", days))
            .created_at(Utc::now() - Duration::days(days))
            .build()
            .unwrap();
        let score = calc.calculate(&agent).unwrap();
        vectors.add_case(TestCase {
            id: format!("TC{:03}", case_id),
            category: "edge_cases".to_string(),
            description: format!("Agent aged {} days", days),
            input: TestInput::from(&agent),
            expected: ExpectedResult::Success(score_to_expected(&score)),
        });
        case_id += 1;
    }
}

/// Validate test vectors against a calculator
pub fn validate_test_vectors(calc: &Calculator, vectors: &TestVectorFile) -> ValidationReport {
    let mut report = ValidationReport {
        total: vectors.test_cases.len(),
        passed: 0,
        failed: 0,
        failures: Vec::new(),
    };
    
    for test_case in &vectors.test_cases {
        match &test_case.expected {
            ExpectedResult::Success(expected) => {
                // Convert TestInput back to AgentData
                let mut builder = AgentDataBuilder::new(&test_case.input.did)
                    .created_at(test_case.input.created_at)
                    .total_interactions(test_case.input.total_interactions)
                    .total_reviews(test_case.input.total_reviews)
                    .identity_verified(test_case.input.identity_verified)
                    .security_audit_passed(test_case.input.security_audit_passed)
                    .open_source(test_case.input.open_source);
                
                if let Some(level) = test_case.input.mcp_level {
                    builder = builder.mcp_level(level);
                }
                
                if test_case.input.total_reviews > 0 {
                    if let Some(rating) = test_case.input.average_rating {
                        builder = builder.with_reviews(test_case.input.total_reviews, rating);
                    }
                }
                
                let agent = builder.build().unwrap();
                
                match calc.calculate(&agent) {
                    Ok(actual) => {
                        if validate_score_match(expected, &actual) {
                            report.passed += 1;
                        } else {
                            report.failed += 1;
                            report.failures.push(ValidationFailure {
                                test_id: test_case.id.clone(),
                                reason: "Score mismatch".to_string(),
                                expected: format!("{:?}", expected),
                                actual: format!("{:?}", actual),
                            });
                        }
                    }
                    Err(e) => {
                        report.failed += 1;
                        report.failures.push(ValidationFailure {
                            test_id: test_case.id.clone(),
                            reason: "Unexpected error".to_string(),
                            expected: format!("{:?}", expected),
                            actual: format!("Error: {:?}", e),
                        });
                    }
                }
            }
            ExpectedResult::Error(_expected_error) => {
                // For error cases, we expect calculation to fail
                report.passed += 1; // Simplified for now
            }
        }
    }
    
    report
}

/// Check if scores match within tolerance
fn validate_score_match(expected: &ExpectedScore, actual: &ReputationScore) -> bool {
    const TOLERANCE: f64 = 0.0001;
    
    (expected.score - actual.score).abs() < TOLERANCE
        && (expected.confidence - actual.confidence).abs() < TOLERANCE
        && expected.is_provisional == actual.is_provisional
        && expected.data_points == actual.data_points
}

/// Save test vectors to file
pub fn save_test_vectors(vectors: &TestVectorFile, path: &Path) -> std::io::Result<()> {
    let json = serde_json::to_string_pretty(vectors)?;
    fs::write(path, json)?;
    Ok(())
}

/// Load test vectors from file
pub fn load_test_vectors(path: &Path) -> std::io::Result<TestVectorFile> {
    let json = fs::read_to_string(path)?;
    let vectors = serde_json::from_str(&json)?;
    Ok(vectors)
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_generate_test_vectors() {
        let vectors = generate_test_vectors();
        
        // Verify we have sufficient test cases
        assert!(vectors.test_cases.len() >= 40, "Should have at least 40 test cases");
        
        // Verify all categories are represented
        let categories: Vec<_> = vectors.test_cases.iter()
            .map(|tc| tc.category.as_str())
            .collect();
        
        assert!(categories.contains(&"new_agents"));
        assert!(categories.contains(&"verified_agents"));
        assert!(categories.contains(&"review_patterns"));
        assert!(categories.contains(&"confidence_levels"));
        assert!(categories.contains(&"edge_cases"));
        assert!(categories.contains(&"high_interaction"));
        
        // Validate against current implementation
        let calc = Calculator::default();
        let report = validate_test_vectors(&calc, &vectors);
        
        if !report.failures.is_empty() {
            for failure in &report.failures {
                eprintln!("Test {} failed: {}", failure.test_id, failure.reason);
            }
        }
        
        assert_eq!(report.failed, 0, "All test vectors should pass validation");
    }
    
    #[test]
    fn test_save_and_load_vectors() {
        let vectors = generate_test_vectors();
        let temp_path = std::env::temp_dir().join("test_vectors.json");
        
        // Save vectors
        save_test_vectors(&vectors, &temp_path).unwrap();
        
        // Load vectors
        let loaded = load_test_vectors(&temp_path).unwrap();
        
        // Verify loaded content
        assert_eq!(loaded.version, vectors.version);
        assert_eq!(loaded.test_cases.len(), vectors.test_cases.len());
        
        // Clean up
        std::fs::remove_file(&temp_path).ok();
    }
}