use serde_json::json;
use super::{Window, fold, windows};
fn read(value: &serde_json::Value) -> Result<Vec<Window>, String> {
windows(value.as_object().expect("an object"))
}
#[test]
fn the_two_transitions_are_read_as_upstream_spells_them() {
let entries = read(&json!({"tools": [
{"tool_use": "toolu_01", "tool": "box2_Bash",
"input": "{\"command\":\"hostname && uptime\"}"},
{"tool_use": "toolu_01", "exit_code": 0},
]}))
.expect("a window");
assert_eq!(
entries,
vec![
Window {
tool_use: "toolu_01".to_owned(),
tool: Some("box2_Bash".to_owned()),
input: Some("{\"command\":\"hostname && uptime\"}".to_owned()),
exit_code: None,
},
Window {
tool_use: "toolu_01".to_owned(),
tool: None,
input: None,
exit_code: Some(0),
},
]
);
}
#[test]
fn an_absent_exit_code_is_in_flight_and_never_a_zero() {
let entries = read(&json!({"tools": [
{"tool_use": "a", "tool": "Bash", "input": "{}"},
{"tool_use": "b", "tool": "Bash", "input": "{}", "exit_code": 0},
]}))
.expect("a window");
assert_eq!(entries[0].exit_code, None);
assert_eq!(entries[1].exit_code, Some(0));
}
#[test]
fn the_two_halves_fold_onto_one_call() {
let mut held = Vec::new();
for entry in read(&json!({"tools": [
{"tool_use": "toolu_01", "tool": "box2_Bash", "input": "{}"},
{"tool_use": "toolu_02", "tool": "Read", "input": "{}"},
{"tool_use": "toolu_01", "exit_code": 2},
]}))
.expect("a window")
{
fold(&mut held, entry);
}
assert_eq!(held.len(), 2, "{held:?}");
assert_eq!(held[0].tool.as_deref(), Some("box2_Bash"));
assert_eq!(held[0].exit_code, Some(2));
assert_eq!(held[1].exit_code, None, "the other call is still in flight");
}
#[test]
fn a_later_transition_fills_and_never_blanks() {
let mut held = Vec::new();
fold(
&mut held,
Window {
tool_use: "x".to_owned(),
tool: Some("Bash".to_owned()),
input: Some("{}".to_owned()),
exit_code: Some(1),
},
);
fold(
&mut held,
Window {
tool_use: "x".to_owned(),
tool: None,
input: None,
exit_code: None,
},
);
assert_eq!(
held,
vec![Window {
tool_use: "x".to_owned(),
tool: Some("Bash".to_owned()),
input: Some("{}".to_owned()),
exit_code: Some(1),
}]
);
}
#[test]
fn an_empty_window_reads_as_a_window_with_nothing_in_it() {
assert_eq!(read(&json!({"tools": []})).expect("a window"), Vec::new());
}