use llm_verify::probes::{Cancel, Depth, Selection};
use llm_verify::{engine, Endpoint, Protocol};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
const BODY: &str = r#"{"id":"msg_01","type":"message","role":"assistant","model":"claude-opus-4-5","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":40,"output_tokens":8}}"#;
async fn stub() -> (String, Arc<AtomicUsize>) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let hits = Arc::new(AtomicUsize::new(0));
let seen = hits.clone();
tokio::spawn(async move {
loop {
let Ok((mut sock, _)) = listener.accept().await else {
break;
};
let seen = seen.clone();
tokio::spawn(async move {
let mut buf = vec![0u8; 64 * 1024];
let Ok(n) = sock.read(&mut buf).await else {
return;
};
if n == 0 {
return;
}
seen.fetch_add(1, Ordering::Relaxed);
let resp = format!(
"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{}",
BODY.len(),
BODY
);
let _ = sock.write_all(resp.as_bytes()).await;
let _ = sock.shutdown().await;
});
}
});
(format!("http://127.0.0.1:{port}"), hits)
}
async fn count(selection: Selection, depth: Depth) -> usize {
let (base_url, hits) = stub().await;
let mut cfg = engine::RunConfig::new(Endpoint {
base_url,
api_key: "k".into(),
protocol: Protocol::Anthropic,
model: "claude-opus-4-5".into(),
..Default::default()
})
.depth(depth)
.seed(0xA11CE);
cfg.selection = selection;
let report = engine::run(cfg, &Cancel::new(), &mut |_| {}).await.unwrap();
assert_eq!(
report.request_count as usize,
hits.load(Ordering::Relaxed),
"the reported request count must match what the endpoint actually received"
);
report.request_count as usize
}
#[tokio::test]
async fn model_only_at_fast_depth_stays_within_its_budget() {
let n = count(Selection::model_only(), Depth::Fast).await;
println!("model_only + fast = {n} requests");
assert!(
(8..=22).contains(&n),
"a sampling run costs {n} requests; if that is intended, update this bound \
and whoever is paying for it"
);
}
#[tokio::test]
async fn turbo_at_fast_depth_is_under_half_of_model_only() {
let turbo = count(Selection::turbo(), Depth::Fast).await;
let sample = count(Selection::model_only(), Depth::Fast).await;
println!("turbo + fast = {turbo} requests, model_only = {sample}");
assert!(
(8..=10).contains(&turbo),
"turbo costs {turbo} requests; if that is intended, update this bound and the \
doc comment on `Selection::turbo` that quotes it"
);
assert!(
turbo * 2 <= sample,
"turbo ({turbo}) is not meaningfully cheaper than model_only ({sample})"
);
}
#[tokio::test]
async fn dropping_the_published_battery_refunds_its_requests() {
let with = count(Selection::turbo(), Depth::Fast).await;
let without = count(Selection::turbo().minus(["capability"]), Depth::Fast).await;
println!("turbo = {with} requests, turbo minus capability = {without}");
assert_eq!(
with - without,
3,
"the battery is three requests at Depth::Fast"
);
}
#[tokio::test]
async fn a_replacement_probe_is_not_deleted_by_the_step_it_replaces() {
struct Bank;
impl llm_verify::probes::Probe for Bank {
fn id(&self) -> &str {
"capability"
}
fn run<'a>(
&'a self,
ctx: &'a llm_verify::probes::Ctx,
) -> llm_verify::probes::ProbeFuture<'a> {
Box::pin(async move {
let req = llm_verify::protocol::ChatRequest::new(
&ctx.client.endpoint.model,
"a question the endpoint has never seen",
);
let _ = ctx.client.chat(&req).await;
vec![llm_verify::report::ProbeResult::new(
"capability",
"battery",
llm_verify::report::Group::Identity,
)
.pass("ok")]
})
}
}
let base = count(Selection::turbo().minus(["capability"]), Depth::Fast).await;
let replaced = count(
Selection::turbo().replacing("capability", Arc::new(Bank)),
Depth::Fast,
)
.await;
assert_eq!(
replaced,
base + 1,
"the replacement probe never issued its request — it was dropped by the \
skip that removed the step it replaces"
);
}
#[tokio::test]
async fn overlapping_the_steps_does_not_change_what_is_asked() {
let sequential = count(Selection::turbo(), Depth::Fast).await;
let (base_url, hits) = stub().await;
let cfg = engine::RunConfig::new(Endpoint {
base_url,
api_key: "k".into(),
protocol: Protocol::Anthropic,
model: "claude-opus-4-5".into(),
..Default::default()
})
.turbo(4)
.seed(0xA11CE);
let report = engine::run(cfg, &Cancel::new(), &mut |_| {}).await.unwrap();
assert_eq!(report.request_count as usize, hits.load(Ordering::Relaxed));
assert_eq!(
report.request_count as usize, sequential,
"the same selection asked a different number of questions when overlapped"
);
let ids: Vec<&str> = report.results.iter().map(|r| r.id.as_str()).collect();
assert_eq!(ids.first(), Some(&"preflight"));
assert!(
ids.iter().position(|i| *i == "self_id") < ids.iter().position(|i| *i == "ttft"),
"identity results should still precede the perf ones: {ids:?}"
);
}
#[tokio::test]
async fn a_forensic_recheck_has_a_known_ceiling() {
let n = count(Selection::model_only(), Depth::Forensic).await;
println!("model_only + forensic = {n} requests");
assert!(
n <= 70,
"an automatically-triggered recheck costs {n} requests; if that is intended, \
raise this bound deliberately — nobody asks for this one"
);
}
#[tokio::test]
async fn the_full_suite_is_several_times_the_cost_of_a_sample() {
let full = count(Selection::all(), Depth::Balanced).await;
let sample = count(Selection::model_only(), Depth::Fast).await;
println!("full + balanced = {full} requests, sample = {sample}");
assert!(
full > sample * 2,
"the sampling configuration is supposed to be the cheap one ({sample} vs {full})"
);
}