use super::{Delta, Stream, follow};
use serde_json::{Value, json};
fn read(v: &Value) -> Result<Stream, String> {
follow(v.as_object().expect("an object"))
}
#[test]
fn a_frame_carries_the_whole_accumulated_fold() {
assert_eq!(
read(
&json!({"ok": true, "kind": "follow", "tools": [], "stream": {
"thinking": "weighing the two seams",
"text": "the seam is real",
"delta": "text",
}})
)
.expect("a fold"),
Stream {
text: Some("the seam is real".to_owned()),
thinking: Some("weighing the two seams".to_owned()),
last_delta: Some(Delta::Text),
tools: Vec::new(),
}
);
}
#[test]
fn an_empty_fold_is_waiting_and_not_an_empty_answer() {
assert_eq!(
read(&json!({"stream": {}, "tools": []})).expect("a fold"),
Stream::default()
);
let thinking_only =
read(&json!({"stream": {"thinking": "…", "delta": "thinking"}, "tools": []}))
.expect("a fold");
assert_eq!(thinking_only.text, None);
assert_eq!(thinking_only.last_delta, Some(Delta::Thinking));
}
#[test]
fn an_unknown_delta_keeps_the_turn_rather_than_the_strictness() {
let read = read(&json!({"stream": {"text": "so far", "delta": "summary"}, "tools": []}))
.expect("a fold");
assert_eq!(read.text.as_deref(), Some("so far"));
assert_eq!(
read.last_delta,
Some(Delta::Unknown("summary".to_owned())),
"the turn survives the word"
);
for (delta, word) in [
(Delta::Text, "text"),
(Delta::Thinking, "thinking"),
(Delta::Unknown("summary".to_owned()), "summary"),
] {
assert_eq!(delta.label(), word);
}
}
#[test]
fn a_frame_with_no_fold_in_it_refuses() {
for (frame, said) in [
(json!({"ok": true, "kind": "follow"}), "\"stream\""),
(json!({"stream": [], "tools": []}), "\"stream\""),
(json!({"stream": {"text": 7}, "tools": []}), "\"text\""),
(json!({"stream": {}}), "\"tools\""),
(json!({"stream": {}, "tools": {}}), "\"tools\""),
(
json!({"stream": {}, "tools": ["toolu_01"]}),
"not a JSON object",
),
(json!({"stream": {}, "tools": [{}]}), "\"tool_use\""),
] {
let refusal = read(&frame).expect_err("refused");
assert!(refusal.contains(said), "{frame}: {refusal}");
}
}
#[test]
fn absorbing_the_append_is_reading_the_whole() {
let split = [
json!({"stream": {"thinking": "weighing ", "delta": "thinking"}, "tools": []}),
json!({"stream": {"text": "the answer ", "delta": "text"}, "tools": []}),
json!({"stream": {"text": "so far.", "delta": "text"}, "tools": []}),
];
let mut fold = Stream::default();
for frame in &split {
fold.absorb(read(frame).expect("a fold"));
}
assert_eq!(
fold,
read(&json!({"tools": [], "stream": {"thinking": "weighing ",
"text": "the answer so far.", "delta": "text"}}))
.expect("a fold"),
"fold(a).absorb(fold(b)) == fold(a ++ b)"
);
}
#[test]
fn a_frame_that_appended_nothing_leaves_the_fold_standing() {
let mut fold = Stream::default();
fold.absorb(
read(&json!({"stream": {"text": "said", "delta": "text"}, "tools": []})).expect("a fold"),
);
let whole = fold.clone();
fold.absorb(read(&json!({"stream": {}, "tools": []})).expect("a fold"));
assert_eq!(fold, whole, "an empty append is not a reset");
assert_eq!(fold.last_delta, Some(Delta::Text));
assert_eq!(fold.thinking, None, "and silence is not an empty string");
}