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
// EMERGENCY M1 FIX: Allow clippy warnings
#![allow(clippy::all)]

use anyhow::Result;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::time::Duration;
use tempfile::TempDir;

/// Test utilities and helpers for CLI testing
///
/// This module provides common functionality for all test suites:
/// - Test data generation
/// - CLI command execution helpers
/// - Output validation utilities
/// - File and directory management
/// - Performance measurement tools

pub const CLI_BINARY: &str = "cqlite";
#[allow(dead_code)]
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// Execute CLI command with arguments
pub fn run_cli(args: &[&str]) -> Result<Output> {
    Command::new("cargo")
        .args(["run", "--bin", CLI_BINARY, "--"])
        .args(args)
        .output()
        .map_err(|e| anyhow::anyhow!("Failed to execute CLI command: {}", e))
}

/// Execute CLI command with timeout (basic implementation)
#[allow(dead_code)]
pub fn run_cli_with_timeout(args: &[&str], _timeout: Duration) -> Result<Output> {
    // For now, this is the same as run_cli
    // In a full implementation, this would use process timeout mechanisms
    run_cli(args)
}

/// Check if CLI command succeeded
pub fn command_succeeded(output: &Output) -> bool {
    output.status.success()
}

/// Check if CLI command failed
pub fn command_failed(output: &Output) -> bool {
    !output.status.success()
}

/// Get stdout as string
pub fn get_stdout(output: &Output) -> Result<String> {
    String::from_utf8(output.stdout.clone())
        .map_err(|e| anyhow::anyhow!("Failed to decode stdout: {}", e))
}

/// Get stderr as string
pub fn get_stderr(output: &Output) -> Result<String> {
    String::from_utf8(output.stderr.clone())
        .map_err(|e| anyhow::anyhow!("Failed to decode stderr: {}", e))
}

/// Get combined output as string
pub fn get_combined_output(output: &Output) -> Result<String> {
    let stdout = get_stdout(output)?;
    let stderr = get_stderr(output)?;
    Ok(format!("{stdout}{stderr}"))
}

/// Check if output contains all given patterns
#[allow(dead_code)]
pub fn output_contains_all(output: &Output, patterns: &[&str]) -> Result<bool> {
    let combined = get_combined_output(output)?;
    Ok(patterns.iter().all(|pattern| combined.contains(pattern)))
}

/// Check if output contains any of the given patterns
pub fn output_contains_any(output: &Output, patterns: &[&str]) -> Result<bool> {
    let combined = get_combined_output(output)?;
    Ok(patterns.iter().any(|pattern| combined.contains(pattern)))
}

/// Validate output format based on expected format
#[allow(dead_code)]
pub fn validate_output_format(output: &Output, format: &str) -> Result<bool> {
    let stdout = get_stdout(output)?;

    match format.to_lowercase().as_str() {
        "json" => Ok(is_valid_json(&stdout)),
        "csv" => Ok(is_valid_csv(&stdout)),
        "table" => Ok(is_table_format(&stdout)),
        _ => Ok(false),
    }
}

/// Check if string is valid JSON
fn is_valid_json(s: &str) -> bool {
    s.trim_start().starts_with('{')
        || s.trim_start().starts_with('[')
        || serde_json::from_str::<serde_json::Value>(s).is_ok()
}

/// Check if string is valid CSV
fn is_valid_csv(s: &str) -> bool {
    s.lines().any(|line| line.contains(',')) && !s.trim().is_empty()
}

/// Check if string is table format
fn is_table_format(s: &str) -> bool {
    s.contains('|') || s.contains('+') || s.contains('-')
}

/// Create temporary database with optional path
pub fn create_temp_database() -> Result<(TempDir, PathBuf)> {
    let temp_dir = TempDir::new()?;
    let db_path = temp_dir.path().join("test.db");
    Ok((temp_dir, db_path))
}

/// Create temporary directory for tests
pub fn create_temp_dir() -> Result<TempDir> {
    TempDir::new().map_err(|e| anyhow::anyhow!("Failed to create temp directory: {}", e))
}

