#[cfg(all(test, feature = "caching"))]
mod d1_cache_tests {
use recoco::base::schema::{BasicValueType, EnrichedValueType, FieldSchema, ValueType};
use thread_flow::monitoring::performance::PerformanceMetrics;
use thread_flow::targets::d1::D1ExportContext;
fn test_field_schema(name: &str, typ: BasicValueType, nullable: bool) -> FieldSchema {
FieldSchema::new(
name,
EnrichedValueType {
typ: ValueType::Basic(typ),
nullable,
attrs: Default::default(),
},
)
}
fn create_test_context() -> D1ExportContext {
let metrics = PerformanceMetrics::new();
let key_schema = vec![test_field_schema("id", BasicValueType::Int64, false)];
let value_schema = vec![test_field_schema("content", BasicValueType::Str, false)];
D1ExportContext::new_with_default_client(
"test-database".to_string(),
"test_table".to_string(),
"test-account".to_string(),
"test-token".to_string(),
key_schema,
value_schema,
metrics,
)
.expect("Failed to create test context")
}
#[tokio::test]
async fn test_cache_initialization() {
let context = create_test_context();
let cache_stats = context.cache_stats().await;
assert_eq!(cache_stats.hits, 0, "Initial cache should have 0 hits");
assert_eq!(cache_stats.misses, 0, "Initial cache should have 0 misses");
assert_eq!(
cache_stats.total_lookups, 0,
"Initial cache should have 0 lookups"
);
}
#[tokio::test]
async fn test_cache_clear() {
let context = create_test_context();
context.clear_cache().await;
let cache_stats = context.cache_stats().await;
assert_eq!(cache_stats.total_lookups, 0, "Cache should still be empty");
}
#[tokio::test]
async fn test_cache_entry_count() {
let context = create_test_context();
let count = context.query_cache.entry_count();
assert_eq!(count, 0, "Initial cache should be empty");
}
#[tokio::test]
async fn test_cache_statistics_integration() {
let context = create_test_context();
let cache_stats = context.cache_stats().await;
let metrics_stats = context.metrics.cache_stats();
assert_eq!(cache_stats.hits, metrics_stats.hits);
assert_eq!(cache_stats.misses, metrics_stats.misses);
}
#[test]
fn test_cache_config() {
use thread_flow::cache::CacheConfig;
let config = CacheConfig {
max_capacity: 10_000,
ttl_seconds: 300,
};
assert_eq!(config.max_capacity, 10_000, "Cache capacity should be 10k");
assert_eq!(config.ttl_seconds, 300, "Cache TTL should be 5 minutes");
}
#[tokio::test]
async fn test_constitutional_compliance_structure() {
let context = create_test_context();
assert!(context.query_cache.entry_count() == 0, "Cache should exist");
let stats = context.metrics.cache_stats();
println!("Cache metrics available: {:?}", stats);
let cache_stats = context.cache_stats().await;
println!("Cache stats available: {:?}", cache_stats);
println!("✅ Cache infrastructure ready for >90% hit rate validation");
}
}
#[cfg(all(test, not(feature = "caching")))]
mod d1_no_cache_tests {
use recoco::base::schema::{BasicValueType, EnrichedValueType, FieldSchema, ValueType};
use thread_flow::monitoring::performance::PerformanceMetrics;
use thread_flow::targets::d1::D1ExportContext;
fn test_field_schema(name: &str, typ: BasicValueType, nullable: bool) -> FieldSchema {
FieldSchema::new(
name,
EnrichedValueType {
typ: ValueType::Basic(typ),
nullable,
attrs: Default::default(),
},
)
}
#[tokio::test]
async fn test_no_cache_mode_works() {
let metrics = PerformanceMetrics::new();
let key_schema = vec![test_field_schema("id", BasicValueType::Int64, false)];
let value_schema = vec![test_field_schema("content", BasicValueType::Str, false)];
let _context = D1ExportContext::new_with_default_client(
"test-database".to_string(),
"test_table".to_string(),
"test-account".to_string(),
"test-token".to_string(),
key_schema,
value_schema,
metrics,
)
.expect("Failed to create context without caching");
assert!(
true,
"D1ExportContext created successfully without caching feature"
);
}
}