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
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
//! Comprehensive tests for dashboard data generation functionality
//!
//! This module tests dashboard metrics calculation, data aggregation,
//! performance indicators, and caching logic.

use super::dashboard::*;
use super::dashboard_test::*;
use super::test_utils::*;
use crate::cli::analytics::{Alert, AlertSeverity, AlertType, AnalyticsEngine};
use crate::cli::cost::{CostEntry, CostFilter, CostTracker};
use crate::cli::error::Result;
use crate::cli::history::{HistoryEntry, HistorySearch, HistoryStore};
use chrono::{DateTime, Duration, TimeZone, Timelike, Utc};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tempfile::TempDir;
use tokio::sync::RwLock;

/// Task 3.2.1: Test dashboard metrics calculation with various time periods
#[cfg(test)]
mod metrics_calculation_tests {
    use super::*;

    #[tokio::test]
    async fn test_daily_metrics_calculation() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Generate 24 hours of data using the fixture's method
        fixture.populate_test_data(1).await?;

        // Get dashboard data
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;

        // Verify daily metrics
        assert!(
            dashboard_data.today_cost >= 0.0,
            "Daily cost should be non-negative"
        );
        assert!(
            dashboard_data.today_commands > 0,
            "Should have commands today"
        );
        assert!(
            dashboard_data.success_rate >= 0.0 && dashboard_data.success_rate <= 100.0,
            "Success rate should be between 0 and 100"
        );

