use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;
fn skim_cmd() -> Command {
Command::cargo_bin("skim").unwrap()
}
#[test]
fn test_guardrail_skips_tiny_files() {
let dir = TempDir::new().unwrap();
let file = dir.path().join("tiny.ts");
std::fs::write(&file, "const x = 1;\n").unwrap();
skim_cmd()
.arg(file.to_str().unwrap())
.arg("--mode=structure")
.arg("--no-cache")
.assert()
.success()
.stderr(predicate::str::contains("[skim:guardrail]").not());
}
#[test]
fn test_guardrail_does_not_trigger_on_normal_file() {
let dir = TempDir::new().unwrap();
let file = dir.path().join("normal.ts");
std::fs::write(
&file,
"import { something } from 'somewhere';\n\
type UserId = string;\n\
interface User {\n\
id: UserId;\n\
name: string;\n\
email: string;\n\
}\n\
function createUser(name: string, email: string): User {\n\
const id = generateId();\n\
return { id, name, email };\n\
}\n\
function deleteUser(id: UserId): void {\n\
const user = findUser(id);\n\
if (!user) throw new Error('not found');\n\
removeFromDatabase(user);\n\
}\n\
export { createUser, deleteUser };\n",
)
.unwrap();
skim_cmd()
.arg(file.to_str().unwrap())
.arg("--mode=structure")
.arg("--no-cache")
.assert()
.success()
.stderr(predicate::str::contains("[skim:guardrail]").not());
}
#[test]
fn test_guardrail_skipped_in_full_mode() {
let dir = TempDir::new().unwrap();
let file = dir.path().join("tiny.ts");
std::fs::write(&file, "const x = 1;\n").unwrap();
skim_cmd()
.arg(file.to_str().unwrap())
.arg("--mode=full")
.arg("--no-cache")
.assert()
.success()
.stderr(predicate::str::contains("[skim:guardrail]").not());
}
#[test]
fn test_guardrail_triggers_when_output_inflates() {
let dir = TempDir::new().unwrap();
let file = dir.path().join("inflating.ts");
let mut source = String::new();
for i in 0..20 {
source.push_str(&format!("function f{i}() {{ }}\n"));
}
assert!(
source.len() >= 256,
"Test file must be >= 256 bytes for guardrail to activate, got {}",
source.len()
);
std::fs::write(&file, &source).unwrap();
skim_cmd()
.arg(file.to_str().unwrap())
.arg("--mode=structure")
.arg("--no-cache")
.assert()
.success()
.stderr(predicate::str::contains("[skim:guardrail]"));
}