generated from nhcarrigan/template
feat: add tests and assert coverage (#71)
### Explanation _No response_ ### Issue _No response_ ### Attestations - [ ] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [ ] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [ ] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [ ] I have pinned the dependencies to a specific patch version. ### Style - [ ] I have run the linter and resolved any errors. - [ ] My pull request uses an appropriate title, matching the conventional commit standards. - [ ] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Co-authored-by: Hikari <hikari@nhcarrigan.com> Reviewed-on: #71 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #71.
This commit is contained in:
@@ -369,6 +369,36 @@ mod tests {
|
||||
assert!((cost - 0.165).abs() < 0.0001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cost_calculation_opus_45() {
|
||||
let cost = calculate_cost(1000, 2000, "claude-opus-4-5-20251101");
|
||||
// Same pricing as Opus 4
|
||||
assert!((cost - 0.165).abs() < 0.0001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cost_calculation_haiku() {
|
||||
let cost = calculate_cost(1000, 2000, "claude-3-5-haiku-20241022");
|
||||
// 1000 input * $1/M = $0.001
|
||||
// 2000 output * $5/M = $0.010
|
||||
// Total = $0.011
|
||||
assert!((cost - 0.011).abs() < 0.0001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cost_calculation_unknown_defaults_to_sonnet() {
|
||||
let cost = calculate_cost(1000, 2000, "some-unknown-model");
|
||||
// Should default to Sonnet pricing
|
||||
assert!((cost - 0.033).abs() < 0.0001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cost_calculation_legacy_sonnet() {
|
||||
let cost = calculate_cost(1000, 2000, "claude-3-5-sonnet-20241022");
|
||||
// Same as Sonnet 4 pricing
|
||||
assert!((cost - 0.033).abs() < 0.0001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_usage_stats_accumulation() {
|
||||
let mut stats = UsageStats::new();
|
||||
@@ -381,6 +411,28 @@ mod tests {
|
||||
assert!((stats.total_cost_usd - 0.033).abs() < 0.0001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_usage_stats_multiple_accumulations() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.add_usage(1000, 1000, "claude-sonnet-4-20250514");
|
||||
stats.add_usage(500, 500, "claude-sonnet-4-20250514");
|
||||
|
||||
assert_eq!(stats.total_input_tokens, 1500);
|
||||
assert_eq!(stats.total_output_tokens, 1500);
|
||||
assert_eq!(stats.session_input_tokens, 1500);
|
||||
assert_eq!(stats.session_output_tokens, 1500);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_usage_stats_model_updated() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.add_usage(1000, 1000, "claude-sonnet-4-20250514");
|
||||
assert_eq!(stats.model, Some("claude-sonnet-4-20250514".to_string()));
|
||||
|
||||
stats.add_usage(500, 500, "claude-opus-4-20250514");
|
||||
assert_eq!(stats.model, Some("claude-opus-4-20250514".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_session_reset() {
|
||||
let mut stats = UsageStats::new();
|
||||
@@ -394,4 +446,230 @@ mod tests {
|
||||
assert_eq!(stats.session_cost_usd, 0.0);
|
||||
assert!(stats.total_cost_usd > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_session_reset_clears_session_stats() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.increment_messages();
|
||||
stats.increment_messages();
|
||||
stats.increment_code_blocks();
|
||||
stats.increment_files_edited();
|
||||
stats.increment_files_created();
|
||||
stats.increment_tool_usage("Read");
|
||||
|
||||
stats.reset_session();
|
||||
|
||||
assert_eq!(stats.session_messages_exchanged, 0);
|
||||
assert_eq!(stats.session_code_blocks_generated, 0);
|
||||
assert_eq!(stats.session_files_edited, 0);
|
||||
assert_eq!(stats.session_files_created, 0);
|
||||
assert!(stats.session_tools_usage.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_increment_messages() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.increment_messages();
|
||||
stats.increment_messages();
|
||||
stats.increment_messages();
|
||||
|
||||
assert_eq!(stats.messages_exchanged, 3);
|
||||
assert_eq!(stats.session_messages_exchanged, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_increment_code_blocks() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.increment_code_blocks();
|
||||
stats.increment_code_blocks();
|
||||
|
||||
assert_eq!(stats.code_blocks_generated, 2);
|
||||
assert_eq!(stats.session_code_blocks_generated, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_increment_files_edited() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.increment_files_edited();
|
||||
|
||||
assert_eq!(stats.files_edited, 1);
|
||||
assert_eq!(stats.session_files_edited, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_increment_files_created() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.increment_files_created();
|
||||
|
||||
assert_eq!(stats.files_created, 1);
|
||||
assert_eq!(stats.session_files_created, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_increment_tool_usage() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.increment_tool_usage("Read");
|
||||
stats.increment_tool_usage("Read");
|
||||
stats.increment_tool_usage("Write");
|
||||
|
||||
assert_eq!(stats.tools_usage.get("Read"), Some(&2));
|
||||
assert_eq!(stats.tools_usage.get("Write"), Some(&1));
|
||||
assert_eq!(stats.session_tools_usage.get("Read"), Some(&2));
|
||||
assert_eq!(stats.session_tools_usage.get("Write"), Some(&1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_session_duration_tracking() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.session_start = Some(Instant::now());
|
||||
|
||||
// Verify duration is returned (u64 is always non-negative)
|
||||
let _duration = stats.get_session_duration();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_session_duration_without_start() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.session_start = None;
|
||||
stats.session_duration_seconds = 100;
|
||||
|
||||
// Should return the stored value when no start time
|
||||
let duration = stats.get_session_duration();
|
||||
assert_eq!(duration, 100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_consecutive_day_true() {
|
||||
assert!(is_consecutive_day("2024-01-15", "2024-01-16"));
|
||||
assert!(is_consecutive_day("2024-12-31", "2025-01-01"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_consecutive_day_false() {
|
||||
assert!(!is_consecutive_day("2024-01-15", "2024-01-15")); // Same day
|
||||
assert!(!is_consecutive_day("2024-01-15", "2024-01-17")); // Gap
|
||||
assert!(!is_consecutive_day("2024-01-15", "2024-01-14")); // Backwards
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_consecutive_day_invalid_dates() {
|
||||
assert!(!is_consecutive_day("invalid", "2024-01-01"));
|
||||
assert!(!is_consecutive_day("2024-01-01", "invalid"));
|
||||
assert!(!is_consecutive_day("invalid", "also-invalid"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_persisted_stats_from_usage_stats() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.total_input_tokens = 5000;
|
||||
stats.total_output_tokens = 10000;
|
||||
stats.total_cost_usd = 1.23;
|
||||
stats.messages_exchanged = 50;
|
||||
stats.sessions_started = 5;
|
||||
stats.consecutive_days = 3;
|
||||
|
||||
let persisted = PersistedStats::from(&stats);
|
||||
|
||||
assert_eq!(persisted.total_input_tokens, 5000);
|
||||
assert_eq!(persisted.total_output_tokens, 10000);
|
||||
assert_eq!(persisted.total_cost_usd, 1.23);
|
||||
assert_eq!(persisted.messages_exchanged, 50);
|
||||
assert_eq!(persisted.sessions_started, 5);
|
||||
assert_eq!(persisted.consecutive_days, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_persisted_stats() {
|
||||
let persisted = PersistedStats {
|
||||
total_input_tokens: 10000,
|
||||
total_output_tokens: 20000,
|
||||
total_cost_usd: 5.50,
|
||||
messages_exchanged: 100,
|
||||
code_blocks_generated: 25,
|
||||
files_edited: 10,
|
||||
files_created: 5,
|
||||
tools_usage: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("Read".to_string(), 50);
|
||||
map
|
||||
},
|
||||
sessions_started: 10,
|
||||
consecutive_days: 7,
|
||||
total_days_used: 14,
|
||||
morning_sessions: 3,
|
||||
night_sessions: 2,
|
||||
last_session_date: Some("2024-06-15".to_string()),
|
||||
};
|
||||
|
||||
let mut stats = UsageStats::new();
|
||||
stats.apply_persisted(persisted);
|
||||
|
||||
assert_eq!(stats.total_input_tokens, 10000);
|
||||
assert_eq!(stats.total_output_tokens, 20000);
|
||||
assert_eq!(stats.total_cost_usd, 5.50);
|
||||
assert_eq!(stats.messages_exchanged, 100);
|
||||
assert_eq!(stats.tools_usage.get("Read"), Some(&50));
|
||||
assert_eq!(stats.consecutive_days, 7);
|
||||
assert_eq!(stats.morning_sessions, 3);
|
||||
assert_eq!(stats.last_session_date, Some("2024-06-15".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_usage_stats_default() {
|
||||
let stats = UsageStats::default();
|
||||
|
||||
assert_eq!(stats.total_input_tokens, 0);
|
||||
assert_eq!(stats.total_output_tokens, 0);
|
||||
assert_eq!(stats.total_cost_usd, 0.0);
|
||||
assert!(stats.model.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_persisted_stats_default() {
|
||||
let persisted = PersistedStats::default();
|
||||
|
||||
assert_eq!(persisted.total_input_tokens, 0);
|
||||
assert!(persisted.last_session_date.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_usage_stats_serialization() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.add_usage(1000, 2000, "claude-sonnet-4-20250514");
|
||||
stats.increment_messages();
|
||||
|
||||
// UsageStats should be serializable (for events)
|
||||
let json = serde_json::to_string(&stats).expect("Failed to serialize");
|
||||
assert!(json.contains("total_input_tokens"));
|
||||
assert!(json.contains("1000"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_persisted_stats_serialization() {
|
||||
let persisted = PersistedStats {
|
||||
total_input_tokens: 1234,
|
||||
total_output_tokens: 5678,
|
||||
total_cost_usd: 0.99,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&persisted).expect("Failed to serialize");
|
||||
let parsed: PersistedStats = serde_json::from_str(&json).expect("Failed to deserialize");
|
||||
|
||||
assert_eq!(parsed.total_input_tokens, 1234);
|
||||
assert_eq!(parsed.total_output_tokens, 5678);
|
||||
assert!((parsed.total_cost_usd - 0.99).abs() < 0.0001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_stats_update_event_serialization() {
|
||||
let mut stats = UsageStats::new();
|
||||
stats.add_usage(100, 200, "claude-sonnet-4-20250514");
|
||||
|
||||
let event = StatsUpdateEvent { stats };
|
||||
let json = serde_json::to_string(&event).expect("Failed to serialize");
|
||||
|
||||
assert!(json.contains("stats"));
|
||||
assert!(json.contains("total_input_tokens"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user