#![allow(clippy::unwrap_used, clippy::panic)]
use assert_cmd::Command;
use predicates::prelude::*;
fn itok() -> Command {
Command::cargo_bin("itok").unwrap()
}
#[test]
fn stdout_and_exit_zero_on_success() {
itok()
.arg("--version")
.assert()
.success()
.stdout(predicate::str::contains("itok"));
}
#[test]
fn stderr_and_exit_two_on_usage_error() {
itok()
.arg("bogus")
.assert()
.failure()
.code(2)
.stderr(predicate::str::contains("unknown"));
}
#[test]
fn estimate_reports_to_stdout() {
itok()
.args(["estimate", "SPEC.md"])
.assert()
.success()
.stdout(predicate::str::contains("itok"));
}
#[test]
fn doctor_unknown_model_exits_two() {
itok()
.args(["doctor", "--model", "gpt-4o", "SPEC.md"])
.assert()
.failure()
.code(2)
.stderr(predicate::str::contains("unknown model"));
}
#[cfg(feature = "bpe")]
#[test]
fn doctor_honors_a_listed_model() {
let dir =
std::env::temp_dir().join(format!("itok-t7-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join(".context-models"), "gpt-4o o200k\n").unwrap();
std::fs::write(dir.join("f.txt"), "hello world\n").unwrap();
itok()
.args(["doctor", "--model", "gpt-4o", "f.txt"])
.current_dir(&dir)
.assert()
.success()
.stdout(predicate::str::contains("(o200k)"));
}
#[cfg(feature = "bpe")]
#[test]
fn doctor_model_window_gives_the_fit_line() {
let dir = std::env::temp_dir()
.join(format!("itok-t14-fit-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join(".context-models"), "qwen o200k 256k\n").unwrap();
std::fs::write(dir.join("f.txt"), "hello world\n").unwrap();
itok()
.args(["doctor", "--model", "qwen", "f.txt"])
.current_dir(&dir)
.assert()
.success()
.stdout(
predicate::str::contains("256000")
.and(predicate::str::contains("pass --window").not()),
);
}
#[cfg(feature = "bpe")]
#[test]
fn explicit_window_overrides_the_model_window() {
let dir = std::env::temp_dir()
.join(format!("itok-t14-override-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join(".context-models"), "qwen o200k 256k\n").unwrap();
std::fs::write(dir.join("f.txt"), "hello world\n").unwrap();
itok()
.args(["doctor", "--model", "qwen", "--window", "1M", "f.txt"])
.current_dir(&dir)
.assert()
.success()
.stdout(
predicate::str::contains("1000000")
.and(predicate::str::contains("256000").not()),
);
}
#[test]
fn fit_emits_a_pipeable_path_list() {
itok()
.args(["fit", "--window", "100M", "Cargo.toml"])
.assert()
.success()
.stdout(
predicate::str::contains("Cargo.toml")
.and(predicate::str::contains("itok").not()),
);
}
#[test]
fn fit_without_a_window_is_a_usage_error() {
itok()
.args(["fit", "Cargo.toml"])
.assert()
.failure()
.code(2)
.stderr(predicate::str::contains("--window"));
}
#[test]
fn cap_filters_real_stdin_and_announces_the_cut() {
itok()
.args(["cap", "2"])
.write_stdin("aaaaaaa\nbbbbbbb\nccccccc\n")
.assert()
.success()
.stdout(
predicate::str::starts_with("aaaaaaa\n")
.and(predicate::str::contains("ccccccc").not())
.and(predicate::str::contains(
"resume: line 2 (byte offset 8)",
)),
);
}
#[cfg(feature = "ollama")]
mod ollama_cassette {
use super::*;
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::thread;
use vcr_cassette::Cassette;
const CASSETTE: &str = include_str!("fixtures/ollama.json");
const DIR: &str = env!("CARGO_MANIFEST_DIR");
fn replay() -> String {
let cassette: Cassette = serde_json::from_str(CASSETTE).unwrap();
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let host = listener.local_addr().unwrap().to_string();
thread::spawn(move || {
for stream in listener.incoming().flatten() {
serve(stream, &cassette);
}
});
host
}
fn serve(mut stream: TcpStream, cassette: &Cassette) {
let req = read_request(&stream);
let body = response_for(cassette, &request_path(&req));
let out = format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\
Content-Type: application/json\r\nConnection: close\r\n\r\n{body}",
body.len()
);
let _ = stream.write_all(out.as_bytes());
let _ = stream.flush();
}
fn read_request(stream: &TcpStream) -> String {
let mut peek = stream.try_clone().unwrap();
let mut buf = Vec::new();
let mut chunk = [0u8; 1024];
while !is_complete(&buf) {
let n = peek.read(&mut chunk).unwrap_or(0);
if n == 0 {
break;
}
buf.extend_from_slice(chunk.get(..n).unwrap_or(&[]));
}
String::from_utf8_lossy(&buf).into_owned()
}
fn is_complete(buf: &[u8]) -> bool {
let text = String::from_utf8_lossy(buf);
let Some(end) = text.find("\r\n\r\n").map(|i| i.saturating_add(4))
else {
return false;
};
text.len().saturating_sub(end) >= content_length(&text)
}
fn content_length(head: &str) -> usize {
head.lines()
.filter(|l| l.to_ascii_lowercase().starts_with("content-length:"))
.find_map(|l| l.split(':').nth(1))
.and_then(|v| v.trim().parse().ok())
.unwrap_or(0)
}
fn request_path(req: &str) -> String {
req.split_whitespace().nth(1).unwrap_or("").to_owned()
}
fn response_for(cassette: &Cassette, path: &str) -> String {
cassette
.http_interactions
.iter()
.find(|i| i.request.uri.path() == path)
.map(|i| i.response.body.string.clone())
.unwrap_or_default()
}
#[test]
fn estimate_ollama_replays_the_exact_count() {
let host = replay();
itok()
.args(["estimate", "--ollama", "Cargo.toml"])
.env("OLLAMA_HOST", &host)
.current_dir(DIR)
.assert()
.success()
.stdout(
predicate::str::contains("42 itok").and(
predicate::str::contains("exact via qwen3-coder:30b@"),
),
);
}
#[test]
fn doctor_ollama_fleet_unions_the_models() {
let fleet = format!("{},{}", replay(), replay());
itok()
.arg("doctor")
.arg("--ollama")
.arg(&fleet)
.current_dir(DIR)
.assert()
.success()
.stdout(
predicate::str::contains("qwen3-coder:30b")
.and(predicate::str::contains("262144")),
);
}
#[test]
fn doctor_ollama_replays_the_enumerate() {
let host = replay();
itok()
.args(["doctor", "--ollama"])
.env("OLLAMA_HOST", &host)
.current_dir(DIR)
.assert()
.success()
.stdout(
predicate::str::contains("qwen3-coder:30b")
.and(predicate::str::contains("262144")),
);
}
#[test]
fn estimate_ollama_json_keeps_method_and_adds_endpoint() {
let host = replay();
itok()
.args(["estimate", "--ollama", "--format", "json", "Cargo.toml"])
.env("OLLAMA_HOST", &host)
.current_dir(DIR)
.assert()
.success()
.stdout(predicate::str::contains("\"method\":\"exact\"").and(
predicate::str::contains("\"endpoint\":\"qwen3-coder:30b@"),
));
}
#[test]
fn doctor_ollama_narrows_by_a_model_prefix() {
let host = replay();
itok()
.args(["doctor", "--ollama", "--model", "qwen3"])
.env("OLLAMA_HOST", &host)
.current_dir(DIR)
.assert()
.success()
.stdout(predicate::str::contains("qwen3-coder:30b"));
}
#[test]
fn doctor_ollama_accepts_a_model_list() {
let host = replay();
itok()
.args(["doctor", "--ollama", "--model", "qwen3,qwen3-coder"])
.env("OLLAMA_HOST", &host)
.current_dir(DIR)
.assert()
.success()
.stdout(
predicate::str::contains("context:")
.and(predicate::str::contains("qwen3-coder:30b")),
);
}
#[test]
fn a_model_list_is_all_or_nothing() {
let host = replay();
itok()
.args(["doctor", "--ollama", "--model", "qwen3,no-such-model"])
.env("OLLAMA_HOST", &host)
.current_dir(DIR)
.assert()
.code(2)
.stderr(predicate::str::contains("no-such-model"));
}
#[test]
fn an_unserved_model_names_the_alternatives() {
let host = replay();
itok()
.args(["doctor", "--ollama", "--model", "no-such-model"])
.env("OLLAMA_HOST", &host)
.current_dir(DIR)
.assert()
.code(2)
.stderr(
predicate::str::contains("no fleet host serves")
.and(predicate::str::contains("qwen3-coder:30b")),
);
}
#[test]
#[ignore = "hits a real ollama host; run with --ignored + OLLAMA_HOST"]
fn live_ollama_smoke() {
itok()
.args(["estimate", "--ollama", "README.md"])
.current_dir(DIR)
.assert()
.success()
.stdout(predicate::str::contains("(exact)"));
}
}