Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions crates/tokscale-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,29 @@ mod tests {
));
}

#[test]
fn test_cursor_parse_path_reprices_zero_cost_composer_1_rows() {
let temp_dir = tempfile::TempDir::new().unwrap();
let cursor_cache_dir = temp_dir.path().join(".config/tokscale/cursor-cache");
std::fs::create_dir_all(&cursor_cache_dir).unwrap();

let csv = r#"Date,Kind,Model,Max Mode,Input (w/ Cache Write),Input (w/o Cache Write),Cache Read,Output Tokens,Total Tokens,Cost
"2026-03-04T12:00:00.000Z","Included","Composer 1","No","1200","1000","5000","2000","8000","0""#;
std::fs::write(cursor_cache_dir.join("usage.csv"), csv).unwrap();

let pricing = pricing::PricingService::new(HashMap::new(), HashMap::new());
let messages = parse_all_messages_with_pricing(
temp_dir.path().to_str().unwrap(),
&["cursor".to_string()],
Some(&pricing),
);

assert_eq!(messages.len(), 1);
assert_eq!(messages[0].client, "cursor");
assert_eq!(messages[0].model_id, "Composer 1");
assert!(messages[0].cost > 0.0);
}

#[test]
fn test_cursor_parse_path_reprices_zero_cost_composer_1_5_rows() {
let temp_dir = tempfile::TempDir::new().unwrap();
Expand All @@ -1824,6 +1847,29 @@ mod tests {
assert!(messages[0].cost > 0.0);
}

#[test]
fn test_cursor_parse_path_reprices_zero_cost_composer_2_rows() {
let temp_dir = tempfile::TempDir::new().unwrap();
let cursor_cache_dir = temp_dir.path().join(".config/tokscale/cursor-cache");
std::fs::create_dir_all(&cursor_cache_dir).unwrap();

let csv = r#"Date,Kind,Model,Max Mode,Input (w/ Cache Write),Input (w/o Cache Write),Cache Read,Output Tokens,Total Tokens,Cost
"2026-03-04T12:00:00.000Z","Included","composer-2","No","1200","1000","5000","2000","8000","0""#;
std::fs::write(cursor_cache_dir.join("usage.csv"), csv).unwrap();

let pricing = pricing::PricingService::new(HashMap::new(), HashMap::new());
let messages = parse_all_messages_with_pricing(
temp_dir.path().to_str().unwrap(),
&["cursor".to_string()],
Some(&pricing),
);

assert_eq!(messages.len(), 1);
assert_eq!(messages[0].client, "cursor");
assert_eq!(messages[0].model_id, "composer-2");
assert!(messages[0].cost > 0.0);
}

#[test]
#[serial_test::serial]
fn test_source_cache_refreshes_stale_date_on_cache_hit() {
Expand Down
131 changes: 131 additions & 0 deletions crates/tokscale-core/src/pricing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,21 @@ impl PricingService {
("gpt-5.3", 0.00000175, 0.000014, Some(1.75e-7)),
("gpt-5.3-codex", 0.00000175, 0.000014, Some(1.75e-7)),
("gpt-5.3-codex-spark", 0.00000175, 0.000014, Some(1.75e-7)),
// Composer 1: $1.25/$10.00 per 1M tokens, $0.125 cache read
// Source: Cursor docs (cursor.com/docs/models#model-pricing)
("composer 1", 0.00000125, 0.00001, Some(1.25e-7)),
("composer-1", 0.00000125, 0.00001, Some(1.25e-7)),
// Composer 1.5: $3.50/$17.50 per 1M tokens, $0.35 cache read
// Source: Cursor docs (cursor.com/docs/models#model-pricing), issue #276
("composer 1.5", 0.0000035, 0.0000175, Some(3.5e-7)),
("composer-1.5", 0.0000035, 0.0000175, Some(3.5e-7)),
// Composer 2: $0.50/$2.50 per 1M input/output, $0.20/M cache read; cache creation free
// Composer 2 Fast: $1.50/$7.50 per 1M, $0.35/M cache read; cache creation free
// Source: Cursor docs (cursor.com/docs/models#model-pricing)
("composer 2", 5e-7, 2.5e-6, Some(2e-7)),
("composer-2", 5e-7, 2.5e-6, Some(2e-7)),
("composer 2 fast", 1.5e-6, 7.5e-6, Some(3.5e-7)),
("composer-2-fast", 1.5e-6, 7.5e-6, Some(3.5e-7)),
];

let mut overrides = HashMap::with_capacity(entries.len());
Expand Down Expand Up @@ -313,6 +324,33 @@ mod tests {
assert!((cost - expected).abs() < 1e-10);
}

#[test]
fn test_cursor_returns_pricing_for_composer_1() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let result = service.lookup_with_source("Composer 1", None).unwrap();
assert_eq!(result.source, "Cursor");
assert_eq!(result.matched_key, "composer 1");
assert_eq!(result.pricing.input_cost_per_token, Some(0.00000125));
assert_eq!(result.pricing.output_cost_per_token, Some(0.00001));
assert_eq!(result.pricing.cache_read_input_token_cost, Some(1.25e-7));
}

#[test]
fn test_cursor_calculate_cost_for_composer_1() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let cost = service.calculate_cost("Composer 1", 1_000_000, 100_000, 50_000, 0, 0);
let expected = 1_000_000.0 * 0.00000125 + 100_000.0 * 0.00001 + 50_000.0 * 1.25e-7;
assert!((cost - expected).abs() < 1e-10);
}