        // Verify that metrics are from today only
        let today_start = Utc::now()
            .date_naive()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_utc();
        for entry in &dashboard_data.recent_activity {
            assert!(
                entry.timestamp >= today_start,
                "Recent activity should only include today's entries"
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_weekly_metrics_calculation() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Generate 7 days of data
        fixture.populate_test_data(7).await?;

        // Calculate weekly summary
        let summary = fixture.analytics_engine.generate_summary(7).await?;

        // Verify weekly metrics
        assert!(
            summary.cost_summary.total_cost > 0.0,
            "Weekly cost should be positive"
        );
        assert!(
            summary.history_stats.total_entries > 0,
            "Should have entries in the week"
        );

        // Verify date range
        let expected_start = Utc::now() - Duration::days(7);
        assert!(
            (summary.period_start - expected_start).num_seconds().abs() < 60,
            "Period start should be approximately 7 days ago"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_monthly_metrics_calculation() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Generate 30 days of data
        fixture.populate_test_data(30).await?;

        // Calculate monthly summary
        let summary = fixture.analytics_engine.generate_summary(30).await?;

        // Verify monthly metrics
        assert!(
            summary.cost_summary.total_cost > 0.0,
            "Monthly cost should be positive"
        );
        assert!(
            summary.history_stats.total_entries > 0,
            "Should have entries in the month"
        );

        // Verify insights generation
        assert!(
            !summary.insights.is_empty(),
            "Should generate insights for monthly data"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_custom_date_range_calculation() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Generate 15 days of data
        fixture.populate_test_data(15).await?;

        // Test custom ranges
        for days in [3, 5, 10, 14] {
            let summary = fixture.analytics_engine.generate_summary(days).await?;

            // Verify period matches requested range
            let duration = summary.period_end - summary.period_start;
            assert!(
                duration.num_days() as u32 >= days - 1,
                "Summary period should cover at least {} days",
                days
            );

            // Verify data is within range
            let cost_tracker = fixture.cost_tracker.read().await;
            let filter = CostFilter {
                since: Some(summary.period_start),
                until: Some(summary.period_end),
                ..Default::default()
            };
            let filtered_summary = cost_tracker.get_filtered_summary(&filter).await?;
            assert_eq!(
                filtered_summary.total_cost, summary.cost_summary.total_cost,
                "Filtered cost should match summary cost"
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_timezone_handling() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Create entries at current time to ensure they show up in today's count
        let now = Utc::now();
        let today_start = now.date_naive().and_hms_opt(0, 0, 0).unwrap().and_utc();

        // Create one entry for today (current time) and one for tomorrow
        let today_entry_time = now - Duration::minutes(30); // 30 minutes ago (definitely today)
        let tomorrow_entry_time = today_start + Duration::days(1) + Duration::hours(1); // Tomorrow

        let mut cost_tracker = fixture.cost_tracker.write().await;
        let mut history_store = fixture.history_store.write().await;

        for (timestamp, idx) in [(today_entry_time, 0), (tomorrow_entry_time, 1)] {
            let session_id = uuid::Uuid::new_v4();

            let mut cost_entry = CostEntry::new(
                session_id,
                format!("command-{}", idx),
                0.10,
                100,
                200,
                1000,
                "claude-3-opus".to_string(),
            );
            cost_entry.timestamp = timestamp;
            cost_tracker.record_cost(cost_entry).await?;

            let mut history_entry = HistoryEntry::new(
                session_id,
                format!("command-{}", idx),
                vec![],
                "output".to_string(),
                true,
                1000,
            );
            history_entry.timestamp = timestamp;
            history_store.store_entry(history_entry).await?;
        }

        drop(cost_tracker);
        drop(history_store);

        // Get today's dashboard data
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;

        // Only the today entry (command-0) should be counted in today's commands
        // The tomorrow entry (command-1) should not be counted
        assert_eq!(
            dashboard_data.today_commands, 1,
            "Should have exactly 1 command today, got {}",
            dashboard_data.today_commands
        );

        // Verify recent activity includes at least the today entry
        assert!(
            !dashboard_data.recent_activity.is_empty(),
            "Recent activity should not be empty"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_hourly_metrics_aggregation() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;
        let manager = &fixture.dashboard_manager;

        // Generate hourly data
        let generator = DashboardTestDataGenerator::new();
        let time_series_data = generator.generate_time_series_data(24);

        // Process into dashboard format
        let live_data = manager.generate_live_data().await?;

        // Verify hourly aggregation
        assert!(
            !live_data.time_series.cost_over_time.is_empty(),
            "Should have hourly cost data"
        );
        assert!(
            !live_data.time_series.commands_over_time.is_empty(),
            "Should have hourly command data"
        );

        // Verify data is chronologically ordered
        for i in 1..live_data.time_series.cost_over_time.len() {
            assert!(
                live_data.time_series.cost_over_time[i].0
                    >= live_data.time_series.cost_over_time[i - 1].0,
                "Time series data should be chronologically ordered"
            );
        }

        Ok(())
    }
}

/// Task 3.2.2: Test dashboard data aggregation from multiple sources
#[cfg(test)]
mod data_aggregation_tests {
    use super::*;

    #[tokio::test]
    async fn test_combining_data_from_sessions_commands_costs() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Create data across multiple sessions
        let sessions = vec![uuid::Uuid::new_v4(), uuid::Uuid::new_v4(), uuid::Uuid::new_v4()];

        let mut cost_tracker = fixture.cost_tracker.write().await;
        let mut history_store = fixture.history_store.write().await;

        // Add varied data for each session
        for (idx, session_id) in sessions.iter().enumerate() {
            let commands = ["analyze", "summarize", "translate"];
            let base_cost = 0.05 * (idx + 1) as f64;

            for (cmd_idx, command) in commands.iter().enumerate() {
                let cost_entry = CostEntry::new(
                    *session_id,
                    command.to_string(),
                    base_cost + (cmd_idx as f64 * 0.01),
                    100 + (cmd_idx as u32 * 50),
                    200 + (cmd_idx as u32 * 100),
                    1000 + (cmd_idx as u64 * 500),
                    "claude-3-opus".to_string(),
                );
                cost_tracker.record_cost(cost_entry).await?;

                let history_entry = HistoryEntry::new(
                    *session_id,
                    command.to_string(),
                    vec!["--option".to_string()],
                    format!("Output for {}", command),
                    true,
                    1000 + (cmd_idx as u64 * 500),
                );
                history_store.store_entry(history_entry).await?;
            }
        }

        drop(cost_tracker);
        drop(history_store);

        // Get aggregated dashboard data
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;

        // Verify aggregation
        assert_eq!(
            dashboard_data.today_commands, 9,
            "Should have 3 sessions * 3 commands = 9 total commands"
        );
        assert!(
            dashboard_data.today_cost > 0.0,
            "Total cost should be sum of all session costs"
        );

        // Verify top commands aggregation
        assert!(
            !dashboard_data.top_commands.is_empty(),
            "Should have top commands aggregated"
        );
        let total_top_cost: f64 = dashboard_data
            .top_commands
            .iter()
            .map(|(_, cost)| cost)
            .sum();
        assert!(
            total_top_cost <= dashboard_data.today_cost,
            "Top commands cost should not exceed total cost"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_data_consistency_across_sources() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;
        let generator = AnalyticsTestDataGenerator::default();

        // Generate consistent test data
        let test_data = generator.generate_test_data(1);
        fixture.load_data(&test_data).await?;

        // Get data from different sources
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;
        let summary = fixture.analytics_engine.generate_summary(1).await?;

        // Verify consistency
        assert_eq!(
            dashboard_data.today_commands, summary.history_stats.total_entries,
            "Command count should be consistent across sources"
        );

        // Cost might differ slightly due to timing, but should be close
        let cost_diff = (dashboard_data.today_cost - summary.cost_summary.total_cost).abs();
        assert!(
            cost_diff < 0.01,
            "Cost data should be consistent (within rounding): {} vs {}",
            dashboard_data.today_cost,
            summary.cost_summary.total_cost
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_handling_missing_data() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Test with no data
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;

        assert_eq!(
            dashboard_data.today_cost, 0.0,
            "Cost should be 0 with no data"
        );
        assert_eq!(
            dashboard_data.today_commands, 0,
            "Command count should be 0 with no data"
        );
        assert!(
            dashboard_data.recent_activity.is_empty(),
            "Recent activity should be empty with no data"
        );

        // Add partial data (cost without history)
        let cost_entry = CostEntry::new(
            uuid::Uuid::new_v4(),
            "test".to_string(),
            0.10,
            100,
            200,
            1000,
            "claude-3-opus".to_string(),
        );
        fixture
            .cost_tracker
            .write()
            .await
            .record_cost(cost_entry)
            .await?;

        // Should handle gracefully
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;
        assert!(
            dashboard_data.today_cost > 0.0,
            "Should show cost even without history data"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_handling_incomplete_data() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;
        let session_id = uuid::Uuid::new_v4();

        // Add history entry without corresponding cost
        let history_entry = HistoryEntry::new(
            session_id,
            "analyze".to_string(),
            vec![],
            "output".to_string(),
            true,
            1500,
        );
        fixture
            .history_store
            .write()
            .await
            .store_entry(history_entry)
            .await?;

        // Should handle gracefully
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;
        assert_eq!(
            dashboard_data.today_commands, 1,
            "Should count command even without cost data"
        );
        assert_eq!(
            dashboard_data.today_cost, 0.0,
            "Cost should be 0 if no cost entry exists"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_data_aggregation_with_filters() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Test different scenarios by directly populating test data for today

        // Test 1: High volume scenario - populate today's data with many commands
        fixture.populate_test_data(1).await?; // Generate test data for today
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;

        // The populate_test_data method should generate some commands for today
        assert!(
            dashboard_data.today_commands > 0,
            "Should have some commands from populate_test_data, got {}",
            dashboard_data.today_commands
        );

        // Test 2: Error spike scenario - add failed commands
        let session_id = uuid::Uuid::new_v4();
        let mut history_store = fixture.history_store.write().await;

        // Add several failed commands
        for i in 0..10 {
            let mut history_entry = HistoryEntry::new(
                session_id,
                format!("failing_cmd_{}", i),
                vec![],
                "Error output".to_string(),
                false, // Failed command
                1000,
            );
            // Set timestamp to now to ensure it's counted in today's data
            history_entry.timestamp = Utc::now() - Duration::minutes(i as i64 * 5);
            history_store.store_entry(history_entry).await?;
        }
        drop(history_store);

        let dashboard_data_with_errors = fixture.analytics_engine.get_dashboard_data().await?;

        // Should now have a lower success rate due to failed commands
        assert!(
            dashboard_data_with_errors.success_rate < 90.0,
            "Success rate should be lower with failed commands, got {}",
            dashboard_data_with_errors.success_rate
        );

        assert!(
            dashboard_data_with_errors.today_commands > dashboard_data.today_commands,
            "Should have more commands after adding failed ones"
        );

        Ok(())
    }
}

/// Task 3.2.3: Test dashboard performance indicators and KPI calculations
#[cfg(test)]
mod kpi_calculation_tests {
    use super::*;

    #[tokio::test]
    async fn test_success_rate_calculations() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;
        let session_id = uuid::Uuid::new_v4();

        // Add mix of successful and failed commands
        let commands =
            vec![("cmd1", true), ("cmd2", true), ("cmd3", false), ("cmd4", true), ("cmd5", false)];

        let mut history_store = fixture.history_store.write().await;
        for (cmd, success) in commands {
            let entry = HistoryEntry::new(
                session_id,
                cmd.to_string(),
                vec![],
                if success { "Success" } else { "Error" }.to_string(),
                success,
                1000,
            );
            history_store.store_entry(entry).await?;
        }
        drop(history_store);

        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;

        // Success rate should be 3/5 = 60%
        assert!(
            (dashboard_data.success_rate - 60.0).abs() < 0.1,
            "Success rate should be approximately 60%, got {}",
            dashboard_data.success_rate
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_average_response_time_metrics() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;
        let session_id = uuid::Uuid::new_v4();

        // Add commands with varying response times
        let response_times = vec![500, 1000, 1500, 2000, 2500];
        let expected_avg = response_times.iter().sum::<u64>() as f64 / response_times.len() as f64;

        let mut history_store = fixture.history_store.write().await;
        for (idx, duration) in response_times.iter().enumerate() {
            let entry = HistoryEntry::new(
                session_id,
                format!("cmd{}", idx),
                vec![],
                "output".to_string(),
                true,
                *duration,
            );
            history_store.store_entry(entry).await?;
        }
        drop(history_store);

        let summary = fixture.analytics_engine.generate_summary(1).await?;

        assert!(
            (summary.performance_metrics.average_response_time - expected_avg).abs() < 1.0,
            "Average response time should be approximately {}, got {}",
            expected_avg,
            summary.performance_metrics.average_response_time
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_cost_efficiency_kpis() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Create commands with different cost efficiency
        let commands = vec![
            ("efficient_cmd", 0.01, 100, 500),   // Low cost, high output
            ("normal_cmd", 0.05, 200, 800),      // Average
            ("expensive_cmd", 0.20, 1000, 2000), // High cost
        ];

        let mut cost_tracker = fixture.cost_tracker.write().await;
        for (cmd, cost, input_tokens, output_tokens) in commands {
            let entry = CostEntry::new(
                uuid::Uuid::new_v4(),
                cmd.to_string(),
                cost,
                input_tokens,
                output_tokens,
                1000,
                "claude-3-opus".to_string(),
            );
            cost_tracker.record_cost(entry).await?;
        }
        drop(cost_tracker);

        let summary = fixture.analytics_engine.generate_summary(1).await?;

        // Verify cost efficiency metrics
        assert!(
            summary.cost_summary.average_cost > 0.0,
            "Should calculate average cost per command"
        );
        assert!(
            summary
                .cost_summary
                .by_command
                .contains_key("expensive_cmd"),
            "Should track cost by command"
        );

        // Verify insights include cost efficiency
        let has_cost_insight = summary
            .insights
            .iter()
            .any(|i| i.contains("cost") || i.contains("expensive"));
        assert!(has_cost_insight, "Should generate insights about costs");

        Ok(())
    }

    #[tokio::test]
    async fn test_user_engagement_metrics() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;
        let generator = AnalyticsTestDataGenerator::default();

        // Generate data with varying engagement patterns
        let test_data = generator.generate_test_data(7);
        fixture.load_data(&test_data).await?;

        let summary = fixture.analytics_engine.generate_summary(7).await?;

        // Verify engagement metrics
        assert!(
            summary.performance_metrics.throughput_commands_per_hour > 0.0,
            "Should calculate command throughput"
        );
        assert!(
            summary.performance_metrics.peak_usage_hour <= 23,
            "Peak usage hour should be valid (0-23)"
        );

        // Verify usage insights
        let has_usage_insight = summary
            .insights
            .iter()
            .any(|i| i.contains("commands per day") || i.contains("usage"));
        assert!(
            has_usage_insight,
            "Should generate insights about usage patterns"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_performance_percentiles() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Generate response times for percentile calculations
        let mut response_times = Vec::new();
        for i in 0..100 {
            response_times.push(500 + (i * 50)); // 500ms to 5500ms
        }

        let session_id = uuid::Uuid::new_v4();
        let mut history_store = fixture.history_store.write().await;

        for (idx, duration) in response_times.iter().enumerate() {
            let entry = HistoryEntry::new(
                session_id,
                format!("cmd{}", idx),
                vec![],
                "output".to_string(),
                true,
                *duration as u64,
            );
            history_store.store_entry(entry).await?;
        }
        drop(history_store);

        // Generate widget data for response time
        let generator = DashboardTestDataGenerator::new();
        let widget_data = generator
            .generate_widget_data(&crate::cli::analytics::dashboard_test::WidgetType::ResponseTime);

        // Verify percentiles are reasonable
        if let Some(p50) = widget_data.get("p50_ms").and_then(|v| v.as_f64()) {
            assert!(p50 > 0.0, "P50 should be positive");
        }
        if let Some(p95) = widget_data.get("p95_ms").and_then(|v| v.as_f64()) {
            assert!(p95 > 0.0, "P95 should be positive");
        }
        if let Some(p99) = widget_data.get("p99_ms").and_then(|v| v.as_f64()) {
            assert!(p99 > 0.0, "P99 should be positive");
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_error_rate_by_command() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Create commands with different error rates
        let commands = vec![
            ("reliable_cmd", vec![true, true, true, true]),
            ("flaky_cmd", vec![true, false, true, false]),
            ("broken_cmd", vec![false, false, false, true]),
        ];

        let mut history_store = fixture.history_store.write().await;
        for (cmd_name, results) in commands {
            for (idx, success) in results.iter().enumerate() {
                let entry = HistoryEntry::new(
                    uuid::Uuid::new_v4(),
                    cmd_name.to_string(),
                    vec![],
                    format!("Result {}", idx),
                    *success,
                    1000,
                );
                history_store.store_entry(entry).await?;
            }
        }
        drop(history_store);

        let summary = fixture.analytics_engine.generate_summary(1).await?;

        // Verify error rates by command
        let error_rates = &summary.performance_metrics.error_rate_by_command;

        assert!(
            error_rates.get("reliable_cmd").unwrap_or(&100.0) < &1.0,
            "Reliable command should have low error rate"
        );
        assert!(
            (error_rates.get("flaky_cmd").unwrap_or(&0.0) - &50.0).abs() < 1.0,
            "Flaky command should have ~50% error rate"
        );
        assert!(
            error_rates.get("broken_cmd").unwrap_or(&0.0) > &70.0,
            "Broken command should have high error rate"
        );

        Ok(())
    }
}

/// Task 3.2.4: Test dashboard data caching and refresh logic
#[cfg(test)]
mod caching_tests {
    use super::*;
    use std::time::Instant;

    #[tokio::test]
    async fn test_cache_invalidation_on_data_updates() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Get initial dashboard data
        let initial_data = fixture.dashboard_manager.generate_live_data().await?;
        let initial_cost = initial_data.current_metrics.today_cost;

        // Add new data
        let cost_entry = CostEntry::new(
            uuid::Uuid::new_v4(),
            "new_command".to_string(),
            0.15,
            200,
            400,
            1500,
            "claude-3-opus".to_string(),
        );
        fixture
            .cost_tracker
            .write()
            .await
            .record_cost(cost_entry)
            .await?;

        // Get updated dashboard data
        let updated_data = fixture.dashboard_manager.generate_live_data().await?;

        // Verify cache was invalidated and data updated
        assert!(
            updated_data.current_metrics.today_cost > initial_cost,
            "Dashboard should reflect new cost data"
        );
        assert!(
            updated_data.timestamp > initial_data.timestamp,
            "Timestamp should be updated"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_partial_cache_updates() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Generate initial data
        let generator = AnalyticsTestDataGenerator::default();
        let test_data = generator.generate_test_data(1);
        fixture.load_data(&test_data).await?;

        // Trigger partial update by adding single new entry
        let history_entry = HistoryEntry::new(
            uuid::Uuid::new_v4(),
            "quick_update".to_string(),
            vec![],
            "output".to_string(),
            true,
            500,
        );
        fixture
            .history_store
            .write()
            .await
            .store_entry(history_entry)
            .await?;

        // Get initial data
        let initial_data = fixture.dashboard_manager.generate_live_data().await?;

        // Add another entry
        let history_entry2 = HistoryEntry::new(
            uuid::Uuid::new_v4(),
            "another_update".to_string(),
            vec![],
            "output2".to_string(),
            true,
            600,
        );
        fixture
            .history_store
            .write()
            .await
            .store_entry(history_entry2)
            .await?;

        // Get updated data
        let updated_data = fixture.dashboard_manager.generate_live_data().await?;

        // Verify partial update worked
        assert!(
            updated_data.current_metrics.today_commands
                > initial_data.current_metrics.today_commands,
            "Command count should increase with new data"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_cache_performance_improvements() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Load substantial data
        let generator = AnalyticsTestDataGenerator {
            session_count: 20,
            commands_per_session: (10, 30),
            ..Default::default()
        };
        let test_data = generator.generate_test_data(7);
        fixture.load_data(&test_data).await?;

        // Measure performance
        let durations =
            crate::cli::analytics::dashboard_test::performance::measure_dashboard_generation(
                &fixture, 10,
            )
            .await;

        // Calculate statistics
        let avg_duration = durations.iter().sum::<u128>() / durations.len() as u128;
        let first_gen = durations[0];
        let subsequent_avg = durations[1..].iter().sum::<u128>() / (durations.len() - 1) as u128;

        // First generation might be slower (cold cache)
        // Subsequent generations should be faster
        assert!(
            subsequent_avg <= first_gen,
            "Subsequent generations should not be slower than first: {} vs {}",
            subsequent_avg,
            first_gen
        );

        // All generations should be reasonably fast
        assert!(
            avg_duration < 50_000, // 50ms
            "Average dashboard generation should be under 50ms, got {}μs",
            avg_duration
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_stale_data_handling() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Add old data (simulating stale cache)
        let old_timestamp = Utc::now() - Duration::hours(2);
        let session_id = uuid::Uuid::new_v4();

        let mut cost_entry = CostEntry::new(
            session_id,
            "old_command".to_string(),
            0.05,
            100,
            200,
            1000,
            "claude-3-opus".to_string(),
        );
        cost_entry.timestamp = old_timestamp;

        fixture
            .cost_tracker
            .write()
            .await
            .record_cost(cost_entry)
            .await?;

        // Generate fresh dashboard data
        let dashboard_data = fixture.dashboard_manager.generate_live_data().await?;

        // Verify timestamp is current, not stale
        let age = Utc::now() - dashboard_data.timestamp;
        assert!(
            age.num_seconds() < 5,
            "Dashboard timestamp should be recent, not stale"
        );

        // Verify system status is current
        assert_eq!(
            dashboard_data.system_status.health,
            HealthStatus::Healthy,
            "System status should reflect current state"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_concurrent_cache_access() -> Result<()> {
        let fixture = Arc::new(DashboardTestFixture::new().await?);

        // Load test data
        let generator = AnalyticsTestDataGenerator::default();
        let test_data = generator.generate_test_data(1);
        fixture.load_data(&test_data).await?;

        // Spawn multiple concurrent readers
        let mut handles = vec![];
        for i in 0..10 {
            let fixture_clone = Arc::clone(&fixture);
            let handle = tokio::spawn(async move {
                let start = Instant::now();
                let data = fixture_clone.dashboard_manager.generate_live_data().await;
                let duration = start.elapsed();
                (i, data, duration)
            });
            handles.push(handle);
        }

        // Collect results
        let mut results = vec![];
        for handle in handles {
            if let Ok(result) = handle.await {
                results.push(result);
            }
        }

        // Verify all requests succeeded
        assert_eq!(results.len(), 10, "All concurrent requests should succeed");

        // Verify consistency
        let first_cost = results[0].1.as_ref().unwrap().current_metrics.today_cost;
        for (idx, data, _) in &results {
            assert_eq!(
                data.as_ref().unwrap().current_metrics.today_cost,
                first_cost,
                "Concurrent access should return consistent data (request {})",
                idx
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_cache_memory_efficiency() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Generate large dataset
        let generator = AnalyticsTestDataGenerator {
            session_count: 50,
            commands_per_session: (20, 50),
            ..Default::default()
        };
        let test_data = generator.generate_test_data(30);
        fixture.load_data(&test_data).await?;

        // Measure memory usage indirectly through generation time
        let start = Instant::now();
        let _ = fixture.dashboard_manager.generate_live_data().await?;
        let duration = start.elapsed();

        // Even with large dataset, generation should be efficient
        assert!(
            duration.as_millis() < 100,
            "Dashboard generation should be efficient even with large datasets"
        );

        // Test widget-specific caching
        let widget_configs = vec![
            crate::cli::analytics::dashboard_test::WidgetType::CostSummary,
            crate::cli::analytics::dashboard_test::WidgetType::CommandActivity,
            crate::cli::analytics::dashboard_test::WidgetType::SuccessRate,
            crate::cli::analytics::dashboard_test::WidgetType::ResponseTime,
        ];

        for widget_type in widget_configs {
            let start = Instant::now();
            let _ = DashboardTestDataGenerator::new().generate_widget_data(&widget_type);
            let duration = start.elapsed();

            assert!(
                duration.as_micros() < 1000,
                "Widget data generation should be very fast (<1ms)"
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_refresh_interval_configuration() -> Result<()> {
        // Test with custom refresh interval
        let temp_dir = TempDir::new()?;
        let cost_tracker = Arc::new(RwLock::new(CostTracker::new(
            temp_dir.path().join("costs.json"),
        )?));
        let history_store = Arc::new(RwLock::new(HistoryStore::new(
            temp_dir.path().join("history.json"),
        )?));

        let analytics_config = crate::cli::analytics::AnalyticsConfig {
            dashboard_refresh_interval: 1, // 1 second for testing
            ..Default::default()
        };

        let analytics_engine = AnalyticsEngine::new(
            Arc::clone(&cost_tracker),
            Arc::clone(&history_store),
            analytics_config,
        );

        let dashboard_config = DashboardConfig {
            refresh_interval_seconds: 1, // 1 second for testing
            ..Default::default()
        };

        let dashboard_manager = DashboardManager::new(analytics_engine, dashboard_config);

        // Test that we can generate data at configured intervals
        let start = Instant::now();
        let mut data_points = vec![];

        // Collect data points for 3 seconds
        while start.elapsed().as_secs() < 3 {
            let data = dashboard_manager.generate_live_data().await?;
            data_points.push(data);
            tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
        }

        // Should have approximately 3 data points
        assert!(
            data_points.len() >= 3,
            "Should generate data points at regular intervals, got {}",
            data_points.len()
        );

        // Verify timestamps are different
        for i in 1..data_points.len() {
            assert!(
                data_points[i].timestamp > data_points[i - 1].timestamp,
                "Timestamps should increase"
            );
        }

        Ok(())
    }
}

/// Edge case and error handling tests
#[cfg(test)]
mod edge_case_tests {
    use super::*;

    #[tokio::test]
    async fn test_empty_database_handling() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Test with completely empty database
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;

        crate::cli::analytics::dashboard_test::assertions::assert_dashboard_data_valid(
            &dashboard_data,
        );
        assert_eq!(dashboard_data.today_cost, 0.0);
        assert_eq!(dashboard_data.today_commands, 0);
        assert!(dashboard_data.recent_activity.is_empty());
        assert!(dashboard_data.top_commands.is_empty());

        Ok(())
    }

    #[tokio::test]
    async fn test_extreme_data_volumes() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Generate extreme volume
        let generator = AnalyticsTestDataGenerator {
            session_count: 100,
            commands_per_session: (50, 100),
            ..Default::default()
        };

        // Only generate 1 day to keep test reasonable
        let test_data = generator.generate_test_data(1);
        fixture.load_data(&test_data).await?;

        // Should handle gracefully
        let start = Instant::now();
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;
        let duration = start.elapsed();

        crate::cli::analytics::dashboard_test::assertions::assert_dashboard_data_valid(
            &dashboard_data,
        );
        assert!(
            duration.as_secs() < 5,
            "Should handle extreme volumes within 5 seconds"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_date_boundary_edge_cases() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Test at exact midnight
        let midnight = Utc::now()
            .date_naive()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_utc();

        let mut cost_entry = CostEntry::new(
            uuid::Uuid::new_v4(),
            "midnight_cmd".to_string(),
            0.05,
            100,
            200,
            1000,
            "claude-3-opus".to_string(),
        );
        cost_entry.timestamp = midnight;

        fixture
            .cost_tracker
            .write()
            .await
            .record_cost(cost_entry)
            .await?;

        // Test at 23:59:59
        let end_of_day = midnight + Duration::days(1) - Duration::seconds(1);
        let mut cost_entry = CostEntry::new(
            uuid::Uuid::new_v4(),
            "endofday_cmd".to_string(),
            0.05,
            100,
            200,
            1000,
            "claude-3-opus".to_string(),
        );
        cost_entry.timestamp = end_of_day;

        fixture
            .cost_tracker
            .write()
            .await
            .record_cost(cost_entry)
            .await?;

        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;
        crate::cli::analytics::dashboard_test::assertions::assert_dashboard_data_valid(
            &dashboard_data,
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_floating_point_precision() -> Result<()> {
        let fixture = DashboardTestFixture::new().await?;

        // Add many small costs that might cause precision issues
        let mut cost_tracker = fixture.cost_tracker.write().await;
        for i in 0..1000 {
            let cost_entry = CostEntry::new(
                uuid::Uuid::new_v4(),
                format!("cmd_{}", i),
                0.0001, // Very small cost
                10,
                20,
                100,
                "claude-3-haiku".to_string(),
            );
            cost_tracker.record_cost(cost_entry).await?;
        }
        drop(cost_tracker);

        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;

        // Total should be approximately 0.1 (1000 * 0.0001)
        let expected = 0.1;
        let tolerance = 0.001;
        assert!(
            (dashboard_data.today_cost - expected).abs() < tolerance,
            "Cost calculation should maintain precision: expected ~{}, got {}",
            expected,
            dashboard_data.today_cost
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_concurrent_data_updates() -> Result<()> {
        let fixture = Arc::new(DashboardTestFixture::new().await?);

        // Spawn multiple writers
        let mut handles = vec![];
        for i in 0..10 {
            let fixture_clone = Arc::clone(&fixture);
            let handle = tokio::spawn(async move {
                let session_id = uuid::Uuid::new_v4();
                let cost_entry = CostEntry::new(
                    session_id,
                    format!("concurrent_cmd_{}", i),
                    0.01 * i as f64,
                    100,
                    200,
                    1000,
                    "claude-3-opus".to_string(),
                );
                fixture_clone
                    .cost_tracker
                    .write()
                    .await
                    .record_cost(cost_entry)
                    .await
            });
            handles.push(handle);
        }

        // Wait for all writes
        for handle in handles {
            handle.await.unwrap()?;
        }

        // Verify data integrity
        let dashboard_data = fixture.analytics_engine.get_dashboard_data().await?;
        assert!(
            dashboard_data.today_cost > 0.0,
            "Should have recorded all concurrent costs"
        );

        Ok(())
    }
}