cqlite-cli 0.11.0

Command-line interface for CQLite โ€” read Apache Cassandra 5.0 SSTables without a cluster
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
//! Comprehensive SSTable Integration Tests for CQLite CLI
//!
//! This test suite validates CLI functionality with real Cassandra SSTable files
//! across multiple versions and data types to ensure production readiness.

use assert_cmd::prelude::*;
use predicates::prelude::*;
use serde_json::json;
use std::fs;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::time::Duration;
use tempfile::TempDir;
use wait_timeout::ChildExt;

/// Test configuration for SSTable integration tests
#[derive(Debug)]
pub struct SSTableTestConfig {
    pub test_data_dir: PathBuf,
    pub temp_dir: TempDir,
    pub cassandra_versions: Vec<String>,
    pub timeout: Duration,
    pub verbose: bool,
}

impl SSTableTestConfig {
    pub fn new() -> std::io::Result<Self> {
        let temp_dir = TempDir::new()?;
        Ok(Self {
            test_data_dir: PathBuf::from("test-env/cassandra5/data/cassandra5-sstables"),
            temp_dir,
            cassandra_versions: vec!["5.0".to_string(), "4.0".to_string(), "3.11".to_string()],
            timeout: Duration::from_secs(30),
            verbose: false,
        })
    }
}

/// Create a CLI command instance with proper timeout
fn get_cli_command() -> Command {
    Command::cargo_bin("cqlite").unwrap()
}

/// Helper function to run a command with timeout
#[allow(dead_code)]
fn run_command_with_timeout(
    mut cmd: Command,
    timeout: Duration,
) -> std::io::Result<std::process::Output> {
    let mut child = cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;

    match child.wait_timeout(timeout)? {
        Some(status) => {
            let mut stdout = Vec::new();
            let mut stderr = Vec::new();

            if let Some(ref mut stdout_handle) = child.stdout {
                std::io::Read::read_to_end(stdout_handle, &mut stdout)?;
            }
            if let Some(ref mut stderr_handle) = child.stderr {
                std::io::Read::read_to_end(stderr_handle, &mut stderr)?;
            }

            Ok(std::process::Output {
                status,
                stdout,
                stderr,
            })
        }
        None => {
            // Timeout occurred, kill the child process
            let _ = child.kill();
            child.wait()?;
            Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "Command timed out",
            ))
        }
    }
}

/// Create test schema files for different data types
fn create_test_schemas(config: &SSTableTestConfig) -> std::io::Result<Vec<(String, PathBuf)>> {
    let mut schemas = Vec::new();

    // Simple user table schema (JSON format)
    let users_schema = json!({
        "keyspace": "test_keyspace",
        "table": "users",
        "columns": {
            "user_id": {"type": "UUID", "kind": "PartitionKey"},
            "email": {"type": "TEXT", "kind": "Regular"},
            "name": {"type": "TEXT", "kind": "Regular"},
            "age": {"type": "INT", "kind": "Regular"},
            "created_at": {"type": "TIMESTAMP", "kind": "Regular"}
        }
    });

    let users_schema_path = config.temp_dir.path().join("users_schema.json");
    fs::write(
        &users_schema_path,
        serde_json::to_string_pretty(&users_schema)?,
    )?;
    schemas.push(("users".to_string(), users_schema_path));

    // Complex data types schema (JSON format)
    let complex_schema = json!({
        "keyspace": "test_keyspace",
        "table": "all_types",
        "columns": {
            "id": {"type": "UUID", "kind": "PartitionKey"},
            "text_col": {"type": "TEXT", "kind": "Regular"},
            "int_col": {"type": "INT", "kind": "Regular"},
            "bigint_col": {"type": "BIGINT", "kind": "Regular"},
            "float_col": {"type": "FLOAT", "kind": "Regular"},
            "double_col": {"type": "DOUBLE", "kind": "Regular"},
            "boolean_col": {"type": "BOOLEAN", "kind": "Regular"},
            "timestamp_col": {"type": "TIMESTAMP", "kind": "Regular"},
            "uuid_col": {"type": "UUID", "kind": "Regular"},
            "blob_col": {"type": "BLOB", "kind": "Regular"},
            "list_col": {"type": "LIST<TEXT>", "kind": "Regular"},
            "set_col": {"type": "SET<INT>", "kind": "Regular"},
            "map_col": {"type": "MAP<TEXT,INT>", "kind": "Regular"}
        }
    });

    let complex_schema_path = config.temp_dir.path().join("all_types_schema.json");
    fs::write(
        &complex_schema_path,
        serde_json::to_string_pretty(&complex_schema)?,
    )?;
    schemas.push(("all_types".to_string(), complex_schema_path));

    // CQL DDL format schema for compatibility testing
    let cql_schema = r#"
CREATE TABLE test_keyspace.products (
    product_id UUID PRIMARY KEY,
    name TEXT,
    price DECIMAL,
    category TEXT,
    tags SET<TEXT>,
    metadata MAP<TEXT, TEXT>,
    created_at TIMESTAMP
) WITH CLUSTERING ORDER BY (created_at DESC)
  AND compaction = {'class': 'SizeTieredCompactionStrategy'}
  AND compression = {'sstable_compression': 'LZ4Compressor'};
"#;

    let cql_schema_path = config.temp_dir.path().join("products_schema.cql");
    fs::write(&cql_schema_path, cql_schema)?;
    schemas.push(("products".to_string(), cql_schema_path));

    Ok(schemas)
}

