claude-sdk-rs 1.0.0

Rust SDK for Claude AI with CLI integration - type-safe async API for Claude Code and direct SDK usage
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
//! Advanced cost tracking and analytics
//!
//! This module provides additional cost tracking capabilities including:
//! - Rate limiting based on cost thresholds
//! - Cost alerts and notifications
//! - Trend analysis and predictions
//! - Budget management

use super::{CostEntry, CostFilter, CostTracker};
use crate::{cli::error::InteractiveError, cli::error::Result, cli::session::SessionId};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};

/// Cost alert configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostAlert {
    pub id: String,
    pub name: String,
    pub threshold_usd: f64,
    pub period_days: u32,
    pub enabled: bool,
    pub notification_channels: Vec<String>,
}

/// Budget configuration for sessions or commands
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Budget {
    pub id: String,
    pub name: String,
    pub limit_usd: f64,
    pub period_days: u32,
    pub scope: BudgetScope,
    pub alert_thresholds: Vec<f64>, // Percentage thresholds (e.g., 50.0, 80.0, 95.0)
    pub created_at: DateTime<Utc>,
}

/// Scope for budget tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BudgetScope {
    Global,
    Session(SessionId),
    Command(String),
}

/// Cost trend analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostTrend {
    pub period_days: u32,
    pub daily_costs: Vec<(DateTime<Utc>, f64)>,
    pub trend_direction: TrendDirection,
    pub projected_monthly_cost: f64,
    pub growth_rate: f64, // Percentage change per day
}

/// Direction of cost trend
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TrendDirection {
    Increasing,
    Decreasing,
    Stable,
}

/// Budget status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetStatus {
    pub budget: Budget,
    pub current_spend: f64,
    pub remaining: f64,
    pub utilization_percent: f64,
    pub days_remaining: u32,
    pub projected_overage: Option<f64>,
    pub alerts_triggered: Vec<String>,
}

/// Advanced cost tracker with analytics capabilities
pub struct AdvancedCostTracker {
    base_tracker: CostTracker,
    budgets: Vec<Budget>,
    alerts: Vec<CostAlert>,
}

impl AdvancedCostTracker {
    /// Create a new advanced cost tracker
    pub fn new(base_tracker: CostTracker) -> Self {
        Self {
            base_tracker,
            budgets: Vec::new(),
            alerts: Vec::new(),
        }
    }

    /// Add a budget constraint
    pub fn add_budget(&mut self, budget: Budget) -> Result<()> {
        // Validate budget
        if budget.limit_usd <= 0.0 {
            return Err(InteractiveError::InvalidInput(
                "Budget limit must be positive".to_string(),
            ));
        }

        if budget.period_days == 0 {
            return Err(InteractiveError::InvalidInput(
                "Budget period must be at least 1 day".to_string(),
            ));
        }

        self.budgets.push(budget);
        Ok(())
    }

    /// Add a cost alert
    pub fn add_alert(&mut self, alert: CostAlert) -> Result<()> {
        if alert.threshold_usd <= 0.0 {
            return Err(InteractiveError::InvalidInput(
                "Alert threshold must be positive".to_string(),
            ));
        }

        self.alerts.push(alert);
        Ok(())
    }

    /// Check if a cost entry would exceed any budget limits
    pub async fn check_budget_limits(&self, entry: &CostEntry) -> Result<Vec<BudgetStatus>> {
        let mut violations = Vec::new();

        for budget in &self.budgets {
            let status = self.get_budget_status(budget).await?;

            // Check if this entry would cause a budget violation
            if status.current_spend + entry.cost_usd > budget.limit_usd {
                violations.push(status);
            }
        }

        Ok(violations)
    }

