use pretty_assertions::assert_eq;
use rho_sdk::model::ModelUsage;
use super::{usd_micros, TurnCost};
use crate::usage::report::Valuation;
fn turn() -> TurnCost {
TurnCost::unpriced()
}
fn billed(micros: u64) -> ModelUsage {
ModelUsage {
output_tokens: Some(10),
cost_usd_micros: Some(micros),
..ModelUsage::default()
}
}
#[test]
fn a_streaming_response_adds_to_the_ledger_only_what_it_has_not_recorded() {
let mut cost = turn();
cost.read_ledger(Some(5_000_000));
let first = [
cost.streaming(&billed(1_000_000)),
cost.streaming(&billed(2_000_000)),
];
cost.read_ledger(Some(7_000_000));
let second = cost.streaming(&billed(2_500_000));
assert_eq!(
(first, second),
([Some(6_000_000), Some(7_000_000)], Some(7_500_000))
);
}
#[test]
fn a_discarded_response_does_not_lower_the_streaming_cost() {
let mut cost = turn();
cost.read_ledger(Some(5_000_000));
let streamed = [
cost.streaming(&billed(2_000_000)),
cost.streaming(&billed(500_000)),
cost.streaming(&billed(2_500_000)),
];
cost.read_ledger(Some(9_500_000));
assert_eq!(
(streamed, cost.streaming(&billed(2_500_000))),
(
[Some(7_000_000), Some(7_000_000), Some(7_500_000)],
Some(9_500_000)
)
);
}
#[test]
fn unpriced_streaming_usage_leaves_the_cost_as_the_ledger_had_it() {
let unpriced = ModelUsage {
output_tokens: Some(10),
..ModelUsage::default()
};
let mut cost = turn();
let none = cost.streaming(&unpriced);
cost.read_ledger(Some(5_000_000));
assert_eq!((none, cost.streaming(&unpriced)), (None, Some(5_000_000)));
}
#[test]
fn usage_costs_dollars_once_a_request_had_a_price() {
let cases = [
(Valuation::Usd(1_250_000), Some(1_250_000)),
(Valuation::Local, None),
(Valuation::Unpriced, None),
];
for (valuation, cost) in cases {
assert_eq!(usd_micros(valuation), cost, "{valuation:?}");
}
}