/// Create test schema files (JSON and CQL)
pub fn create_test_schema_files(temp_dir: &TempDir) -> Result<(PathBuf, PathBuf)> {
    let json_schema = temp_dir.path().join("test_schema.json");
    let cql_schema = temp_dir.path().join("test_schema.cql");

    let json_content = r#"{
  "keyspace": "test_keyspace",
  "table": "test_table",
  "partition_keys": [
    {
      "name": "id",
      "data_type": "uuid",
      "position": 0
    }
  ],
  "clustering_keys": [
    {
      "name": "timestamp",
      "data_type": "timestamp",
      "position": 0,
      "order": "ASC"
    }
  ],
  "columns": [
    {
      "name": "id",
      "data_type": "uuid",
      "nullable": false,
      "default": null
    },
    {
      "name": "name",
      "data_type": "text",
      "nullable": true,
      "default": null
    },
    {
      "name": "email",
      "data_type": "text",
      "nullable": true,
      "default": null
    },
    {
      "name": "timestamp",
      "data_type": "timestamp",
      "nullable": false,
      "default": null
    }
  ],
  "comments": {}
}"#;

    let cql_content = r#"CREATE TABLE test_keyspace.test_table (
  id uuid,
  name text,
  email text,
  timestamp timestamp,
  PRIMARY KEY (id, timestamp)
);"#;

    std::fs::write(&json_schema, json_content)?;
    std::fs::write(&cql_schema, cql_content)?;

    Ok((json_schema, cql_schema))
}

/// Create mock SSTable directory structure
pub fn create_mock_sstable_dir(temp_dir: &TempDir, table_name: &str) -> Result<PathBuf> {
    let sstable_dir = temp_dir
        .path()
        .join(format!("{table_name}-46436710673711f0b2cf19d64e7cbecb"));
    std::fs::create_dir_all(&sstable_dir)?;

    // Create essential SSTable files
    let files = vec![
        ("nb-1-big-Data.db", b"mock sstable data".as_ref()),
        (
            "nb-1-big-TOC.txt",
            b"Data.db\nStatistics.db\nTOC.txt".as_ref(),
        ),
        ("nb-1-big-Statistics.db", b"mock statistics".as_ref()),
        ("nb-1-big-Index.db", b"mock index".as_ref()),
        ("nb-1-big-Filter.db", b"mock filter".as_ref()),
    ];

    for (filename, content) in files {
        let file_path = sstable_dir.join(filename);
        std::fs::write(file_path, content)?;
    }

    Ok(sstable_dir)
}

/// Create test configuration file
pub fn create_test_config(temp_dir: &TempDir) -> Result<PathBuf> {
    let config_path = temp_dir.path().join("test_config.toml");

    let config_content = r#"
[connection]
timeout_ms = 30000
retry_attempts = 3
pool_size = 10

[output]
max_rows = 1000
colors = true
timestamp_format = "%Y-%m-%d %H:%M:%S"

[performance]
cache_size_mb = 64
query_timeout_ms = 30000
memory_limit_mb = 256

[logging]
level = "info"
format = "Pretty"

[repl]
enable_history = true
enable_completion = true
enable_colors = true
show_timing = false
page_size = 50
enable_paging = true
max_history_size = 1000
prompt = "cqlite> "
prompt_continuation = "    -> "

default_database = "test.db"
"#;

    std::fs::write(&config_path, config_content)?;
    Ok(config_path)
}