    /// Get budget status for a specific budget
    pub async fn get_budget_status(&self, budget: &Budget) -> Result<BudgetStatus> {
        let now = Utc::now();
        let period_start = now - Duration::days(budget.period_days as i64);

        let filter = CostFilter {
            since: Some(period_start),
            until: Some(now),
            session_id: match &budget.scope {
                BudgetScope::Session(id) => Some(*id),
                _ => None,
            },
            command_pattern: match &budget.scope {
                BudgetScope::Command(cmd) => Some(cmd.clone()),
                _ => None,
            },
            ..Default::default()
        };

        let summary = self.base_tracker.get_filtered_summary(&filter).await?;
        let current_spend = summary.total_cost;
        let remaining = budget.limit_usd - current_spend;
        let utilization_percent = (current_spend / budget.limit_usd) * 100.0;

        // Calculate projected overage based on trend
        let trend = self
            .calculate_cost_trend(budget.period_days, &filter)
            .await?;
        let days_remaining = budget.period_days;
        let projected_overage = if trend.projected_monthly_cost > budget.limit_usd {
            Some(trend.projected_monthly_cost - budget.limit_usd)
        } else {
            None
        };

        // Check which alert thresholds have been triggered
        let mut alerts_triggered = Vec::new();
        for threshold in &budget.alert_thresholds {
            if utilization_percent >= *threshold {
                alerts_triggered.push(format!("{}% budget utilization", threshold));
            }
        }

        Ok(BudgetStatus {
            budget: budget.clone(),
            current_spend,
            remaining,
            utilization_percent,
            days_remaining,
            projected_overage,
            alerts_triggered,
        })
    }

    /// Calculate cost trend analysis
    pub async fn calculate_cost_trend(
        &self,
        period_days: u32,
        filter: &CostFilter,
    ) -> Result<CostTrend> {
        let now = Utc::now();
        let period_start = now - Duration::days(period_days as i64);

        let mut daily_filter = filter.clone();
        let mut daily_costs = Vec::new();

        // Calculate daily costs for the period
        for day in 0..period_days {
            let day_start = period_start + Duration::days(day as i64);
            let day_end = day_start + Duration::days(1);

            daily_filter.since = Some(day_start);
            daily_filter.until = Some(day_end);

            let day_summary = self
                .base_tracker
                .get_filtered_summary(&daily_filter)
                .await?;
            daily_costs.push((day_start, day_summary.total_cost));
        }

        // Calculate trend direction and growth rate
        let total_costs: Vec<f64> = daily_costs.iter().map(|(_, cost)| *cost).collect();
        let (trend_direction, growth_rate) = self.calculate_trend_direction(&total_costs);

        // Project monthly cost based on current trend
        let recent_avg = if total_costs.len() >= 7 {
            total_costs.iter().rev().take(7).sum::<f64>() / 7.0
        } else {
            total_costs.iter().sum::<f64>() / total_costs.len().max(1) as f64
        };

        let projected_monthly_cost = recent_avg * 30.0 * (1.0 + growth_rate / 100.0);

        Ok(CostTrend {
            period_days,
            daily_costs,
            trend_direction,
            projected_monthly_cost,
            growth_rate,
        })
    }

    /// Get cost optimization recommendations
    pub async fn get_optimization_recommendations(&self) -> Result<Vec<String>> {
        let mut recommendations = Vec::new();

        // Get global summary
        let global_summary = self.base_tracker.get_global_summary().await?;

        // Analyze by command
        let top_commands = self.base_tracker.get_top_commands(10).await?;
        if let Some((most_expensive_cmd, cost)) = top_commands.first() {
            if *cost > global_summary.total_cost * 0.3 {
                recommendations.push(format!(
                    "Command '{}' accounts for {:.1}% of total costs. Consider optimizing or reducing usage.",
                    most_expensive_cmd,
                    (*cost / global_summary.total_cost) * 100.0
                ));
            }
        }

        // Analyze by model
        let mut model_costs: Vec<_> = global_summary.by_model.iter().collect();
        model_costs.sort_by(|a, b| b.1.partial_cmp(a.1).unwrap_or(std::cmp::Ordering::Equal));

        if let Some((most_expensive_model, cost)) = model_costs.first() {
            if **cost > global_summary.total_cost * 0.5 {
                recommendations.push(format!(
                    "Model '{}' accounts for {:.1}% of total costs. Consider using more cost-effective models for routine tasks.",
                    most_expensive_model,
                    (**cost / global_summary.total_cost) * 100.0
                ));
            }
        }

        // Analyze cost trends
        let trend = self
            .calculate_cost_trend(30, &CostFilter::default())
            .await?;
        match trend.trend_direction {
            TrendDirection::Increasing if trend.growth_rate > 10.0 => {
                recommendations.push(format!(
                    "Costs are increasing by {:.1}% per day. Review recent usage patterns and consider setting budgets.",
                    trend.growth_rate
                ));
            }
            _ => {}
        }

        // Analyze for many small commands
        let all_entries = self
            .base_tracker
            .get_entries(&CostFilter::default())
            .await?;
        let small_command_count = all_entries.iter().filter(|e| e.cost_usd < 0.01).count();

        if small_command_count > 10 {
            recommendations.push(format!(
                "Found {} commands with cost < $0.01. Consider batching small operations to reduce overhead.",
                small_command_count
            ));
        }

        // Budget recommendations
        if self.budgets.is_empty() {
            recommendations
                .push("Consider setting up budgets to track and control spending.".to_string());
        } else {
            for budget in &self.budgets {
                let status = self.get_budget_status(budget).await?;
                if status.utilization_percent > 80.0 {
                    recommendations.push(format!(
                        "Budget '{}' is {:.1}% utilized. Consider reviewing usage or increasing the budget.",
                        budget.name,
                        status.utilization_percent
                    ));
                }
            }
        }

        Ok(recommendations)
    }

