use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
const DECLARING_DOCS: &[&str] = &[
"README.md",
"README.pt-BR.md",
"llms.txt",
"llms.pt-BR.txt",
"llms-full.txt",
"docs/AGENTS.md",
"docs/AGENTS.pt-BR.md",
"docs/HOW_TO_USE.md",
"docs/HOW_TO_USE.pt-BR.md",
"docs/COOKBOOK.md",
"docs/COOKBOOK.pt-BR.md",
"docs/HEADLESS_INVOCATION.md",
"docs/HEADLESS_INVOCATION.pt-BR.md",
"INTEGRATIONS.md",
"INTEGRATIONS.pt-BR.md",
];
const ENTRY_MARKER: &str = "SettingKey {";
const KEY_FIELD: &str = "key: \"";
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}
fn read_repo_file(relative: &str) -> String {
let path = repo_root().join(relative);
std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("cannot read {}: {e}", path.display()))
}
fn registry_key_count() -> usize {
let source = read_repo_file("src/config/registry.rs");
let mut keys = BTreeSet::new();
for entry in source.split(ENTRY_MARKER).skip(1) {
let Some(start) = entry.find(KEY_FIELD) else {
continue;
};
let rest = &entry[start + KEY_FIELD.len()..];
let Some(end) = rest.find('"') else {
continue;
};
keys.insert(rest[..end].to_string());
}
keys.len()
}
fn schema_file_count() -> usize {
let dir = repo_root().join("docs/schemas");
let entries = std::fs::read_dir(&dir)
.unwrap_or_else(|e| panic!("cannot read {}: {e}", dir.display()))
.filter_map(Result::ok);
entries
.filter(|e| e.path().extension().is_some_and(|ext| ext == "json"))
.count()
}
fn counts_before(line: &str, noun: &str) -> Vec<usize> {
let words: Vec<&str> = line.split_whitespace().collect();
let mut found = Vec::new();
for (i, word) in words.iter().enumerate() {
let bare = word.trim_matches(|c: char| !c.is_alphanumeric());
if bare != noun || i == 0 {
continue;
}
let previous = words[i - 1];
if previous.trim_end_matches(['.', ',']).ends_with('+') {
continue;
}
let digits = previous.trim_matches(|c: char| !c.is_ascii_digit());
if let Ok(n) = digits.parse::<usize>() {
found.push(n);
}
}
found
}
fn claims_the_schema_catalogue(line: &str) -> bool {
line.contains("schemas cover") || line.contains("schemas cobrem")
}
const CURRENCY_MARKERS: &[&str] = &["current", "atual"];
const CURRENCY_PHRASES: &[&str] = &[
"current release",
"current version",
"release atual",
"versão atual",
"versao atual",
"current (",
];
const ORIGIN_QUALIFIERS: &[&str] = &["since", "desde", "same since"];
const TRAILING_CURRENCY: &[&str] = &["(current", "(atual"];
fn last_version_before(line: &str, upto: usize) -> Option<String> {
let head = &line[..upto];
let bytes = head.as_bytes();
let mut end = bytes.len();
while end > 0 {
if !bytes[end - 1].is_ascii_digit() {
end -= 1;
continue;
}
let mut start = end;
let mut dots = 0;
while start > 0 && (bytes[start - 1].is_ascii_digit() || bytes[start - 1] == b'.') {
if bytes[start - 1] == b'.' {
dots += 1;
}
start -= 1;
}
let candidate = head[start..end].trim_matches('.');
if dots >= 2 && candidate.split('.').count() == 3 {
return Some(candidate.to_string());
}
end = start;
}
None
}
fn first_version_after(line: &str, from: usize) -> Option<String> {
let tail = &line[from..];
let bytes = tail.as_bytes();
for start in 0..bytes.len() {
if !bytes[start].is_ascii_digit() {
continue;
}
if start > 0 && (bytes[start - 1].is_ascii_digit() || bytes[start - 1] == b'.') {
continue;
}
let mut end = start;
let mut dots = 0;
while end < bytes.len() && (bytes[end].is_ascii_digit() || bytes[end] == b'.') {
if bytes[end] == b'.' {
dots += 1;
}
end += 1;
}
let candidate = tail[start..end].trim_end_matches('.');
if dots >= 2 && candidate.split('.').count() == 3 {
return Some(candidate.to_string());
}
}
None
}
fn declared_current_versions(text: &str) -> Vec<(usize, String)> {
const ANCHOR: &str = "crate `version = \"";
let mut found: Vec<(usize, String)> = Vec::new();
for (n, line) in text.lines().enumerate() {
let lowered = line.to_lowercase();
if CURRENCY_MARKERS.iter().any(|m| lowered.contains(m)) {
let mut rest = line;
while let Some(at) = rest.find(ANCHOR) {
rest = &rest[at + ANCHOR.len()..];
if let Some(end) = rest.find('"') {
found.push((n + 1, rest[..end].to_string()));
}
}
}
for phrase in CURRENCY_PHRASES {
let Some(at) = lowered.find(phrase) else {
continue;
};
let from = at + phrase.len();
if ORIGIN_QUALIFIERS
.iter()
.any(|q| lowered[from..].trim_start().starts_with(q))
{
continue;
}
if let Some(v) = first_version_after(line, from) {
found.push((n + 1, v));
}
}
for opener in TRAILING_CURRENCY {
let mut at = 0;
while let Some(rel) = lowered[at..].find(opener) {
let hit = at + rel;
if let Some(v) = last_version_before(line, hit) {
found.push((n + 1, v));
}
at = hit + opener.len();
}
}
}
{
let mut unique = Vec::new();
for item in found {
if !unique.contains(&item) {
unique.push(item);
}
}
found = unique;
}
found
}
fn existing_docs() -> Vec<(&'static str, String)> {
DECLARING_DOCS
.iter()
.filter(|rel| repo_root().join(rel).exists())
.map(|rel| (*rel, read_repo_file(rel)))
.collect()
}
#[test]
fn the_gate_found_the_documents_it_is_supposed_to_read() {
let docs = existing_docs();
assert!(
docs.len() >= 10,
"the scan resolved only {} of {} declaring documents — the path list is \
broken and every other test in this file is passing on an empty set",
docs.len(),
DECLARING_DOCS.len()
);
}
#[test]
fn no_document_declares_a_crate_version_other_than_the_shipped_one() {
let shipped = env!("CARGO_PKG_VERSION");
let mut wrong = Vec::new();
for (doc, text) in existing_docs() {
for (line, declared) in declared_current_versions(&text) {
if declared != shipped {
wrong.push(format!("{doc}:{line} introduces {declared} as current"));
}
}
}
assert!(
wrong.is_empty(),
"the crate ships {shipped}, but {} document(s) introduce it as another \
version; an agent reading the manifest first believes the wrong \
surface:\n {}",
wrong.len(),
wrong.join("\n ")
);
}
#[test]
fn every_declared_key_count_matches_the_registry() {
let real = registry_key_count();
let mut wrong = Vec::new();
for (doc, text) in existing_docs() {
for (n, line) in text.lines().enumerate() {
for noun in ["keys", "chaves"] {
for claimed in counts_before(line, noun) {
if !line.contains("config set") {
continue;
}
if claimed != real {
wrong.push(format!("{doc}:{} claims {claimed} {noun}", n + 1));
}
}
}
}
}
assert!(
wrong.is_empty(),
"src/config/registry.rs holds {real} keys, but {} declaration(s) \
disagree:\n {}",
wrong.len(),
wrong.join("\n ")
);
}
#[test]
fn every_declared_schema_count_matches_the_directory() {
let real = schema_file_count();
let mut wrong = Vec::new();
for (doc, text) in existing_docs() {
for (n, line) in text.lines().enumerate() {
if !claims_the_schema_catalogue(line) {
continue;
}
for claimed in counts_before(line, "schemas") {
if claimed != real {
wrong.push(format!("{doc}:{} claims {claimed} schemas", n + 1));
}
}
}
}
assert!(
wrong.is_empty(),
"docs/schemas/ holds {real} JSON schema file(s), but {} declaration(s) \
disagree:\n {}",
wrong.len(),
wrong.join("\n ")
);
}
fn release_order(changelog: &str) -> Vec<String> {
let mut order = Vec::new();
for line in changelog.lines() {
let Some(rest) = line.strip_prefix("## [") else {
continue;
};
let Some(end) = rest.find(']') else {
continue;
};
let version = &rest[..end];
if version.starts_with(|c: char| c.is_ascii_digit()) {
order.push(version.to_string());
}
}
order
}
#[test]
fn both_changelogs_list_their_releases_in_the_same_order() {
let en = release_order(&read_repo_file("CHANGELOG.md"));
let pt = release_order(&read_repo_file("CHANGELOG.pt-BR.md"));
assert!(
en.len() > 50,
"the changelog scan found {} releases, too few to be the real file",
en.len()
);
assert_eq!(
en.len(),
pt.len(),
"CHANGELOG.md lists {} releases and CHANGELOG.pt-BR.md lists {}",
en.len(),
pt.len()
);
let first_divergence = en.iter().zip(pt.iter()).position(|(a, b)| a != b);
assert!(
first_divergence.is_none(),
"the two changelogs diverge at position {}: EN has {} where pt-BR has {}",
first_divergence.unwrap(),
en[first_divergence.unwrap()],
pt[first_divergence.unwrap()]
);
}
#[test]
fn the_count_scanner_reads_the_number_that_precedes_the_noun() {
assert_eq!(
counts_before("- 64 schemas cover `init`", "schemas"),
vec![64]
);
assert_eq!(
counts_before("### key reference (61 keys, v1.2.5)", "keys"),
vec![61]
);
}
#[test]
fn the_count_scanner_ignores_a_noun_with_no_leading_number() {
assert!(counts_before("the schemas live under docs/", "schemas").is_empty());
assert!(counts_before("schemas cover everything", "schemas").is_empty());
}
#[test]
fn the_count_scanner_treats_a_trailing_plus_as_an_approximation() {
assert!(counts_before("regenerates 70+ schemas", "schemas").is_empty());
assert!(counts_before("regenera 70+ schemas", "schemas").is_empty());
}
#[test]
fn the_catalogue_filter_separates_a_total_from_a_subset() {
assert!(claims_the_schema_catalogue(
"- 64 schemas cover `init`, `remember`"
));
assert!(claims_the_schema_catalogue(
"- 64 schemas cobrem `init`, `remember`"
));
assert!(!claims_the_schema_catalogue(
"BUG-15: 7 schemas JSON atualizados: enum expandida"
));
}
fn versions_only(text: &str) -> Vec<String> {
declared_current_versions(text)
.into_iter()
.map(|(_, v)| v)
.collect()
}
#[test]
fn the_version_scanner_requires_a_currency_marker() {
let now = "v1.2.2 (current — crate `version = \"1.2.2\"`; and v1.0.90 added x)";
assert_eq!(versions_only(now), vec!["1.2.2".to_string()]);
let now_pt = "v1.2.2 (atual — crate `version = \"1.2.2\"`)";
assert_eq!(versions_only(now_pt), vec!["1.2.2".to_string()]);
let history = "Official release name v1.1.06; crate `version = \"1.1.6\"` — pin `=1.1.6`.";
assert!(versions_only(history).is_empty());
assert!(versions_only("v1.0.90 added the opencode backend").is_empty());
}
#[test]
fn the_version_scanner_reads_every_currency_phrasing_in_the_corpus() {
assert_eq!(
versions_only("> **Current release: v1.2.2 — agent-native output surface**"),
vec!["1.2.2".to_string()]
);
assert_eq!(
versions_only("> **Release atual: v1.2.2 — superfície agent-native**"),
vec!["1.2.2".to_string()]
);
assert_eq!(
versions_only("- Current version **1.2.2** (crate `1.2.2`; inherits schema v16)"),
vec!["1.2.2".to_string()]
);
assert_eq!(
versions_only("- Versão atual **1.2.2** (crate `1.2.2`; herda schema v16)"),
vec!["1.2.2".to_string()]
);
assert_eq!(
versions_only("> **CURRENT (v1.2.2; same since v1.2.0):** DEFAULT_EMBEDDING_DIM=1024"),
vec!["1.2.2".to_string()]
);
assert_eq!(
versions_only("> **v1.2.2 (current):** agent-native output surface"),
vec!["1.2.2".to_string()]
);
assert_eq!(
versions_only("- **v1.2.2 (atual):** superfície de saída agent-native"),
vec!["1.2.2".to_string()]
);
assert_eq!(
versions_only("sqlite-graphrag **v1.2.5** (current — crate `1.2.5`)"),
vec!["1.2.5".to_string()]
);
assert!(versions_only("> **CURRENT (since v1.2.0):** DEFAULT_EMBEDDING_DIM=1024").is_empty());
assert!(versions_only("> **ATUAL (desde v1.2.0):** dim default 1024").is_empty());
assert!(versions_only("A versão atual de schema é 13").is_empty());
assert!(versions_only("The current release ships no local model").is_empty());
}
#[test]
fn the_version_reader_stops_at_the_first_complete_triple() {
assert_eq!(
first_version_after("x v1.2.5 y", 0).as_deref(),
Some("1.2.5")
);
assert_eq!(
first_version_after("ships 1.2.5.", 0).as_deref(),
Some("1.2.5")
);
assert_eq!(first_version_after("v1.2 only", 0), None);
assert_eq!(first_version_after("no digits here", 0), None);
}
#[test]
fn the_registry_and_schema_counters_return_a_plausible_repository() {
assert!(
registry_key_count() > 40,
"the registry scan found {} keys, too few to be the real registry",
registry_key_count()
);
assert!(
schema_file_count() > 40,
"the schema scan found {} files, too few to be the real directory",
schema_file_count()
);
assert!(Path::new(&repo_root().join("Cargo.toml")).exists());
}