/// Test basic CLI functionality
#[test]
fn test_cli_help_and_version() {
    // Test help command
    let mut cmd = get_cli_command();
    cmd.arg("--help");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("CQLite"))
        .stdout(predicate::str::contains("Usage:"))
        .stdout(predicate::str::contains("read-sstable"))
        .stdout(predicate::str::contains("info"))
        .stdout(predicate::str::contains("query"));

    // Test version command
    let mut cmd = get_cli_command();
    cmd.arg("--version");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains(env!("CARGO_PKG_VERSION")));
}

/// Test SSTable info command with different file types
#[test]
fn test_sstable_info_command() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;

    // Skip test if test data directory doesn't exist
    if !config.test_data_dir.exists() {
        println!("Skipping SSTable info test - test data not available");
        return Ok(());
    }

    // Find SSTable directories in test data
    let test_dirs: Vec<_> = fs::read_dir(&config.test_data_dir)?
        .filter_map(|entry| entry.ok())
        .filter(|entry| entry.path().is_dir())
        .take(2) // Test first 2 directories
        .collect();

    for dir_entry in test_dirs {
        let sstable_dir = dir_entry.path();

        println!("Testing info command with: {}", sstable_dir.display());

        // Test basic info command
        let mut cmd = get_cli_command();
        cmd.arg("info")
            .arg(&sstable_dir)
            .arg("--timeout")
            .arg(config.timeout.as_secs().to_string());

        let output = cmd.output()?;

        // Verify command executed successfully
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            println!("Command failed for {}: {}", sstable_dir.display(), stderr);
            continue; // Continue with next directory instead of failing
        }

        let stdout = String::from_utf8_lossy(&output.stdout);

        // Verify output contains expected information
        assert!(
            stdout.contains("SSTable Directory Information")
                || stdout.contains("SSTable Information")
        );
        assert!(stdout.contains("Directory:") || stdout.contains("File:"));

        // Test detailed info command
        let mut cmd = get_cli_command();
        cmd.arg("info")
            .arg(&sstable_dir)
            .arg("--detailed")
            .arg("--timeout")
            .arg(config.timeout.as_secs().to_string());

        let detailed_output = cmd.output()?;

        if detailed_output.status.success() {
            let detailed_stdout = String::from_utf8_lossy(&detailed_output.stdout);
            // Detailed output should contain component information
            assert!(
                detailed_stdout.contains("Components:") || detailed_stdout.contains("Generation")
            );
        }
    }

    Ok(())
}

