use serde_json::json;
use trusty_code::llm::{ChatMessage, ChatRequest, OpenAiCompatClient};
use trusty_common::inference::credentials::{KeyStore, MemoryKeyStore};
use trusty_common::inference::test_support::MockInferenceServer;
fn canned_response() -> serde_json::Value {
json!({
"id": "gen-mock",
"model": "served/model",
"choices": [{
"message": {"role": "assistant", "content": "pong-mock", "tool_calls": []},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 5, "completion_tokens": 2, "total_tokens": 7}
})
}
fn request_for(model: &str) -> ChatRequest {
ChatRequest {
model: model.to_string(),
messages: vec![
ChatMessage::system("You are a concise assistant."),
ChatMessage::user("Reply with exactly the word: pong"),
],
temperature: Some(0.0),
max_tokens: Some(16),
tools: None,
tool_choice: None,
usage: None,
}
}
#[tokio::test]
async fn openrouter_routes_through_shared_adapter_and_injects_usage() {
let server = MockInferenceServer::spawn(200, canned_response())
.await
.expect("spawn mock");
let store = MemoryKeyStore::new();
store.set("openrouter", "sk-or-mock").expect("seed key");
let client = OpenAiCompatClient::with_config(
Box::new(store),
server.url().to_string(),
"http://127.0.0.1:1/unused".to_string(),
"http://127.0.0.1:1/unused".to_string(),
"http://127.0.0.1:1/unused".to_string(),
);
let resp = client
.chat(&request_for("openai/gpt-4o-mini"))
.await
.expect("chat through shared adapter");
assert_eq!(resp.first_text().as_deref(), Some("pong-mock"));
assert_eq!(resp.token_usage().prompt_tokens, 5);
let captured = server.last_request().expect("one request captured");
assert_eq!(captured.method, "POST");
assert_eq!(captured.path, "/chat/completions");
assert!(
captured
.header("authorization")
.is_some_and(|v| v.starts_with("Bearer ")),
"shared adapter must send a Bearer auth header"
);
let body = captured.body.expect("json body");
assert_eq!(body["model"], "openai/gpt-4o-mini", "slug sent verbatim");
assert_eq!(body["messages"][0]["role"], "system");
assert_eq!(
body["usage"],
json!({"include": true}),
"shared OpenRouter adapter must inject the detailed-usage directive"
);
}
#[tokio::test]
async fn fireworks_routes_through_shared_adapter_strips_prefix_and_omits_usage() {
let server = MockInferenceServer::spawn(200, canned_response())
.await
.expect("spawn mock");
let store = MemoryKeyStore::new();
store.set("fireworks", "fw-mock").expect("seed key");
let client = OpenAiCompatClient::with_config(
Box::new(store),
"http://127.0.0.1:1/unused".to_string(),
server.url().to_string(),
"http://127.0.0.1:1/unused".to_string(),
"http://127.0.0.1:1/unused".to_string(),
);
let resp = client
.chat(&request_for(
"fireworks/accounts/fireworks/models/llama-v3p1-8b-instruct",
))
.await
.expect("chat through shared fireworks adapter");
assert_eq!(resp.first_text().as_deref(), Some("pong-mock"));
let captured = server.last_request().expect("one request captured");
assert_eq!(captured.path, "/chat/completions");
let body = captured.body.expect("json body");
assert_eq!(
body["model"], "accounts/fireworks/models/llama-v3p1-8b-instruct",
"the fireworks/ routing prefix must be stripped to the provider-native id"
);
assert!(
body.get("usage").is_none() || body["usage"].is_null(),
"Fireworks must NOT receive the OpenRouter-only usage directive"
);
}
#[tokio::test]
async fn together_routes_through_shared_adapter_strips_prefix_and_omits_usage() {
let server = MockInferenceServer::spawn(200, canned_response())
.await
.expect("spawn mock");
let store = MemoryKeyStore::new();
store.set("together", "tgp_v1_mock").expect("seed key");
let client = OpenAiCompatClient::with_config(
Box::new(store),
"http://127.0.0.1:1/unused".to_string(),
"http://127.0.0.1:1/unused".to_string(),
server.url().to_string(),
"http://127.0.0.1:1/unused".to_string(),
);
let resp = client
.chat(&request_for(
"together/meta-llama/Llama-3.3-70B-Instruct-Turbo",
))
.await
.expect("chat through shared together adapter");
assert_eq!(resp.first_text().as_deref(), Some("pong-mock"));
assert_eq!(resp.token_usage().prompt_tokens, 5);
let captured = server.last_request().expect("one request captured");
assert_eq!(captured.method, "POST");
assert_eq!(captured.path, "/chat/completions");
assert!(
captured
.header("authorization")
.is_some_and(|v| v.starts_with("Bearer ")),
"shared adapter must send a Bearer auth header"
);
let body = captured.body.expect("json body");
assert_eq!(
body["model"], "meta-llama/Llama-3.3-70B-Instruct-Turbo",
"the together/ routing prefix must be stripped to the bare Together slug"
);
assert!(
body.get("usage").is_none() || body["usage"].is_null(),
"Together must NOT receive the OpenRouter-only usage directive"
);
}
#[tokio::test]
async fn atlascloud_routes_through_shared_adapter_strips_prefix_and_omits_usage() {
let server = MockInferenceServer::spawn(200, canned_response())
.await
.expect("spawn mock");
let store = MemoryKeyStore::new();
store.set("atlascloud", "ac_mock").expect("seed key");
let client = OpenAiCompatClient::with_config(
Box::new(store),
"http://127.0.0.1:1/unused".to_string(),
"http://127.0.0.1:1/unused".to_string(),
"http://127.0.0.1:1/unused".to_string(),
server.url().to_string(),
);
let resp = client
.chat(&request_for("atlascloud/openai/gpt-5.6-sol"))
.await
.expect("chat through shared atlascloud adapter");
assert_eq!(resp.first_text().as_deref(), Some("pong-mock"));
assert_eq!(resp.token_usage().prompt_tokens, 5);
let captured = server.last_request().expect("one request captured");
assert_eq!(captured.method, "POST");
assert_eq!(captured.path, "/chat/completions");
assert!(
captured
.header("authorization")
.is_some_and(|v| v.starts_with("Bearer ")),
"shared adapter must send a Bearer auth header"
);
let body = captured.body.expect("json body");
assert_eq!(
body["model"], "openai/gpt-5.6-sol",
"only the leading atlascloud/ routing prefix must be stripped, preserving the nested id"
);
assert!(
body.get("usage").is_none() || body["usage"].is_null(),
"AtlasCloud must NOT receive the OpenRouter-only usage directive"
);
}