Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
use super::*;
use macro_user_id::user_id::MacroUserIdStr;
fn user() -> MacroUserIdStr<'static> {
SYSTEM_USER_ID.clone()
}
fn completion(feature: AiFeature, total: Option<f32>) -> CompletionUsage {
CompletionUsage {
feature,
user: user(),
entity: None,
agent_api_key_id: None,
cost: Usage {
amount: UsageAmount::Tokens {
input: 1000,
output: 500,
cache_read: 0,
cache_write: 0,
},
model: "claude-opus-4-8".to_string(),
price: total.map(|t| Price {
pricing: ModelPricing::Tokens {
input: 5.0,
output: 25.0,
cache_read: None,
cache_write: None,
},
total: t,
}),
created_at: Utc::now(),
},
}
}
#[test]
fn price_compute_uses_per_million_rates() {
let usage = Usage {
amount: UsageAmount::Tokens {
input: 1_000_000,
output: 1_000_000,
cache_read: 0,
cache_write: 0,
},
model: "m".to_string(),
price: None,
created_at: Utc::now(),
};
let price = Price::compute(
ModelPricing::Tokens {
input: 5.0,
output: 25.0,
cache_read: None,
cache_write: None,
},
usage.amount,
)
.unwrap();
assert!((price.total - 30.0).abs() < 1e-3);
}
#[test]
fn price_compute_bills_each_cache_dimension_at_its_own_rate() {
let price = Price::compute(
ModelPricing::Tokens {
input: 5.0,
output: 25.0,
cache_read: Some(0.5),
cache_write: Some(6.25),
},
UsageAmount::Tokens {
input: 2_000,
output: 1_000,
cache_read: 140_000,
cache_write: 6_000,
},
)
.unwrap();
// 2k * $5 + 1k * $25 + 140k * $0.50 + 6k * $6.25, per million.
assert!((price.total - 0.1425).abs() < 1e-6);
assert_eq!(
price.pricing,
ModelPricing::Tokens {
input: 5.0,
output: 25.0,
cache_read: Some(0.5),
cache_write: Some(6.25),
}
);
}
#[test]
fn price_compute_needs_no_cache_rate_when_no_cache_tokens_were_used() {
let price = Price::compute(
ModelPricing::Tokens {
input: 30.0,
output: 180.0,
cache_read: None,
cache_write: None,
},
UsageAmount::Tokens {
input: 1_000_000,
output: 0,
cache_read: 0,
cache_write: 0,
},
)
.unwrap();
assert!((price.total - 30.0).abs() < 1e-4);
}
#[test]
fn price_compute_leaves_cache_reads_unpriced_without_a_cache_read_rate() {
let price = Price::compute(
ModelPricing::Tokens {
input: 1.32,
output: 3.96,
cache_read: None,
cache_write: Some(1.65),
},
UsageAmount::Tokens {
input: 1_000,
output: 100,
cache_read: 5_000,
cache_write: 0,
},
);
assert_eq!(price, None);
}
#[test]
fn price_compute_leaves_cache_writes_unpriced_without_a_cache_write_rate() {
let price = Price::compute(
ModelPricing::Tokens {
input: 5.0,
output: 30.0,
cache_read: Some(0.5),
cache_write: None,
},
UsageAmount::Tokens {
input: 1_000,
output: 100,
cache_read: 0,
cache_write: 5_000,
},
);
assert_eq!(price, None);
}
#[test]
fn negative_or_non_finite_cache_rates_are_invalid() {
for (cache_read, cache_write) in [
(Some(-0.1), None),
(None, Some(f32::NAN)),
(Some(f32::INFINITY), Some(1.0)),
] {
assert!(matches!(
ModelPricing::Tokens {
input: 1.0,
output: 2.0,
cache_read,
cache_write,
}
.validate(),
Err(UsageError::InvalidPricing)
));
}
}
#[test]
fn summarize_groups_by_feature_and_totals() {
let rows = vec![
completion(AiFeature::Chat, Some(1.0)),
completion(AiFeature::Chat, Some(2.5)),
completion(AiFeature::Memory, Some(0.5)),
completion(AiFeature::Memory, None), // unpriced rows contribute 0
];
let summary = summarize(rows);
assert_eq!(summary.entries.len(), 2);
assert!((summary.total - 4.0).abs() < 1e-4);
let chat = summary
.entries
.iter()
.find(|f| f.feature == AiFeature::Chat)
.unwrap();
assert_eq!(chat.entries.len(), 2);
assert!((chat.total - 3.5).abs() < 1e-4);
let memory = summary
.entries
.iter()
.find(|f| f.feature == AiFeature::Memory)
.unwrap();
assert_eq!(memory.entries.len(), 2);
assert!((memory.total - 0.5).abs() < 1e-4);
}
#[test]
fn ai_feature_roundtrips_through_snake_case() {
assert_eq!(
AiFeature::DynamicCompletionsApi.to_string(),
"dynamic_completions_api"
);
assert_eq!(
"channel_bot".parse::<AiFeature>().unwrap(),
AiFeature::ChannelBot
);
}
#[test]
fn system_user_is_valid() {
assert_eq!(SYSTEM_USER_ID.as_ref(), "macro|[email protected]");
}
#[test]
fn normalize_model_id_strips_provider_prefix() {
assert_eq!(
normalize_model_id("anthropic/claude-opus-5"),
"claude-opus-5"
);
assert_eq!(normalize_model_id("openai/gpt-5.6"), "gpt-5.6");
assert_eq!(normalize_model_id("fireworks/kimi-k3"), "kimi-k3");
assert_eq!(
normalize_model_id("google/gemini-3.8-flash"),
"gemini-3.8-flash"
);
assert_eq!(normalize_model_id("claude-haiku-4-5"), "claude-haiku-4-5");
// A trailing slash is not a provider prefix; leave the id alone.
assert_eq!(normalize_model_id("weird/"), "weird/");
}
#[derive(Clone, Default)]
struct FakeRepo {
pricing: std::sync::Arc<std::sync::Mutex<Option<ModelPricing>>>,
rows: std::sync::Arc<std::sync::Mutex<Vec<CompletionUsage>>>,
counting: std::sync::Arc<std::sync::Mutex<Vec<bool>>>,
recorded: std::sync::Arc<tokio::sync::Notify>,
}
impl UsageRepo for FakeRepo {
async fn insert_usage(&self, usage: &CompletionUsage, count_usage: bool) -> Result<()> {
self.rows.lock().unwrap().push(usage.clone());
self.counting.lock().unwrap().push(count_usage);
self.recorded.notify_one();
Ok(())
}
async fn get_pricing(&self, _model: &str) -> Result<Option<ModelPricing>> {
Ok(*self.pricing.lock().unwrap())
}
async fn set_pricing(&self, _model: &str, pricing: ModelPricing) -> Result<()> {
*self.pricing.lock().unwrap() = Some(pricing);
Ok(())
}
async fn query_usage(&self, _params: &UsageApiParams) -> Result<Vec<CompletionUsage>> {
Ok(self.rows.lock().unwrap().clone())
}
}
#[tokio::test]
async fn records_duration_with_the_model_rate_and_user_attribution() {
let repo = FakeRepo::default();
*repo.pricing.lock().unwrap() = Some(ModelPricing::Audio { per_minute: 0.006 });
let event = UsageContext::new(AiFeature::Dictation, user())
.into_audio_event("whisper-1".into(), std::time::Duration::from_millis(90_500));
UsageServiceImpl::new(repo.clone())
.record_now(event)
.await
.unwrap();
let rows = repo.rows.lock().unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].user, user());
assert_eq!(rows[0].feature, AiFeature::Dictation);
assert_eq!(rows[0].cost.model, "whisper-1");
assert_eq!(
rows[0].cost.amount,
UsageAmount::Audio {
duration: std::time::Duration::from_millis(90_500)
}
);
let price = rows[0].cost.price.unwrap();
assert_eq!(price.pricing, ModelPricing::Audio { per_minute: 0.006 });
assert!((price.total - 0.00905).abs() < 1e-7);
}
#[tokio::test]
async fn missing_audio_rate_keeps_usage_unpriced() {
for rates in [
None,
Some(ModelPricing::Tokens {
input: 0.0,
output: 0.0,
cache_read: None,
cache_write: None,
}),
] {
let repo = FakeRepo::default();
*repo.pricing.lock().unwrap() = rates;
let event = UsageContext::new(AiFeature::Dictation, user()).into_audio_event(
"unpriced-audio-model".into(),
std::time::Duration::from_secs(60),
);
UsageServiceImpl::new(repo.clone())
.record_now(event)
.await
.unwrap();
let rows = repo.rows.lock().unwrap();
assert_eq!(
rows[0].cost.amount,
UsageAmount::Audio {
duration: std::time::Duration::from_secs(60)
}
);
assert!(rows[0].cost.price.is_none());
}
}
#[tokio::test]
async fn both_recording_paths_persist_the_same_counting_policy() {
let real_user = MacroUserIdStr::try_from("macro|[email protected]".to_owned()).unwrap();
for enforcement in [AiUsageEnforcement::Disabled, AiUsageEnforcement::Enabled] {
for actor in [real_user.clone(), SYSTEM_USER_ID.clone()] {
for feature in [
AiFeature::Chat,
AiFeature::AiEditing,
AiFeature::Memory,
AiFeature::AiProjection,
AiFeature::CallSummary,
AiFeature::Dictation,
] {
for pricing in [
None,
Some(ModelPricing::Tokens {
input: 5.0,
output: 25.0,
cache_read: None,
cache_write: None,
}),
] {
let repo = FakeRepo::default();
*repo.pricing.lock().unwrap() = pricing;
let service = UsageServiceImpl::new(repo.clone()).with_enforcement(enforcement);
let event = UsageContext::new(feature, actor.clone()).into_event(
"anthropic/test-model".into(),
UsageAmount::Tokens {
input: 100,
output: 200,
cache_read: 0,
cache_write: 0,
},
);
let expected = enforcement == AiUsageEnforcement::Enabled
&& actor == real_user
&& matches!(feature, AiFeature::Chat | AiFeature::AiEditing);
service.record_now(event.clone()).await.unwrap();
repo.recorded.notified().await;
service.record(event);
tokio::time::timeout(
std::time::Duration::from_secs(2),
repo.recorded.notified(),
)
.await
.expect("background recording must finish");
assert_eq!(*repo.counting.lock().unwrap(), vec![expected, expected]);
let rows = repo.rows.lock().unwrap();
assert_eq!(rows.len(), 2);
assert!(rows.iter().all(|row| row.user == actor
&& row.feature == feature
&& row.cost.model == "test-model"
&& row.cost.price.is_some() == pricing.is_some()));
}
}
}
}
}
#[tokio::test]
async fn usage_on_a_team_provider_key_is_recorded_against_it_and_never_counted() {
let repo = FakeRepo::default();
let service = UsageServiceImpl::new(repo.clone()).with_enforcement(AiUsageEnforcement::Enabled);
let actor = MacroUserIdStr::try_from("macro|[email protected]".to_owned()).unwrap();
let key = macro_uuid::generate_uuid_v7();
let event = UsageContext::new(AiFeature::AgentSession, actor)
.with_agent_api_key(key)
.into_event(
"anthropic/claude-opus-5".into(),
UsageAmount::Tokens {
input: 100,
output: 200,
cache_read: 0,
cache_write: 0,
},
);
assert!(!service.counts_usage(&event));
service.record_now(event).await.unwrap();
assert_eq!(*repo.counting.lock().unwrap(), vec![false]);
assert_eq!(repo.rows.lock().unwrap()[0].agent_api_key_id, Some(key));
}
#[tokio::test]
async fn default_disabled_writes_do_not_change_previously_counted_usage() {
let repo = FakeRepo::default();
let enabled = UsageServiceImpl::new(repo.clone()).with_enforcement(AiUsageEnforcement::Enabled);
let disabled = UsageServiceImpl::new(repo.clone());
let actor = MacroUserIdStr::try_from("macro|[email protected]".to_owned()).unwrap();
let event = UsageContext::new(AiFeature::Chat, actor).into_event(
"model".into(),
UsageAmount::Tokens {
input: 100,
output: 200,
cache_read: 0,
cache_write: 0,
},
);
enabled.record_now(event.clone()).await.unwrap();
disabled.record_now(event).await.unwrap();
assert_eq!(*repo.counting.lock().unwrap(), vec![true, false]);
let summary = disabled
.get_usage(user(), UsageApiParams::default())
.await
.unwrap();
assert_eq!(
summary.entries[0].entries.len(),
2,
"analytics stays unfiltered"
);
}
#[tokio::test]
async fn admin_policy_protects_usage_and_price_changes() {
let repo = FakeRepo::default();
let service = UsageServiceImpl::new(repo.clone());
let other = MacroUserIdStr::try_from("macro|[email protected]".to_owned()).unwrap();
assert!(matches!(
service
.get_usage(other.clone(), UsageApiParams::default())
.await,
Err(UsageError::Forbidden)
));
assert!(matches!(
service
.set_pricing(
other,
"whisper-1".into(),
ModelPricing::Tokens {
input: 0.0,
output: 0.0,
cache_read: None,
cache_write: None,
}
)
.await,
Err(UsageError::Forbidden)
));
assert!(repo.pricing.lock().unwrap().is_none());
service
.set_pricing(
user(),
"whisper-1".into(),
ModelPricing::Tokens {
input: 0.0,
output: 0.0,
cache_read: None,
cache_write: None,
},
)
.await
.unwrap();
assert!(
service
.get_usage(user(), UsageApiParams::default())
.await
.is_ok()
);
}
#[tokio::test]
async fn rejects_invalid_prices_before_writing() {
let repo = FakeRepo::default();
let service = UsageServiceImpl::new(repo.clone());
for pricing in [-0.001, f32::NAN, f32::INFINITY]
.into_iter()
.flat_map(|price| {
[
ModelPricing::Audio { per_minute: price },
ModelPricing::ProviderCredits { per_credit: price },
]
})
{
assert!(matches!(
service
.set_pricing(user(), "whisper-1".into(), pricing)
.await,
Err(UsageError::InvalidPricing)
));
}
assert!(repo.pricing.lock().unwrap().is_none());
}
#[tokio::test]
async fn records_cache_tokens_priced_at_the_model_cache_rates() {
let repo = FakeRepo::default();
*repo.pricing.lock().unwrap() = Some(ModelPricing::Tokens {
input: 5.0,
output: 25.0,
cache_read: Some(0.5),
cache_write: Some(6.25),
});
let event = UsageContext::new(AiFeature::AgentSession, user()).into_event(
"anthropic/claude-opus-5".into(),
UsageAmount::Tokens {
input: 1_000_000,
output: 0,
cache_read: 1_000_000,
cache_write: 1_000_000,
},
);
UsageServiceImpl::new(repo.clone())
.record_now(event)
.await
.unwrap();
let rows = repo.rows.lock().unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].cost.model, "claude-opus-5");
assert_eq!(
rows[0].cost.amount,
UsageAmount::Tokens {
input: 1_000_000,
output: 0,
cache_read: 1_000_000,
cache_write: 1_000_000,
}
);
assert!((rows[0].cost.price.unwrap().total - 11.75).abs() < 1e-4);
}
#[tokio::test]
async fn cache_tokens_without_a_cache_rate_record_an_unpriced_row() {
let repo = FakeRepo::default();
*repo.pricing.lock().unwrap() = Some(ModelPricing::Tokens {
input: 0.35,
output: 1.5,
cache_read: None,
cache_write: None,
});
let event = UsageContext::new(AiFeature::AgentSession, user()).into_event(
"fireworks/muse-glimmer-30b".into(),
UsageAmount::Tokens {
input: 1_000,
output: 100,
cache_read: 9_000,
cache_write: 0,
},
);
UsageServiceImpl::new(repo.clone())
.record_now(event)
.await
.unwrap();
let rows = repo.rows.lock().unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(
rows[0].cost.amount,
UsageAmount::Tokens {
input: 1_000,
output: 100,
cache_read: 9_000,
cache_write: 0,
}
);
assert!(rows[0].cost.price.is_none());
}
#[tokio::test]
async fn records_provider_credits_at_the_model_credit_rate() {
let repo = FakeRepo::default();
*repo.pricing.lock().unwrap() = Some(ModelPricing::ProviderCredits { per_credit: 0.008 });
let event = UsageContext::new(AiFeature::WebSearch, user())
.into_provider_credits_event("tavily".into(), 2);
UsageServiceImpl::new(repo.clone())
.record_now(event)
.await
.unwrap();
let rows = repo.rows.lock().unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].user, user());
assert_eq!(rows[0].feature, AiFeature::WebSearch);
assert_eq!(rows[0].cost.model, "tavily");
assert_eq!(
rows[0].cost.amount,
UsageAmount::ProviderCredits { credits: 2 }
);
let price = rows[0].cost.price.unwrap();
assert_eq!(
price.pricing,
ModelPricing::ProviderCredits { per_credit: 0.008 }
);
assert!((price.total - 0.016).abs() < 1e-7);
}
#[tokio::test]
async fn credits_and_other_units_never_price_each_other() {
let token_rates = ModelPricing::Tokens {
input: 1.0,
output: 1.0,
cache_read: None,
cache_write: None,
};
let credit_rate = ModelPricing::ProviderCredits { per_credit: 0.008 };
let credits = UsageAmount::ProviderCredits { credits: 2 };
for (pricing, amount) in [
(token_rates, credits),
(ModelPricing::Audio { per_minute: 0.006 }, credits),
(
credit_rate,
UsageAmount::Tokens {
input: 1_000,
output: 1_000,
cache_read: 0,
cache_write: 0,
},
),
(
credit_rate,
UsageAmount::Audio {
duration: std::time::Duration::from_secs(60),
},
),
] {
assert_eq!(
Price::compute(pricing, amount),
None,
"{pricing:?} {amount:?}"
);
}
}