    /// Export comprehensive cost report
    pub async fn export_detailed_report(&self, path: &std::path::Path) -> Result<()> {
        use std::io::Write;

        let mut file = std::fs::File::create(path)?;

        // Write report header
        writeln!(file, "# Claude AI Interactive - Cost Report")?;
        writeln!(
            file,
            "Generated: {}\n",
            Utc::now().format("%Y-%m-%d %H:%M:%S UTC")
        )?;

        // Global summary
        let global_summary = self.base_tracker.get_global_summary().await?;
        writeln!(file, "## Global Summary")?;
        writeln!(file, "- Total Cost: ${:.4}", global_summary.total_cost)?;
        writeln!(file, "- Total Commands: {}", global_summary.command_count)?;
        writeln!(
            file,
            "- Average Cost per Command: ${:.4}",
            global_summary.average_cost
        )?;
        writeln!(file, "- Total Tokens: {}", global_summary.total_tokens)?;
        writeln!(
            file,
            "- Date Range: {} to {}",
            global_summary.date_range.0.format("%Y-%m-%d"),
            global_summary.date_range.1.format("%Y-%m-%d")
        )?;
        writeln!(file)?;

        // Top commands
        writeln!(file, "## Top Commands by Cost")?;
        let top_commands = self.base_tracker.get_top_commands(10).await?;
        for (i, (cmd, cost)) in top_commands.iter().enumerate() {
            writeln!(file, "{}. {} - ${:.4}", i + 1, cmd, cost)?;
        }
        writeln!(file)?;

        // Cost by model
        writeln!(file, "## Cost by Model")?;
        let mut model_costs: Vec<_> = global_summary.by_model.iter().collect();
        model_costs.sort_by(|a, b| b.1.partial_cmp(a.1).unwrap_or(std::cmp::Ordering::Equal));
        for (model, cost) in model_costs {
            writeln!(file, "- {}: ${:.4}", model, cost)?;
        }
        writeln!(file)?;

        // Budget status
        if !self.budgets.is_empty() {
            writeln!(file, "## Budget Status")?;
            for budget in &self.budgets {
                let status = self.get_budget_status(budget).await?;
                writeln!(file, "### {}", budget.name)?;
                writeln!(file, "- Limit: ${:.2}", budget.limit_usd)?;
                writeln!(file, "- Current Spend: ${:.4}", status.current_spend)?;
                writeln!(file, "- Remaining: ${:.4}", status.remaining)?;
                writeln!(file, "- Utilization: {:.1}%", status.utilization_percent)?;
                if let Some(overage) = status.projected_overage {
                    writeln!(file, "- Projected Overage: ${:.4}", overage)?;
                }
                writeln!(file)?;
            }
        }

        // Recommendations
        let recommendations = self.get_optimization_recommendations().await?;
        if !recommendations.is_empty() {
            writeln!(file, "## Optimization Recommendations")?;
            for (i, rec) in recommendations.iter().enumerate() {
                writeln!(file, "{}. {}", i + 1, rec)?;
            }
        }

        Ok(())
    }

