LINE1 10 20 30 40 50 60 70 80 90 1001234567use std::sync::{Arc, OnceLock};89use local_decisions::config::Config;10use local_decisions::decisions::{self, AppState, ExtractRequest};11use local_decisions::engine::Engine;12use local_decisions::server;13use serde_json::{Value, json};1415static ENGINE: OnceLock<Option<Arc<Engine>>> = OnceLock::new();1617fn checkpoint_available(config: &Config) -> bool {18 let required = [19 "config.json",20 "encoder_config/config.json",21 "tokenizer.json",22 "model.safetensors",23 ];24 required25 .iter()26 .all(|file| std::fs::metadata(config.model_dir.join(file)).is_ok_and(|meta| meta.len() > 0))27}2829fn engine() -> Option<Arc<Engine>> {30 ENGINE31 .get_or_init(|| {32 let config = Config::from_env().expect("configuration");33 if !checkpoint_available(&config) {34 eprintln!("skipping: no checkpoint in {}", config.model_dir.display());35 return None;36 }37 match actix_web::rt::System::new().block_on(Engine::load(&config)) {38 Ok(engine) => Some(Arc::new(engine)),39 Err(error) => panic!("cannot load the model: {error}"),40 }41 })42 .clone()43}4445async fn engine_async() -> Option<Arc<Engine>> {46 if let Some(loaded) = ENGINE.get() {47 return loaded.clone();48 }49 let config = Config::from_env().expect("configuration");50 if !checkpoint_available(&config) {51 return None;52 }53 let loaded = Some(Arc::new(Engine::load(&config).await.expect("engine")));54 let _ = ENGINE.set(loaded.clone());55 loaded56}5758fn state(engine: Arc<Engine>) -> AppState {59 AppState {60 engine,61 gate: std::sync::Mutex::new(()),62 }63}6465fn extract(state: &AppState, body: Value) -> Value {66 let request: ExtractRequest = serde_json::from_value(body).expect("request");67 decisions::extract(state, &request.text, &request).expect("extraction")68}6970#[test]71fn properties_become_values() {72 let Some(engine) = engine() else { return };73 let state = state(engine);7475 let answer = extract(76 &state,77 json!({78 "text": "the tool costs 2000 and you need to pay taxes of 210",79 "properties": ["taxes", "costs"]80 }),81 );8283 84 assert!(answer.is_object(), "{answer}");85 let object = answer.as_object().expect("object");86 assert_eq!(object.len(), 2, "{answer}");87 assert!(88 object.contains_key("taxes") && object.contains_key("costs"),89 "{answer}"90 );91 for value in object.values() {92 assert!(value.is_string() || value.is_null(), "{answer}");93 }94}9596#[test]97fn confidences_and_offsets_are_opt_in() {98 let Some(engine) = engine() else { return };99 let state = state(engine);100 let answer = extract(101 &state,102 json!({103 "text": "Berlin is the capital of Germany.",104 "properties": ["country"],105 "include_confidence": true,106 "include_spans": true107 }),108 );109 let value = &answer["country"];110 if value.is_null() {111 return;112 }113 let confidence = value["confidence"].as_f64().expect("confidence");114 let start = value["start"].as_u64().expect("start");115 let end = value["end"].as_u64().expect("end");116 assert!((0.0..=1.0).contains(&confidence), "{answer}");117 assert!(start < end, "{answer}");118}119120#[test]121fn list_fields_collect_every_span() {122 let Some(engine) = engine() else { return };123 let state = state(engine);124 let answer = extract(125 &state,126 json!({127 "text": "Acme Corp hired Dana Reed as the new CTO in Berlin.",128 "properties": [{ "name": "company", "dtype": "list" }],129 "threshold": 0.3130 }),131 );132 assert!(answer["company"].is_array(), "{answer}");133}134135#[test]136fn structures_are_answered_by_name() {137 let Some(engine) = engine() else { return };138 let state = state(engine);139 let answer = extract(140 &state,141 json!({142 "text": "Invoice 1842 was paid on 4 May 2026.",143 "structures": { "invoice": ["number", "date"] }144 }),145 );146 let instances = answer["invoice"].as_array().expect("array of instances");147 if let Some(first) = instances.first() {148 assert!(first.is_object(), "{first}");149 }150}151152#[test]153fn malformed_extraction_requests_are_rejected() {154 let Some(engine) = engine() else { return };155 let state = state(engine);156 for body in [157 json!({ "text": "hello", "properties": [] }),158 json!({ "text": " ", "properties": ["a"] }),159 json!({ "text": "hello", "properties": [""] }),160 json!({ "text": "hello", "properties": ["a"], "threshold": 4 }),161 json!({ "text": "hello", "properties": [{ "name": "a", "dtype": "int" }] }),162 ] {163 let request: ExtractRequest = serde_json::from_value(body.clone()).expect("request");164 assert!(165 decisions::extract(&state, &request.text, &request).is_err(),166 "{body} should be rejected"167 );168 }169}170171#[actix_web::test]172async fn extract_over_http_returns_json() {173 let Some(engine) = engine_async().await else {174 return;175 };176 let state = Arc::new(state(engine));177 let app = actix_web::test::init_service(178 actix_web::App::new()179 .app_data(actix_web::web::Data::from(state.clone()))180 .configure(|config| server::configure(config, None)),181 )182 .await;183184 let request = actix_web::test::TestRequest::post()185 .uri("/v1/extract")186 .set_json(json!({187 "text": "the tool costs 2000 and you need to pay taxes of 210",188 "properties": ["taxes", "costs"]189 }))190 .to_request();191 let response = actix_web::test::call_service(&app, request).await;192 assert!(response.status().is_success());193 let body: Value = actix_web::test::read_body_json(response).await;194 assert!(body.is_object(), "{body}");195 assert_eq!(196 body.as_object().map(serde_json::Map::len),197 Some(2),198 "{body}"199 );200201 let invalid = actix_web::test::TestRequest::post()202 .uri("/v1/extract")203 .set_json(json!({ "text": "hello" }))204 .to_request();205 let response = actix_web::test::call_service(&app, invalid).await;206 assert_eq!(response.status(), actix_web::http::StatusCode::BAD_REQUEST);207}