use super::*;
use crate::constrain::{ConstraintError, ConstraintRequest};
use crate::session::ConstrainedStop;
fn refusal_kind(e: RealizarError) -> ConstraintError {
match e {
RealizarError::Constraint(c) => c,
other => panic!("not a constraint refusal: {other}"),
}
}
#[test]
fn only_a_prompt_that_leaves_think_open_is_a_thinking_prompt() {
let no_think = "<|im_start|>user\nhi<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n";
let open = "<|im_start|>user\nhi<|im_end|>\n<|im_start|>assistant\n<think>\n";
let plain = "<|im_start|>user\nhi<|im_end|>\n<|im_start|>assistant\n";
assert!(!prompt_opens_thinking(no_think));
assert!(prompt_opens_thinking(open));
assert!(!prompt_opens_thinking(plain));
assert!(prompt_opens_thinking(&format!(
"{no_think}ok<|im_end|>\n{open}"
)));
}
#[test]
fn a_constrained_finish_is_reported_as_itself() {
use run_report::FinishReason;
assert_eq!(
gguf_finish_reason(Some(ConstrainedStop::Complete), &[1, 2], &[9], 64),
FinishReason::ConstraintComplete
);
assert_eq!(
gguf_finish_reason(Some(ConstrainedStop::Length), &[1, 2], &[9], 2),
FinishReason::Length
);
assert_eq!(
gguf_finish_reason(None, &[1, 9], &[9], 64),
FinishReason::Stop
);
assert_eq!(
gguf_finish_reason(None, &[1, 2], &[9], 2),
FinishReason::Length
);
}
#[test]
fn a_format_that_applies_no_constraint_refuses_a_constrained_run_only() {
let plain = InferenceConfig::new("m.apr");
assert!(refuse_constraint_on(&plain, "apr", "x").is_ok());
let constrained = InferenceConfig::new("m.apr")
.with_constraint(ConstraintRequest::Lark(r#"start: "a""#.to_string()));
match refusal_kind(
refuse_constraint_on(&constrained, "apr", "not scheduled").expect_err("refused"),
) {
ConstraintError::UnsupportedPath { path, removed_by } => {
assert_eq!(path, "apr");
assert_eq!(removed_by, "not scheduled");
},
other => panic!("{other}"),
}
}
#[test]
fn the_mock_backend_refuses_a_constrained_run() {
let mut config = InferenceConfig::new("m.gguf")
.with_prompt("hi")
.with_constraint(ConstraintRequest::Lark(r#"start: "a""#.to_string()));
config.use_mock_backend = true;
let e = run_inference_report(&config).expect_err("refused");
assert!(
matches!(refusal_kind(e), ConstraintError::UnsupportedPath { path, .. } if path == "mock")
);
}
fn pygmy_model() -> (tempfile::NamedTempFile, crate::gguf::OwnedQuantizedModel) {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
use std::io::Write;
let mut f = tempfile::NamedTempFile::with_suffix(".gguf").expect("temp");
f.write_all(&build_executable_pygmy_gguf()).expect("write");
let mapped = crate::gguf::MappedGGUFModel::from_path(f.path()).expect("map");
let model = crate::gguf::OwnedQuantizedModel::from_mapped(&mapped).expect("load");
(f, model)
}
fn path_of(
config: &InferenceConfig,
gen_config: &crate::gguf::QuantizedGenerateConfig,
model: &crate::gguf::OwnedQuantizedModel,
is_qwen35: bool,
) -> Option<&'static str> {
unconstrained_gguf_path(config, gen_config, model, is_qwen35).map(|(path, _)| path)
}
#[test]
fn the_moe_loop_refuses_and_no_gpu_takes_the_engine() {
let (_f, mut model) = pygmy_model();
let cpu = InferenceConfig::new("m.gguf").without_gpu();
let greedy = crate::gguf::QuantizedGenerateConfig::default();
assert_eq!(path_of(&cpu, &greedy, &model, false), None);
assert_eq!(path_of(&cpu, &greedy, &model, true), None);
model.config.architecture = "qwen3moe".to_string();
assert_eq!(path_of(&cpu, &greedy, &model, false), Some("qwen3-moe"));
}
#[test]
fn trace_refuses_on_the_dense_loop_only() {
let (_f, model) = pygmy_model();
let cpu = InferenceConfig::new("m.gguf").without_gpu();
let traced = crate::gguf::QuantizedGenerateConfig {
trace: true,
..Default::default()
};
assert_eq!(path_of(&cpu, &traced, &model, false), Some("gguf-trace"));
assert_eq!(path_of(&cpu, &traced, &model, true), None);
}
#[test]
fn the_engine_runs_wherever_the_dispatch_takes_it_and_only_other_loops_refuse() {
let (_f, model) = pygmy_model();
let greedy = crate::gguf::QuantizedGenerateConfig::default();
let sampled = crate::gguf::QuantizedGenerateConfig {
temperature: 0.8,
top_k: 40,
..Default::default()
};
let traced = crate::gguf::QuantizedGenerateConfig {
trace: true,
..Default::default()
};
let bare = InferenceConfig::new("m.gguf");
let forced = InferenceConfig::new("m.gguf").with_accel_forced(true);
assert_eq!(path_of(&bare, &greedy, &model, false), None);
assert_eq!(path_of(&bare, &greedy, &model, true), None);
assert_eq!(path_of(&forced, &greedy, &model, true), None);
assert_eq!(path_of(&bare, &traced, &model, true), None);
assert_eq!(path_of(&forced, &sampled, &model, false), None);
if cfg!(feature = "cuda") {
assert_eq!(path_of(&forced, &greedy, &model, false), None);
assert_eq!(
path_of(&bare, &traced, &model, false),
Some("gguf-cuda-trace")
);
} else {
let wgpu = cfg!(feature = "gpu").then_some("gguf-wgpu");
assert_eq!(path_of(&forced, &greedy, &model, false), wgpu);
assert_eq!(path_of(&bare, &traced, &model, false), Some("gguf-trace"));
}
}
#[test]
fn the_dense_cuda_attempt_is_the_build_and_the_request() {
let bare = InferenceConfig::new("m.gguf");
let cpu = InferenceConfig::new("m.gguf").without_gpu();
assert_eq!(dense_cuda_attempted(&bare, false), cfg!(feature = "cuda"));
assert!(!dense_cuda_attempted(&cpu, false));
assert!(!dense_cuda_attempted(&bare, true));
assert_eq!(session_cuda_attempted(&bare), cfg!(feature = "cuda"));
assert!(!session_cuda_attempted(&cpu));
}
#[cfg(feature = "structured-output")]
fn q4k_model_with_vocab() -> (tempfile::NamedTempFile, crate::gguf::MappedGGUFModel) {
use crate::gguf::test_factory::build_minimal_llama_gguf_with;
use std::io::Write;
let vocab: Vec<String> = ["<unk>", "</s>", "a", "b"]
.into_iter()
.map(String::from)
.chain((4..32).map(|i| format!("x{i}")))
.collect();
let vocab: Vec<&str> = vocab.iter().map(String::as_str).collect();
let bytes = build_minimal_llama_gguf_with(32, 64, 64, 4, 4, |b| {
b.add_string("tokenizer.ggml.model", "llama")
.add_string_array("tokenizer.ggml.tokens", &vocab)
.add_u32("tokenizer.ggml.eos_token_id", 1)
});
let mut f = tempfile::NamedTempFile::with_suffix(".gguf").expect("temp");
f.write_all(&bytes).expect("write");
let mapped = crate::gguf::MappedGGUFModel::from_path(f.path()).expect("map");
(f, mapped)
}
#[cfg(feature = "structured-output")]
#[test]
fn a_dense_constrained_run_reports_the_attempt_the_dispatch_made() {
let (_f, mapped) = q4k_model_with_vocab();
let request = ConstraintRequest::Lark(r#"start: "a" | "b""#.to_string());
let gen_config = crate::gguf::QuantizedGenerateConfig {
max_tokens: 4,
temperature: 0.0,
top_k: 1,
stop_tokens: vec![1],
..Default::default()
};
for (config, attempted) in [
(InferenceConfig::new("m.gguf"), cfg!(feature = "cuda")),
(InferenceConfig::new("m.gguf").without_gpu(), false),
] {
let model = crate::gguf::OwnedQuantizedModel::from_mapped(&mapped).expect("load");
assert!(!model_has_legacy_quant(&model), "Q4_K is not legacy");
let (tokens, used_gpu, gpu_attempted, stop) = generate_gguf_constrained(
&request,
&config,
&mapped,
Some(model),
None,
&[2],
&gen_config,
)
.expect("a constrained turn");
assert_eq!(gpu_attempted, attempted, "no_gpu={}", config.no_gpu);
assert!(
!used_gpu || gpu_attempted,
"the device served an unattempted turn"
);
assert_eq!(stop, ConstrainedStop::Complete);
assert!(matches!(tokens.get(1), Some(2 | 3)), "{tokens:?}");
}
}