    /// Get cost trend analysis (simpler interface)
    pub async fn analyze_cost_trend(&self, period_days: u32) -> Result<CostTrend> {
        let filter = CostFilter::default();
        self.calculate_cost_trend(period_days, &filter).await
    }

    /// Check alerts for violations
    pub async fn check_alerts(&self) -> Result<Vec<(CostAlert, f64)>> {
        let mut triggered = Vec::new();

        for alert in &self.alerts {
            if !alert.enabled {
                continue;
            }

            let period_start = Utc::now() - Duration::days(alert.period_days as i64);
            let filter = CostFilter {
                since: Some(period_start),
                ..Default::default()
            };

            let summary = self.base_tracker.get_filtered_summary(&filter).await?;

            if summary.total_cost > alert.threshold_usd {
                triggered.push((alert.clone(), summary.total_cost));
            }
        }

        Ok(triggered)
    }

    /// Generate comprehensive cost report (alias for export_detailed_report)
    pub async fn generate_report(&self, path: &std::path::Path) -> Result<()> {
        self.export_detailed_report(path).await
    }

    // Private helper methods

    fn calculate_trend_direction(&self, costs: &[f64]) -> (TrendDirection, f64) {
        if costs.len() < 2 {
            return (TrendDirection::Stable, 0.0);
        }

        // Simple linear regression to determine trend
        let n = costs.len() as f64;
        let x_mean = (n - 1.0) / 2.0;
        let y_mean = costs.iter().sum::<f64>() / n;

        let mut numerator = 0.0;
        let mut denominator = 0.0;

        for (i, cost) in costs.iter().enumerate() {
            let x = i as f64;
            numerator += (x - x_mean) * (cost - y_mean);
            denominator += (x - x_mean).powi(2);
        }

        let slope = if denominator != 0.0 {
            numerator / denominator
        } else {
            0.0
        };

        let growth_rate = if y_mean != 0.0 {
            (slope / y_mean) * 100.0
        } else {
            0.0
        };

        let direction = if slope > 0.1 {
            TrendDirection::Increasing
        } else if slope < -0.1 {
            TrendDirection::Decreasing
        } else {
            TrendDirection::Stable
        };

        (direction, growth_rate)
    }

    // Public accessor methods for testing
    #[cfg(test)]
    pub fn budgets(&self) -> &Vec<Budget> {
        &self.budgets
    }

    #[cfg(test)]
    pub fn alerts(&self) -> &Vec<CostAlert> {
        &self.alerts
    }

    #[cfg(test)]
    pub async fn record_cost(&mut self, entry: CostEntry) -> Result<()> {
        self.base_tracker.record_cost(entry).await
    }

    /// Get number of budgets (for testing)
    #[cfg(test)]
    pub fn budget_count(&self) -> usize {
        self.budgets.len()
    }

    /// Get number of alerts (for testing)  
    #[cfg(test)]
    pub fn alert_count(&self) -> usize {
        self.alerts.len()
    }

    /// Get budget by index (for testing)
    #[cfg(test)]
    pub fn get_budget(&self, index: usize) -> Option<&Budget> {
        self.budgets.get(index)
    }

    /// Get cost entries with filtering (for testing)
    #[cfg(test)]
    pub async fn get_entries(&self, filter: &CostFilter) -> Result<Vec<CostEntry>> {
        self.base_tracker.get_entries(filter).await
    }

    /// Export to CSV (for testing)
    #[cfg(test)]
    pub async fn export_csv(&self, path: &PathBuf) -> Result<()> {
        self.base_tracker.export_csv(path).await
    }

    /// Get global summary (for testing)
    #[cfg(test)]
    pub async fn get_global_summary(&self) -> Result<CostSummary> {
        self.base_tracker.get_global_summary().await
    }
}

