aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / crates / aien-inference-client / tests / http_client_tests.rs
| 1 | use aien_inference_client::HttpInferenceClient; |
| 2 | use aien_inference_protocol::*; |
| 3 | use aien_protocol_types::{Digest32, ProtocolVersion}; |
| 4 | use axum::{ |
| 5 | extract::Json, |
| 6 | routing::{get, post}, |
| 7 | Router, |
| 8 | }; |
| 9 | use std::net::SocketAddr; |
| 10 | use tokio::net::TcpListener; |
| 11 | use uuid::Uuid; |
| 12 | |
| 13 | async fn mock_infer(Json(req): Json<InferenceRequest>) -> Json<InferenceResponse> { |
| 14 | Json(InferenceResponse { |
| 15 | request_id: req.request_id, |
| 16 | content: "HTTP server response verified".to_string(), |
| 17 | finish_reason: "stop".to_string(), |
| 18 | prompt_tokens: 10, |
| 19 | completion_tokens: 5, |
| 20 | context: req.context, |
| 21 | }) |
| 22 | } |
| 23 | |
| 24 | async fn mock_branch(Json(req): Json<BranchContextRequest>) -> Json<BranchContextReceipt> { |
| 25 | let mut child = req.parent_context.clone(); |
| 26 | child.branch_id = req.child_branch_id; |
| 27 | child.generation = child.generation.next(); |
| 28 | |
| 29 | Json(BranchContextReceipt { |
| 30 | operation_id: req.operation_id, |
| 31 | child_context: child, |
| 32 | shared_pages: 8, |
| 33 | copied_pages: 0, |
| 34 | lease_expires_at_monotonic: 123456, |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | async fn mock_capabilities() -> Json<InferenceCapabilities> { |
| 39 | Json(InferenceCapabilities { |
| 40 | protocol: ProtocolVersion::new(1, 0), |
| 41 | context_branching: true, |
| 42 | physical_cow: true, |
| 43 | streaming: false, |
| 44 | cancellation: true, |
| 45 | max_context_tokens: Some(8192), |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | #[tokio::test] |
| 50 | async fn test_http_inference_client_full_wire_lifecycle() { |
| 51 | let app = Router::new() |
| 52 | .route("/v1/chat/completions", post(mock_infer)) |
| 53 | .route("/v1/context/branch", post(mock_branch)) |
| 54 | .route("/v1/capabilities", get(mock_capabilities)); |
| 55 | |
| 56 | let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 57 | let addr: SocketAddr = listener.local_addr().unwrap(); |
| 58 | |
| 59 | tokio::spawn(async move { |
| 60 | axum::serve(listener, app).await.unwrap(); |
| 61 | }); |
| 62 | |
| 63 | let client = |
| 64 | HttpInferenceClient::new(format!("http://{}", addr), Some("test-token".to_string())); |
| 65 | |
| 66 | // 1. Test get_capabilities |
| 67 | let caps = client |
| 68 | .get_capabilities() |
| 69 | .await |
| 70 | .expect("get_capabilities failed"); |
| 71 | assert!(caps.context_branching); |
| 72 | assert!(caps.physical_cow); |
| 73 | assert_eq!(caps.max_context_tokens, Some(8192)); |
| 74 | |
| 75 | // 2. Test infer |
| 76 | let req = InferenceRequest { |
| 77 | request_id: Uuid::new_v4(), |
| 78 | model: "atlas-omni".to_string(), |
| 79 | messages: vec![InferenceMessage { |
| 80 | role: "user".to_string(), |
| 81 | content: "hello via HTTP".to_string(), |
| 82 | }], |
| 83 | context: None, |
| 84 | max_tokens: 32, |
| 85 | temperature: 0.0, |
| 86 | stop_sequences: vec![], |
| 87 | }; |
| 88 | let res = client.infer(req).await.expect("infer failed"); |
| 89 | assert_eq!(res.content, "HTTP server response verified"); |
| 90 | assert_eq!(res.completion_tokens, 5); |
| 91 | |
| 92 | // 3. Test branch_context |
| 93 | let dummy_ctx = InferenceContextRef { |
| 94 | abi_version: 1, |
| 95 | context_id: ContextId::new_v4(), |
| 96 | branch_id: BranchId::new_v4(), |
| 97 | generation: Generation(1), |
| 98 | model: ModelFingerprint { |
| 99 | weights_digest: Digest32::ZERO, |
| 100 | model_config_digest: Digest32::ZERO, |
| 101 | }, |
| 102 | tokenizer: TokenizerFingerprint { |
| 103 | tokenizer_digest: Digest32::ZERO, |
| 104 | }, |
| 105 | kv_format: KvFormatFingerprint { |
| 106 | format_version: 1, |
| 107 | dtype: "BF16".to_string(), |
| 108 | }, |
| 109 | logical_state_digest: Digest32::ZERO, |
| 110 | token_count: 100, |
| 111 | lineage: ContextLineage { |
| 112 | parent_context: None, |
| 113 | parent_branch: None, |
| 114 | parent_digest: None, |
| 115 | fork_token_index: None, |
| 116 | }, |
| 117 | isolation: CacheIsolationKey { |
| 118 | domain_id: Uuid::new_v4(), |
| 119 | domain_digest: Digest32::ZERO, |
| 120 | }, |
| 121 | binding: None, |
| 122 | recovery: ContextRecipeRef { |
| 123 | recipe_id: Uuid::new_v4(), |
| 124 | recipe_digest: Digest32::ZERO, |
| 125 | }, |
| 126 | }; |
| 127 | |
| 128 | let branch_req = BranchContextRequest { |
| 129 | operation_id: Uuid::new_v4(), |
| 130 | parent_context: dummy_ctx, |
| 131 | child_branch_id: BranchId::new_v4(), |
| 132 | isolation: CacheIsolationKey { |
| 133 | domain_id: Uuid::new_v4(), |
| 134 | domain_digest: Digest32::ZERO, |
| 135 | }, |
| 136 | }; |
| 137 | |
| 138 | let receipt = client |
| 139 | .branch_context(branch_req) |
| 140 | .await |
| 141 | .expect("branch_context failed"); |
| 142 | assert_eq!(receipt.shared_pages, 8); |
| 143 | assert_eq!(receipt.copied_pages, 0); |
| 144 | assert_eq!(receipt.child_context.generation, Generation(2)); |
| 145 | } |