#[test]
fn test_cursor_returns_pricing_for_hyphenated_composer_1() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let result = service.lookup_with_source("composer-1", None).unwrap();
assert_eq!(result.source, "Cursor");
assert_eq!(result.matched_key, "composer-1");
}

#[test]
fn test_cursor_returns_pricing_for_composer_1_5() {
let service = PricingService::new(HashMap::new(), HashMap::new());
Expand Down Expand Up @@ -340,6 +378,99 @@ mod tests {
assert_eq!(result.matched_key, "composer-1.5");
}

#[test]
fn test_cursor_returns_pricing_for_composer_2() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let result = service.lookup_with_source("composer-2", None).unwrap();
assert_eq!(result.source, "Cursor");
assert_eq!(result.matched_key, "composer-2");
assert_eq!(result.pricing.input_cost_per_token, Some(5e-7));
assert_eq!(result.pricing.output_cost_per_token, Some(2.5e-6));
assert_eq!(result.pricing.cache_read_input_token_cost, Some(2e-7));
assert_eq!(result.pricing.cache_creation_input_token_cost, None);
}

#[test]
fn test_cursor_returns_pricing_for_composer_2_spaced() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let result = service.lookup_with_source("Composer 2", None).unwrap();
assert_eq!(result.source, "Cursor");
assert_eq!(result.matched_key, "composer 2");
}

#[test]
fn test_cursor_returns_pricing_for_composer_2_fast() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let result = service.lookup_with_source("composer-2-fast", None).unwrap();
assert_eq!(result.source, "Cursor");
assert_eq!(result.matched_key, "composer-2-fast");
assert_eq!(result.pricing.input_cost_per_token, Some(1.5e-6));
assert_eq!(result.pricing.output_cost_per_token, Some(7.5e-6));
assert_eq!(result.pricing.cache_read_input_token_cost, Some(3.5e-7));
assert_eq!(result.pricing.cache_creation_input_token_cost, None);
}

#[test]
fn test_cursor_returns_pricing_for_composer_2_fast_spaced() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let result = service.lookup_with_source("Composer 2 Fast", None).unwrap();
assert_eq!(result.source, "Cursor");
assert_eq!(result.matched_key, "composer 2 fast");
}

#[test]
fn test_cursor_calculate_cost_for_composer_2() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let cost = service.calculate_cost("composer-2", 1_000_000, 100_000, 50_000, 0, 0);
let expected = 1_000_000.0 * 5e-7 + 100_000.0 * 2.5e-6 + 50_000.0 * 2e-7;
assert!((cost - expected).abs() < 1e-10);
}

#[test]
fn test_cursor_calculate_cost_composer_2_cache_write_free() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let with_write = service.calculate_cost("composer-2", 0, 0, 0, 500_000, 0);
let without_write = service.calculate_cost("composer-2", 0, 0, 0, 0, 0);
assert!((with_write - without_write).abs() < 1e-10);
}

#[test]
fn test_cursor_calculate_cost_for_composer_2_fast() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let cost = service.calculate_cost("composer-2-fast", 1_000_000, 100_000, 50_000, 0, 0);
let expected = 1_000_000.0 * 1.5e-6 + 100_000.0 * 7.5e-6 + 50_000.0 * 3.5e-7;
assert!((cost - expected).abs() < 1e-10);
}

#[test]
fn test_cursor_calculate_cost_composer_2_fast_cache_write_free() {
let service = PricingService::new(HashMap::new(), HashMap::new());
let with_write = service.calculate_cost("composer-2-fast", 0, 0, 0, 500_000, 0);
let without_write = service.calculate_cost("composer-2-fast", 0, 0, 0, 0, 0);
assert!(
(with_write - without_write).abs() < 1e-10,
"Cache creation should be free for Composer 2 Fast"
);
}

#[test]
fn test_cursor_composer_lookup_case_insensitive() {
let service = PricingService::new(HashMap::new(), HashMap::new());

let lower = service.lookup_with_source("composer 1", None);
let upper = service.lookup_with_source("COMPOSER 1", None);
let mixed = service.lookup_with_source("Composer 1", None);

assert!(lower.is_some(), "lowercase should resolve");
assert!(upper.is_some(), "UPPERCASE should resolve");
assert!(mixed.is_some(), "Mixed Case should resolve");

assert_eq!(
lower.unwrap().pricing.input_cost_per_token,
upper.unwrap().pricing.input_cost_per_token
);
}

#[test]
fn test_from_cached_datasets_returns_none_when_both_sources_missing() {
assert!(PricingService::from_cached_datasets(None, None).is_none());
Expand Down