File size: 7,119 Bytes
afa0cbf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | //! HTTP contract coverage for account analytics report reads.
use super::*;
use codex_http_client::HttpClientFactory;
use codex_http_client::OutboundProxyPolicy;
use pretty_assertions::assert_eq;
use serde_json::json;
use wiremock::Mock;
use wiremock::MockServer;
use wiremock::ResponseTemplate;
use wiremock::matchers::method;
use wiremock::matchers::path;
use wiremock::matchers::query_param;
#[tokio::test]
async fn account_analytics_uses_personal_daily_scope_and_plugin_contract() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/api/codex/analytics/daily-plugin-usage-metrics"))
.and(query_param("start_date", "2026-01-03"))
.and(query_param("end_date", "2026-01-09"))
.and(query_param("group_by", "day"))
.and(query_param("workspace_user", "true"))
.and(query_param("top_plugin_limit", "10"))
.respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(json!({
"data": [{"date": "2026-01-03", "plugin_usage_overviews": [{
"plugin_id": null, "plugin_name": "example", "marketplace": null,
"display_name": "Example plugin", "invocation_counts": 12
}]}]
})))
.expect(/*r*/ 1)
.mount(&server)
.await;
let client = Client::new(
server.uri(),
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
);
let response = client
.get_account_analytics(
AnalyticsReport::Plugins { limit: 10 },
"2026-01-03",
"2026-01-09",
)
.await
.unwrap();
assert_eq!(
response,
AnalyticsResponse::Plugins(models::PluginUsageMetricsResponse {
data: vec![models::PluginUsageBucket {
date: "2026-01-03".into(),
plugin_usage_overviews: vec![models::PluginUsageOverview {
plugin_id: None,
plugin_name: "example".into(),
display_name: "Example plugin".into(),
marketplace: None,
invocation_counts: 12,
}],
}],
data_freshness_ts: None,
group_by: None,
})
);
}
#[tokio::test]
async fn account_analytics_preserves_signed_credit_events() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/wham/usage/credit-usage-events"))
.respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(json!({
"data": [{"date": "2026-01-03", "product_surface": "cli", "credit_amount": -0.004}]
})))
.expect(/*r*/ 1)
.mount(&server)
.await;
let client = Client::new(
server.uri(),
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
)
.with_path_style(PathStyle::ChatGptApi);
let response = client
.get_account_analytics(AnalyticsReport::Credits, "2026-01-03", "2026-01-09")
.await
.unwrap();
assert_eq!(
response,
AnalyticsResponse::Credits(models::CreditUsageEventsResponse {
data: vec![models::CreditUsageEventBySurface {
date: "2026-01-03".into(),
product_surface: "cli".into(),
credit_amount: -0.004,
usage_id: None,
}],
})
);
}
#[tokio::test]
async fn account_analytics_rejects_another_reports_shape_without_logging_the_body() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/api/codex/analytics/daily-workspace-usage-counts"))
.respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(json!({
"data": [{"date": "2026-01-03", "product_surface": "sensitive-value", "credit_amount": 10}]
})))
.expect(/*r*/ 1)
.mount(&server)
.await;
let client = Client::new(
server.uri(),
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
);
let error = client
.get_account_analytics(AnalyticsReport::Messages, "2026-01-03", "2026-01-09")
.await
.unwrap_err();
assert_eq!(error.to_string(), "Invalid analytics response.");
}
#[tokio::test]
async fn account_analytics_decodes_each_endpoint_and_accepts_new_string_values() {
for (report, route, body, expected) in [
(
AnalyticsReport::Usage,
"usage/daily-token-usage-breakdown",
json!({"data": [{"date": "2026-01-03", "product_surface_usage_values": {"future_surface": 1.0}}], "units": "future_unit"}),
AnalyticsResponse::Usage(models::DailyProductSurfaceUsageResponse {
data: vec![models::DailyProductSurfaceUsage {
date: "2026-01-03".into(),
product_surface_usage_values: [("future_surface".into(), 1.0)].into(),
..Default::default()
}],
units: Some("future_unit".into()),
..Default::default()
}),
),
(
AnalyticsReport::EnterpriseCredits { breakdown: "model" },
"usage/daily-workspace-user-credit-usage",
json!({"data": [], "series": [], "breakdown": "model"}),
AnalyticsResponse::EnterpriseCredits(models::CurrentUserCreditUsageResponse {
breakdown: "model".into(),
..Default::default()
}),
),
(
AnalyticsReport::Messages,
"analytics/daily-workspace-usage-counts",
json!({"data": []}),
AnalyticsResponse::Messages(models::DailyWorkspaceUsageCountResponse::default()),
),
(
AnalyticsReport::Skills { limit: 10 },
"analytics/daily-skill-usage-metrics",
json!({"data": []}),
AnalyticsResponse::Skills(models::DailySkillUsageMetricsResponse::default()),
),
(
AnalyticsReport::WorkspaceCredits,
"usage/daily-workspace-user-token-usage-breakdown",
json!({"data": []}),
AnalyticsResponse::Usage(models::DailyProductSurfaceUsageResponse::default()),
),
(
AnalyticsReport::EnterpriseTokens,
"usage/daily-workspace-user-token-usage-breakdown",
json!({"data": []}),
AnalyticsResponse::Usage(models::DailyProductSurfaceUsageResponse::default()),
),
] {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path(format!("/api/codex/{route}")))
.respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(body))
.expect(/*r*/ 1)
.mount(&server)
.await;
let client = Client::new(
server.uri(),
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
);
assert_eq!(
client
.get_account_analytics(report, "2026-01-03", "2026-01-09")
.await
.unwrap(),
expected
);
}
}
|