pub mod request;
pub mod response;
pub use request::{BatchEndpoint, Batches, CompletionWindow, CreateBatchRequest};
pub use response::{BatchError, BatchErrors, BatchListResponse, BatchObject, BatchStatus, RequestCounts};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_batch_endpoint_serialization() {
let endpoint = BatchEndpoint::ChatCompletions;
let json = serde_json::to_string(&endpoint).unwrap();
assert_eq!(json, "\"/v1/chat/completions\"");
let endpoint = BatchEndpoint::Embeddings;
let json = serde_json::to_string(&endpoint).unwrap();
assert_eq!(json, "\"/v1/embeddings\"");
}
#[test]
fn test_batch_endpoint_as_str() {
assert_eq!(BatchEndpoint::ChatCompletions.as_str(), "/v1/chat/completions");
assert_eq!(BatchEndpoint::Embeddings.as_str(), "/v1/embeddings");
assert_eq!(BatchEndpoint::Completions.as_str(), "/v1/completions");
assert_eq!(BatchEndpoint::Responses.as_str(), "/v1/responses");
assert_eq!(BatchEndpoint::Moderations.as_str(), "/v1/moderations");
}
#[test]
fn test_completion_window_serialization() {
let window = CompletionWindow::Hours24;
let json = serde_json::to_string(&window).unwrap();
assert_eq!(json, "\"24h\"");
}
#[test]
fn test_create_batch_request_new() {
let request = CreateBatchRequest::new("file-abc123", BatchEndpoint::ChatCompletions);
assert_eq!(request.input_file_id, "file-abc123");
assert_eq!(request.endpoint, BatchEndpoint::ChatCompletions);
assert_eq!(request.completion_window, CompletionWindow::Hours24);
assert!(request.metadata.is_none());
}
#[test]
fn test_create_batch_request_with_metadata() {
use std::collections::HashMap;
let mut metadata = HashMap::new();
metadata.insert("customer_id".to_string(), "user_123".to_string());
let request = CreateBatchRequest::new("file-abc123", BatchEndpoint::ChatCompletions).with_metadata(metadata.clone());
assert_eq!(request.metadata, Some(metadata));
}
#[test]
fn test_create_batch_request_serialization() {
let request = CreateBatchRequest::new("file-abc123", BatchEndpoint::ChatCompletions);
let json = serde_json::to_string(&request).unwrap();
assert!(json.contains("\"input_file_id\":\"file-abc123\""));
assert!(json.contains("\"endpoint\":\"/v1/chat/completions\""));
assert!(json.contains("\"completion_window\":\"24h\""));
assert!(!json.contains("metadata")); }
#[test]
fn test_batch_status_deserialization() {
let json = r#"{"id":"batch_123","object":"batch","endpoint":"/v1/chat/completions","input_file_id":"file-123","completion_window":"24h","status":"in_progress","created_at":1234567890,"has_more":false}"#;
let batch: BatchObject = serde_json::from_str(json).unwrap();
assert_eq!(batch.status, BatchStatus::InProgress);
let json = r#"{"id":"batch_123","object":"batch","endpoint":"/v1/chat/completions","input_file_id":"file-123","completion_window":"24h","status":"completed","created_at":1234567890,"has_more":false}"#;
let batch: BatchObject = serde_json::from_str(json).unwrap();
assert_eq!(batch.status, BatchStatus::Completed);
let json = r#"{"id":"batch_123","object":"batch","endpoint":"/v1/chat/completions","input_file_id":"file-123","completion_window":"24h","status":"failed","created_at":1234567890,"has_more":false}"#;
let batch: BatchObject = serde_json::from_str(json).unwrap();
assert_eq!(batch.status, BatchStatus::Failed);
}
#[test]
fn test_request_counts_deserialization() {
let json = r#"{"total":100,"completed":50,"failed":2}"#;
let counts: RequestCounts = serde_json::from_str(json).unwrap();
assert_eq!(counts.total, 100);
assert_eq!(counts.completed, 50);
assert_eq!(counts.failed, 2);
}
#[test]
fn test_batch_list_response_deserialization() {
let json = r#"{"object":"list","data":[],"first_id":null,"last_id":null,"has_more":false}"#;
let response: BatchListResponse = serde_json::from_str(json).unwrap();
assert_eq!(response.object, "list");
assert!(response.data.is_empty());
assert!(!response.has_more);
}
#[test]
fn test_batch_errors_deserialization() {
let json = r#"{"object":"list","data":[{"code":"invalid_request","message":"Invalid input","param":"messages","line":5}]}"#;
let errors: BatchErrors = serde_json::from_str(json).unwrap();
assert_eq!(errors.data.len(), 1);
assert_eq!(errors.data[0].code, "invalid_request");
assert_eq!(errors.data[0].line, Some(5));
}
}