File size: 2,162 Bytes
2109241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18de23f
2109241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Consumer checks for the safe JSON service and actual allocated C ABI strings.

use std::ffi::CStr;

use lispm_llm_native_api::{
    lispm_llm_abi_version, lispm_llm_request, lispm_llm_string_free, request,
};
use serde_json::{Value, json};

/// Malformed operations never cross into project filesystem mutation.
#[test]
fn strict_protocol_rejects_unknown_fields_and_relative_paths() -> Result<(), serde_json::Error> {
    for input in [
        r#"{"op":"unknown"}"#,
        r#"{"op":"version","extra":true}"#,
        r#"{"op":"project-info","project":"relative"}"#,
        r#"{"op":"project-info","project":"/tmp","project":"/other"}"#,
        r#"{"op":"job-poll","job":-1}"#,
        r#"{"op":"project-create","project":"/tmp/p","base_model":"/tmp/m","training_data":"/tmp/t","benchmark_data":"/tmp/b","task_suite":{"data":"relative","runner":"/tmp/r","emacs":"/bin/emacs","timeout_seconds":60}}"#,
    ] {
        let response: Value = serde_json::from_str(&request(input))?;
        assert_eq!(response.get("ok"), Some(&json!(false)), "{input}");
    }
    Ok(())
}

/// The exported ABI owns response allocation and handles null/invalid UTF-8 inputs.
#[test]
fn c_abi_allocations_round_trip_without_emacs() -> Result<(), serde_json::Error> {
    assert_eq!(lispm_llm_abi_version(), 1);
    for (input, valid) in [
        (c"{\"op\":\"version\"}".as_ptr(), true),
        (std::ptr::null(), false),
        (c"\xff".as_ptr(), false),
    ] {
        // SAFETY: each input is null or a static readable NUL-terminated string.
        let pointer = unsafe { lispm_llm_request(input) };
        assert!(!pointer.is_null());
        // SAFETY: the ABI returns an owned NUL-terminated allocation.
        let bytes = unsafe { CStr::from_ptr(pointer) }.to_bytes().to_owned();
        // SAFETY: this is the exact live response pointer, released once.
        unsafe { lispm_llm_string_free(pointer) };
        let response: Value = serde_json::from_slice(&bytes)?;
        assert_eq!(response.get("ok"), Some(&json!(valid)));
    }
    // SAFETY: null release is explicitly allowed.
    unsafe { lispm_llm_string_free(std::ptr::null_mut()) };
    Ok(())
}