/// Create test data files for import/export testing
pub fn create_test_data_files(temp_dir: &TempDir) -> Result<(PathBuf, PathBuf, PathBuf)> {
    let json_file = temp_dir.path().join("test_data.json");
    let csv_file = temp_dir.path().join("test_data.csv");
    let large_csv_file = temp_dir.path().join("large_test_data.csv");

    // JSON test data
    let json_data = r#"[
  {
    "id": "123e4567-e89b-12d3-a456-426614174000",
    "name": "Alice Johnson",
    "email": "alice@example.com",
    "age": 28
  },
  {
    "id": "123e4567-e89b-12d3-a456-426614174001", 
    "name": "Bob Smith",
    "email": "bob@example.com",
    "age": 34
  },
  {
    "id": "123e4567-e89b-12d3-a456-426614174002",
    "name": "Carol Davis", 
    "email": "carol@example.com",
    "age": 29
  }
]"#;

    // CSV test data
    let csv_data = "id,name,email,age\n123e4567-e89b-12d3-a456-426614174000,Alice Johnson,alice@example.com,28\n123e4567-e89b-12d3-a456-426614174001,Bob Smith,bob@example.com,34\n123e4567-e89b-12d3-a456-426614174002,Carol Davis,carol@example.com,29";

    std::fs::write(&json_file, json_data)?;
    std::fs::write(&csv_file, csv_data)?;

    // Create larger CSV for performance testing
    let mut large_csv_content = String::from("id,name,email,age,city,country\n");
    for i in 0..1000 {
        large_csv_content.push_str(&format!(
            "123e4567-e89b-12d3-a456-{:012},User{},user{}@example.com,{},City{},Country{}\n",
            i,
            i,
            i,
            20 + (i % 50),
            i % 100,
            i % 10
        ));
    }
    std::fs::write(&large_csv_file, large_csv_content)?;

    Ok((json_file, csv_file, large_csv_file))
}

/// Extract timing information from CLI output
#[allow(dead_code)]
pub fn extract_timing_ms(output: &Output) -> Option<f64> {
    let stdout = get_stdout(output).ok()?;

    for line in stdout.lines() {
        if line.contains("executed in") && line.contains("ms") {
            // Look for patterns like "Query executed in 123.45ms"
            if let Some(ms_part) = line.split("in ").nth(1) {
                if let Some(ms_str) = ms_part.split("ms").next() {
                    if let Ok(ms) = ms_str.trim().parse::<f64>() {
                        return Some(ms);
                    }
                }
            }
        }

        if line.contains("Timing:") && line.contains("ms") {
            // Look for patterns like "Timing: 123.45ms"
            if let Some(ms_part) = line.split("Timing: ").nth(1) {
                if let Some(ms_str) = ms_part.split("ms").next() {
                    if let Ok(ms) = ms_str.trim().parse::<f64>() {
                        return Some(ms);
                    }
                }
            }
        }
    }

    None
}

/// Check if CLI binary is available
pub fn cli_available() -> bool {
    Command::new("cargo")
        .args(["check", "--bin", CLI_BINARY])
        .output()
        .map(|output| output.status.success())
        .unwrap_or(false)
}

/// Performance measurement utilities
#[allow(dead_code)]
pub struct PerformanceMeasurement {
    pub start_time: std::time::Instant,
    pub end_time: Option<std::time::Instant>,
}

impl PerformanceMeasurement {
    #[allow(dead_code)]
    pub fn start() -> Self {
        Self {
            start_time: std::time::Instant::now(),
            end_time: None,
        }
    }

    #[allow(dead_code)]
    pub fn stop(&mut self) {
        self.end_time = Some(std::time::Instant::now());
    }

    #[allow(dead_code)]
    pub fn duration(&self) -> Option<Duration> {
        self.end_time.map(|end| end.duration_since(self.start_time))
    }

    #[allow(dead_code)]
    pub fn duration_ms(&self) -> Option<f64> {
        self.duration().map(|d| d.as_secs_f64() * 1000.0)
    }
}

/// Test result validation
pub struct TestValidator {
    pub _passed: usize,
    pub _failed: usize,
    pub _errors: Vec<String>,
}

impl Default for TestValidator {
    fn default() -> Self {
        Self::new()
    }
}

impl TestValidator {
    pub fn new() -> Self {
        Self {
            _passed: 0,
            _failed: 0,
            _errors: Vec::new(),
        }
    }

    #[allow(dead_code)]
    pub fn assert_success(&mut self, output: &Output, description: &str) {
        if command_succeeded(output) {
            self._passed += 1;
        } else {
            self._failed += 1;
            let error = format!(
                "{}: Command failed - stderr: {}",
                description,
                get_stderr(output).unwrap_or_else(|_| "Unable to decode stderr".to_string())
            );
            self._errors.push(error);
        }
    }

    #[allow(dead_code)]
    pub fn assert_failure(&mut self, output: &Output, description: &str) {
        if command_failed(output) {
            self._passed += 1;
        } else {
            self._failed += 1;
            let error = format!("{description}: Expected command to fail but it succeeded");
            self._errors.push(error);
        }
    }

