Repository navigation
Expand file tree
/
Copy pathcompact_cmd.rs
More file actions
1069 lines (908 loc) · 35.2 KB
/
Copy pathcompact_cmd.rs
File metadata and controls
1069 lines (908 loc) · 35.2 KB
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
//! Compaction command for Cortex CLI.
//!
//! Provides data compaction and cleanup functionality:
//! - Run manual compaction cycles
//! - Prune old log files
//! - Vacuum session database
//! - Configure auto-compaction settings
//!
//! # Race Condition Safety
//!
//! All operations acquire a lock to prevent concurrent access:
//! - Only one compaction process can run at a time
//! - Active CLI commands are protected from interference
//! - Stale locks are automatically detected and removed
use anyhow::{Context, Result};
use clap::Parser;
use serde::Serialize;
use std::path::{Path, PathBuf};
/// Compact CLI command.
#[derive(Debug, Parser)]
pub struct CompactCli {
#[command(subcommand)]
pub subcommand: Option<CompactSubcommand>,
}
/// Compact subcommands.
#[derive(Debug, clap::Subcommand)]
pub enum CompactSubcommand {
/// Run a compaction cycle (logs + sessions)
Run(CompactRunArgs),
/// Prune old log files
Logs(CompactLogsArgs),
/// Vacuum session database (clean orphaned files)
Vacuum(CompactVacuumArgs),
/// Show compaction status and statistics
Status(CompactStatusArgs),
/// Configure auto-compaction settings
Config(CompactConfigArgs),
}
/// Arguments for compact run command.
#[derive(Debug, Parser)]
pub struct CompactRunArgs {
/// Output as JSON
#[arg(long)]
pub json: bool,
/// Dry run - show what would be done without actually doing it
#[arg(long)]
pub dry_run: bool,
/// Force compaction even if lock is held (use with caution)
#[arg(long)]
pub force: bool,
}
/// Arguments for compact logs command.
#[derive(Debug, Parser)]
pub struct CompactLogsArgs {
/// Output as JSON
#[arg(long)]
pub json: bool,
/// Dry run - show what would be deleted without actually deleting
#[arg(long)]
pub dry_run: bool,
/// Keep logs from last N days (default: 7)
#[arg(long, default_value = "7")]
pub keep_days: u32,
/// Maximum log file size in MB for rotation (default: 10)
#[arg(long, default_value = "10")]
pub max_size_mb: u64,
}
/// Arguments for compact vacuum command.
#[derive(Debug, Parser)]
pub struct CompactVacuumArgs {
/// Output as JSON
#[arg(long)]
pub json: bool,
/// Dry run - show what would be cleaned without actually cleaning
#[arg(long)]
pub dry_run: bool,
/// Delete sessions older than N days (0 = keep all)
#[arg(long, default_value = "0")]
pub session_days: u32,
}
/// Arguments for compact status command.
#[derive(Debug, Parser)]
pub struct CompactStatusArgs {
/// Output as JSON
#[arg(long)]
pub json: bool,
}
/// Arguments for compact config command.
#[derive(Debug, Parser)]
pub struct CompactConfigArgs {
/// Enable auto-compaction
#[arg(long)]
pub enable: bool,
/// Disable auto-compaction
#[arg(long)]
pub disable: bool,
/// Set compaction interval in hours
#[arg(long)]
pub interval_hours: Option<u64>,
/// Set log retention period in days
#[arg(long)]
pub log_retention_days: Option<u32>,
/// Output current config as JSON
#[arg(long)]
pub json: bool,
}
// ============================================================================
// Path Utilities
// ============================================================================
/// Get the data directory for Cortex.
fn get_data_dir() -> PathBuf {
// Check environment variable override first
if let Ok(val) = std::env::var("CORTEX_DATA_DIR")
&& !val.is_empty()
{
return PathBuf::from(val);
}
dirs::data_dir()
.map(|d| d.join("Cortex"))
.unwrap_or_else(|| {
dirs::home_dir()
.map(|h| h.join(".local").join("share").join("Cortex"))
.unwrap_or_else(|| PathBuf::from(".cortex"))
})
}
/// Get the cache/logs directory.
fn get_logs_dir() -> PathBuf {
dirs::cache_dir()
.map(|c| c.join("cortex").join("logs"))
.unwrap_or_else(|| get_data_dir().join("logs"))
}
/// Get the sessions directory.
fn get_sessions_dir() -> PathBuf {
get_data_dir().join("sessions")
}
/// Get the history directory.
fn get_history_dir() -> PathBuf {
get_data_dir().join("history")
}
// ============================================================================
// Statistics
// ============================================================================
/// Compaction status information.
#[derive(Debug, Serialize)]
struct CompactionStatus {
data_dir: PathBuf,
logs_dir: PathBuf,
sessions_dir: PathBuf,
history_dir: PathBuf,
log_files_count: usize,
log_files_size: u64,
log_files_size_human: String,
session_files_count: usize,
history_files_count: usize,
orphaned_history_count: usize,
total_data_size: u64,
total_data_size_human: String,
lock_held: bool,
}
/// Calculate directory size and file count.
fn dir_stats(path: &PathBuf) -> (usize, u64) {
let mut count = 0;
let mut size = 0u64;
if path.is_dir()
&& let Ok(entries) = std::fs::read_dir(path)
{
for entry in entries.flatten() {
if entry.path().is_file() {
count += 1;
if let Ok(meta) = entry.metadata() {
size += meta.len();
}
}
}
}
(count, size)
}
/// Format bytes as human-readable string.
fn format_size(bytes: u64) -> String {
const KB: u64 = 1024;
const MB: u64 = KB * 1024;
const GB: u64 = MB * 1024;
if bytes >= GB {
format!("{:.2} GB", bytes as f64 / GB as f64)
} else if bytes >= MB {
format!("{:.2} MB", bytes as f64 / MB as f64)
} else if bytes >= KB {
format!("{:.2} KB", bytes as f64 / KB as f64)
} else {
format!("{} B", bytes)
}
}
/// Count orphaned history files (no matching session).
fn count_orphaned_history(sessions_dir: &PathBuf, history_dir: &PathBuf) -> usize {
use std::collections::HashSet;
// Collect session IDs
let session_ids: HashSet<String> = if sessions_dir.exists() {
std::fs::read_dir(sessions_dir)
.map(|entries| {
entries
.flatten()
.filter_map(|e| {
let path = e.path();
if path.extension().is_some_and(|ext| ext == "json") {
path.file_stem().and_then(|s| s.to_str()).map(String::from)
} else {
None
}
})
.collect()
})
.unwrap_or_default()
} else {
HashSet::new()
};
// Count orphaned history files
if history_dir.exists() {
std::fs::read_dir(history_dir)
.map(|entries| {
entries
.flatten()
.filter(|e| {
let path = e.path();
if path.extension().is_some_and(|ext| ext == "jsonl") {
path.file_stem()
.and_then(|s| s.to_str())
.is_some_and(|id| !session_ids.contains(id))
} else {
false
}
})
.count()
})
.unwrap_or(0)
} else {
0
}
}
/// Check if compaction lock is held.
fn is_lock_held(data_dir: &Path) -> bool {
let lock_path = data_dir.join(".compaction.lock");
if lock_path.exists()
&& let Ok(metadata) = std::fs::metadata(&lock_path)
&& let Ok(modified) = metadata.modified()
{
let age = std::time::SystemTime::now()
.duration_since(modified)
.unwrap_or_default();
// Lock is valid if less than 1 hour old
return age < std::time::Duration::from_secs(3600);
}
false
}
// ============================================================================
// Command Implementation
// ============================================================================
impl CompactCli {
/// Run the compact command.
pub async fn run(self) -> Result<()> {
match self.subcommand {
None => run_status(CompactStatusArgs { json: false }).await,
Some(CompactSubcommand::Run(args)) => run_compact(args).await,
Some(CompactSubcommand::Logs(args)) => run_logs(args).await,
Some(CompactSubcommand::Vacuum(args)) => run_vacuum(args).await,
Some(CompactSubcommand::Status(args)) => run_status(args).await,
Some(CompactSubcommand::Config(args)) => run_config(args).await,
}
}
}
async fn run_status(args: CompactStatusArgs) -> Result<()> {
let data_dir = get_data_dir();
let logs_dir = get_logs_dir();
let sessions_dir = get_sessions_dir();
let history_dir = get_history_dir();
let (log_count, log_size) = dir_stats(&logs_dir);
let (session_count, _) = dir_stats(&sessions_dir);
let (history_count, history_size) = dir_stats(&history_dir);
let orphaned_count = count_orphaned_history(&sessions_dir, &history_dir);
let total_size = log_size + history_size;
let status = CompactionStatus {
data_dir: data_dir.clone(),
logs_dir,
sessions_dir,
history_dir,
log_files_count: log_count,
log_files_size: log_size,
log_files_size_human: format_size(log_size),
session_files_count: session_count,
history_files_count: history_count,
orphaned_history_count: orphaned_count,
total_data_size: total_size,
total_data_size_human: format_size(total_size),
lock_held: is_lock_held(&data_dir),
};
if args.json {
println!("{}", serde_json::to_string_pretty(&status)?);
return Ok(());
}
println!("Compaction Status");
println!("{}", "=".repeat(50));
println!();
println!("Directories:");
println!(" Data: {}", status.data_dir.display());
println!(" Logs: {}", status.logs_dir.display());
println!(" Sessions: {}", status.sessions_dir.display());
println!(" History: {}", status.history_dir.display());
println!();
println!("Statistics:");
println!(
" Log files: {} ({})",
status.log_files_count, status.log_files_size_human
);
println!(" Session files: {}", status.session_files_count);
println!(" History files: {}", status.history_files_count);
if status.orphaned_history_count > 0 {
println!(
" Orphaned files: {} (can be cleaned with vacuum)",
status.orphaned_history_count
);
}
println!();
println!("Total data size: {}", status.total_data_size_human);
if status.lock_held {
println!();
println!("⚠️ Compaction lock is currently held by another process");
}
println!();
println!("Use 'cortex compact run' to run a compaction cycle.");
println!("Use 'cortex compact logs' to prune old log files.");
println!("Use 'cortex compact vacuum' to clean orphaned history files.");
Ok(())
}
async fn run_compact(args: CompactRunArgs) -> Result<()> {
use cortex_compact::{AutoCompactionConfig, AutoCompactionScheduler};
let data_dir = get_data_dir();
let logs_dir = get_logs_dir();
let sessions_dir = get_sessions_dir();
let history_dir = get_history_dir();
// Check lock unless force is specified
if !args.force && is_lock_held(&data_dir) {
if args.json {
let output = serde_json::json!({
"success": false,
"error": "Compaction lock held by another process",
"hint": "Use --force to override"
});
println!("{}", serde_json::to_string_pretty(&output)?);
} else {
println!("⚠️ Compaction lock is held by another process.");
println!("Wait for the other process to complete, or use --force to override.");
}
return Ok(());
}
if args.dry_run {
let log_files_count = dir_stats(&logs_dir).0;
let session_files_count = dir_stats(&sessions_dir).0;
let history_files_count = dir_stats(&history_dir).0;
let orphaned_history_count = count_orphaned_history(&sessions_dir, &history_dir);
if args.json {
let output = serde_json::json!({
"dry_run": true,
"would_process": {
"log_files": log_files_count,
"session_files": session_files_count,
"history_files": history_files_count,
"orphaned_files_to_clean": orphaned_history_count
},
"message": "Dry run completed - no changes made"
});
println!("{}", serde_json::to_string_pretty(&output)?);
} else {
println!("Dry run mode - no changes will be made.");
println!();
println!("Would process:");
println!(" Log files: {}", log_files_count);
println!(" Session files: {}", session_files_count);
println!(" History files: {}", history_files_count);
println!(" Orphaned files to clean: {}", orphaned_history_count);
}
return Ok(());
}
let config = AutoCompactionConfig::default();
let scheduler =
AutoCompactionScheduler::new(config, data_dir, logs_dir, sessions_dir, history_dir);
println!("Running compaction cycle...");
let stats = scheduler
.run_once()
.context("Failed to run compaction cycle")?;
if args.json {
println!("{}", serde_json::to_string_pretty(&stats)?);
return Ok(());
}
println!();
println!("Compaction completed in {}ms", stats.duration_ms);
if let Some(ref log_result) = stats.log_pruning
&& (log_result.files_deleted > 0 || log_result.files_rotated > 0)
{
println!();
println!("Log pruning:");
println!(" Files deleted: {}", log_result.files_deleted);
println!(" Bytes freed: {}", format_size(log_result.bytes_freed));
println!(" Files rotated: {}", log_result.files_rotated);
}
if let Some(ref vacuum_result) = stats.vacuum
&& (vacuum_result.orphaned_cleaned > 0 || vacuum_result.sessions_deleted > 0)
{
println!();
println!("Database vacuum:");
println!(" Sessions processed: {}", vacuum_result.sessions_processed);
println!(
" Orphaned files cleaned: {}",
vacuum_result.orphaned_cleaned
);
println!(" Sessions deleted: {}", vacuum_result.sessions_deleted);
println!(" Bytes freed: {}", format_size(vacuum_result.bytes_freed));
}
Ok(())
}
async fn run_logs(args: CompactLogsArgs) -> Result<()> {
use cortex_compact::{AutoCompactionConfig, LogPruner};
let logs_dir = get_logs_dir();
if !logs_dir.exists() {
if args.json {
let output = serde_json::json!({
"success": true,
"message": "No logs directory found"
});
println!("{}", serde_json::to_string_pretty(&output)?);
} else {
println!("No logs directory found. Nothing to prune.");
}
return Ok(());
}
if args.dry_run {
let (count, size) = dir_stats(&logs_dir);
if args.json {
let output = serde_json::json!({
"dry_run": true,
"logs_dir": logs_dir.display().to_string(),
"total_files": count,
"total_size": size,
"total_size_human": format_size(size),
"keep_days": args.keep_days,
"max_size_mb": args.max_size_mb,
});
println!("{}", serde_json::to_string_pretty(&output)?);
} else {
println!("Dry run - no changes will be made.");
println!();
println!("Logs directory: {}", logs_dir.display());
println!("Total files: {}", count);
println!("Total size: {}", format_size(size));
println!();
println!(
"Would prune files older than {} days or larger than {} MB",
args.keep_days, args.max_size_mb
);
}
return Ok(());
}
let config = AutoCompactionConfig {
log_retention_days: args.keep_days,
max_log_file_size: args.max_size_mb * 1024 * 1024,
..Default::default()
};
let pruner = LogPruner::new(config);
let result = pruner
.prune(&logs_dir)
.context("Failed to prune log files")?;
if args.json {
println!("{}", serde_json::to_string_pretty(&result)?);
return Ok(());
}
if result.files_deleted == 0 && result.files_rotated == 0 {
println!("No log files to prune.");
} else {
println!("Log pruning completed:");
println!(" Files deleted: {}", result.files_deleted);
println!(" Bytes freed: {}", format_size(result.bytes_freed));
println!(" Files rotated: {}", result.files_rotated);
}
if !result.errors.is_empty() {
println!();
println!("Warnings:");
for error in &result.errors {
println!(" - {}", error);
}
}
Ok(())
}
async fn run_vacuum(args: CompactVacuumArgs) -> Result<()> {
use cortex_compact::{AutoCompactionConfig, DatabaseVacuumer};
let sessions_dir = get_sessions_dir();
let history_dir = get_history_dir();
if args.dry_run {
let orphaned_count = count_orphaned_history(&sessions_dir, &history_dir);
let (session_count, _) = dir_stats(&sessions_dir);
let (history_count, history_size) = dir_stats(&history_dir);
if args.json {
let output = serde_json::json!({
"dry_run": true,
"sessions_dir": sessions_dir.display().to_string(),
"history_dir": history_dir.display().to_string(),
"session_files": session_count,
"history_files": history_count,
"history_size": history_size,
"history_size_human": format_size(history_size),
"orphaned_files": orphaned_count,
"session_retention_days": args.session_days,
});
println!("{}", serde_json::to_string_pretty(&output)?);
} else {
println!("Dry run - no changes will be made.");
println!();
println!("Sessions directory: {}", sessions_dir.display());
println!("History directory: {}", history_dir.display());
println!();
println!("Session files: {}", session_count);
println!(
"History files: {} ({})",
history_count,
format_size(history_size)
);
println!("Orphaned history files: {}", orphaned_count);
if args.session_days > 0 {
println!();
println!(
"Would delete sessions older than {} days",
args.session_days
);
}
}
return Ok(());
}
let config = AutoCompactionConfig {
session_retention_days: args.session_days,
..Default::default()
};
let vacuumer = DatabaseVacuumer::new(config);
let result = vacuumer
.vacuum(&sessions_dir, &history_dir)
.context("Failed to vacuum database")?;
if args.json {
println!("{}", serde_json::to_string_pretty(&result)?);
return Ok(());
}
if result.orphaned_cleaned == 0 && result.sessions_deleted == 0 {
println!("Database is clean. No orphaned files found.");
} else {
println!("Database vacuum completed:");
println!(" Sessions processed: {}", result.sessions_processed);
println!(" Orphaned files cleaned: {}", result.orphaned_cleaned);
if result.sessions_deleted > 0 {
println!(" Old sessions deleted: {}", result.sessions_deleted);
}
println!(" Bytes freed: {}", format_size(result.bytes_freed));
}
if !result.errors.is_empty() {
println!();
println!("Warnings:");
for error in &result.errors {
println!(" - {}", error);
}
}
Ok(())
}
async fn run_config(args: CompactConfigArgs) -> Result<()> {
// Load current config
let config = AutoCompactionConfig::default();
if args.json && !args.enable && !args.disable && args.interval_hours.is_none() {
// Just display current config
let output = serde_json::json!({
"enabled": config.enabled,
"interval_secs": config.interval_secs,
"interval_hours": config.interval_secs / 3600,
"log_retention_days": config.log_retention_days,
"session_retention_days": config.session_retention_days,
"max_log_file_size_mb": config.max_log_file_size / (1024 * 1024),
"vacuum_on_startup": config.vacuum_on_startup,
"min_free_disk_space_mb": config.min_free_disk_space_mb,
});
println!("{}", serde_json::to_string_pretty(&output)?);
return Ok(());
}
if !args.enable
&& !args.disable
&& args.interval_hours.is_none()
&& args.log_retention_days.is_none()
{
// Display config in human-readable format
println!("Auto-Compaction Configuration");
println!("{}", "=".repeat(40));
println!(" Enabled: {}", config.enabled);
println!(
" Interval: {} hours ({} seconds)",
config.interval_secs / 3600,
config.interval_secs
);
println!(" Log retention: {} days", config.log_retention_days);
println!(
" Session retention: {} days (0 = keep forever)",
config.session_retention_days
);
println!(
" Max log file size: {} MB",
config.max_log_file_size / (1024 * 1024)
);
println!(" Vacuum on startup: {}", config.vacuum_on_startup);
println!(
" Min free disk space: {} MB",
config.min_free_disk_space_mb
);
println!();
println!("Note: Configuration is currently built-in defaults.");
println!("Custom configuration will be available in a future update.");
return Ok(());
}
// Handle config changes
// Note: This would require persistent config storage which is not yet implemented
println!("Configuration changes:");
if args.enable {
println!(" Would enable auto-compaction");
}
if args.disable {
println!(" Would disable auto-compaction");
}
if let Some(hours) = args.interval_hours {
println!(" Would set interval to {} hours", hours);
}
if let Some(days) = args.log_retention_days {
println!(" Would set log retention to {} days", days);
}
println!();
println!("Note: Configuration persistence not yet implemented.");
println!("Settings will use defaults on next run.");
Ok(())
}
/// Auto-compaction configuration (re-exported for use in command).
use cortex_compact::AutoCompactionConfig;
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;
// ========================================================================
// format_size tests
// ========================================================================
#[test]
fn test_format_size_bytes() {
assert_eq!(format_size(0), "0 B");
assert_eq!(format_size(1), "1 B");
assert_eq!(format_size(512), "512 B");
assert_eq!(format_size(1023), "1023 B");
}
#[test]
fn test_format_size_kilobytes() {
assert_eq!(format_size(1024), "1.00 KB");
assert_eq!(format_size(1536), "1.50 KB");
assert_eq!(format_size(2048), "2.00 KB");
assert_eq!(format_size(1024 * 500), "500.00 KB");
}
#[test]
fn test_format_size_megabytes() {
assert_eq!(format_size(1024 * 1024), "1.00 MB");
assert_eq!(format_size(1024 * 1024 * 5), "5.00 MB");
assert_eq!(format_size(1024 * 1024 + 512 * 1024), "1.50 MB");
}
#[test]
fn test_format_size_gigabytes() {
assert_eq!(format_size(1024 * 1024 * 1024), "1.00 GB");
assert_eq!(format_size(1024 * 1024 * 1024 * 2), "2.00 GB");
assert_eq!(
format_size(1024 * 1024 * 1024 + 512 * 1024 * 1024),
"1.50 GB"
);
}
// ========================================================================
// dir_stats tests
// ========================================================================
#[test]
fn test_dir_stats_empty_directory() {
let temp = tempdir().expect("failed to create temp dir");
let path = temp.path().to_path_buf();
let (count, size) = dir_stats(&path);
assert_eq!(count, 0, "empty directory should have 0 files");
assert_eq!(size, 0, "empty directory should have 0 bytes");
}
#[test]
fn test_dir_stats_nonexistent_directory() {
let path = PathBuf::from("/nonexistent/directory/that/does/not/exist");
let (count, size) = dir_stats(&path);
assert_eq!(count, 0, "nonexistent directory should return 0 files");
assert_eq!(size, 0, "nonexistent directory should return 0 bytes");
}
#[test]
fn test_dir_stats_with_files() {
let temp = tempdir().expect("failed to create temp dir");
let path = temp.path().to_path_buf();
// Create some test files with known content
fs::write(temp.path().join("file1.txt"), "hello").expect("failed to write file1");
fs::write(temp.path().join("file2.txt"), "world!").expect("failed to write file2");
fs::write(temp.path().join("file3.json"), "{}").expect("failed to write file3");
let (count, size) = dir_stats(&path);
assert_eq!(count, 3, "should count 3 files");
// "hello" = 5 bytes, "world!" = 6 bytes, "{}" = 2 bytes = 13 total
assert_eq!(size, 13, "total size should be 13 bytes");
}
#[test]
fn test_dir_stats_ignores_subdirectories() {
let temp = tempdir().expect("failed to create temp dir");
let path = temp.path().to_path_buf();
// Create a file and a subdirectory
fs::write(temp.path().join("file.txt"), "content").expect("failed to write file");
fs::create_dir(temp.path().join("subdir")).expect("failed to create subdir");
fs::write(
temp.path().join("subdir").join("nested.txt"),
"nested content",
)
.expect("failed to write nested file");
let (count, size) = dir_stats(&path);
// Should only count top-level files
assert_eq!(count, 1, "should only count top-level files");
assert_eq!(size, 7, "should only count size of top-level files");
}
// ========================================================================
// count_orphaned_history tests
// ========================================================================
#[test]
fn test_count_orphaned_history_no_orphans() {
let temp = tempdir().expect("failed to create temp dir");
let sessions_dir = temp.path().join("sessions");
let history_dir = temp.path().join("history");
fs::create_dir_all(&sessions_dir).expect("failed to create sessions dir");
fs::create_dir_all(&history_dir).expect("failed to create history dir");
// Create matching session and history files
fs::write(sessions_dir.join("session1.json"), "{}").expect("failed to write session");
fs::write(history_dir.join("session1.jsonl"), "").expect("failed to write history");
let count = count_orphaned_history(&sessions_dir, &history_dir);
assert_eq!(count, 0, "no orphaned files when session exists");
}
#[test]
fn test_count_orphaned_history_with_orphans() {
let temp = tempdir().expect("failed to create temp dir");
let sessions_dir = temp.path().join("sessions");
let history_dir = temp.path().join("history");
fs::create_dir_all(&sessions_dir).expect("failed to create sessions dir");
fs::create_dir_all(&history_dir).expect("failed to create history dir");
// Create session file
fs::write(sessions_dir.join("session1.json"), "{}").expect("failed to write session");
// Create matching and orphaned history files
fs::write(history_dir.join("session1.jsonl"), "").expect("failed to write history");
fs::write(history_dir.join("orphan1.jsonl"), "").expect("failed to write orphan1");
fs::write(history_dir.join("orphan2.jsonl"), "").expect("failed to write orphan2");
let count = count_orphaned_history(&sessions_dir, &history_dir);
assert_eq!(count, 2, "should count 2 orphaned history files");
}
#[test]
fn test_count_orphaned_history_empty_directories() {
let temp = tempdir().expect("failed to create temp dir");
let sessions_dir = temp.path().join("sessions");
let history_dir = temp.path().join("history");
fs::create_dir_all(&sessions_dir).expect("failed to create sessions dir");
fs::create_dir_all(&history_dir).expect("failed to create history dir");
let count = count_orphaned_history(&sessions_dir, &history_dir);
assert_eq!(count, 0, "empty directories should have no orphans");
}
#[test]
fn test_count_orphaned_history_nonexistent_directories() {
let sessions_dir = PathBuf::from("/nonexistent/sessions");
let history_dir = PathBuf::from("/nonexistent/history");
let count = count_orphaned_history(&sessions_dir, &history_dir);
assert_eq!(count, 0, "nonexistent directories should return 0");
}
#[test]
fn test_count_orphaned_history_ignores_non_jsonl_files() {
let temp = tempdir().expect("failed to create temp dir");
let sessions_dir = temp.path().join("sessions");
let history_dir = temp.path().join("history");
fs::create_dir_all(&sessions_dir).expect("failed to create sessions dir");
fs::create_dir_all(&history_dir).expect("failed to create history dir");
// Create non-jsonl files in history dir
fs::write(history_dir.join("readme.txt"), "readme").expect("failed to write readme");
fs::write(history_dir.join("data.json"), "{}").expect("failed to write json");
let count = count_orphaned_history(&sessions_dir, &history_dir);
assert_eq!(count, 0, "should ignore non-jsonl files");
}
// ========================================================================
// is_lock_held tests
// ========================================================================
#[test]
fn test_is_lock_held_no_lock_file() {
let temp = tempdir().expect("failed to create temp dir");
let data_dir = temp.path().to_path_buf();
let held = is_lock_held(&data_dir);
assert!(!held, "no lock file means lock is not held");
}
#[test]
fn test_is_lock_held_fresh_lock() {
let temp = tempdir().expect("failed to create temp dir");
let data_dir = temp.path().to_path_buf();
let lock_path = data_dir.join(".compaction.lock");
// Create a fresh lock file
fs::write(&lock_path, "lock").expect("failed to write lock file");
let held = is_lock_held(&data_dir);
assert!(held, "fresh lock file should be considered held");
}
#[test]
fn test_is_lock_held_nonexistent_directory() {
let data_dir = PathBuf::from("/nonexistent/data/dir");
let held = is_lock_held(&data_dir);
assert!(!held, "nonexistent directory should not have lock held");
}
// ========================================================================
// CompactionStatus serialization tests
// ========================================================================
#[test]
fn test_compaction_status_serialization() {
let status = CompactionStatus {
data_dir: PathBuf::from("/data"),
logs_dir: PathBuf::from("/data/logs"),
sessions_dir: PathBuf::from("/data/sessions"),
history_dir: PathBuf::from("/data/history"),
log_files_count: 10,
log_files_size: 1024 * 1024,
log_files_size_human: "1.00 MB".to_string(),
session_files_count: 5,
history_files_count: 5,
orphaned_history_count: 2,
total_data_size: 2 * 1024 * 1024,
total_data_size_human: "2.00 MB".to_string(),
lock_held: false,
};
let json = serde_json::to_string(&status).expect("serialization should succeed");
assert!(
json.contains("log_files_count"),
"JSON should contain log_files_count"
);
assert!(json.contains("10"), "JSON should contain count value");
assert!(
json.contains("1.00 MB"),
"JSON should contain human-readable size"
);
assert!(
json.contains("orphaned_history_count"),
"JSON should contain orphaned count"
);
assert!(json.contains("lock_held"), "JSON should contain lock_held");
}