/// Test SSTable read command with schema files
#[test]
fn test_sstable_read_command() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;
    let schemas = create_test_schemas(&config)?;

    // Skip test if test data directory doesn't exist
    if !config.test_data_dir.exists() {
        println!("Skipping SSTable read test - test data not available");
        return Ok(());
    }

    // Find test directories that match our schema names
    let test_dirs: Vec<_> = fs::read_dir(&config.test_data_dir)?
        .filter_map(|entry| entry.ok())
        .filter(|entry| {
            let path = entry.path();
            path.is_dir()
                && (path
                    .file_name()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .contains("users")
                    || path
                        .file_name()
                        .unwrap()
                        .to_str()
                        .unwrap()
                        .contains("all_types"))
        })
        .collect();

    for dir_entry in test_dirs {
        let sstable_dir = dir_entry.path();
        let dir_name = sstable_dir.file_name().unwrap().to_str().unwrap();

        // Find matching schema
        let schema_info = if dir_name.contains("users") {
            schemas.iter().find(|(name, _)| name == "users")
        } else if dir_name.contains("all_types") {
            schemas.iter().find(|(name, _)| name == "all_types")
        } else {
            continue;
        };

        if let Some((_, schema_path)) = schema_info {
            println!(
                "Testing read command with: {} and schema: {}",
                sstable_dir.display(),
                schema_path.display()
            );

            // Test read command with limit
            let mut cmd = get_cli_command();
            cmd.arg("read")
                .arg(&sstable_dir)
                .arg("--schema")
                .arg(schema_path)
                .arg("--limit")
                .arg("5")
                .arg("--timeout")
                .arg(config.timeout.as_secs().to_string());

            let output = cmd.output()?;

            if !output.status.success() {
                let stderr = String::from_utf8_lossy(&output.stderr);
                println!(
                    "Read command failed for {}: {}",
                    sstable_dir.display(),
                    stderr
                );
                continue;
            }

            let stdout = String::from_utf8_lossy(&output.stdout);

            // Verify output contains table data or appropriate messages
            assert!(
                stdout.contains("Live Table Data")
                    || stdout.contains("No data to display")
                    || stdout.contains("Reading live SSTable data")
            );

            // Test different output formats
            for format in &["json", "csv"] {
                let mut cmd = get_cli_command();
                cmd.arg("read")
                    .arg(&sstable_dir)
                    .arg("--schema")
                    .arg(schema_path)
                    .arg("--format")
                    .arg(format)
                    .arg("--limit")
                    .arg("3")
                    .arg("--timeout")
                    .arg(config.timeout.as_secs().to_string());

                let format_output = cmd.output()?;

                if format_output.status.success() {
                    let format_stdout = String::from_utf8_lossy(&format_output.stdout);

                    match *format {
                        "json" => {
                            // Should contain JSON array or valid JSON structure
                            assert!(format_stdout.contains("[") || format_stdout.contains("{}"));
                        }
                        "csv" => {
                            // Should contain CSV headers or data
                            assert!(format_stdout.contains(",") || format_stdout.contains("NULL"));
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    Ok(())
}

/// Test SELECT query functionality
#[test]
fn test_sstable_select_command() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;
    let schemas = create_test_schemas(&config)?;

    // Skip test if test data directory doesn't exist
    if !config.test_data_dir.exists() {
        println!("Skipping SSTable select test - test data not available");
        return Ok(());
    }

    // Find test directories
    let test_dirs: Vec<_> = fs::read_dir(&config.test_data_dir)?
        .filter_map(|entry| entry.ok())
        .filter(|entry| {
            let path = entry.path();
            path.is_dir()
                && path
                    .file_name()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .contains("users")
        })
        .take(1) // Test with one directory
        .collect();

    for dir_entry in test_dirs {
        let sstable_dir = dir_entry.path();

        if let Some((_, schema_path)) = schemas.iter().find(|(name, _)| name == "users") {
            println!("Testing select command with: {}", sstable_dir.display());

            // Test basic SELECT query
            let queries = vec![
                "SELECT * FROM users LIMIT 5",
                "SELECT user_id, email FROM users LIMIT 3",
                "SELECT COUNT(*) FROM users",
            ];

            for query in queries {
                let mut cmd = get_cli_command();
                cmd.arg("select")
                    .arg(&sstable_dir)
                    .arg("--schema")
                    .arg(schema_path)
                    .arg(query)
                    .arg("--timeout")
                    .arg(config.timeout.as_secs().to_string());

                let output = cmd.output()?;

                if !output.status.success() {
                    let stderr = String::from_utf8_lossy(&output.stderr);
                    println!("Select query '{query}' failed: {stderr}");
                    continue;
                }

                let stdout = String::from_utf8_lossy(&output.stdout);

                // Verify output contains query results or appropriate messages
                assert!(
                    stdout.contains("Query Summary")
                        || stdout.contains("rows returned")
                        || stdout.contains("LIVE SSTable file")
                        || stdout.contains("No data")
                );
            }

            // Test SELECT with different output formats
            let mut cmd = get_cli_command();
            cmd.arg("select")
                .arg(&sstable_dir)
                .arg("--schema")
                .arg(schema_path)
                .arg("SELECT * FROM users LIMIT 2")
                .arg("--format")
                .arg("json")
                .arg("--timeout")
                .arg(config.timeout.as_secs().to_string());

            let json_output = cmd.output()?;

            if json_output.status.success() {
                let json_stdout = String::from_utf8_lossy(&json_output.stdout);
                // Should contain JSON output
                assert!(json_stdout.contains("{") || json_stdout.contains("[]"));
            }
        }
    }

    Ok(())
}

/// Test version detection and compatibility
#[test]
fn test_version_detection() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;

    // Skip test if test data directory doesn't exist
    if !config.test_data_dir.exists() {
        println!("Skipping version detection test - test data not available");
        return Ok(());
    }

    // Find test directories
    let test_dirs: Vec<_> = fs::read_dir(&config.test_data_dir)?
        .filter_map(|entry| entry.ok())
        .filter(|entry| entry.path().is_dir())
        .take(1)
        .collect();

    for dir_entry in test_dirs {
        let sstable_dir = dir_entry.path();

        println!("Testing version detection with: {}", sstable_dir.display());

        // Test auto-detection
        let mut cmd = get_cli_command();
        cmd.arg("info")
            .arg(&sstable_dir)
            .arg("--auto-detect")
            .arg("--timeout")
            .arg(config.timeout.as_secs().to_string());

        let output = cmd.output()?;

        if output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            // Should contain version information
            assert!(
                stdout.contains("Detected version")
                    || stdout.contains("version")
                    || stdout.contains("format")
            );
        }

        // Test explicit Cassandra version
        for version in &config.cassandra_versions {
            let mut cmd = get_cli_command();
            cmd.arg("info")
                .arg(&sstable_dir)
                .arg("--cassandra-version")
                .arg(version)
                .arg("--timeout")
                .arg(config.timeout.as_secs().to_string());

            let version_output = cmd.output()?;

            if version_output.status.success() {
                let version_stdout = String::from_utf8_lossy(&version_output.stdout);
                // Should handle the specified version
                assert!(
                    version_stdout.contains("Cassandra compatibility")
                        || version_stdout.contains(version)
                        || version_stdout.contains("Directory Information")
                );
            }
        }
    }

    Ok(())
}

/// Test error handling with invalid inputs
#[test]
fn test_error_handling() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;

    // Test with non-existent file
    let mut cmd = get_cli_command();
    cmd.arg("info").arg("/non/existent/path");
    // .timeout(config.timeout); // Removed timeout method call

    let output = cmd.output()?;
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("No such file") || stderr.contains("not found") || !stderr.is_empty());

    // Test read command without schema
    let mut cmd = get_cli_command();
    cmd.arg("read").arg("/tmp");
    // .timeout(config.timeout); // Removed timeout method call

    let output = cmd.output()?;
    assert!(!output.status.success());

    // Test with invalid schema file
    let invalid_schema = config.temp_dir.path().join("invalid.json");
    fs::write(&invalid_schema, "{ invalid json }")?;

    let mut cmd = get_cli_command();
    cmd.arg("read")
        .arg("/tmp")
        .arg("--schema")
        .arg(&invalid_schema);
    // .timeout(config.timeout); // Removed timeout method call

    let output = cmd.output()?;
    assert!(!output.status.success());

    // Test with invalid Cassandra version
    let mut cmd = get_cli_command();
    cmd.arg("info")
        .arg("/tmp")
        .arg("--cassandra-version")
        .arg("99.99");
    // .timeout(config.timeout); // Removed timeout method call

    let output = cmd.output()?;
    assert!(!output.status.success());

    Ok(())
}

