use serde_json::{Value, json};
use crate::render::{Form, rendered, tail};
use crate::reply::stream::Stream;
#[track_caller]
fn says(frame: &Value, needles: &[&str]) {
let said = rendered(frame);
for needle in needles {
assert!(said.contains(needle), "{frame}\n-> {said}");
}
}
#[test]
fn a_follow_frame_is_the_text_that_landed_or_a_heartbeat() {
let frame = |stream| json!({"ok": true, "kind": "follow", "tools": [], "stream": stream});
assert_eq!(
rendered(&frame(json!({"delta": "text", "text": "391"}))),
"391"
);
assert_eq!(
rendered(&frame(json!({"delta": "thinking", "thinking": "counting"}))),
"(thinking) counting"
);
assert_eq!(
rendered(&frame(json!({"delta": "thinking"}))),
"┊ … thinking"
);
assert_eq!(rendered(&frame(json!({}))), "┊ …");
}
#[test]
fn a_follow_frame_says_what_is_running_where_and_how_it_ended() {
says(
&json!({"ok": true, "kind": "follow", "stream": {"delta": "text", "text": "on it"},
"tools": [
{"tool_use": "toolu_01", "tool": "box2_Bash",
"input": "{\"command\":\"hostname && uptime\"}"}]}),
&["on it", "┊ running", "box2_Bash", "hostname && uptime"],
);
says(
&json!({"ok": true, "kind": "follow", "stream": {},
"tools": [{"tool_use": "toolu_01", "tool": "box2_Bash",
"input": "{}", "exit_code": 2}]}),
&["┊ exit 2", "box2_Bash"],
);
assert_eq!(
rendered(&json!({"ok": true, "kind": "follow", "tools": [],
"stream": {"delta": "thinking"}})),
"┊ … thinking"
);
}
#[test]
fn a_closing_entry_read_alone_is_named_by_its_invocation() {
says(
&json!({"ok": true, "kind": "follow", "stream": {},
"tools": [{"tool_use": "toolu_01", "exit_code": 0}]}),
&["exit 0", "toolu_01"],
);
}
#[test]
fn a_held_read_names_the_call_that_landed_out_of_the_fold() {
let mut fold = Stream::default();
let opened = tail(
&json!({"ok": true, "kind": "follow", "stream": {"delta": "text", "text": "running it"},
"tools": [{"tool_use": "toolu_01", "tool": "box2_Bash",
"input": "{\"command\":\"uptime\"}"}]}),
&mut fold,
Form::Rendered,
);
assert!(opened.contains("running"), "{opened}");
assert!(opened.contains("box2_Bash"), "{opened}");
let landed = tail(
&json!({"ok": true, "kind": "follow", "stream": {},
"tools": [{"tool_use": "toolu_01", "exit_code": 0}]}),
&mut fold,
Form::Rendered,
);
assert!(landed.contains("exit 0"), "{landed}");
assert!(
landed.contains("box2_Bash"),
"the closing entry restates no name, so the fold must: {landed}"
);
assert!(
!landed.contains("running it"),
"the prose is the APPEND and never the accumulation: {landed}"
);
}
#[test]
fn a_held_read_says_a_refusal_and_prints_the_frame_where_asked() {
let mut fold = Stream::default();
let refusal = json!({"ok": false, "error": "unknown conversation"});
assert!(
tail(&refusal, &mut fold, Form::Rendered).contains("unknown conversation"),
"a refusal mid-stream is the engine answering"
);
let unreadable = json!({"ok": true, "kind": "follow"});
assert!(
tail(&unreadable, &mut fold, Form::Rendered).contains("cannot read"),
"and a frame with no fold in it is this seat's own statement"
);
assert_eq!(
tail(&refusal, &mut fold, Form::Json),
refusal.to_string(),
"the machine form is the frame, byte for byte"
);
}
#[test]
fn a_follow_frame_says_a_call_the_boundary_parked_and_why() {
says(
&json!({"ok": true, "kind": "follow", "stream": {},
"tools": [{"tool_use": "toolu_02", "tool": "box2_service_status",
"held": "box2_service_status {} classified opaque"}]}),
&["┊ held", "box2_service_status", "classified opaque"],
);
}
#[test]
fn an_answered_park_reads_as_one_call_going_held_then_posted_then_complete() {
let mut fold = Stream::default();
let parked = tail(
&json!({"ok": true, "kind": "follow", "stream": {},
"tools": [{"tool_use": "toolu_02", "tool": "box2_service_status",
"held": "classified opaque"}]}),
&mut fold,
Form::Rendered,
);
assert!(parked.contains("held"), "{parked}");
let ran = tail(
&json!({"ok": true, "kind": "follow", "stream": {},
"tools": [{"tool_use": "toolu_02", "exit_code": 0}]}),
&mut fold,
Form::Rendered,
);
assert!(ran.contains("exit 0"), "{ran}");
assert!(
ran.contains("box2_service_status"),
"the closing entry restates no name, so the fold must: {ran}"
);
}