use std::collections::BTreeMap;
use clap::Command;
use frink_core::argv::arg_for_token;
const TABLE: &str = include_str!("../tests/llama_cpp_flags.txt");
const PINNED_NOT_ACCEPTED: &[(&str, usize)] = &[("cli", 144), ("server", 222), ("bench", 20)];
#[cfg(feature = "serve")]
const PINNED_SERVER_ENV_NOT_ACCEPTED: usize = 121;
#[derive(Debug)]
struct Row {
tools: Vec<&'static str>,
spellings: Vec<&'static str>,
neg: Vec<&'static str>,
#[cfg_attr(not(feature = "serve"), allow(dead_code))]
env: Option<&'static str>,
}
impl Row {
fn all_spellings(&self) -> impl Iterator<Item = &&'static str> {
self.spellings.iter().chain(&self.neg)
}
fn label(&self) -> String {
self.all_spellings().copied().collect::<Vec<_>>().join(",")
}
}
fn rows() -> Vec<Row> {
let list = |s: &'static str| -> Vec<&'static str> {
if s == "-" {
Vec::new()
} else {
s.split(',').collect()
}
};
TABLE
.lines()
.filter(|l| !l.starts_with('#') && !l.trim().is_empty())
.map(|l| {
let cols: Vec<&'static str> = l.split('\t').collect();
assert_eq!(cols.len(), 4, "malformed row: {l}");
Row {
tools: list(cols[0]),
spellings: list(cols[1]),
neg: list(cols[2]),
env: (cols[3] != "-").then_some(cols[3]),
}
})
.collect()
}
fn tool_command(tool: &str) -> Option<Command> {
let root = crate::argv::built_cli();
let sub = match tool {
"cli" => "run",
"server" => "serve",
"bench" => "bench",
other => panic!("unknown tool column {other}"),
};
if sub == "serve" && !cfg!(feature = "serve") {
return None;
}
root.find_subcommand(sub).cloned()
}
fn deliberate_differences() -> Vec<(String, String)> {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/CLI.md");
let doc = std::fs::read_to_string(&path).expect("docs/CLI.md");
let start = doc
.find("## Deliberate differences from llama.cpp")
.expect("docs/CLI.md has a `## Deliberate differences from llama.cpp` section");
let section = &doc[start..];
let end = section[3..].find("\n## ").map_or(section.len(), |i| i + 3);
let mut out = Vec::new();
for line in section[..end].lines() {
let cells: Vec<&str> = line.split('|').map(str::trim).collect();
if cells.len() < 4 {
continue;
}
let flag = cells[2].trim_matches('`');
let is_tool = |t: &str| PINNED_NOT_ACCEPTED.iter().any(|(name, _)| *name == t);
if !flag.starts_with('-') || !cells[1].split(',').all(|t| is_tool(t.trim())) {
continue;
}
for tool in cells[1].split(',').map(str::trim) {
out.push((tool.to_string(), flag.to_string()));
}
}
out
}
enum Class {
Accepted,
Different,
NotAccepted(Vec<&'static str>),
}
fn classify(cmd: &Command, row: &Row, tool: &str, different: &[(String, String)]) -> Class {
if different
.iter()
.any(|(t, f)| t == tool && row.all_spellings().any(|s| s == f))
{
return Class::Different;
}
let missing: Vec<&'static str> = row
.all_spellings()
.copied()
.filter(|s| arg_for_token(cmd, s).is_none())
.collect();
if missing.is_empty() {
Class::Accepted
} else {
Class::NotAccepted(missing)
}
}
#[test]
fn every_llama_cpp_flag_is_accepted_listed_as_different_or_counted() {
let rows = rows();
let different = deliberate_differences();
let mut report = String::new();
let mut counts = BTreeMap::new();
for &(tool, pinned) in PINNED_NOT_ACCEPTED {
let Some(cmd) = tool_command(tool) else {
continue;
};
let mut not_accepted = Vec::new();
let (mut accepted, mut differs, mut total) = (0, 0, 0);
for row in rows.iter().filter(|r| r.tools.contains(&tool)) {
total += 1;
match classify(&cmd, row, tool, &different) {
Class::Accepted => accepted += 1,
Class::Different => differs += 1,
Class::NotAccepted(missing) => {
let partial = missing.len() < row.all_spellings().count();
not_accepted.push(if partial {
format!("{} (missing {})", row.label(), missing.join(","))
} else {
row.label()
});
}
}
}
report.push_str(&format!(
"\n{tool}: {total} llama.cpp flags, {accepted} accepted, {differs} deliberately \
different, {} not accepted (pinned {pinned}):\n",
not_accepted.len()
));
for f in ¬_accepted {
report.push_str(&format!(" {f}\n"));
}
counts.insert(tool, (not_accepted.len(), pinned));
}
println!("{report}");
for (tool, (now, pinned)) in counts {
assert!(
now <= pinned,
"{tool}: {now} llama.cpp flags are not accepted, above the pin of {pinned}. A flag \
stopped parsing, or a difference row was dropped from docs/CLI.md.{report}"
);
assert_eq!(
now, pinned,
"{tool}: the gap closed to {now}; lower PINNED_NOT_ACCEPTED in \
crates/frink-cli/src/llama_flags.rs to {now} so it cannot reopen.{report}"
);
}
}
#[test]
fn every_deliberate_difference_names_a_real_llama_cpp_flag_of_its_tool() {
let rows = rows();
for (tool, flag) in deliberate_differences() {
assert!(
rows.iter()
.any(|r| r.tools.contains(&tool.as_str()) && r.all_spellings().any(|s| *s == flag)),
"docs/CLI.md lists `{flag}` as a deliberate difference for `{tool}`, and llama.cpp's \
{tool} has no such flag at the pin"
);
}
}
#[cfg(feature = "serve")]
#[test]
fn llama_server_env_vars_map_to_the_flag_llama_cpp_declares_them_on() {
let rows = rows();
let cmd = tool_command("server").expect("serve is built");
let different = deliberate_differences();
let table = frink_server::llama_env::LLAMA_ENV_FLAGS;
for (var, long) in table {
let arg = arg_for_token(&cmd, &format!("--{long}"))
.unwrap_or_else(|| panic!("{var} -> --{long}: no such frink-server flag"));
let row = rows
.iter()
.filter(|r| r.tools.contains(&"server"))
.find(|r| {
r.all_spellings()
.any(|s| arg_for_token(&cmd, s).is_some_and(|a| a.get_id() == arg.get_id()))
})
.unwrap_or_else(|| panic!("--{long} is no llama-server flag, so {var} maps nothing"));
assert_eq!(
row.env,
Some(*var),
"{var} -> --{long}, but llama.cpp declares {:?} on {}",
row.env,
row.label()
);
}
let mut missing = Vec::new();
for row in rows.iter().filter(|r| r.tools.contains(&"server")) {
let Some(var) = row.env else { continue };
if table.iter().any(|(v, _)| *v == var) {
continue;
}
if matches!(classify(&cmd, row, "server", &different), Class::Accepted) {
panic!(
"{} is accepted by frink-server and its variable {var} is not in LLAMA_ENV_FLAGS",
row.label()
);
}
missing.push(var);
}
println!(
"server env: {} of {} llama-server variables not read:\n {}",
missing.len(),
rows.iter()
.filter(|r| r.tools.contains(&"server") && r.env.is_some())
.count(),
missing.join("\n ")
);
assert!(
missing.len() <= PINNED_SERVER_ENV_NOT_ACCEPTED,
"{} llama-server variables are not read, above the pin of \
{PINNED_SERVER_ENV_NOT_ACCEPTED}",
missing.len()
);
assert_eq!(
missing.len(),
PINNED_SERVER_ENV_NOT_ACCEPTED,
"the env gap closed; lower PINNED_SERVER_ENV_NOT_ACCEPTED to {}",
missing.len()
);
}