/// Test schema format auto-detection
#[test]
fn test_schema_format_detection() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;
    let schemas = create_test_schemas(&config)?;

    // Skip test if test data directory doesn't exist
    if !config.test_data_dir.exists() {
        println!("Skipping schema format test - test data not available");
        return Ok(());
    }

    // Find a test directory
    let test_dir = fs::read_dir(&config.test_data_dir)?
        .filter_map(|entry| entry.ok())
        .find(|entry| entry.path().is_dir())
        .map(|entry| entry.path());

    if let Some(sstable_dir) = test_dir {
        // Test JSON schema format
        if let Some((_, json_schema_path)) = schemas.iter().find(|(name, _)| name == "users") {
            let mut cmd = get_cli_command();
            cmd.arg("read")
                .arg(&sstable_dir)
                .arg("--schema")
                .arg(json_schema_path)
                .arg("--limit")
                .arg("1")
                .arg("--timeout")
                .arg(config.timeout.as_secs().to_string());

            let output = cmd.output()?;

            if output.status.success() {
                let stdout = String::from_utf8_lossy(&output.stdout);
                assert!(stdout.contains("Schema loaded") || stdout.contains("Live Table Data"));
            }
        }

        // Test CQL schema format
        if let Some((_, cql_schema_path)) = schemas.iter().find(|(name, _)| name == "products") {
            let mut cmd = get_cli_command();
            cmd.arg("read")
                .arg(&sstable_dir)
                .arg("--schema")
                .arg(cql_schema_path)
                .arg("--limit")
                .arg("1")
                .arg("--timeout")
                .arg(config.timeout.as_secs().to_string());

            let output = cmd.output()?;

            // CQL schema might not match the test data, but should handle format correctly
            if !output.status.success() {
                let stderr = String::from_utf8_lossy(&output.stderr);
                // Should give a meaningful error about schema mismatch, not format parsing
                assert!(
                    stderr.contains("schema")
                        || stderr.contains("table")
                        || stderr.contains("column")
                );
            }
        }
    }

    Ok(())
}