// Note: Types are already imported from super module

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;
    use uuid::Uuid;

    async fn create_test_tracker() -> AdvancedCostTracker {
        let temp_dir = tempdir().unwrap();
        let storage_path = temp_dir.path().join("test_costs.json");
        let base_tracker = CostTracker::new(storage_path).unwrap();
        AdvancedCostTracker::new(base_tracker)
    }

    #[tokio::test]
    async fn test_budget_creation() {
        let mut tracker = create_test_tracker().await;

        let budget = Budget {
            id: Uuid::new_v4().to_string(),
            name: "Monthly Budget".to_string(),
            limit_usd: 100.0,
            period_days: 30,
            scope: BudgetScope::Global,
            alert_thresholds: vec![50.0, 80.0, 95.0],
            created_at: Utc::now(),
        };

        tracker.add_budget(budget.clone()).unwrap();
        assert_eq!(tracker.budget_count(), 1);
        assert_eq!(tracker.get_budget(0).unwrap().name, "Monthly Budget");
    }

    #[tokio::test]
    async fn test_invalid_budget() {
        let mut tracker = create_test_tracker().await;

        // Test negative budget limit
        let invalid_budget = Budget {
            id: Uuid::new_v4().to_string(),
            name: "Invalid Budget".to_string(),
            limit_usd: -10.0,
            period_days: 30,
            scope: BudgetScope::Global,
            alert_thresholds: vec![],
            created_at: Utc::now(),
        };

        assert!(tracker.add_budget(invalid_budget).is_err());

        // Test zero period days
        let invalid_budget2 = Budget {
            id: Uuid::new_v4().to_string(),
            name: "Invalid Budget 2".to_string(),
            limit_usd: 100.0,
            period_days: 0,
            scope: BudgetScope::Global,
            alert_thresholds: vec![],
            created_at: Utc::now(),
        };

        assert!(tracker.add_budget(invalid_budget2).is_err());
    }

    #[tokio::test]
    async fn test_budget_status() {
        let mut tracker = create_test_tracker().await;
        let session_id = Uuid::new_v4();

        // Add budget
        let budget = Budget {
            id: Uuid::new_v4().to_string(),
            name: "Test Budget".to_string(),
            limit_usd: 50.0,
            period_days: 30,
            scope: BudgetScope::Global,
            alert_thresholds: vec![50.0, 80.0],
            created_at: Utc::now() - Duration::days(5),
        };
        tracker.add_budget(budget.clone()).unwrap();

        // Add some costs
        for i in 0..5 {
            tracker
                .base_tracker
                .record_cost(CostEntry::new(
                    session_id,
                    format!("cmd_{}", i),
                    5.0,
                    100,
                    200,
                    1000,
                    "claude-3-opus".to_string(),
                ))
                .await
                .unwrap();
        }

        let status = tracker.get_budget_status(&budget).await.unwrap();

        assert_eq!(status.budget.id, budget.id);
        assert!((status.current_spend - 25.0).abs() < 0.0001);
        assert!((status.remaining - 25.0).abs() < 0.0001);
        assert!((status.utilization_percent - 50.0).abs() < 0.1);
        assert_eq!(status.alerts_triggered.len(), 1); // 50% alert
    }

    #[tokio::test]
    async fn test_cost_alerts() {
        let mut tracker = create_test_tracker().await;

        let alert = CostAlert {
            id: Uuid::new_v4().to_string(),
            name: "High Cost Alert".to_string(),
            threshold_usd: 10.0,
            period_days: 1,
            enabled: true,
            notification_channels: vec!["email".to_string(), "slack".to_string()],
        };

        tracker.add_alert(alert.clone()).unwrap();
        assert_eq!(tracker.alert_count(), 1);

        // Add costs that should trigger alert
        let session_id = Uuid::new_v4();
        tracker
            .record_cost(CostEntry::new(
                session_id,
                "expensive_command".to_string(),
                15.0,
                500,
                1000,
                3000,
                "claude-3-opus".to_string(),
            ))
            .await
            .unwrap();

        let triggered = tracker.check_alerts().await.unwrap();
        assert_eq!(triggered.len(), 1);
        assert_eq!(triggered[0].0.id, alert.id);
        assert!((triggered[0].1 - 15.0).abs() < 0.0001);
    }

    #[tokio::test]
    async fn test_cost_trend_analysis() {
        let mut tracker = create_test_tracker().await;
        let session_id = Uuid::new_v4();

        // Add costs over several days with increasing trend
        for day in 0..7 {
            let cost = 10.0 + (day as f64 * 2.0); // Increasing costs
            let timestamp = Utc::now() - Duration::days(6 - day);

            let mut entry = CostEntry::new(
                session_id,
                format!("cmd_day_{}", day),
                cost,
                100,
                200,
                1000,
                "claude-3-opus".to_string(),
            );
            entry.timestamp = timestamp;

            tracker.record_cost(entry).await.unwrap();
        }

        let trend = tracker.analyze_cost_trend(7).await.unwrap();

        assert_eq!(trend.period_days, 7);
        assert!(matches!(trend.trend_direction, TrendDirection::Increasing));
        assert!(trend.growth_rate > 0.0);
        assert!(trend.projected_monthly_cost > 0.0);
        assert_eq!(trend.daily_costs.len(), 7);
    }

    #[tokio::test]
    async fn test_stable_trend() {
        let mut tracker = create_test_tracker().await;
        let session_id = Uuid::new_v4();

        // Add consistent costs
        for day in 0..5 {
            let timestamp = Utc::now() - Duration::days(4 - day);
            let mut entry = CostEntry::new(
                session_id,
                format!("cmd_{}", day),
                10.0, // Same cost each day
                100,
                200,
                1000,
                "claude-3-opus".to_string(),
            );
            entry.timestamp = timestamp;
            tracker.record_cost(entry).await.unwrap();
        }

        let trend = tracker.analyze_cost_trend(5).await.unwrap();
        assert!(matches!(trend.trend_direction, TrendDirection::Stable));
    }

    #[tokio::test]
    async fn test_session_budget() {
        let mut tracker = create_test_tracker().await;
        let session_id = Uuid::new_v4();

        // Add session-specific budget
        let budget = Budget {
            id: Uuid::new_v4().to_string(),
            name: "Session Budget".to_string(),
            limit_usd: 20.0,
            period_days: 30,
            scope: BudgetScope::Session(session_id),
            alert_thresholds: vec![90.0],
            created_at: Utc::now(),
        };
        tracker.add_budget(budget.clone()).unwrap();

        // Add costs to different sessions
        tracker
            .record_cost(CostEntry::new(
                session_id,
                "cmd1".to_string(),
                15.0,
                100,
                200,
                1000,
                "claude-3-opus".to_string(),
            ))
            .await
            .unwrap();

        let other_session = Uuid::new_v4();
        tracker
            .record_cost(CostEntry::new(
                other_session,
                "cmd2".to_string(),
                10.0,
                100,
                200,
                1000,
                "claude-3-opus".to_string(),
            ))
            .await
            .unwrap();

        let status = tracker.get_budget_status(&budget).await.unwrap();
        assert!((status.current_spend - 15.0).abs() < 0.0001); // Only session costs
        assert!((status.remaining - 5.0).abs() < 0.0001);
    }

    #[tokio::test]
    async fn test_command_budget() {
        let mut tracker = create_test_tracker().await;
        let session_id = Uuid::new_v4();

        // Add command-specific budget
        let budget = Budget {
            id: Uuid::new_v4().to_string(),
            name: "Analyze Command Budget".to_string(),
            limit_usd: 10.0,
            period_days: 7,
            scope: BudgetScope::Command("analyze".to_string()),
            alert_thresholds: vec![],
            created_at: Utc::now(),
        };
        tracker.add_budget(budget.clone()).unwrap();

        // Add various commands
        tracker
            .record_cost(CostEntry::new(
                session_id,
                "analyze".to_string(),
                8.0,
                100,
                200,
                1000,
                "claude-3-opus".to_string(),
            ))
            .await
            .unwrap();

        tracker
            .record_cost(CostEntry::new(
                session_id,
                "generate".to_string(),
                5.0,
                100,
                200,
                1000,
                "claude-3-opus".to_string(),
            ))
            .await
            .unwrap();

        let status = tracker.get_budget_status(&budget).await.unwrap();
        assert!((status.current_spend - 8.0).abs() < 0.0001); // Only analyze command
    }

    #[tokio::test]
    async fn test_optimization_recommendations() {
        let mut tracker = create_test_tracker().await;
        let session_id = Uuid::new_v4();

        // Add costs with patterns that should trigger recommendations

        // Many small commands
        for _i in 0..20 {
            tracker
                .base_tracker
                .record_cost(CostEntry::new(
                    session_id,
                    "small_cmd".to_string(),
                    0.001,
                    10,
                    20,
                    100,
                    "claude-3-haiku".to_string(),
                ))
                .await
                .unwrap();
        }

        // One expensive command
        tracker
            .record_cost(CostEntry::new(
                session_id,
                "expensive_cmd".to_string(),
                5.0,
                1000,
                2000,
                5000,
                "claude-3-opus".to_string(),
            ))
            .await
            .unwrap();

        let recommendations = tracker.get_optimization_recommendations().await.unwrap();
        assert!(!recommendations.is_empty());

        // Should recommend batching small commands
        assert!(recommendations.iter().any(|r| r.contains("batch")));
    }

    #[tokio::test]
    async fn test_generate_report() {
        let mut tracker = create_test_tracker().await;
        let session_id = Uuid::new_v4();

        // Add some test data
        tracker
            .record_cost(CostEntry::new(
                session_id,
                "test_cmd".to_string(),
                1.5,
                150,
                300,
                2000,
                "claude-3-opus".to_string(),
            ))
            .await
            .unwrap();

        // Add a budget
        let budget = Budget {
            id: Uuid::new_v4().to_string(),
            name: "Test Budget".to_string(),
            limit_usd: 10.0,
            period_days: 30,
            scope: BudgetScope::Global,
            alert_thresholds: vec![],
            created_at: Utc::now(),
        };
        tracker.add_budget(budget).unwrap();

        let temp_dir = tempdir().unwrap();
        let report_path = temp_dir.path().join("cost_report.md");

        tracker.generate_report(&report_path).await.unwrap();

        assert!(report_path.exists());
        let content = tokio::fs::read_to_string(&report_path).await.unwrap();
        assert!(content.contains("# Claude AI Interactive - Cost Report"));
        assert!(content.contains("Total Cost:"));
        assert!(content.contains("Budget Status"));
    }

    #[test]
    fn test_trend_calculation() {
        let tracker = AdvancedCostTracker {
            base_tracker: CostTracker::new(PathBuf::from("/tmp/test")).unwrap(),
            budgets: vec![],
            alerts: vec![],
        };

        // Test increasing trend
        let costs = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let (direction, growth_rate) = tracker.calculate_trend_direction(&costs);
        assert!(matches!(direction, TrendDirection::Increasing));
        assert!(growth_rate > 0.0);

        // Test decreasing trend
        let costs = vec![5.0, 4.0, 3.0, 2.0, 1.0];
        let (direction, growth_rate) = tracker.calculate_trend_direction(&costs);
        assert!(matches!(direction, TrendDirection::Decreasing));
        assert!(growth_rate < 0.0);

        // Test stable trend
        let costs = vec![2.0, 2.1, 2.0, 1.9, 2.0];
        let (direction, _) = tracker.calculate_trend_direction(&costs);
        assert!(matches!(direction, TrendDirection::Stable));

        // Test insufficient data
        let costs = vec![1.0];
        let (direction, growth_rate) = tracker.calculate_trend_direction(&costs);
        assert!(matches!(direction, TrendDirection::Stable));
        assert_eq!(growth_rate, 0.0);
    }

    #[tokio::test]
    async fn test_projected_overage() {
        let mut tracker = create_test_tracker().await;
        let session_id = Uuid::new_v4();

        // Add budget that starts 10 days ago
        let budget = Budget {
            id: Uuid::new_v4().to_string(),
            name: "Overage Test Budget".to_string(),
            limit_usd: 100.0,
            period_days: 30,
            scope: BudgetScope::Global,
            alert_thresholds: vec![],
            created_at: Utc::now() - Duration::days(10),
        };
        tracker.add_budget(budget.clone()).unwrap();

        // Add costs that would project over budget
        // $50 in 10 days = $5/day, projected to $150 in 30 days
        for i in 0..10 {
            let timestamp = Utc::now() - Duration::days(9 - i);
            let mut entry = CostEntry::new(
                session_id,
                format!("cmd_{}", i),
                5.0,
                100,
                200,
                1000,
                "claude-3-opus".to_string(),
            );
            entry.timestamp = timestamp;
            tracker.record_cost(entry).await.unwrap();
        }

        let status = tracker.get_budget_status(&budget).await.unwrap();
        assert!(status.projected_overage.is_some());
        let overage = status.projected_overage.unwrap();
        assert!(overage > 0.0); // Should project overage
    }
}