lean_ctx/core/context_kernel/
quality_e2e.rs1#[cfg(test)]
4mod tests {
5 use super::super::accounting_fix;
6 use super::super::hotpath_wiring;
7 use super::super::outcome_signal;
8 use super::super::types::ReceiptOutcome;
9
10 fn assert_close(actual: f64, expected: f64) {
11 assert!(
12 (actual - expected).abs() < 1e-9,
13 "expected {expected}, got {actual}"
14 );
15 }
16
17 #[test]
18 fn full_pipeline_dedup_to_accounting() {
19 let integration = hotpath_wiring::kernel_integrate("test query", "/tmp/test", 1_000, 300);
20
21 assert!(integration.accounting.delivered_tokens > 0);
22 assert!(integration.accounting.phantom_savings_pct >= 0.0);
23 }
24
25 #[test]
26 fn outcome_signal_first_pass() {
27 let inference = outcome_signal::infer_outcome(1, false, 500);
28
29 assert_eq!(inference.outcome, ReceiptOutcome::Accepted);
30 assert_eq!(format!("{:?}", inference.signal), "FirstPass");
31 assert!(inference.confidence >= 0.8);
32 }
33
34 #[test]
35 fn outcome_signal_retry_rejected() {
36 let inference = outcome_signal::infer_outcome(3, true, 200);
37
38 assert_eq!(inference.outcome, ReceiptOutcome::Rejected);
39 assert_eq!(format!("{:?}", inference.signal), "Retry");
40 }
41
42 #[test]
43 fn honest_accounting_vs_phantom() {
44 let accounting = accounting_fix::compute_honest_accounting(1_000, 300, 100, 50);
45
46 assert_eq!(accounting.delivered_tokens, 450);
47 assert_close(accounting.actual_compression_ratio, 0.55);
48 assert_close(accounting.reported_compression_ratio, 0.70);
49 assert_close(accounting.phantom_savings_pct, 0.15);
50 }
51
52 #[test]
53 fn outcome_tracker_quality_trend() {
54 let mut tracker = outcome_signal::OutcomeTracker::default();
55 for _ in 0..3 {
56 let inf = outcome_signal::infer_outcome(1, false, 100);
57 tracker.record(&inf);
58 }
59 for _ in 0..5 {
60 let inf = outcome_signal::infer_outcome(3, true, 100);
61 tracker.record(&inf);
62 }
63
64 assert_close(tracker.acceptance_rate(), 0.375);
65 assert!(tracker.is_degrading(5));
66 }
67
68 #[test]
69 fn integration_budget_never_exceeds_cap() {
70 for (original, compressed) in [
71 (0, 0),
72 (100, 25),
73 (1_000, 300),
74 (10_000, 9_500),
75 (usize::MAX, usize::MAX),
76 ] {
77 let integration = hotpath_wiring::kernel_integrate(
78 "budget cap query",
79 "/tmp/test",
80 original,
81 compressed,
82 );
83
84 assert!(integration.budget_used <= 150);
85 }
86 }
87}