use std::sync::Arc;
use tokio::net::TcpListener;
use crate::config::AppConfig;
use crate::handlers::{AppState, health_check, mock_handler, echo_handler, status_handler, models_handler, streaming_handler};
use metrics_exporter_prometheus::PrometheusHandle;
use tower_http::trace::TraceLayer;
use axum::{
extract::Request,
middleware::{self, Next},
response::IntoResponse,
routing::{get, post, any},
Router,
Extension,
};
pub async fn start_server(config: AppConfig, metrics_handle: PrometheusHandle, registry: Arc<crate::provider::ProviderRegistry>) -> Result<(), Box<dyn std::error::Error>> {
let addr = format!("{}:{}", config.host, config.port);
let listener = TcpListener::bind(&addr).await?;
let local_addr = listener.local_addr().unwrap();
let mut config = config;
config.port = local_addr.port();
let app = create_app(config, Some(metrics_handle), registry).await;
println!("🚀 VidaiMock is running at http://{}", local_addr);
tracing::info!("Listening on {}", local_addr);
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await?;
Ok(())
}
pub async fn create_app(config: AppConfig, metrics_handle: Option<PrometheusHandle>, registry: Arc<crate::provider::ProviderRegistry>) -> Router {
let state = Arc::new(AppState {
config: Arc::new(config.clone()),
registry,
});
let mut app = Router::new()
.route("/health", get(health_check))
.route("/status", get(status_handler));
let mut registered_paths = std::collections::HashSet::new();
for endpoint in &config.endpoints {
if endpoint.format == "echo" {
app = app.route(&endpoint.path, any(echo_handler));
} else if endpoint.path.contains("stream") {
app = app.route(&endpoint.path, post(streaming_handler));
} else {
app = app.route(&endpoint.path, post(mock_handler));
}
registered_paths.insert(endpoint.path.clone());
}
macro_rules! register_default {
($path:expr, $method:ident, $handler:ident) => {
if !registered_paths.contains($path) {
app = app.route($path, $method($handler));
registered_paths.insert($path.to_string());
}
};
}
register_default!("/v1/chat/completions", post, mock_handler);
register_default!("/v1/chat/completions/stream", post, mock_handler);
register_default!("/v1/models", get, models_handler);
register_default!("/v1/embeddings", post, mock_handler);
register_default!("/v1/images/generations", post, mock_handler);
register_default!("/v1/responses", post, mock_handler);
register_default!("/v1/moderations", post, mock_handler);
register_default!("/error/{code}", post, mock_handler);
register_default!("/v1/engines/{engine}/embeddings", post, mock_handler);
register_default!("/v1beta/models/{model_action}", post, mock_handler);
register_default!("/v1beta/models", get, models_handler);
register_default!("/v1beta/openai/models", get, models_handler);
register_default!("/v1beta/openai/embeddings", post, mock_handler);
register_default!("/v1/models/{model_action}", post, mock_handler);
register_default!("/openai/deployments/{deployment}/chat/completions", post, mock_handler);
register_default!("/openai/deployments/{deployment}/embeddings", post, mock_handler);
if !registered_paths.contains("/v1/models/{model_action}") {
app = app.route("/v1/models/{model_action}", get(models_handler));
}
register_default!("/v1/messages/stream", post, mock_handler);
register_default!("/model/{model_id}/invoke", post, mock_handler);
register_default!("/model/{model_id}/converse", post, mock_handler);
register_default!("/model/{model_id}/invoke-with-response-stream", post, mock_handler);
register_default!("/model/{model_id}/converse-stream", post, mock_handler);
register_default!("/v1/projects/{project}/locations/{location}/publishers/google/models/{model_action}", post, mock_handler);
if let Some(handle) = metrics_handle {
app = app.route("/metrics", get(move || std::future::ready(handle.render())));
}
app.fallback(post(mock_handler))
.layer(Extension(state))
.layer(TraceLayer::new_for_http())
.layer(middleware::from_fn(track_metrics))
}
async fn track_metrics(req: Request, next: Next) -> impl IntoResponse {
let start = std::time::Instant::now();
let path = req.uri().path().to_owned();
let method = req.method().clone();
let response = next.run(req).await;
let latency = start.elapsed().as_secs_f64();
let status = response.status().as_u16().to_string();
metrics::counter!("http_requests_total", "method" => method.to_string(), "path" => path.clone(), "status" => status).increment(1);
metrics::histogram!("http_request_duration_seconds", "method" => method.to_string(), "path" => path).record(latency);
response
}
async fn shutdown_signal() {
let ctrl_c = async {
tokio::signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
tracing::info!("Signal received, starting graceful shutdown...");
}
#[cfg(test)]
mod tests {
use super::*;
use axum::http::{Request, StatusCode};
use axum::body::Body;
use tower::ServiceExt; use std::path::PathBuf;
use std::collections::HashMap;
fn get_test_config() -> AppConfig {
AppConfig {
host: "127.0.0.1".to_string(),
port: 0,
workers: 1,
log_level: "debug".to_string(),
config_dir: PathBuf::from("config"),
latency: crate::config::LatencyConfig {
mode: "benchmark".to_string(),
base_ms: 0,
jitter_pct: 0.0,
},
endpoints: vec![crate::config::EndpointConfig {
path: "/v1/chat/completions".to_string(),
format: "openai".to_string(),
content_type: None,
}],
response_file: None,
chaos: crate::config::ChaosConfig {
enabled: false,
drop_pct: 0.0,
malformed_pct: 0.0,
trickle_ms: 0,
disconnect_pct: 0.0,
},
isolated: false,
}
}
fn get_test_registry() -> Arc<crate::provider::ProviderRegistry> {
let mut registry = crate::provider::ProviderRegistry::new();
let config = crate::provider::ProviderConfig {
name: "openai".to_string(),
matcher: "^/v1/chat/completions$".to_string(),
request_mapping: HashMap::new(),
response_template: None,
response_body: Some(r#"{"id": "test-id", "object": "chat.completion", "model": "test-model"}"#.to_string()),
stream: None,
status_code: None,
error_template: None,
priority: 0,
};
registry.add_provider(config).unwrap();
Arc::new(registry)
}
fn get_embedded_registry() -> Arc<crate::provider::ProviderRegistry> {
let mut registry = crate::provider::ProviderRegistry::new();
registry.load_from_dir(&PathBuf::from("non_existent_dir_regression")).unwrap();
Arc::new(registry)
}
async fn drain_body(resp: axum::http::Response<Body>) -> Vec<u8> {
let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
bytes.to_vec()
}
#[tokio::test]
async fn test_health_check() {
let config = get_test_config();
let registry = Arc::new(crate::provider::ProviderRegistry::new());
let app = create_app(config, None, registry).await;
let response = app
.oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_mock_endpoint() {
let config = get_test_config();
let app = create_app(config, None, get_test_registry()).await;
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from("{}"))
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(body["id"], "test-id");
}
#[tokio::test]
async fn test_echo_endpoint() {
let mut config = get_test_config();
config.endpoints = vec![crate::config::EndpointConfig {
path: "/echo".to_string(),
format: "echo".to_string(),
content_type: None,
}];
let app = create_app(config, None, get_test_registry()).await;
let body_content = r#"{"test": "echo"}"#;
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/echo")
.body(Body::from(body_content))
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
assert_eq!(&bytes[..], body_content.as_bytes());
}
#[tokio::test]
async fn test_multiprovider_endpoints() {
let config = get_test_config();
let mut registry = crate::provider::ProviderRegistry::new();
registry.add_provider(crate::provider::ProviderConfig {
name: "openai".to_string(),
matcher: "^/v1/chat/completions$".to_string(),
request_mapping: HashMap::new(),
response_template: None,
response_body: Some(r#"{"object": "chat.completion"}"#.to_string()),
stream: None,
status_code: None,
error_template: None,
priority: 0,
}).unwrap();
registry.add_provider(crate::provider::ProviderConfig {
name: "anthropic".to_string(),
matcher: "^/v1/messages$".to_string(),
request_mapping: HashMap::new(),
response_template: None,
response_body: Some(r#"{"type": "message"}"#.to_string()),
stream: None,
status_code: None,
error_template: None,
priority: 0,
}).unwrap();
registry.add_provider(crate::provider::ProviderConfig {
name: "gemini".to_string(),
matcher: "^/gemini$".to_string(),
request_mapping: HashMap::new(),
response_template: None,
response_body: Some(r#"{"candidates": []}"#.to_string()),
stream: None,
status_code: None,
error_template: None,
priority: 0,
}).unwrap();
let app = create_app(config, None, Arc::new(registry)).await;
let response = app.clone()
.oneshot(Request::builder().method("POST").uri("/v1/chat/completions").header("content-type", "application/json").body(Body::from("{}")).unwrap())
.await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body_str = String::from_utf8(bytes.to_vec()).unwrap();
assert!(body_str.contains("chat.completion"));
let response = app.clone()
.oneshot(Request::builder().method("POST").uri("/v1/messages").header("content-type", "application/json").body(Body::from("{}")).unwrap())
.await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body_str = String::from_utf8(bytes.to_vec()).unwrap();
assert!(body_str.contains("type\": \"message"));
let response = app.clone()
.oneshot(Request::builder().method("POST").uri("/gemini").header("content-type", "application/json").body(Body::from("{}")).unwrap())
.await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body_str = String::from_utf8(bytes.to_vec()).unwrap();
assert!(body_str.contains("candidates"));
}
#[tokio::test]
async fn test_status_endpoint() {
let config = get_test_config();
let app = create_app(config, None, get_test_registry()).await;
let response = app
.oneshot(Request::builder().uri("/status").body(Body::empty()).unwrap())
.await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(body["port"], 0);
assert_eq!(body["latency"]["mode"], "benchmark");
}
#[tokio::test]
async fn test_vm001_openai_stream_emits_finish_reason() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}],"max_tokens":5,"stream":true}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let bytes = drain_body(resp).await;
let text = String::from_utf8(bytes).unwrap();
assert!(text.contains(r#""finish_reason":"stop""#),
"stream must include a chunk with finish_reason=stop before [DONE]; got:\n{}", text);
assert!(text.ends_with("data: [DONE]\n\n"),
"stream must end with 'data: [DONE]\\n\\n'; got tail:\n{}",
&text[text.len().saturating_sub(100)..]);
}
#[tokio::test]
async fn test_vm002_openai_stream_usage_chunk_has_blank_line_terminator() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}],"max_tokens":5,"stream":true,"stream_options":{"include_usage":true}}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
assert!(text.contains("\"usage\":{"),
"stream must include a usage chunk when include_usage=true");
let usage_idx = text.find("\"usage\":{").unwrap();
let tail = &text[usage_idx..];
let done_idx = tail.find("data: [DONE]")
.expect("expected [DONE] frame after usage chunk");
let between = &tail[..done_idx];
assert!(between.ends_with("\n\n"),
"usage chunk must be terminated with blank line before [DONE]; got bytes: {:?}",
between.as_bytes().iter().rev().take(6).rev().collect::<Vec<_>>());
}
#[tokio::test]
async fn test_vm005_chaos_status_query_returns_provider_shape_error() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions?chaos_status=503")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE);
let content_type = resp.headers().get("content-type").cloned();
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text)
.expect("chaos response must be valid JSON (not plain text)");
assert_eq!(content_type.unwrap().to_str().unwrap(), "application/json");
assert!(json.get("error").is_some(),
"OpenAI error envelope must have top-level 'error' key; got: {}", text);
}
#[tokio::test]
async fn test_vm005_chaos_status_on_streaming_returns_non_streaming_error() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}],"stream":true}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions?chaos_status=500")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
let text = String::from_utf8(drain_body(resp).await).unwrap();
assert!(!text.starts_with("data:"),
"chaos on streaming must return non-streaming JSON body; got:\n{}", text);
let _json: serde_json::Value = serde_json::from_str(&text)
.expect("streaming chaos response body must be valid JSON");
}
#[tokio::test]
async fn test_vm005_chaos_drop_header_returns_json_error_envelope() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.header("x-vidai-chaos-drop", "100")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
let content_type = resp.headers().get("content-type").cloned()
.expect("missing content-type header");
assert_eq!(content_type.to_str().unwrap(), "application/json",
"chaos response must carry application/json content-type");
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text)
.expect("chaos response must be valid JSON");
assert!(json.get("error").is_some(),
"OpenAI error envelope must have top-level 'error' key");
}
#[tokio::test]
async fn test_vm006_anthropic_stream_events_have_blank_line_separators() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"claude","max_tokens":30,"stream":true,"messages":[{"role":"user","content":"hi"}]}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/messages")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let text = String::from_utf8(drain_body(resp).await).unwrap();
let first_event_line = text.lines()
.find(|l| l.starts_with("event: "))
.expect("no event: lines found in Anthropic stream");
assert_eq!(first_event_line, "event: message_start",
"Anthropic streaming must start with message_start; got: {}", first_event_line);
let bytes = text.as_bytes();
let mut pos = 0usize;
let mut events_seen = 0usize;
while let Some(idx) = text[pos..].find("\nevent: ") {
let absolute = pos + idx;
if events_seen >= 1 {
assert!(absolute >= 1 && bytes[absolute - 1] == b'\n',
"event at byte {} not preceded by blank line; context: {:?}",
absolute,
&text[absolute.saturating_sub(20)..(absolute + 20).min(text.len())]);
}
events_seen += 1;
pos = absolute + 1;
}
assert!(events_seen >= 7,
"expected at least 7 Anthropic event types; found {}", events_seen);
}
#[tokio::test]
async fn test_vm006_anthropic_stream_event_order() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"claude","max_tokens":30,"stream":true,"messages":[{"role":"user","content":"hi"}]}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/messages")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
let events: Vec<&str> = text.lines()
.filter(|l| l.starts_with("event: "))
.collect();
let pos_start = events.iter().position(|e| *e == "event: message_start")
.expect("missing message_start");
let pos_block_start = events.iter().position(|e| *e == "event: content_block_start")
.expect("missing content_block_start");
let pos_first_delta = events.iter().position(|e| *e == "event: content_block_delta")
.expect("missing content_block_delta");
let pos_stop = events.iter().position(|e| *e == "event: message_stop")
.expect("missing message_stop");
assert!(pos_start < pos_block_start,
"message_start must precede content_block_start");
assert!(pos_block_start < pos_first_delta,
"content_block_start must precede content_block_delta");
assert!(pos_first_delta < pos_stop,
"content_block_delta must precede message_stop");
}
#[tokio::test]
async fn test_vm007_anthropic_missing_max_tokens_returns_400() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"claude","messages":[{"role":"user","content":"hi"}]}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/messages")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST,
"missing max_tokens must return HTTP 400");
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text)
.expect("error body must be valid JSON");
assert_eq!(json.get("type").and_then(|v| v.as_str()), Some("error"),
"Anthropic error envelope must have type=error; got: {}", text);
assert_eq!(
json["error"]["type"].as_str(),
Some("invalid_request_error"),
"Anthropic validation errors must carry type=invalid_request_error"
);
let message = json["error"]["message"].as_str().unwrap_or("");
assert!(
message.contains("max_tokens"),
"error message must name the missing field; got: {}",
message
);
}
#[tokio::test]
async fn test_vm007_anthropic_with_max_tokens_returns_200() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"claude","max_tokens":30,"messages":[{"role":"user","content":"hi"}]}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/messages")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_vm008_gemini_stream_no_done_sentinel() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}"#;
let resp = app.oneshot(
Request::builder().method("POST")
.uri("/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let text = String::from_utf8(drain_body(resp).await).unwrap();
assert!(!text.contains("[DONE]"),
"Gemini stream must not emit [DONE] sentinel; got:\n{}", text);
assert!(text.contains("data: {"),
"Gemini stream should contain at least one JSON data frame");
}
#[tokio::test]
async fn test_vm008_gemini_stream_finishreason_only_on_final_chunk() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}"#;
let resp = app.oneshot(
Request::builder().method("POST")
.uri("/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
let frames: Vec<serde_json::Value> = text.lines()
.filter_map(|l| l.strip_prefix("data: "))
.filter(|j| j.starts_with('{'))
.filter_map(|j| serde_json::from_str(j).ok())
.collect();
assert!(frames.len() >= 2,
"expected at least 2 frames (>=1 intermediate + 1 final); got {}",
frames.len());
let final_frame = frames.last().unwrap();
let final_finish = final_frame["candidates"][0]["finishReason"].as_str();
assert_eq!(final_finish, Some("STOP"),
"final chunk must carry finishReason=STOP; got frame: {}", final_frame);
assert!(final_frame.get("usageMetadata").is_some(),
"final chunk must carry usageMetadata; got: {}", final_frame);
for (i, frame) in frames.iter().take(frames.len() - 1).enumerate() {
let finish = &frame["candidates"][0]["finishReason"];
assert!(
finish.is_null(),
"intermediate chunk {} must have finishReason=null; got {}; frame: {}",
i, finish, frame
);
assert!(
frame.get("usageMetadata").is_none(),
"intermediate chunk {} must NOT carry usageMetadata; got frame: {}",
i, frame
);
}
}
#[tokio::test]
async fn test_openai_non_streaming_still_works() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(json["object"], "chat.completion");
assert!(json["choices"][0]["message"].is_object());
}
#[tokio::test]
async fn test_xmockstatus_header_still_works() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.header("x-mock-status", "429")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::TOO_MANY_REQUESTS);
let text = String::from_utf8(drain_body(resp).await).unwrap();
let _: serde_json::Value = serde_json::from_str(&text)
.expect("body must be valid JSON");
}
#[tokio::test]
async fn test_vm009_openai_tool_loop_terminates_on_tool_result() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"model": "gpt-4o-mini",
"tools": [{"type": "function", "function": {"name": "get_weather", "parameters": {}}}],
"messages": [
{"role": "user", "content": "What is the weather in London?"},
{"role": "assistant", "tool_calls": [
{"id": "call_1", "type": "function",
"function": {"name": "get_weather", "arguments": "{\"city\":\"London\"}"}}
]},
{"role": "tool", "tool_call_id": "call_1", "content": "15°C cloudy"}
]
}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
let choice = &json["choices"][0];
let msg = &choice["message"];
assert_eq!(choice["finish_reason"], "stop",
"finish_reason must be 'stop' after tool result, not 'tool_calls'; got:\n{}", text);
assert!(msg.get("tool_calls").is_none() || msg["tool_calls"].is_null(),
"response must NOT include tool_calls after a tool result; got:\n{}", text);
assert!(msg["content"].as_str().is_some(),
"response must include text content after a tool result; got:\n{}", text);
}
#[tokio::test]
async fn test_vm009_openai_tools_without_result_still_returns_tool_calls() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"model": "gpt-4o-mini",
"tools": [{"type": "function", "function": {"name": "get_weather", "parameters": {}}}],
"messages": [{"role": "user", "content": "Weather?"}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(json["choices"][0]["finish_reason"], "tool_calls");
assert!(json["choices"][0]["message"]["tool_calls"].is_array());
}
#[tokio::test]
async fn test_vm009_anthropic_tool_loop_terminates_on_tool_result() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"model": "claude",
"max_tokens": 200,
"tools": [{"name": "get_weather", "description": "x", "input_schema": {"type":"object"}}],
"messages": [
{"role": "user", "content": "Weather in London?"},
{"role": "assistant", "content": [
{"type": "tool_use", "id": "t1", "name": "get_weather", "input": {"city": "London"}}
]},
{"role": "user", "content": [
{"type": "tool_result", "tool_use_id": "t1", "content": "15°C cloudy"}
]}
]
}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/messages")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(json["stop_reason"], "end_turn",
"stop_reason must be 'end_turn' after tool_result, not 'tool_use'; got:\n{}", text);
let first_block_type = json["content"][0]["type"].as_str();
assert_eq!(first_block_type, Some("text"),
"first content block must be 'text' after tool_result, not 'tool_use'; got:\n{}", text);
}
#[tokio::test]
async fn test_vm009_anthropic_tools_without_result_still_returns_tool_use() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"model": "claude",
"max_tokens": 200,
"tools": [{"name": "get_weather", "description": "x", "input_schema": {"type":"object"}}],
"messages": [{"role": "user", "content": "Weather?"}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/messages")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(json["stop_reason"], "tool_use");
assert_eq!(json["content"][0]["type"], "tool_use");
}
#[tokio::test]
async fn test_vm009_gemini_tool_loop_terminates_on_function_response() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"contents": [
{"role": "user", "parts": [{"text": "Weather in London?"}]},
{"role": "model", "parts": [{"functionCall": {"name": "get_weather", "args": {"city":"London"}}}]},
{"role": "user", "parts": [{"functionResponse": {"name": "get_weather", "response": {"temp": 15}}}]}
],
"tools": [{"functionDeclarations": [{"name": "get_weather", "parameters": {}}]}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST")
.uri("/v1beta/models/gemini-2.5-flash:generateContent")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
let first_part = &json["candidates"][0]["content"]["parts"][0];
assert!(first_part.get("text").is_some(),
"first part must carry text after functionResponse; got:\n{}", text);
assert!(first_part.get("functionCall").is_none(),
"first part must NOT be functionCall after functionResponse; got:\n{}", text);
assert!(json["candidates"][0].get("finishMessage").is_none(),
"finishMessage must be absent on the text-response branch; got:\n{}", text);
}
#[tokio::test]
async fn test_vm009_gemini_tools_without_response_still_returns_function_call() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"contents": [{"role": "user", "parts": [{"text": "Weather?"}]}],
"tools": [{"functionDeclarations": [{"name": "get_weather", "parameters": {}}]}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST")
.uri("/v1beta/models/gemini-2.5-flash:generateContent")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
assert!(json["candidates"][0]["content"]["parts"][0].get("functionCall").is_some());
}
#[tokio::test]
async fn test_vm010_openai_echoes_caller_tool_name() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "hi"}],
"tools": [{"type": "function", "function": {"name": "canary_tool_a", "parameters": {}}}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let json: serde_json::Value = serde_json::from_str(
&String::from_utf8(drain_body(resp).await).unwrap()
).unwrap();
let tc = &json["choices"][0]["message"]["tool_calls"][0];
assert_eq!(tc["function"]["name"], "canary_tool_a",
"OpenAI response must echo caller's tool name; got: {}", tc);
assert_eq!(tc["function"]["arguments"], "{}",
"OpenAI args should default to empty object; got: {}", tc);
}
#[tokio::test]
async fn test_vm010_openai_streaming_echoes_caller_tool_name() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "hi"}],
"stream": true,
"tools": [{"type": "function", "function": {"name": "canary_tool_s", "parameters": {}}}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
assert!(text.contains("\"name\":\"canary_tool_s\""),
"OpenAI streaming tool_calls must echo caller's name; got:\n{}", text);
assert!(!text.contains("\"name\":\"get_weather\""),
"OpenAI streaming must not emit the hardcoded demo name; got:\n{}", text);
}
#[tokio::test]
async fn test_vm010_anthropic_echoes_caller_tool_name() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"model": "claude", "max_tokens": 200,
"messages": [{"role": "user", "content": "hi"}],
"tools": [{"name": "canary_tool_b", "description": "x", "input_schema": {"type": "object"}}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/messages")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let json: serde_json::Value = serde_json::from_str(
&String::from_utf8(drain_body(resp).await).unwrap()
).unwrap();
assert_eq!(json["content"][0]["type"], "tool_use");
assert_eq!(json["content"][0]["name"], "canary_tool_b");
}
#[tokio::test]
async fn test_vm010_gemini_echoes_caller_tool_name() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"contents": [{"role": "user", "parts": [{"text": "hi"}]}],
"tools": [{"functionDeclarations": [{"name": "canary_tool_c", "parameters": {"type":"OBJECT"}}]}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST")
.uri("/v1beta/models/gemini-2.5-flash:generateContent")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let json: serde_json::Value = serde_json::from_str(
&String::from_utf8(drain_body(resp).await).unwrap()
).unwrap();
let fc = &json["candidates"][0]["content"]["parts"][0]["functionCall"];
assert_eq!(fc["name"], "canary_tool_c");
}
fn assert_data_frames_are_single_line_json(body: &str, label: &str) {
let mut frame_count = 0;
for line in body.lines() {
let Some(payload) = line.strip_prefix("data: ") else { continue };
if payload.is_empty() || payload == "[DONE]" {
panic!("{label}: empty 'data:' line found — indicates broken multi-line frame");
}
serde_json::from_str::<serde_json::Value>(payload)
.unwrap_or_else(|e| panic!(
"{label}: data frame must be valid JSON; parse error: {e}\nframe: {payload}\nfull body:\n{body}"
));
assert!(!payload.contains('\n'),
"{label}: data frame must be single-line; got embedded \\n in:\n{payload}");
frame_count += 1;
}
assert!(frame_count >= 1,
"{label}: expected at least one data: frame; got 0. body:\n{body}");
}
#[tokio::test]
async fn test_vm011_gemini_streaming_with_tools_single_line_frames() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"contents": [{"role": "user", "parts": [{"text": "hi"}]}],
"tools": [{"functionDeclarations": [{"name": "canary_t", "parameters": {"type":"OBJECT"}}]}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST")
.uri("/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
assert_data_frames_are_single_line_json(&text, "gemini stream+tools");
assert!(text.contains("\"name\":\"canary_t\""),
"Gemini streaming tool_call must echo caller's name; got:\n{}", text);
}
#[tokio::test]
async fn test_vm011_anthropic_streaming_with_tools_emits_tool_use_block() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"model": "claude", "max_tokens": 50, "stream": true,
"messages": [{"role": "user", "content": "hi"}],
"tools": [{"name": "canary_t", "description": "x", "input_schema": {"type": "object"}}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/messages")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
assert!(
text.contains("\"type\":\"tool_use\""),
"Anthropic streaming with tools must emit tool_use content block; got:\n{}", text
);
assert!(
text.contains("\"name\":\"canary_t\""),
"tool_use block must echo caller's name; got:\n{}", text
);
assert!(
text.contains("\"type\":\"input_json_delta\""),
"Anthropic streaming tool deltas must be input_json_delta; got:\n{}", text
);
assert!(
text.contains("\"stop_reason\":\"tool_use\""),
"message_delta must carry stop_reason=tool_use; got:\n{}", text
);
}
#[tokio::test]
async fn test_vm011_openai_streaming_with_tools_single_line_frames() {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let body = r#"{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "hi"}],
"stream": true,
"tools": [{"type": "function", "function": {"name": "canary_t", "parameters": {}}}]
}"#;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
let text = String::from_utf8(drain_body(resp).await).unwrap();
let body_without_done: String = text.lines()
.filter(|l| *l != "data: [DONE]")
.collect::<Vec<_>>()
.join("\n");
assert_data_frames_are_single_line_json(&body_without_done, "openai stream+tools");
}
async fn build_isolated_app(
test_dir_name: &str,
provider_yamls: &[(&str, &str)], ) -> (axum::Router, std::path::PathBuf) {
let dir = std::env::current_dir().unwrap()
.join(format!("target/test_isolated_e2e/{}", test_dir_name));
if dir.exists() { std::fs::remove_dir_all(&dir).unwrap(); }
std::fs::create_dir_all(dir.join("providers")).unwrap();
for (name, body) in provider_yamls {
std::fs::write(dir.join("providers").join(name), body).unwrap();
}
let mut config = get_test_config();
config.isolated = true;
config.config_dir = dir.clone();
let registry = crate::provider::init_registry_with_options(&dir, true);
let app = create_app(config, None, registry).await;
(app, dir)
}
#[tokio::test]
async fn test_isolated_models_endpoint_lists_only_user_providers() {
let (app, dir) = build_isolated_app(
"models_only_user",
&[("solo.yaml", "name: \"solo\"\nmatcher: \"^/solo$\"\nresponse_body: \"{}\"\n")],
).await;
let resp = app.oneshot(
Request::builder().uri("/v1/models").body(Body::empty()).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body: serde_json::Value = serde_json::from_slice(
&axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap()
).unwrap();
let ids: Vec<&str> = body["data"].as_array().unwrap()
.iter()
.map(|m| m["id"].as_str().unwrap())
.collect();
assert_eq!(ids, vec!["solo"],
"isolated /v1/models must list only user providers; got {:?}", ids);
std::fs::remove_dir_all(dir).unwrap();
}
#[tokio::test]
async fn test_isolated_models_endpoint_empty_dir_returns_empty_list() {
let (app, dir) = build_isolated_app("models_empty", &[]).await;
let resp = app.oneshot(
Request::builder().uri("/v1/models").body(Body::empty()).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body: serde_json::Value = serde_json::from_slice(
&axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap()
).unwrap();
let list = body["data"].as_array().expect("data must be an array");
assert_eq!(list.len(), 0,
"isolated + empty dir must return an empty model list, not a fake gpt-4; got {:?}", list);
std::fs::remove_dir_all(dir).unwrap();
}
#[tokio::test]
async fn test_isolated_bundled_routes_404_with_helpful_message() {
let (app, dir) = build_isolated_app("bundled_404",
&[("solo.yaml", "name: \"solo\"\nmatcher: \"^/solo$\"\nresponse_body: \"{}\"\n")],
).await;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from("{}")).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let text = String::from_utf8(
axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap().to_vec()
).unwrap();
assert!(text.contains("isolated mode"),
"404 body should mention isolated mode to help users diagnose; got: {}", text);
std::fs::remove_dir_all(dir).unwrap();
}
#[tokio::test]
async fn test_isolated_user_provider_serves_normally() {
let (app, dir) = build_isolated_app("user_serves",
&[("solo.yaml",
"name: \"solo\"\nmatcher: \"^/solo$\"\nresponse_body: '{\"hello\":\"world\"}'\n")],
).await;
let resp = app.oneshot(
Request::builder().method("POST").uri("/solo")
.header("content-type", "application/json")
.body(Body::from("{}")).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body: serde_json::Value = serde_json::from_slice(
&axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap()
).unwrap();
assert_eq!(body["hello"], "world");
std::fs::remove_dir_all(dir).unwrap();
}
#[tokio::test]
async fn test_non_isolated_404_message_unchanged() {
let app = create_app(get_test_config(), None, get_test_registry()).await;
let resp = app.oneshot(
Request::builder().method("POST").uri("/totally/unknown/path")
.header("content-type", "application/json")
.body(Body::from("{}")).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let text = String::from_utf8(
axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap().to_vec()
).unwrap();
assert!(!text.contains("isolated mode"),
"default-mode 404 must not mention isolated mode; got: {}", text);
assert!(text.contains("/status"),
"404 should suggest /status for diagnosis; got: {}", text);
}
#[tokio::test]
async fn test_isolated_status_endpoint_exposes_flag() {
let (app, dir) = build_isolated_app("status_flag",
&[("solo.yaml", "name: \"solo\"\nmatcher: \"^/solo$\"\nresponse_body: \"{}\"\n")],
).await;
let resp = app.oneshot(
Request::builder().uri("/status").body(Body::empty()).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body: serde_json::Value = serde_json::from_slice(
&axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap()
).unwrap();
assert_eq!(body["isolated"], serde_json::json!(true),
"/status must expose isolated=true so users can confirm runtime mode");
std::fs::remove_dir_all(dir).unwrap();
}
#[tokio::test]
async fn test_default_mode_status_reports_isolated_false() {
let config = get_test_config();
let app = create_app(config, None, get_test_registry()).await;
let resp = app.oneshot(
Request::builder().uri("/status").body(Body::empty()).unwrap()
).await.unwrap();
let body: serde_json::Value = serde_json::from_slice(
&axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap()
).unwrap();
assert_eq!(body["isolated"], serde_json::json!(false));
}
async fn post_responses(body: &'static str) -> String {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let resp = app.oneshot(
Request::builder().method("POST").uri("/v1/responses")
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
String::from_utf8(drain_body(resp).await).unwrap()
}
const RESPONSES_WITH_TOOLS_STREAM: &str = r#"{
"model": "gpt-4o-mini",
"stream": true,
"input": [{"role": "user", "content": "Call the get_weather tool now."}],
"tools": [{"type": "function", "name": "get_weather",
"parameters": {"type": "object", "properties": {}}}]
}"#;
#[tokio::test]
async fn test_vm012_responses_streaming_with_tools_emits_function_call() {
let text = post_responses(RESPONSES_WITH_TOOLS_STREAM).await;
assert!(!text.contains("[[object]]"),
"streaming must not stringify the structured tool chunk; got:\n{}", text);
assert!(text.contains(r#""type":"function_call""#),
"stream must contain a function_call output item; got:\n{}", text);
assert!(text.contains("response.function_call_arguments.delta"),
"stream must emit function_call_arguments.delta; got:\n{}", text);
assert!(text.contains("response.function_call_arguments.done"),
"stream must emit function_call_arguments.done; got:\n{}", text);
assert!(!text.contains("response.output_text.delta"),
"tool-mode stream must not emit text deltas; got:\n{}", text);
assert!(text.contains(r#""name":"get_weather""#),
"the caller's tool name must be echoed back; got:\n{}", text);
}
#[tokio::test]
async fn test_vm012_responses_streaming_tool_call_emits_output_item_done() {
let text = post_responses(RESPONSES_WITH_TOOLS_STREAM).await;
let done_at = text.find("response.output_item.done")
.unwrap_or_else(|| panic!("missing output_item.done; got:\n{}", text));
let completed_at = text.find("response.completed")
.unwrap_or_else(|| panic!("missing response.completed; got:\n{}", text));
assert!(done_at < completed_at,
"output_item.done must precede response.completed; got:\n{}", text);
let done_line = text.lines()
.find(|l| l.contains("response.function_call_arguments.done") && l.starts_with("data:"))
.unwrap_or_else(|| panic!("no data line for arguments.done; got:\n{}", text));
assert!(done_line.contains(r#""name":"get_weather""#),
"function_call_arguments.done must include the function name; got:\n{}", done_line);
}
#[tokio::test]
async fn test_vm012_responses_streaming_without_tools_still_streams_text() {
let text = post_responses(r#"{
"model": "gpt-4o-mini",
"stream": true,
"input": [{"role": "user", "content": "hello"}]
}"#).await;
assert!(text.contains("response.output_text.delta"),
"text mode must emit output_text deltas; got:\n{}", text);
assert!(!text.contains(r#""type":"function_call""#),
"no tools in request => no function_call; got:\n{}", text);
assert!(text.contains("response.content_part.added"),
"text mode must still open a content part; got:\n{}", text);
}
#[tokio::test]
async fn test_vm012_responses_streaming_tool_loop_terminates_on_result() {
let text = post_responses(r#"{
"model": "gpt-4o-mini",
"stream": true,
"input": [
{"role": "user", "content": "What is the weather?"},
{"type": "function_call", "call_id": "c1", "name": "get_weather", "arguments": "{}"},
{"type": "function_call_output", "call_id": "c1", "output": "15C cloudy"}
],
"tools": [{"type": "function", "name": "get_weather", "parameters": {}}]
}"#).await;
assert!(!text.contains(r#""type":"function_call""#),
"must not call the tool again after a tool result; got:\n{}", text);
assert!(text.contains("response.output_text.delta"),
"must synthesize text after a tool result; got:\n{}", text);
assert!(!text.contains("[[object]]"),
"termination must be real text, not a stringified chunk; got:\n{}", text);
}
#[tokio::test]
async fn test_vm012_responses_tool_loop_terminates_on_result() {
let text = post_responses(r#"{
"model": "gpt-4o-mini",
"input": [
{"role": "user", "content": "What is the weather?"},
{"type": "function_call", "call_id": "c1", "name": "get_weather", "arguments": "{}"},
{"type": "function_call_output", "call_id": "c1", "output": "15C cloudy"}
],
"tools": [{"type": "function", "name": "get_weather", "parameters": {}}]
}"#).await;
let json: serde_json::Value = serde_json::from_str(&text)
.unwrap_or_else(|e| panic!("invalid JSON ({}); got:\n{}", e, text));
let kinds: Vec<&str> = json["output"].as_array().unwrap().iter()
.filter_map(|o| o["type"].as_str()).collect();
assert!(!kinds.contains(&"function_call"),
"must not call the tool again after a tool result; got:\n{}", text);
assert!(kinds.contains(&"message"),
"must return a message after a tool result; got:\n{}", text);
}
#[tokio::test]
async fn test_vm012_responses_tools_without_result_still_returns_function_call() {
let text = post_responses(r#"{
"model": "gpt-4o-mini",
"input": [{"role": "user", "content": "What is the weather?"}],
"tools": [{"type": "function", "name": "get_weather", "parameters": {}}]
}"#).await;
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(json["output"][0]["type"], "function_call",
"tools without a result must still produce a function_call; got:\n{}", text);
}
#[tokio::test]
async fn test_vm012_responses_accepts_string_input_with_tools() {
let text = post_responses(r#"{
"model": "gpt-4o-mini",
"input": "What is the weather?",
"tools": [{"type": "function", "name": "get_weather", "parameters": {}}]
}"#).await;
let json: serde_json::Value = serde_json::from_str(&text)
.unwrap_or_else(|e| panic!("string input must render valid JSON ({}); got:\n{}", e, text));
assert_eq!(json["output"][0]["type"], "function_call",
"string input with tools must still produce a function_call; got:\n{}", text);
let stream = post_responses(r#"{
"model": "gpt-4o-mini",
"stream": true,
"input": "What is the weather?",
"tools": [{"type": "function", "name": "get_weather", "parameters": {}}]
}"#).await;
assert!(stream.contains("response.function_call_arguments.done"),
"string input must stream a function call, not error; got:\n{}", stream);
}
#[tokio::test]
async fn test_vm012_responses_empty_tools_array_streams_text() {
let text = post_responses(r#"{
"model": "gpt-4o-mini",
"stream": true,
"input": [{"role": "user", "content": "hello"}],
"tools": []
}"#).await;
assert!(!text.contains(r#""type":"function_call""#),
"empty tools array must not produce a function_call; got:\n{}", text);
assert!(text.contains("response.output_text.delta"),
"empty tools array must stream text; got:\n{}", text);
}
async fn post_path(uri: &'static str, body: &'static str) -> String {
let app = create_app(get_test_config(), None, get_embedded_registry()).await;
let resp = app.oneshot(
Request::builder().method("POST").uri(uri)
.header("content-type", "application/json")
.body(Body::from(body)).unwrap()
).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK, "{} should return 200", uri);
String::from_utf8(drain_body(resp).await).unwrap()
}
const COMPAT_TOOLS_BODY: &str = r#"{
"model": "gpt-4",
"messages": [{"role": "user", "content": "What is the weather?"}],
"tools": [{"type": "function", "function": {
"name": "get_weather", "parameters": {"type": "object", "properties": {}}}}]
}"#;
const COMPAT_TOOL_RESULT_BODY: &str = r#"{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "What is the weather?"},
{"role": "assistant", "tool_calls": [{"id": "call_1", "type": "function",
"function": {"name": "get_weather", "arguments": "{}"}}]},
{"role": "tool", "tool_call_id": "call_1", "content": "15C cloudy"}
],
"tools": [{"type": "function", "function": {
"name": "get_weather", "parameters": {"type": "object", "properties": {}}}}]
}"#;
fn assert_tool_call(text: &str, who: &str) {
let json: serde_json::Value = serde_json::from_str(text)
.unwrap_or_else(|e| panic!("{}: invalid JSON ({}); got:\n{}", who, e, text));
let choice = &json["choices"][0];
assert_eq!(choice["finish_reason"], "tool_calls",
"{}: finish_reason must be tool_calls; got:\n{}", who, text);
assert_eq!(choice["message"]["tool_calls"][0]["function"]["name"], "get_weather",
"{}: must echo the caller's tool name; got:\n{}", who, text);
}
fn assert_terminated(text: &str, who: &str) {
let json: serde_json::Value = serde_json::from_str(text)
.unwrap_or_else(|e| panic!("{}: invalid JSON ({}); got:\n{}", who, e, text));
let choice = &json["choices"][0];
assert_eq!(choice["finish_reason"], "stop",
"{}: must stop after a tool result; got:\n{}", who, text);
assert!(choice["message"].get("tool_calls").is_none()
|| choice["message"]["tool_calls"].is_null(),
"{}: must not call the tool again after a result; got:\n{}", who, text);
assert!(choice["message"]["content"].as_str().is_some(),
"{}: must synthesize text after a tool result; got:\n{}", who, text);
}
#[tokio::test]
async fn test_vm013_azure_returns_tool_calls() {
let uri = "/openai/deployments/gpt-4/chat/completions";
assert_tool_call(&post_path(uri, COMPAT_TOOLS_BODY).await, "azure");
assert_terminated(&post_path(uri, COMPAT_TOOL_RESULT_BODY).await, "azure");
}
#[tokio::test]
async fn test_vm013_mistral_returns_tool_calls() {
let uri = "/v1/mistral/chat/completions";
assert_tool_call(&post_path(uri, COMPAT_TOOLS_BODY).await, "mistral");
assert_terminated(&post_path(uri, COMPAT_TOOL_RESULT_BODY).await, "mistral");
}
#[tokio::test]
async fn test_vm013_groq_returns_tool_calls_and_keeps_usage_shape() {
let uri = "/groq/v1/chat/completions";
let text = post_path(uri, COMPAT_TOOLS_BODY).await;
assert_tool_call(&text, "groq");
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
assert!(json["usage"].get("queue_time").is_some(),
"groq tool response must keep its usage shape; got:\n{}", text);
assert_eq!(json["system_fingerprint"], "fp_mock_groq");
assert_terminated(&post_path(uri, COMPAT_TOOL_RESULT_BODY).await, "groq");
}
#[tokio::test]
async fn test_vm013_openrouter_returns_tool_calls_and_keeps_envelope() {
let uri = "/api/v1/chat/completions";
let text = post_path(uri, COMPAT_TOOLS_BODY).await;
assert_tool_call(&text, "openrouter");
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(json["provider"], "MockProvider",
"openrouter tool response must keep `provider`; got:\n{}", text);
assert!(json["usage"].get("total_cost").is_some(),
"openrouter tool response must keep total_cost; got:\n{}", text);
assert_terminated(&post_path(uri, COMPAT_TOOL_RESULT_BODY).await, "openrouter");
}
#[tokio::test]
async fn test_vm013_compat_streaming_emits_tool_calls_delta() {
let text = post_path("/openai/deployments/gpt-4/chat/completions", r#"{
"model": "gpt-4",
"stream": true,
"messages": [{"role": "user", "content": "What is the weather?"}],
"tools": [{"type": "function", "function": {
"name": "get_weather", "parameters": {}}}]
}"#).await;
assert!(!text.contains("[[object]]"),
"structured chunk must not be stringified; got:\n{}", text);
assert!(text.contains(r#""tool_calls""#),
"stream must carry a tool_calls delta; got:\n{}", text);
assert!(text.contains(r#""name":"get_weather""#),
"stream must echo the caller's tool name; got:\n{}", text);
}
#[tokio::test]
async fn test_vm013_compat_without_tools_still_returns_text() {
let text = post_path("/v1/mistral/chat/completions", r#"{
"model": "mistral-medium",
"messages": [{"role": "user", "content": "hello"}]
}"#).await;
let json: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(json["choices"][0]["finish_reason"], "stop");
assert!(json["choices"][0]["message"].get("tool_calls").is_none()
|| json["choices"][0]["message"]["tool_calls"].is_null(),
"no tools in request => no tool_calls; got:\n{}", text);
}
#[tokio::test]
async fn test_vm012_responses_stream_ids_are_stable_across_events() {
let text = post_responses(RESPONSES_WITH_TOOLS_STREAM).await;
let ids: std::collections::HashSet<&str> = text.match_indices("\"id\":\"resp_")
.map(|(i, _)| {
let start = i + "\"id\":\"".len();
let rest = &text[start..];
&rest[..rest.find('"').unwrap()]
})
.collect();
assert_eq!(ids.len(), 1,
"response id must be identical in every event of one stream, found {:?}; got:\n{}",
ids, text);
let item_ids: std::collections::HashSet<&str> = text.match_indices("\"item_id\":\"")
.map(|(i, _)| {
let start = i + "\"item_id\":\"".len();
let rest = &text[start..];
&rest[..rest.find('"').unwrap()]
})
.collect();
assert!(item_ids.len() <= 1,
"function call item_id must be stable, found {:?}; got:\n{}", item_ids, text);
}
}