use std::fs;
use std::process::Command;
fn vibesql_binary() -> &'static str {
env!("CARGO_BIN_EXE_vibesql")
}
#[test]
fn test_command_mode_persistence() {
let test_db = "/tmp/test_vibesql_cmd_mode.db";
let _ = fs::remove_file(test_db);
let output = Command::new(vibesql_binary())
.args(["--database", test_db, "-c", "CREATE TABLE test_users (id INTEGER, name TEXT)"])
.output()
.expect("Failed to execute command");
assert!(output.status.success(), "CREATE TABLE should succeed");
let output = Command::new(vibesql_binary())
.args(["--database", test_db, "-c", "INSERT INTO test_users VALUES (1, 'Alice')"])
.output()
.expect("Failed to execute command");
assert!(output.status.success(), "INSERT should succeed");
assert!(std::path::Path::new(test_db).exists(), "Database file should exist");
let content = fs::read_to_string(test_db).expect("Should be able to read database file");
assert!(
content.to_uppercase().contains("CREATE TABLE TEST_USERS"),
"Database should contain CREATE TABLE statement"
);
assert!(content.contains("Alice"), "Database should contain inserted data");
let output = Command::new(vibesql_binary())
.args(["--database", test_db, "-c", "SELECT * FROM test_users"])
.output()
.expect("Failed to execute command");
assert!(output.status.success(), "SELECT should succeed");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("Alice"), "Query should return inserted data");
let _ = fs::remove_file(test_db);
}
#[test]
fn test_multiple_sessions_persistence() {
let test_db = "/tmp/test_vibesql_multi_session.db";
let _ = fs::remove_file(test_db);
let output = Command::new(vibesql_binary())
.args([
"--database",
test_db,
"-c",
"CREATE TABLE products (id INTEGER, name TEXT, price REAL)",
])
.output()
.expect("Failed to execute command");
assert!(output.status.success());
let output = Command::new(vibesql_binary())
.args(["--database", test_db, "-c", "INSERT INTO products VALUES (1, 'Widget', 9.99)"])
.output()
.expect("Failed to execute command");
assert!(output.status.success());
let output = Command::new(vibesql_binary())
.args(["--database", test_db, "-c", "INSERT INTO products VALUES (2, 'Gadget', 19.99)"])
.output()
.expect("Failed to execute command");
assert!(output.status.success());
let output = Command::new(vibesql_binary())
.args(["--database", test_db, "-c", "SELECT * FROM products"])
.output()
.expect("Failed to execute command");
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("Widget"), "Should contain first row");
assert!(stdout.contains("Gadget"), "Should contain second row");
let _ = fs::remove_file(test_db);
}
#[test]
fn test_binary_file_error_message() {
let test_db = "/tmp/test_vibesql_binary.db";
let mut binary_data = b"SQLite format 3\0".to_vec();
binary_data.extend_from_slice(&[0xFF, 0xFE, 0xFD, 0xFC, 0x00, 0x01, 0x02]);
fs::write(test_db, binary_data).expect("Failed to create test file");
let output = Command::new(vibesql_binary())
.args(["--database", test_db, "-c", "SELECT 1"])
.output()
.expect("Failed to execute command");
assert!(!output.status.success(), "Should fail for binary file");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("binary SQLite database") || stderr.contains("SQL dump format"),
"Error message should mention binary format and SQL dump format. Got: {}",
stderr
);
let _ = fs::remove_file(test_db);
}
#[test]
fn test_new_database_file_creation() {
let test_db = "/tmp/test_vibesql_new_file.db";
let _ = fs::remove_file(test_db);
let output = Command::new(vibesql_binary())
.args(["--database", test_db, "-c", "CREATE TABLE new_table (id INTEGER)"])
.output()
.expect("Failed to execute command");
assert!(output.status.success(), "Should succeed creating new database file");
assert!(std::path::Path::new(test_db).exists(), "Database file should be created");
let content = fs::read_to_string(test_db).expect("Should be able to read database file");
assert!(
content.to_uppercase().contains("CREATE TABLE NEW_TABLE"),
"Database should contain the table"
);
let _ = fs::remove_file(test_db);
}