use super::*;
#[test]
fn token_usage_default_is_zeros() {
let u = TokenUsage::default();
assert_eq!(u.prompt_tokens, 0);
assert_eq!(u.completion_tokens, 0);
assert_eq!(u.cache_read_tokens, 0);
assert_eq!(u.cache_creation_tokens, 0);
}
#[test]
fn token_usage_accumulates() {
let mut a = TokenUsage::new(10, 5, 2, 1);
a.add(&TokenUsage::new(3, 7, 0, 4));
assert_eq!(a, TokenUsage::new(13, 12, 2, 5));
}
#[test]
fn token_usage_cost_accumulates() {
let mut both = TokenUsage::new(10, 5, 2, 1);
both.cost_usd = Some(0.01);
let mut other = TokenUsage::new(3, 7, 0, 4);
other.cost_usd = Some(0.02);
both.add(&other);
assert_eq!(both.cost_usd, Some(0.03));
let mut one_sided = TokenUsage::new(10, 5, 2, 1);
one_sided.cost_usd = Some(0.01);
one_sided.add(&TokenUsage::new(3, 7, 0, 4));
assert_eq!(one_sided.cost_usd, Some(0.01));
let mut neither = TokenUsage::new(10, 5, 2, 1);
neither.add(&TokenUsage::new(3, 7, 0, 4));
assert_eq!(neither.cost_usd, None);
}
#[test]
fn cost_usd_known_sonnet() {
let c = cost_usd("anthropic/claude-sonnet-4-5", 1_000_000, 0, 0, 0);
assert!((c - 3.0).abs() < 1e-9, "got {c}");
}
#[test]
fn cost_usd_known_haiku() {
let c = cost_usd("anthropic/claude-haiku-4", 0, 1_000_000, 0, 0);
assert!((c - 4.0).abs() < 1e-9, "got {c}");
}
#[test]
fn cost_usd_opus_5_matches_published_rates() {
assert!((cost_usd("anthropic/claude-opus-5", 1_000_000, 0, 0, 0) - 5.0).abs() < 1e-9);
assert!((cost_usd("anthropic/claude-opus-5", 0, 1_000_000, 0, 0) - 25.0).abs() < 1e-9);
assert!((cost_usd("anthropic/claude-opus-5", 0, 0, 1_000_000, 0) - 0.50).abs() < 1e-9);
assert!((cost_usd("anthropic/claude-opus-5", 0, 0, 0, 1_000_000) - 6.25).abs() < 1e-9);
}
#[test]
fn cost_usd_sonnet_5_matches_published_rates() {
assert!((cost_usd("anthropic/claude-sonnet-5", 1_000_000, 0, 0, 0) - 2.0).abs() < 1e-9);
assert!((cost_usd("anthropic/claude-sonnet-5", 0, 1_000_000, 0, 0) - 10.0).abs() < 1e-9);
assert!((cost_usd("anthropic/claude-sonnet-5", 0, 0, 1_000_000, 0) - 0.20).abs() < 1e-9);
assert!((cost_usd("anthropic/claude-sonnet-5", 0, 0, 0, 1_000_000) - 2.50).abs() < 1e-9);
}
#[test]
fn cost_usd_haiku_4_5_matches_published_rates() {
for slug in ["anthropic/claude-haiku-4.5", "claude-haiku-4-5"] {
assert!(
(cost_usd(slug, 1_000_000, 0, 0, 0) - 1.0).abs() < 1e-9,
"input rate for {slug}"
);
assert!(
(cost_usd(slug, 0, 1_000_000, 0, 0) - 5.0).abs() < 1e-9,
"output rate for {slug}"
);
assert!(
(cost_usd(slug, 0, 0, 1_000_000, 0) - 0.10).abs() < 1e-9,
"cache-read rate for {slug}"
);
assert!(
(cost_usd(slug, 0, 0, 0, 1_000_000) - 1.25).abs() < 1e-9,
"cache-write rate for {slug}"
);
}
}
#[test]
fn cost_usd_legacy_4x_rows_are_unchanged() {
assert!((cost_usd("anthropic/claude-sonnet-4-5", 1_000_000, 0, 0, 0) - 3.0).abs() < 1e-9);
assert!((cost_usd("anthropic/claude-opus-4-5", 0, 1_000_000, 0, 0) - 75.0).abs() < 1e-9);
assert!((cost_usd("anthropic/claude-haiku-4", 1_000_000, 0, 0, 0) - 0.80).abs() < 1e-9);
assert!((cost_usd("some-vendor/unheard-of", 1_000_000, 0, 0, 0) - 3.0).abs() < 1e-9);
}
#[test]
fn cost_usd_cache_read_is_cheaper() {
let fresh = cost_usd("claude-sonnet-4-6", 1_000_000, 0, 0, 0);
let cached = cost_usd("claude-sonnet-4-6", 0, 0, 1_000_000, 0);
assert!(cached < fresh);
assert!((cached - 0.30).abs() < 1e-9);
}
#[test]
fn cost_usd_sonnet_cache_creation_matches_spec() {
let c = cost_usd("anthropic/claude-sonnet-4-6", 0, 0, 0, 1_000_000);
assert!(
(c - 3.75).abs() < 1e-9,
"expected $3.75 for 1M cache write tokens on sonnet-4-6, got {c}"
);
}
#[test]
fn cost_usd_sonnet_cache_read_is_one_tenth_of_input() {
let fresh_input = cost_usd("claude-sonnet-4-6", 1_000_000, 0, 0, 0);
let cache_read = cost_usd("claude-sonnet-4-6", 0, 0, 1_000_000, 0);
let ratio = cache_read / fresh_input;
assert!(
(ratio - 0.10).abs() < 1e-9,
"cache_read should be 10% of input cost, got {ratio} (fresh={fresh_input}, cached={cache_read})"
);
}
#[test]
fn cost_usd_sonnet_mixed_cache_hit_scenario() {
let c = cost_usd("anthropic/claude-sonnet-4-6", 100, 50, 9_000, 1_000);
let expected = 0.0003 + 0.00075 + 0.0027 + 0.00375;
assert!((c - expected).abs() < 1e-9, "expected {expected}, got {c}");
}
#[test]
fn cost_usd_unknown_defaults_to_sonnet() {
let u = cost_usd("some/unknown-model", 1_000_000, 0, 0, 0);
assert!((u - 3.0).abs() < 1e-9);
}
#[test]
fn collector_records_phases() {
let mut c = PerfCollector::new(7, "prescriptive", "write x");
c.record_phase(
"research",
500,
"claude-sonnet-4-5",
&TokenUsage::new(1000, 500, 0, 0),
);
c.record_phase(
"code",
1200,
"claude-sonnet-4-5",
&TokenUsage::new(2000, 1000, 500, 200),
);
let r = c.build_record();
assert_eq!(r.phases.len(), 2);
assert_eq!(r.phases[0].prompt_tokens, 1000);
assert_eq!(r.totals.prompt_tokens, 3000);
assert_eq!(r.totals.completion_tokens, 1500);
assert_eq!(r.totals.cache_read_tokens, 500);
assert_eq!(r.totals.cache_creation_tokens, 200);
assert!(r.totals.cost_usd > 0.0);
assert_eq!(r.build, 7);
assert_eq!(r.workflow, "prescriptive");
}
#[test]
fn record_phase_prefers_authoritative_cost() {
let mut c = PerfCollector::new(1, "wf", "task");
let mut usage = TokenUsage::new(1_000_000, 0, 0, 0);
usage.cost_usd = Some(0.000001); c.record_phase("turn", 10, "anthropic/claude-sonnet-4-5", &usage);
let r = c.build_record();
assert_eq!(r.phases[0].cost_usd, 0.000001);
}
#[test]
fn truncate_preview_respects_limit() {
let s = "x".repeat(200);
let p = truncate_preview(&s, 120);
assert_eq!(p.chars().count(), 123, "120 chars + ellipsis");
assert!(p.ends_with("..."));
}
#[test]
fn truncate_preview_short_string_unchanged() {
let p = truncate_preview("hi", 120);
assert_eq!(p, "hi");
}
#[test]
fn filename_stamp_format() {
let s = filename_stamp("2026-04-22T17:31:30Z", 42);
assert_eq!(s, "20260422-173130-build42");
}
#[tokio::test]
async fn collector_flush_records_failed_status() {
let tmp = tempfile::tempdir().unwrap();
let mut c = PerfCollector::new(11, "test-wf", "broken task");
c.record_phase(
"research",
50,
"claude-sonnet-4-6",
&TokenUsage::new(100, 50, 0, 0),
);
c.set_status("partial");
c.set_failed_phase("plan");
c.flush(tmp.path()).await.unwrap();
let runs = tmp.path().join("runs");
let mut entries = tokio::fs::read_dir(&runs).await.unwrap();
let mut rec: Option<PerfRecord> = None;
while let Some(e) = entries.next_entry().await.unwrap() {
if e.path().extension().and_then(|s| s.to_str()) == Some("json") {
let bytes = tokio::fs::read(e.path()).await.unwrap();
rec = Some(serde_json::from_slice(&bytes).unwrap());
}
}
let rec = rec.expect("perf json written");
assert_eq!(rec.status, "partial");
assert_eq!(rec.failed_phase.as_deref(), Some("plan"));
}
#[test]
fn collector_default_status_is_success() {
let c = PerfCollector::new(1, "wf", "task");
let r = c.build_record();
assert_eq!(r.status, "success");
assert!(r.failed_phase.is_none());
}
#[tokio::test]
async fn test_run_record_serializes_test_counts() {
let tmp = tempfile::tempdir().unwrap();
let mut c = PerfCollector::new(99, "test-wf", "task with tests");
c.record_phase("qa", 10, "claude-sonnet-4-6", &TokenUsage::new(10, 5, 0, 0));
c.set_test_counts(42, 0);
c.flush(tmp.path()).await.unwrap();
let runs = tmp.path().join("runs");
let mut entries = tokio::fs::read_dir(&runs).await.unwrap();
let mut rec: Option<PerfRecord> = None;
while let Some(e) = entries.next_entry().await.unwrap() {
if e.path().extension().and_then(|s| s.to_str()) == Some("json") {
let bytes = tokio::fs::read(e.path()).await.unwrap();
rec = Some(serde_json::from_slice(&bytes).unwrap());
}
}
let rec = rec.expect("perf json written");
assert_eq!(rec.tests_passed, Some(42));
assert_eq!(rec.tests_failed, Some(0));
let json_str = serde_json::to_string(&rec).unwrap();
assert!(json_str.contains("\"tests_passed\":42"));
assert!(json_str.contains("\"tests_failed\":0"));
let log = tokio::fs::read_to_string(tmp.path().join("runs.log"))
.await
.unwrap();
assert!(
log.contains("tests_passed=42"),
"runs.log missing tests_passed=42: {log}"
);
assert!(
log.contains("tests_failed=0"),
"runs.log missing tests_failed=0: {log}"
);
}
#[tokio::test]
async fn run_record_omits_test_counts_when_none() {
let tmp = tempfile::tempdir().unwrap();
let mut c = PerfCollector::new(100, "test-wf", "no qa");
c.record_phase(
"research",
5,
"claude-sonnet-4-6",
&TokenUsage::new(10, 5, 0, 0),
);
c.flush(tmp.path()).await.unwrap();
let log = tokio::fs::read_to_string(tmp.path().join("runs.log"))
.await
.unwrap();
assert!(
log.contains("tests_passed=-"),
"expected '-' placeholder: {log}"
);
assert!(
log.contains("tests_failed=-"),
"expected '-' placeholder: {log}"
);
}
#[tokio::test]
async fn collector_flush_writes_json_and_log() {
let tmp = tempfile::tempdir().unwrap();
let mut c = PerfCollector::new(5, "test-wf", "hello task");
c.record_phase(
"plan",
100,
"claude-sonnet-4-5",
&TokenUsage::new(100, 50, 0, 0),
);
c.flush(tmp.path()).await.unwrap();
let runs = tmp.path().join("runs");
assert!(runs.exists());
let mut entries = tokio::fs::read_dir(&runs).await.unwrap();
let mut found_json = false;
while let Some(e) = entries.next_entry().await.unwrap() {
if e.path().extension().and_then(|s| s.to_str()) == Some("json") {
found_json = true;
let bytes = tokio::fs::read(e.path()).await.unwrap();
let rec: PerfRecord = serde_json::from_slice::<PerfRecord>(&bytes).unwrap();
assert_eq!(rec.build, 5);
assert_eq!(rec.workflow, "test-wf");
assert_eq!(rec.phases.len(), 1);
assert_eq!(rec.phases[0].name, "plan");
}
}
assert!(found_json, "expected at least one .json run file");
let log = tokio::fs::read_to_string(tmp.path().join("runs.log"))
.await
.unwrap();
assert!(log.contains("build=5"));
assert!(log.contains("workflow=test-wf"));
}