#[test]
fn test_completion_request_clone() {
let req = CompletionRequest {
stream: false,
n: crate::api::ChoiceCount::ONE,
model: "test".to_string(),
prompt: "Hello".to_string(),
max_tokens: Some(50),
temperature: Some(0.7),
top_p: Some(0.9),
stop: Some(vec!["stop".to_string()]),
};
let cloned = req.clone();
assert_eq!(cloned.model, req.model);
assert_eq!(cloned.prompt, req.prompt);
assert_eq!(cloned.max_tokens, req.max_tokens);
assert_eq!(cloned.temperature, req.temperature);
assert_eq!(cloned.top_p, req.top_p);
assert_eq!(cloned.stop, req.stop);
}
#[test]
fn test_completion_request_debug() {
let req = CompletionRequest {
stream: false,
n: crate::api::ChoiceCount::ONE,
model: "debug-model".to_string(),
prompt: "debug prompt".to_string(),
max_tokens: None,
temperature: None,
top_p: None,
stop: None,
};
let debug_str = format!("{:?}", req);
assert!(debug_str.contains("CompletionRequest"));
assert!(debug_str.contains("debug-model"));
}
#[test]
fn test_model_metadata_response_with_lineage() {
let lineage = ModelLineage {
uri: "pacha://test-model:v1.0".to_string(),
version: "1.0.0".to_string(),
recipe: Some("training-recipe".to_string()),
parent: None,
content_hash: "blake3:test".to_string(),
};
let response = ModelMetadataResponse {
id: "test-model".to_string(),
name: "Test Model".to_string(),
format: Some("GGUF".to_string()),
size_bytes: Some(4_000_000_000),
quantization: Some("Q4_K_M".to_string()),
context_length: Some(8192),
model_max_context_length: None,
architecture: None,
lineage: Some(lineage),
loaded: true,
};
let json = serde_json::to_string(&response).expect("serialize");
assert!(json.contains("pacha://test-model:v1.0"));
assert!(json.contains("training-recipe"));
let parsed: ModelMetadataResponse = serde_json::from_str(&json).expect("deserialize");
assert!(parsed.lineage.is_some());
assert_eq!(parsed.lineage.expect("test value should be present").uri, "pacha://test-model:v1.0");
}
#[test]
fn test_model_metadata_response_large_size() {
let response = ModelMetadataResponse {
id: "large-model".to_string(),
name: "Large Model".to_string(),
format: Some("SafeTensors".to_string()),
size_bytes: Some(70_000_000_000), quantization: None,
context_length: Some(32768),
model_max_context_length: None,
architecture: None,
lineage: None,
loaded: false,
};
let json = serde_json::to_string(&response).expect("serialize");
let parsed: ModelMetadataResponse = serde_json::from_str(&json).expect("deserialize");
assert_eq!(parsed.size_bytes, Some(70_000_000_000));
assert_eq!(parsed.context_length, Some(32768));
}
#[test]
fn test_model_metadata_response_clone() {
let response = ModelMetadataResponse {
id: "test".to_string(),
name: "Test".to_string(),
format: Some("GGUF".to_string()),
size_bytes: Some(1000),
quantization: Some("Q4_0".to_string()),
context_length: Some(2048),
model_max_context_length: None,
architecture: None,
lineage: None,
loaded: true,
};
let cloned = response.clone();
assert_eq!(cloned.id, response.id);
assert_eq!(cloned.quantization, response.quantization);
}
#[test]
fn test_model_metadata_response_debug() {
let response = ModelMetadataResponse {
id: "debug".to_string(),
name: "Debug".to_string(),
format: Some("APR".to_string()),
size_bytes: Some(0),
quantization: None,
context_length: Some(512),
model_max_context_length: None,
architecture: None,
lineage: None,
loaded: false,
};
let debug_str = format!("{:?}", response);
assert!(debug_str.contains("ModelMetadataResponse"));
}
#[test]
fn test_reload_request_with_both_fields() {
let req = ReloadRequest {
model: Some("my-model".to_string()),
path: Some("/models/my-model.gguf".to_string()),
};
let json = serde_json::to_string(&req).expect("serialize");
assert!(json.contains("my-model"));
assert!(json.contains("/models/my-model.gguf"));
let parsed: ReloadRequest = serde_json::from_str(&json).expect("deserialize");
assert_eq!(parsed.model, Some("my-model".to_string()));
assert_eq!(parsed.path, Some("/models/my-model.gguf".to_string()));
}
#[test]
fn test_reload_request_path_only() {
let req = ReloadRequest {
model: None,
path: Some("/path/to/model.safetensors".to_string()),
};
let json = serde_json::to_string(&req).expect("serialize");
let parsed: ReloadRequest = serde_json::from_str(&json).expect("deserialize");
assert!(parsed.model.is_none());
assert!(parsed.path.is_some());
}
#[test]
fn test_reload_request_model_only() {
let req = ReloadRequest {
model: Some("model-id".to_string()),
path: None,
};
let json = serde_json::to_string(&req).expect("serialize");
let parsed: ReloadRequest = serde_json::from_str(&json).expect("deserialize");
assert!(parsed.model.is_some());
assert!(parsed.path.is_none());
}
#[test]
fn test_reload_request_clone() {
let req = ReloadRequest {
model: Some("test".to_string()),
path: Some("/path".to_string()),
};
let cloned = req.clone();
assert_eq!(cloned.model, req.model);
assert_eq!(cloned.path, req.path);
}
#[test]
fn test_reload_request_debug() {
let req = ReloadRequest {
model: Some("debug".to_string()),
path: None,
};
let debug_str = format!("{:?}", req);
assert!(debug_str.contains("ReloadRequest"));
}
#[tokio::test]
async fn test_realize_embed_empty_input() {
let app = create_test_app_shared();
let req_body = serde_json::json!({
"input": "",
"model": null
});
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/realize/embed")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_string(&req_body).expect("JSON serialization failed")))
.expect("test value should be present"),
)
.await
.expect("test value should be present");
let status = response.status();
if status != StatusCode::OK && status != StatusCode::BAD_REQUEST {
return; }
}
#[tokio::test]
async fn test_realize_embed_long_input() {
let app = create_test_app_shared();
let long_text = "word ".repeat(10000);
let req_body = serde_json::json!({
"input": long_text,
"model": null
});
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/realize/embed")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_string(&req_body).expect("JSON serialization failed")))
.expect("test value should be present"),
)
.await
.expect("test value should be present");
let status = response.status();
assert_eq!(
status,
StatusCode::SERVICE_UNAVAILABLE,
"no model is resident: expected 503"
);
}
#[tokio::test]
async fn test_realize_embed_unicode_input() {
let app = create_test_app_shared();
let req_body = serde_json::json!({
"input": "Hello 世界! \u{1F30D} مرحبا 你好 Привет",
"model": null
});
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/realize/embed")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_string(&req_body).expect("JSON serialization failed")))
.expect("test value should be present"),
)
.await
.expect("test value should be present");
let status = response.status();
assert_eq!(
status,
StatusCode::SERVICE_UNAVAILABLE,
"no model is resident: expected 503"
);
}
#[tokio::test]
async fn test_realize_reload_without_registry() {
let app = create_test_app_shared();
let req_body = serde_json::json!({
"model": "test",
"path": "/tmp/test.gguf"
});
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/realize/reload")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_string(&req_body).expect("JSON serialization failed")))
.expect("test value should be present"),
)
.await
.expect("test value should be present");
assert_eq!(response.status(), StatusCode::NOT_IMPLEMENTED);
}
#[tokio::test]
async fn test_realize_reload_invalid_json() {
let app = create_test_app_shared();
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/realize/reload")
.header("content-type", "application/json")
.body(Body::from("not valid json"))
.expect("test value should be present"),
)
.await
.expect("test value should be present");
assert!(
response.status() == StatusCode::BAD_REQUEST
|| response.status() == StatusCode::NOT_FOUND
|| response.status() == StatusCode::INTERNAL_SERVER_ERROR
|| response.status() == StatusCode::SERVICE_UNAVAILABLE
|| response.status() == StatusCode::UNPROCESSABLE_ENTITY
);
}
#[tokio::test]
async fn test_openai_completions_empty_prompt() {
let app = create_test_app_shared();
let req_body = serde_json::json!({
"model": "test",
"prompt": ""
});
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/v1/completions")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_string(&req_body).expect("JSON serialization failed")))
.expect("test value should be present"),
)
.await
.expect("test value should be present");
let status = response.status();
if status != StatusCode::OK && status != StatusCode::BAD_REQUEST {
return; }
}
#[tokio::test]
async fn test_openai_completions_with_temperature() {
let app = create_test_app_shared();
let req_body = serde_json::json!({
"model": "test",
"prompt": "Hello",
"temperature": 0.5,
"max_tokens": 5
});
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/v1/completions")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_string(&req_body).expect("JSON serialization failed")))
.expect("test value should be present"),
)
.await
.expect("test value should be present");
crate::api::test_helpers::assert_no_model_status(response.status());
}
#[tokio::test]
async fn test_openai_completions_with_top_p() {
let app = create_test_app_shared();
let req_body = serde_json::json!({
"model": "test",
"prompt": "Hello",
"top_p": 0.9,
"max_tokens": 5
});
let response = app
.oneshot(
Request::builder()
.method("POST")
.uri("/v1/completions")
.header("content-type", "application/json")
.body(Body::from(serde_json::to_string(&req_body).expect("JSON serialization failed")))
.expect("test value should be present"),
)
.await
.expect("test value should be present");
crate::api::test_helpers::assert_no_model_status(response.status());
}