#![allow(clippy::unwrap_used)] #![allow(deprecated)]
use assert_cmd::Command;
use predicates::prelude::*;
use std::io::Write;
use tempfile::NamedTempFile;
const SMALL_HISTORY: &str = include_str!("../benches/fixtures/small_history.txt");
const MEDIUM_HISTORY: &str = include_str!("../benches/fixtures/medium_history.txt");
const LARGE_HISTORY: &str = include_str!("../benches/fixtures/large_history.txt");
fn aprender_shell() -> Command {
Command::cargo_bin("aprender-shell").expect("Failed to find aprender-shell binary")
}
fn create_fixture_history(content: &str) -> NamedTempFile {
let mut file = NamedTempFile::new().expect("Failed to create temp file");
for line in content.lines() {
let trimmed = line.trim();
if !trimmed.is_empty() && !trimmed.starts_with('#') {
writeln!(file, "{}", trimmed).expect("Failed to write command");
}
}
file
}
#[test]
fn test_real_001_train_small_history() {
let history = create_fixture_history(SMALL_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success()
.stdout(predicate::str::contains("Training"))
.stdout(predicate::str::contains("Model saved"));
}
#[test]
fn test_real_001_suggest_git_commands() {
let history = create_fixture_history(SMALL_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args(["suggest", "git ", "-m", model.path().to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("git")); }
#[test]
fn test_real_001_suggest_cargo_commands() {
let history = create_fixture_history(SMALL_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args(["suggest", "cargo ", "-m", model.path().to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("cargo")); }
#[test]
fn test_real_002_train_medium_history() {
let history = create_fixture_history(MEDIUM_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success()
.stdout(predicate::str::contains("Commands loaded"));
}
#[test]
fn test_real_002_stats_medium_history() {
let history = create_fixture_history(MEDIUM_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args(["stats", "-m", model.path().to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("N-gram size"))
.stdout(predicate::str::contains("Vocabulary size"));
}
#[test]
fn test_real_002_docker_suggestions() {
let history = create_fixture_history(MEDIUM_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args(["suggest", "docker ", "-m", model.path().to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("docker")); }
#[test]
fn test_real_002_kubectl_suggestions() {
let history = create_fixture_history(MEDIUM_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args(["suggest", "kubectl ", "-m", model.path().to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("kubectl")); }
#[test]
fn test_real_003_train_large_history() {
let history = create_fixture_history(LARGE_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success()
.stdout(predicate::str::contains("Model saved"));
}
#[test]
#[ignore = "Flaky latency test - fails under CI/coverage load"]
fn test_real_003_suggest_latency_acceptable() {
use std::time::Instant;
let history = create_fixture_history(LARGE_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args(["suggest", "git ", "-m", model.path().to_str().unwrap()])
.assert()
.success();
let start = Instant::now();
aprender_shell()
.args(["suggest", "git ", "-m", model.path().to_str().unwrap()])
.assert()
.success();
let elapsed = start.elapsed();
assert!(
elapsed.as_millis() < 200,
"Large model suggestion took {}ms, should be <200ms",
elapsed.as_millis()
);
}
#[test]
fn test_real_003_partial_token_completion() {
let history = create_fixture_history(LARGE_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args(["suggest", "git co", "-m", model.path().to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("git co")); }
#[test]
fn test_real_004_validate_medium_history() {
let history = create_fixture_history(MEDIUM_HISTORY);
aprender_shell()
.args(["validate", "-f", history.path().to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("VALIDATION RESULTS"))
.stdout(predicate::str::contains("Hit@"));
}
#[test]
fn test_real_005_augment_small_history() {
let history = create_fixture_history(SMALL_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"augment",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
"-a",
"0.5",
])
.assert()
.success()
.stdout(predicate::str::contains("Data Augmentation"));
}
#[test]
fn test_real_005_augment_with_code_eda() {
let history = create_fixture_history(MEDIUM_HISTORY);
let model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"augment",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
"--use-code-eda",
])
.assert()
.success()
.stdout(predicate::str::contains("CodeEDA"));
}
#[test]
fn test_real_006_analyze_medium_history() {
let history = create_fixture_history(MEDIUM_HISTORY);
aprender_shell()
.args(["analyze", "-f", history.path().to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("Command Analysis"))
.stdout(predicate::str::contains("git"))
.stdout(predicate::str::contains("cargo"))
.stdout(predicate::str::contains("docker"));
}
#[test]
fn test_real_006_analyze_large_history() {
let history = create_fixture_history(LARGE_HISTORY);
aprender_shell()
.args([
"analyze",
"-f",
history.path().to_str().unwrap(),
"--top",
"5",
])
.assert()
.success()
.stdout(predicate::str::contains("Top 5 Base Commands"));
}
#[test]
fn test_real_007_export_import_roundtrip() {
let history = create_fixture_history(MEDIUM_HISTORY);
let model = NamedTempFile::new().unwrap();
let export_file = NamedTempFile::new().unwrap();
let reimported_model = NamedTempFile::new().unwrap();
aprender_shell()
.args([
"train",
"-f",
history.path().to_str().unwrap(),
"-o",
model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args([
"export",
export_file.path().to_str().unwrap(),
"-m",
model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args([
"import",
export_file.path().to_str().unwrap(),
"-o",
reimported_model.path().to_str().unwrap(),
])
.assert()
.success();
aprender_shell()
.args([
"suggest",
"git ",
"-m",
reimported_model.path().to_str().unwrap(),
])
.assert()
.success()
.stdout(predicate::str::contains("git"));
}
include!("parts/real_world_tests_008.rs");