#[ allow( unused_imports ) ]
use super::*;
mod private
{
use std::path::{ Path, PathBuf };
pub fn collect_rs_files( dir : &Path ) -> Vec< PathBuf >
{
let mut result = vec![];
let Ok( entries ) = std::fs::read_dir( dir ) else { return result; };
for entry in entries.flatten()
{
let path = entry.path();
if path.is_dir()
{
result.extend( collect_rs_files( &path ) );
}
else if path.extension().is_some_and( | ext | ext == "rs" )
{
result.push( path );
}
}
result
}
pub fn read_tests_files() -> Vec< ( PathBuf, String ) >
{
let base = Path::new( env!( "CARGO_MANIFEST_DIR" ) );
collect_rs_files( &base.join( "tests" ) )
.into_iter()
.filter_map( | p | std::fs::read_to_string( &p ).ok().map( | c | ( p, c ) ) )
.collect()
}
}
#[ test ]
fn test_in_07()
{
let files = private::read_tests_files();
let mut wrong_order = 0usize;
let mut missing_gate = 0usize;
for ( path, content ) in &files
{
let lines : Vec< &str > = content.lines().collect();
for window in lines.windows( 2 )
{
let prev = window[ 0 ].trim();
let next = window[ 1 ].trim();
let prev_is_test = prev == "#[ tokio::test ]" || prev == "#[ test ]"
|| prev.contains( "tokio::test" );
let next_is_gate = next.contains( "cfg( feature = \"integration\"" )
|| next.contains( "cfg(feature = \"integration\"" );
if prev_is_test && next_is_gate
{
eprintln!( "IN-07: wrong gate order in {path:?}" );
wrong_order += 1;
}
}
for ( idx, line ) in lines.iter().enumerate()
{
if line.trim().starts_with( "#[" ) && line.trim().contains( "tokio::test" )
{
let lookahead_end = ( idx + 25 ).min( lines.len() );
let calls_api = lines[ idx..lookahead_end ].iter().any( | l |
( l.contains( "from_workspace()" ) || l.contains( "from_env()" ) )
&& !l.trim_start().starts_with( "//" )
);
if calls_api
{
let start = idx.saturating_sub( 4 );
let has_gate = lines[ start..idx ].iter().any( | l |
l.contains( "cfg( feature = \"integration\"" )
|| l.contains( "cfg(feature = \"integration\"" )
);
if !has_gate
{
eprintln!( "IN-07: API-calling test at line {} lacks integration gate in {path:?}", idx + 1 );
missing_gate += 1;
}
}
}
}
}
assert_eq!( wrong_order, 0,
"IN-07: found {wrong_order} reversed gate/test pairs — cfg must come before test attribute" );
assert_eq!( missing_gate, 0,
"IN-07: found {missing_gate} async test functions without integration feature gate" );
}
#[ test ]
fn test_in_08()
{
let saved = std::env::var( "ANTHROPIC_API_KEY" ).ok();
std::env::remove_var( "ANTHROPIC_API_KEY" );
let result = the_module::Client::from_env();
if let Some( k ) = saved { std::env::set_var( "ANTHROPIC_API_KEY", k ); }
assert!( result.is_err(), "IN-08: from_env() with no key must return Err" );
let msg = result.unwrap_err().to_string();
assert!( !msg.is_empty(), "IN-08: error message must be non-empty and actionable" );
}
#[ test ]
fn test_in_09()
{
let wire_mock = format!( "{}mock", "wire" );
let http_mock = format!( "{}mock", "http" );
let files = private::read_tests_files();
let mut mock_count = 0usize;
for ( path, content ) in &files
{
if content.contains( &wire_mock ) || content.contains( &http_mock )
{
eprintln!( "IN-09: mock HTTP server found in {path:?}" );
mock_count += 1;
}
}
assert_eq!( mock_count, 0,
"IN-09: found {mock_count} test file(s) importing mock HTTP servers — all tests must use real API" );
}
#[ test ]
fn test_in_10()
{
let sk_ant_test = format!( "sk-ant{}", "-test-" );
let files = private::read_tests_files();
let mut fake_count = 0usize;
for ( path, content ) in &files
{
if content.contains( &sk_ant_test )
{
eprintln!( "IN-10: hardcoded test key found in {path:?}" );
fake_count += 1;
}
}
assert_eq!( fake_count, 0,
"IN-10: found {fake_count} test file(s) with hardcoded API key test strings" );
}
#[ test ]
fn test_in_11()
{
let ok_client1 = format!( "if let {}( client )", "Ok" );
let ok_client2 = format!( "if let {}(client)", "Ok" );
let files = private::read_tests_files();
let mut skip_count = 0usize;
for ( path, content ) in &files
{
if content.contains( &ok_client1 ) || content.contains( &ok_client2 )
{
eprintln!( "IN-11: conditional skip logic found in {path:?}" );
skip_count += 1;
}
}
assert_eq!( skip_count, 0,
"IN-11: found {skip_count} test file(s) with conditional skip logic — use .expect() unconditionally" );
}
#[ test ]
fn test_in_12()
{
let ignore_spaced = format!( "#[ {} ]", "ignore" );
let ignore_compact = format!( "#[{}]", "ignore" );
let commented_test_attr = format!( "// #[ {} ]", "test" );
let commented_fn = format!( "// {}fn test_", "async " );
let commented_sync_fn = format!( "// {}test_", "fn " );
let files = private::read_tests_files();
let mut ignore_count = 0usize;
let mut commented_count = 0usize;
for ( path, content ) in &files
{
if content.contains( &ignore_spaced ) || content.contains( &ignore_compact )
{
eprintln!( "IN-12: disabled-test attribute found in {path:?}" );
ignore_count += 1;
}
if content.contains( &commented_test_attr )
|| content.contains( &commented_fn )
|| content.contains( &commented_sync_fn )
{
eprintln!( "IN-12: commented-out test found in {path:?}" );
commented_count += 1;
}
}
assert_eq!( ignore_count, 0,
"IN-12: found {ignore_count} test file(s) with {ignore_compact} — all tests must be fixed or removed" );
assert_eq!( commented_count, 0,
"IN-12: found {commented_count} test file(s) with commented-out test functions — remove or restore" );
}