    #[allow(dead_code)]
    pub fn assert_contains(&mut self, output: &Output, pattern: &str, description: &str) {
        match output_contains_all(output, &[pattern]) {
            Ok(true) => self._passed += 1,
            Ok(false) => {
                self._failed += 1;
                let error = format!("{description}: Output does not contain '{pattern}'");
                self._errors.push(error);
            }
            Err(e) => {
                self._failed += 1;
                let error = format!("{description}: Error checking output: {e}");
                self._errors.push(error);
            }
        }
    }

    #[allow(dead_code)]
    pub fn summary(&self) -> String {
        format!("Tests: {} passed, {} failed", self._passed, self._failed)
    }

    #[allow(dead_code)]
    pub fn has_failures(&self) -> bool {
        self._failed > 0
    }
}

/// Common test scenarios
pub mod scenarios {
    use super::*;

    /// Test basic CLI help and version
    #[allow(dead_code)]
    pub fn test_basic_cli_info() -> Result<()> {
        let mut validator = TestValidator::new();

        // Test help
        let help_output = run_cli(&["--help"])?;
        validator.assert_success(&help_output, "CLI help");
        validator.assert_contains(&help_output, "CQLite", "Help contains product name");

        // Test version
        let version_output = run_cli(&["--version"])?;
        validator.assert_success(&version_output, "CLI version");

        if validator.has_failures() {
            eprintln!("Basic CLI info test failures:");
            for error in &validator._errors {
                eprintln!("  - {error}");
            }
            return Err(anyhow::anyhow!("Basic CLI info tests failed"));
        }

        println!("✅ Basic CLI info tests: {}", validator.summary());
        Ok(())
    }

    /// Test all output formats
    #[allow(dead_code)]
    pub fn test_output_formats(db_path: &Path) -> Result<()> {
        let mut validator = TestValidator::new();
        let formats = ["table", "json", "csv"];

        for format in &formats {
            let output = run_cli(&[
                "--database",
                db_path.to_str().unwrap(),
                "--format",
                format,
                "query",
                "SELECT 1 as test",
            ])?;

            validator.assert_success(&output, &format!("Format {format}"));
        }

        if validator.has_failures() {
            eprintln!("Output format test failures:");
            for error in &validator._errors {
                eprintln!("  - {error}");
            }
            return Err(anyhow::anyhow!("Output format tests failed"));
        }

        println!("✅ Output format tests: {}", validator.summary());
        Ok(())
    }
}

/// Test environment setup and cleanup
pub struct TestEnvironment {
    pub temp_dir: TempDir,
    pub db_path: PathBuf,
    pub config_path: PathBuf,
    pub schema_files: (PathBuf, PathBuf),
    pub _data_files: (PathBuf, PathBuf, PathBuf),
}

impl TestEnvironment {
    /// Create a complete test environment
    pub fn setup() -> Result<Self> {
        let temp_dir = create_temp_dir()?;
        let (_, db_path) = create_temp_database()?;
        let config_path = create_test_config(&temp_dir)?;
        let schema_files = create_test_schema_files(&temp_dir)?;
        let data_files = create_test_data_files(&temp_dir)?;

        Ok(Self {
            temp_dir,
            db_path,
            config_path,
            schema_files,
            _data_files: data_files,
        })
    }

    /// Get database path as string
    pub fn db_path_str(&self) -> &str {
        self.db_path.to_str().unwrap()
    }

    /// Get config path as string
    pub fn config_path_str(&self) -> &str {
        self.config_path.to_str().unwrap()
    }
}

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

    #[test]
    fn test_helper_functions() -> Result<()> {
        // Test environment setup
        let env = TestEnvironment::setup()?;
        assert!(env.temp_dir.path().exists());
        assert!(env.config_path.exists());

        // Test output format detection
        assert!(is_valid_json(r#"{"test": "value"}"#));
        assert!(is_valid_csv("a,b,c\n1,2,3"));
        assert!(is_table_format("+---+---+\n| a | b |\n+---+---+"));

        Ok(())
    }
}