#![ allow( clippy::uninlined_format_args ) ]
#![ allow( clippy::format_push_string ) ]
#![ allow( clippy::doc_markdown ) ]
#![ allow( clippy::too_many_lines ) ]
#![ allow( clippy::manual_assert ) ]
use std::{ fs, path::PathBuf };
const VALID_CURRENT_MODELS: &[ &str ] = &
[
"claude-sonnet-4-5-20250929",
"claude-haiku-4-5-20251001",
"claude-opus-4-1-20250805",
];
const VALID_LEGACY_MODELS: &[ &str ] = &
[
"claude-sonnet-4-20250514",
"claude-3-7-sonnet-20250219",
"claude-opus-4-20250514",
"claude-3-5-haiku-20241022",
"claude-3-haiku-20240307",
];
const VALID_MODEL_ALIASES: &[ &str ] = &
[
"claude-sonnet-4-5",
"claude-haiku-4-5",
"claude-opus-4-1",
];
#[ test ]
fn examples_use_valid_model_names()
{
let examples_dir = PathBuf::from( env!( "CARGO_MANIFEST_DIR" ) ).join( "examples" );
if !examples_dir.exists()
{
panic!( "Examples directory not found : {}", examples_dir.display() );
}
let mut invalid_models = Vec::new();
let mut files_checked = 0;
for entry in fs::read_dir( &examples_dir ).expect( "Failed to read examples directory" )
{
let entry = entry.expect( "Failed to read directory entry" );
let path = entry.path();
if path.extension().and_then( | s | s.to_str() ) != Some( "rs" )
{
continue;
}
files_checked += 1;
let content = fs::read_to_string( &path )
.unwrap_or_else( | e | panic!( "Failed to read file {}: {}", path.display(), e ) );
let lines : Vec< &str > = content.lines().collect();
let mut models_in_file = Vec::new();
for line in &lines
{
if let Some( start ) = line.find( ".model(" )
{
if let Some( quote_start ) = line[ start.. ].find( '"' )
{
let from_quote = &line[ start + quote_start + 1.. ];
if let Some( quote_end ) = from_quote.find( '"' )
{
let model = &from_quote[ ..quote_end ];
if model.starts_with( "claude-" )
{
models_in_file.push( model.to_string() );
}
}
}
}
if line.contains( "model" ) && line.contains( ':' )
{
if let Some( colon_pos ) = line.find( ':' )
{
let after_colon = &line[ colon_pos + 1.. ];
if let Some( quote_start ) = after_colon.find( '"' )
{
let from_quote = &after_colon[ quote_start + 1.. ];
if let Some( quote_end ) = from_quote.find( '"' )
{
let model = &from_quote[ ..quote_end ];
if model.starts_with( "claude-" )
{
models_in_file.push( model.to_string() );
}
}
}
}
}
}
for model_name in models_in_file
{
let is_valid = VALID_CURRENT_MODELS.contains( &model_name.as_str() )
|| VALID_LEGACY_MODELS.contains( &model_name.as_str() )
|| VALID_MODEL_ALIASES.contains( &model_name.as_str() );
if !is_valid
{
invalid_models.push( ( path.file_name().unwrap().to_string_lossy().to_string(), model_name ) );
}
}
}
assert!(
files_checked > 0,
"No example files found in {}. Expected at least 9 files.",
examples_dir.display()
);
if !invalid_models.is_empty()
{
let mut error_msg = format!(
"\nFound {} invalid model name(s) in example files:\n",
invalid_models.len()
);
for ( file, model ) in &invalid_models
{
error_msg.push_str( &format!( " - {}: '{}'\n", file, model ) );
}
error_msg.push_str( "\nValid current models:\n" );
for model in VALID_CURRENT_MODELS
{
error_msg.push_str( &format!( " - {}\n", model ) );
}
error_msg.push_str( "\nValid legacy models:\n" );
for model in VALID_LEGACY_MODELS
{
error_msg.push_str( &format!( " - {}\n", model ) );
}
error_msg.push_str( "\nValid aliases:\n" );
for model in VALID_MODEL_ALIASES
{
error_msg.push_str( &format!( " - {}\n", model ) );
}
panic!( "{error_msg}" );
}
println!( "✅ All {} example files use valid Claude model names", files_checked );
}
#[ test ]
fn example_documentation_matches_filename()
{
let examples_dir = PathBuf::from( env!( "CARGO_MANIFEST_DIR" ) ).join( "examples" );
if !examples_dir.exists()
{
return;
}
let mut mismatches = Vec::new();
for entry in fs::read_dir( &examples_dir ).expect( "Failed to read examples directory" )
{
let entry = entry.expect( "Failed to read directory entry" );
let path = entry.path();
if path.extension().and_then( | s | s.to_str() ) != Some( "rs" )
{
continue;
}
let filename = path.file_stem().unwrap().to_str().unwrap();
let content = fs::read_to_string( &path )
.unwrap_or_else( | e | panic!( "Failed to read file {}: {}", path.display(), e ) );
for line in content.lines()
{
if line.contains( "cargo run --example " ) || line.contains( "cargo_run_--example_" )
{
if let Some( pos ) = line.find( "--example " )
{
let after_example = &line[ pos + 10.. ]; let documented_name = after_example
.split_whitespace()
.next()
.unwrap_or( "" )
.trim_matches( |c : char| !c.is_alphanumeric() && c != '_' && c != '-' );
if !documented_name.is_empty() && documented_name != filename
{
mismatches.push( ( filename.to_string(), documented_name.to_string() ) );
}
}
}
}
}
if !mismatches.is_empty()
{
let mut error_msg = format!(
"\nFound {} example filename/documentation mismatch(es):\n",
mismatches.len()
);
for ( filename, documented ) in &mismatches
{
error_msg.push_str( &format!(
" - File : {}.rs, Documentation says : 'cargo run --example {}'\n",
filename, documented
) );
}
panic!( "{error_msg}" );
}
}