pub use crate::credits::{Credits, Usd, CREDITS_PER_USD};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Costs {
#[serde(default)]
pub ai_cost: Usd,
#[serde(default)]
pub compute_cost: Usd,
#[serde(default)]
pub file_cost: Usd,
#[serde(default)]
pub bytes_transferred_cost: Usd,
#[serde(default)]
pub total_cost: Usd,
#[serde(default)]
pub transform_cost: Usd,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub vendor: Option<VendorCosts>,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct VendorCosts {
#[serde(default)]
pub provider: Option<String>,
#[serde(default)]
pub route: String,
#[serde(default)]
pub vendor_cost: Usd,
#[serde(default)]
pub billed_cost: Usd,
#[serde(default)]
pub byok: bool,
#[serde(default)]
pub attempts: u32,
}
impl VendorCosts {
pub fn billed(&self) -> Credits {
Credits::from(self.billed_cost)
}
pub fn charged_by_vendor(&self) -> Credits {
Credits::from(self.vendor_cost)
}
}
impl Costs {
pub fn total(&self) -> Credits {
Credits::from(self.total_cost)
}
pub fn ai(&self) -> Credits {
Credits::from(self.ai_cost)
}
pub fn compute(&self) -> Credits {
Credits::from(self.compute_cost)
}
pub fn file(&self) -> Credits {
Credits::from(self.file_cost)
}
pub fn bytes_transferred(&self) -> Credits {
Credits::from(self.bytes_transferred_cost)
}
pub fn transform(&self) -> Credits {
Credits::from(self.transform_cost)
}
pub fn sum_of_parts(&self) -> Credits {
Credits::from(
self.ai_cost
+ self.compute_cost
+ self.file_cost
+ self.bytes_transferred_cost
+ self.transform_cost,
)
}
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::unreachable,
clippy::indexing_slicing,
clippy::string_slice
)]
use super::*;
#[test]
fn ten_thousand_credits_is_one_dollar() {
assert_eq!(Credits(10_000.0).to_usd(), 1.0);
assert_eq!(Credits::from_usd(0.25).get(), 2_500.0);
}
#[test]
fn a_cost_block_off_the_wire_is_dollars_and_converts_to_credits() {
let costs: Costs = serde_json::from_str(
r#"{"ai_cost":0.0,
"bytes_transferred_cost":3.015e-08,
"compute_cost":1.666666666666667e-09,
"file_cost":8.385e-07,
"total_cost":1.0870316666666668e-05,
"transform_cost":1e-05}"#,
)
.expect("costs");
assert_eq!(costs.total_cost, Usd::new(1.0870316666666668e-05));
assert!((costs.total().get() - 0.10870316666666668).abs() < 1e-12);
assert!((costs.transform().get() - 0.1).abs() < 1e-12);
assert!((costs.sum_of_parts().get() - costs.total().get()).abs() < 1e-12);
assert_eq!(costs.ai(), Credits::ZERO);
}
#[test]
fn costs_read_a_partial_block() {
let costs: Costs =
serde_json::from_str(r#"{"compute_cost":2.0,"total_cost":2.0}"#).expect("costs");
assert_eq!(costs.total(), Credits(20_000.0));
assert_eq!(costs.ai_cost, Usd::ZERO);
assert_eq!(costs.sum_of_parts(), Credits(20_000.0));
}
#[test]
fn a_missing_cost_block_reads_as_nothing_spent() {
let costs: Costs = serde_json::from_str("{}").expect("costs");
assert_eq!(costs.total(), Credits::ZERO);
assert_eq!(costs.sum_of_parts(), Credits::ZERO);
}
#[test]
fn a_null_cost_line_reads_as_nothing_rather_than_failing_the_page() {
let costs: Costs = serde_json::from_str(
r#"{"ai_cost":null,"compute_cost":2.0,"total_cost":null,"transform_cost":1.0}"#,
)
.expect("costs with null lines");
assert_eq!(costs.ai_cost, Usd::ZERO);
assert_eq!(costs.total_cost, Usd::ZERO);
assert_eq!(costs.compute(), Credits(20_000.0));
}
#[test]
fn a_cost_written_as_a_string_still_counts_against_the_budget() {
let costs: Costs =
serde_json::from_str(r#"{"total_cost":"1.5e-05","compute_cost":"0.25"}"#)
.expect("costs as strings");
assert!((costs.total().get() - 0.15).abs() < 1e-9);
assert_eq!(costs.compute(), Credits(2_500.0));
let err = serde_json::from_str::<Costs>(r#"{"total_cost":"free"}"#)
.expect_err("a word is not a charge");
assert!(err.to_string().contains("free"), "{err}");
}
#[test]
fn a_negative_cost_line_cannot_pull_the_spend_down() {
let costs: Costs =
serde_json::from_str(r#"{"total_cost":-0.5,"compute_cost":1.0}"#).expect("costs");
assert_eq!(costs.total(), Credits::ZERO);
assert_eq!(costs.compute(), Credits(10_000.0));
}
#[test]
fn an_absurd_cost_still_trips_a_budget_rather_than_wrapping() {
let costs: Costs =
serde_json::from_str(r#"{"total_cost":1.7976931348623157e308}"#).expect("costs");
assert!(costs.total().get() > 1e300);
assert!(costs.total().get().is_infinite() || costs.total().get() > 0.0);
assert!(serde_json::from_str::<Costs>(r#"{"total_cost":1e400}"#).is_err());
}
#[test]
fn a_vendor_served_page_names_the_vendor_and_the_total_includes_its_charge() {
let costs: Costs = serde_json::from_str(
r#"{"file_cost":0.0,"transform_cost":0.00001,"compute_cost":0.0002,
"ai_cost":0.0,"bytes_transferred_cost":0.0,"total_cost":0.00321,
"vendor":{"provider":"vendor","route":"vendor.unlocker",
"vendor_cost":0.002,"billed_cost":0.003,"byok":false,"attempts":2},
"total_cost_formatted":"$0.00321"}"#,
)
.expect("costs");
let vendor = costs.vendor.as_ref().expect("a vendor");
assert_eq!(vendor.provider.as_deref(), Some("vendor"));
assert_eq!(vendor.route, "vendor.unlocker");
assert_eq!(vendor.charged_by_vendor(), Credits(20.0));
assert_eq!(vendor.billed(), Credits(30.0));
assert!(!vendor.byok);
assert_eq!(vendor.attempts, 2);
assert!((costs.total().get() - 32.1).abs() < 1e-9);
assert!((costs.sum_of_parts().get() - 2.1).abs() < 1e-9);
assert!(costs.total() > costs.sum_of_parts());
}
#[test]
fn a_page_the_service_fetched_itself_names_no_vendor() {
let costs: Costs =
serde_json::from_str(r#"{"compute_cost":2.0,"total_cost":2.0}"#).expect("costs");
assert!(costs.vendor.is_none());
assert!(!serde_json::to_string(&costs)
.expect("json")
.contains("vendor"));
}
#[test]
fn credits_add_up() {
let total: Credits = [Credits(1.5), Credits(2.5)].into_iter().sum();
assert_eq!(total, Credits(4.0));
}
}