#[cfg(test)]
mod smoke_tests {
use sqz_engine::{AstParser, SqzEngine};
use sqz_engine::preset::{Preset, PresetParser};
#[test]
fn test_engine_initialises_without_network() {
let engine = SqzEngine::new();
assert!(
engine.is_ok(),
"SqzEngine::new() must succeed: {:?}",
engine.err()
);
}
#[test]
fn test_90_plus_cli_patterns() {
assert!(
crate::cli_proxy::CLI_PATTERNS.len() >= 90,
"expected ≥90 CLI patterns, got {}",
crate::cli_proxy::CLI_PATTERNS.len()
);
}
#[test]
fn test_18_plus_grammars_loaded() {
let parser = AstParser::new();
let count = parser.supported_languages().len();
assert!(
count >= 18,
"expected ≥18 supported languages, got {count}"
);
}
#[test]
fn test_default_preset_is_valid() {
let preset = Preset::default();
let result = PresetParser::validate(&preset);
assert!(
result.is_ok(),
"Preset::default() must pass validation: {:?}",
result.err()
);
}
#[test]
fn test_default_preset_round_trips() {
let preset = Preset::default();
let toml = PresetParser::to_toml(&preset).expect("serialize default preset");
let parsed = PresetParser::parse(&toml).expect("parse serialized default preset");
assert_eq!(preset.preset.name, parsed.preset.name);
}
}
#[cfg(test)]
mod cli_proxy_tests {
use crate::cli_proxy::CliProxy;
#[test]
fn test_intercept_output_end_to_end() {
let proxy = CliProxy::new().expect("engine init");
let raw = "On branch main\nnothing to commit, working tree clean\n";
let result = proxy.intercept_output("git status", raw);
assert!(!result.is_empty(), "compressed output must not be empty");
}
#[test]
fn test_intercept_output_transparent_on_empty_input() {
let proxy = CliProxy::new().expect("engine init");
let result = proxy.intercept_output("cargo build", "");
let _ = result;
}
#[test]
fn test_at_least_90_cli_patterns() {
assert!(
crate::cli_proxy::CLI_PATTERNS.len() >= 90,
"expected ≥90 patterns, got {}",
crate::cli_proxy::CLI_PATTERNS.len()
);
}
#[test]
fn test_known_commands_recognised() {
for cmd in &["git", "cargo", "npm", "docker", "kubectl", "aws"] {
assert!(
CliProxy::is_known_command(cmd),
"'{cmd}' should be a known command"
);
}
}
#[test]
fn test_unknown_command_not_recognised() {
assert!(!CliProxy::is_known_command("my_totally_custom_tool_xyz"));
}
}
#[cfg(test)]
mod shell_hook_tests {
use crate::shell_hook::{install_hook_to_file, ShellHook};
use std::fs;
use tempfile::TempDir;
#[test]
fn test_install_hook_writes_script() {
let dir = TempDir::new().unwrap();
let rc = dir.path().join(".bashrc");
let result = install_hook_to_file(
&rc,
"# sqz — context intelligence layer (auto-installed)\nsqz_run() { \"$@\" | sqz compress; }",
"# sqz — context intelligence layer (auto-installed)",
);
assert!(result.unwrap(), "first install should return true");
let content = fs::read_to_string(&rc).unwrap();
assert!(content.contains("sqz compress"));
}
#[test]
fn test_install_hook_idempotent() {
let dir = TempDir::new().unwrap();
let rc = dir.path().join(".zshrc");
let script = "# sqz — context intelligence layer (auto-installed)\nsqz_run() {}";
let sentinel = "# sqz — context intelligence layer (auto-installed)";
install_hook_to_file(&rc, script, sentinel).unwrap();
let second = install_hook_to_file(&rc, script, sentinel).unwrap();
assert!(!second, "second install should be a no-op");
let content = fs::read_to_string(&rc).unwrap();
assert_eq!(content.matches(sentinel).count(), 1);
}
#[test]
fn test_install_hook_failure_returns_error() {
let bad_path = std::path::Path::new("/root/.bashrc_sqz_test_unwritable");
let result = install_hook_to_file(
bad_path,
"# sqz — context intelligence layer (auto-installed)\n",
"# sqz — context intelligence layer (auto-installed)",
);
if result.is_err() {
let err = result.unwrap_err();
assert!(err.to_string().contains("sqz hook installation failed"));
}
}
#[test]
fn test_init_creates_default_preset() {
let dir = TempDir::new().unwrap();
let preset_dir = dir.path().join(".sqz").join("presets");
std::fs::create_dir_all(&preset_dir).unwrap();
let preset_path = preset_dir.join("default.toml");
assert!(!preset_path.exists());
let default_toml = "[meta]\nname = \"default\"\nversion = \"1\"\n";
fs::write(&preset_path, default_toml).unwrap();
assert!(preset_path.exists());
let content = fs::read_to_string(&preset_path).unwrap();
assert!(content.contains("[meta]"));
}
#[test]
fn test_all_shell_variants_have_scripts() {
for hook in &[
ShellHook::Bash,
ShellHook::Zsh,
ShellHook::Fish,
ShellHook::PowerShell,
] {
assert!(
!hook.hook_script().is_empty(),
"{hook:?} hook script must not be empty"
);
assert!(
hook.hook_script().contains("sqz compress"),
"{hook:?} hook script must reference 'sqz compress'"
);
}
}
#[test]
fn test_rc_paths_are_distinct() {
let paths: Vec<_> = [
ShellHook::Bash,
ShellHook::Zsh,
ShellHook::Fish,
ShellHook::PowerShell,
]
.iter()
.map(|h| h.rc_path())
.collect();
let unique: std::collections::HashSet<_> = paths.iter().collect();
assert_eq!(unique.len(), paths.len(), "each shell must have a unique RC path");
}
}