/// Performance benchmark test for CLI operations
#[test]
fn test_performance_benchmarks() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;

    // Skip test if test data directory doesn't exist
    if !config.test_data_dir.exists() {
        println!("Skipping performance test - test data not available");
        return Ok(());
    }

    let test_dirs: Vec<_> = fs::read_dir(&config.test_data_dir)?
        .filter_map(|entry| entry.ok())
        .filter(|entry| entry.path().is_dir())
        .take(1)
        .collect();

    for dir_entry in test_dirs {
        let sstable_dir = dir_entry.path();

        println!(
            "Running performance benchmark with: {}",
            sstable_dir.display()
        );

        // Benchmark info command
        let start = std::time::Instant::now();
        let mut cmd = get_cli_command();
        cmd.arg("info").arg(&sstable_dir);
        // .timeout(Duration::from_secs(60)); // Removed timeout method call

        let output = cmd.output()?;
        let info_duration = start.elapsed();

        if output.status.success() {
            println!("Info command took: {info_duration:?}");

            // Performance should be reasonable (less than 30 seconds for most files)
            assert!(info_duration < Duration::from_secs(30));
        }

        // Benchmark detailed info command
        let start = std::time::Instant::now();
        let mut cmd = get_cli_command();
        cmd.arg("info").arg(&sstable_dir).arg("--detailed");
        // .timeout(Duration::from_secs(60)); // Removed timeout method call

        let detailed_output = cmd.output()?;
        let detailed_duration = start.elapsed();

        if detailed_output.status.success() {
            println!("Detailed info command took: {detailed_duration:?}");

            // Detailed analysis may take longer but should still be reasonable
            assert!(detailed_duration < Duration::from_secs(60));
        }
    }

    Ok(())
}

