STRA-338 S21: golden fixture of the pure Responses protocol (validation, prompt rendering, response trace, tool-call parsing), recorded from the code before it moves
This commit is contained in:
@@ -212,6 +212,8 @@ struct Emitter {
|
||||
events: Vec<Value>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod golden;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
//! Golden test of the pure Responses protocol: request validation, prompt rendering, the response trace and the
|
||||
//! tool-call text parser. The expected values in `tests/fixtures/golden/responses_pure.json` were generated from the code
|
||||
//! as it was before it moved from this crate to `leleka-gateway-domain` (STRA-338 S21), and are compared as text, so the
|
||||
//! JSON key order and the SSE framing are part of what is checked.
|
||||
|
||||
use super::*;
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
fn fixture_path() -> PathBuf {
|
||||
PathBuf::from(concat!(
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/tests/fixtures/golden/responses_pure.json"
|
||||
))
|
||||
}
|
||||
|
||||
fn decode(case: &Value) -> Result<CreateRequest, Value> {
|
||||
serde_json::from_value(case["request"].clone())
|
||||
.map_err(|error| json!({"decode_error": error.to_string()}))
|
||||
}
|
||||
|
||||
fn item_json(item: &OutputItem) -> Value {
|
||||
match item {
|
||||
OutputItem::Reasoning { deltas, complete } => {
|
||||
json!({"type": "reasoning", "deltas": deltas, "complete": complete})
|
||||
}
|
||||
OutputItem::Text { deltas } => json!({"type": "text", "deltas": deltas}),
|
||||
OutputItem::FunctionCall {
|
||||
name,
|
||||
call_id,
|
||||
argument_deltas,
|
||||
} => {
|
||||
json!({"type": "function_call", "name": name, "call_id": call_id, "argument_deltas": argument_deltas})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn render_case(case: &Value) -> Value {
|
||||
let request = match decode(case) {
|
||||
Ok(request) => request,
|
||||
Err(error) => return error,
|
||||
};
|
||||
match render_request(&request, &tests::test_marks()) {
|
||||
Ok((prompt, images)) => json!({"prompt": prompt, "images": images}),
|
||||
Err(error) => json!({"error": error}),
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_case(case: &Value) -> Value {
|
||||
let request = match decode(case) {
|
||||
Ok(request) => request,
|
||||
Err(error) => return error,
|
||||
};
|
||||
match request.validate() {
|
||||
Ok(()) => json!({"ok": true}),
|
||||
Err((param, message)) => json!({"param": param, "message": message}),
|
||||
}
|
||||
}
|
||||
|
||||
fn trace_case(case: &Value) -> Value {
|
||||
let request = match decode(case) {
|
||||
Ok(request) => request,
|
||||
Err(error) => return error,
|
||||
};
|
||||
let items: Vec<OutputItem> = serde_json::from_value(case["items"].clone()).unwrap();
|
||||
let usage: Usage = serde_json::from_value(case["usage"].clone()).unwrap();
|
||||
let incomplete = case["incomplete"].as_bool().unwrap();
|
||||
let built = ResponseTrace::build_with_terminal(
|
||||
&request,
|
||||
&items,
|
||||
usage,
|
||||
"resp_golden_1",
|
||||
1_700_000_000,
|
||||
1_700_000_007,
|
||||
incomplete,
|
||||
);
|
||||
match built {
|
||||
Ok(trace) => json!({"response": trace.response, "frames": trace.sse_frames().concat()}),
|
||||
Err(error) => json!({"error": error}),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_case(case: &Value) -> Value {
|
||||
let request = match decode(case) {
|
||||
Ok(request) => request,
|
||||
Err(error) => return error,
|
||||
};
|
||||
let output = case["output"].as_str().unwrap().to_owned();
|
||||
let natural_stop = case["natural_stop"].as_bool().unwrap();
|
||||
match parse_output_items_after(&request, output, "resp_golden_1", natural_stop) {
|
||||
Ok(items) => json!({"items": items.iter().map(item_json).collect::<Vec<_>>()}),
|
||||
Err(error) => json!({"error": error}),
|
||||
}
|
||||
}
|
||||
|
||||
const SECTIONS: [(&str, fn(&Value) -> Value); 4] = [
|
||||
("render", render_case),
|
||||
("validate", validate_case),
|
||||
("trace", trace_case),
|
||||
("parse", parse_case),
|
||||
];
|
||||
|
||||
fn load() -> Value {
|
||||
serde_json::from_str(&fs::read_to_string(fixture_path()).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pure_protocol_output_matches_the_recorded_golden() {
|
||||
let golden = load();
|
||||
let mut checked = 0;
|
||||
for (section, compute) in SECTIONS {
|
||||
for case in golden[section].as_array().unwrap() {
|
||||
let actual = compute(case).to_string();
|
||||
let expected = case["expected"].to_string();
|
||||
assert_eq!(actual, expected, "{section}: {}", case["name"]);
|
||||
checked += 1;
|
||||
}
|
||||
}
|
||||
assert!(checked >= 90, "only {checked} golden cases");
|
||||
}
|
||||
|
||||
/// Rewrites the expected values from the current code. Only for a deliberate change of the protocol output.
|
||||
#[test]
|
||||
#[ignore = "regenerates tests/fixtures/golden/responses_pure.json"]
|
||||
fn regenerate() {
|
||||
let mut golden = load();
|
||||
for (section, compute) in SECTIONS {
|
||||
for case in golden[section].as_array_mut().unwrap() {
|
||||
let expected = compute(case);
|
||||
case["expected"] = expected;
|
||||
}
|
||||
}
|
||||
fs::write(
|
||||
fixture_path(),
|
||||
serde_json::to_string_pretty(&golden).unwrap() + "\n",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
+3111
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user