/// Integration test with multiple data types and complex queries
#[test]
fn test_complex_data_types() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;
    let schemas = create_test_schemas(&config)?;

    // Skip test if test data directory doesn't exist
    if !config.test_data_dir.exists() {
        println!("Skipping complex data types test - test data not available");
        return Ok(());
    }

    // Find all_types test directory
    let test_dir = fs::read_dir(&config.test_data_dir)?
        .filter_map(|entry| entry.ok())
        .find(|entry| {
            let path = entry.path();
            path.is_dir()
                && path
                    .file_name()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .contains("all_types")
        })
        .map(|entry| entry.path());

    if let Some(sstable_dir) = test_dir {
        if let Some((_, schema_path)) = schemas.iter().find(|(name, _)| name == "all_types") {
            println!("Testing complex data types with: {}", sstable_dir.display());

            // Test reading data with complex types
            let mut cmd = get_cli_command();
            cmd.arg("read")
                .arg(&sstable_dir)
                .arg("--schema")
                .arg(schema_path)
                .arg("--format")
                .arg("json")
                .arg("--limit")
                .arg("3");
            // .timeout(Duration::from_secs(45)); // Removed timeout method call

            let output = cmd.output()?;

            if output.status.success() {
                let stdout = String::from_utf8_lossy(&output.stdout);

                // Should handle complex data types appropriately
                assert!(
                    stdout.contains("[") || stdout.contains("{}") || stdout.contains("No data")
                );
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                println!("Complex types test failed: {stderr}");

                // Should provide meaningful error messages for unsupported types
                assert!(
                    stderr.contains("type")
                        || stderr.contains("parsing")
                        || stderr.contains("schema")
                );
            }
        }
    }

    Ok(())
}

/// Test CLI with corrupted or incomplete files
#[test]
fn test_corrupted_file_handling() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;

    // Create a corrupted "SSTable" file
    let corrupted_file = config.temp_dir.path().join("corrupted-data.db");
    fs::write(&corrupted_file, b"This is not a valid SSTable file")?;

    // Create a valid schema for the test
    let schema = json!({
        "keyspace": "test",
        "table": "corrupted",
        "columns": {
            "id": {"type": "UUID", "kind": "PartitionKey"},
            "data": {"type": "TEXT", "kind": "Regular"}
        }
    });

    let schema_path = config.temp_dir.path().join("corrupted_schema.json");
    fs::write(&schema_path, serde_json::to_string_pretty(&schema)?)?;

    // Test CLI with corrupted file
    let mut cmd = get_cli_command();
    cmd.arg("read-sstable").arg(&corrupted_file);
    // .timeout(config.timeout); // Removed timeout method call

    let output = cmd.output()?;

    // Check if the command executed (may succeed with placeholder message)
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Either should fail with error message, or succeed with placeholder message
    let has_error = !output.status.success()
        && (stderr.contains("Failed to open SSTable")
            || stderr.contains("corruption")
            || stderr.contains("invalid")
            || stderr.contains("magic number"));

    let has_placeholder = output.status.success()
        && (stdout.contains("SSTable reading functionality needs to be updated")
            || stdout.contains("Note:"));

    assert!(
        has_error || has_placeholder,
        "Expected either error message or placeholder message. stdout: '{stdout}', stderr: '{stderr}'"
    );

    Ok(())
}

/// Test CLI memory usage and resource management
#[test]
fn test_resource_management() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;

    // Skip test if test data directory doesn't exist
    if !config.test_data_dir.exists() {
        println!("Skipping resource management test - test data not available");
        return Ok(());
    }

    // Find largest available test file for stress testing
    let largest_dir = fs::read_dir(&config.test_data_dir)?
        .filter_map(|entry| entry.ok())
        .filter(|entry| entry.path().is_dir())
        .max_by_key(|entry| {
            // Estimate directory size by counting files
            fs::read_dir(entry.path())
                .map(|dir| dir.count())
                .unwrap_or(0)
        });

    if let Some(dir_entry) = largest_dir {
        let sstable_dir = dir_entry.path();

        println!(
            "Testing resource management with: {}",
            sstable_dir.display()
        );

        // Test info command - should not consume excessive memory
        let mut cmd = get_cli_command();
        cmd.arg("info").arg(&sstable_dir).arg("--detailed");
        // .timeout(Duration::from_secs(120)); // Removed timeout method call

        let output = cmd.output()?;

        if output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);

            // Should complete successfully even with large files
            assert!(stdout.contains("SSTable Directory Information"));

            // Check that output is reasonable (not truncated due to memory issues)
            assert!(stdout.len() > 100); // Should have substantial output
            assert!(stdout.len() < 1_000_000); // But not excessive
        }
    }

    Ok(())
}

/// Helper function to validate test environment
pub fn validate_test_environment() -> Result<(), Box<dyn std::error::Error>> {
    let config = SSTableTestConfig::new()?;

    println!("๐Ÿ” Validating CQLite CLI Test Environment");
    println!("{}", "=".repeat(45));

    // Check if CLI binary exists
    match get_cli_command().arg("--version").output() {
        Ok(output) if output.status.success() => {
            let version = String::from_utf8_lossy(&output.stdout);
            println!("โœ… CQLite CLI binary found: {}", version.trim());
        }
        _ => {
            println!("โŒ CQLite CLI binary not found or not working");
            println!("   Run: cargo build --release");
            return Err("CLI binary not available".into());
        }
    }

    // Check test data availability
    if config.test_data_dir.exists() {
        let test_files: Vec<_> = fs::read_dir(&config.test_data_dir)?
            .filter_map(|entry| entry.ok())
            .filter(|entry| entry.path().is_dir())
            .collect();

        println!(
            "โœ… Test data directory found with {} SSTable directories",
            test_files.len()
        );

        for file in test_files.iter().take(3) {
            println!("   - {}", file.file_name().to_string_lossy());
        }

        if test_files.len() > 3 {
            println!("   ... and {} more", test_files.len() - 3);
        }
    } else {
        println!(
            "โš ๏ธ  Test data directory not found: {}",
            config.test_data_dir.display()
        );
        println!("   This is optional - tests will be skipped if data is not available");
        println!("   To generate test data:");
        println!("   cd test-env/cassandra5 && ./manage.sh start && ./scripts/extract-sstables.sh");
    }

    println!("\n๐Ÿš€ Environment validation complete - ready for testing!");

    Ok(())
}

#[cfg(test)]
mod integration_tests {
    use super::*;

    /// Test runner that can be called from external scripts
    #[allow(dead_code)]
    pub fn run_comprehensive_cli_tests() -> Result<(), Box<dyn std::error::Error>> {
        println!("๐Ÿงช Running Comprehensive CQLite SSTable CLI Integration Tests");
        println!("{}", "=".repeat(65));

        let config = SSTableTestConfig::new()?;

        if !config.test_data_dir.exists() {
            println!(
                "โš ๏ธ  Test data directory not found: {}",
                config.test_data_dir.display()
            );
            println!("   Run the following to generate test data:");
            println!(
                "   cd test-env/cassandra5 && ./manage.sh start && ./scripts/extract-sstables.sh"
            );
            return Ok(());
        }

        println!(
            "โœ… Test data directory found: {}",
            config.test_data_dir.display()
        );

        // Run a subset of tests programmatically
        println!("\n๐Ÿ“‹ Running basic CLI functionality tests...");
        test_cli_help_and_version();

        println!("๐Ÿ“‹ Running SSTable info command tests...");
        test_sstable_info_command()?;

        println!("๐Ÿ“‹ Running error handling tests...");
        test_error_handling()?;

        println!("๐Ÿ“‹ Running version detection tests...");
        test_version_detection()?;

        println!("\n๐ŸŽ‰ All CLI integration tests completed successfully!");

        Ok(())
    }
}