use std::collections::HashMap;
use serde_json::Value;
use super::{events, history, types};
use crate::session::{Event, Status};
use crate::turn::{Delta, Item, ToolDetail, ToolStatus};
fn capture() -> Value {
serde_json::from_str(include_str!("codex-observation-0.159.0.json")).unwrap()
}
#[test]
fn stored_descendants_retain_native_ancestry_in_source_metadata() {
let capture = capture();
let threads = capture["threads"].as_array().unwrap();
for (raw, id, parent, depth) in [
(&threads[1], "child", "root", 1),
(&threads[2], "grandchild", "child", 2),
] {
let thread: types::Thread = serde_json::from_value(raw.clone()).unwrap();
assert_eq!(thread.id, id);
assert_eq!(thread.model.as_deref(), Some("gpt-6-luna"));
assert_eq!(
thread
.source
.pointer("/subAgent/thread_spawn/parent_thread_id"),
Some(&Value::String(parent.into()))
);
assert_eq!(
thread.source.pointer("/subAgent/thread_spawn/depth"),
Some(&Value::from(depth))
);
assert_eq!(history::summary(thread).status, Status::NotLoaded);
}
}
#[test]
fn child_streams_keep_turn_ids_and_unknown_activity_payloads() {
let capture = capture();
let mut text_by_thread = HashMap::<String, String>::new();
let mut finished_threads = Vec::new();
let mut activity_count = 0;
for notification in capture["notifications"].as_array().unwrap() {
let method = notification["method"].as_str().unwrap();
let params = ¬ification["params"];
let thread_id = params["threadId"].as_str().unwrap();
match events::from_notification(method, params.clone()) {
Event::ItemDelta {
turn_id,
item_id,
delta: Delta::Text { text },
} => {
assert_eq!(Some(turn_id.as_str()), params["turnId"].as_str());
assert_eq!(Some(item_id.as_str()), params["itemId"].as_str());
text_by_thread
.entry(thread_id.into())
.or_default()
.push_str(&text);
}
Event::ItemEnd {
item: Item::Other { kind, raw, .. },
..
} if kind == "subAgentActivity" || kind == "collabAgentToolCall" => {
assert_eq!(raw, params["item"]);
activity_count += 1;
}
Event::TurnEnd { turn } => {
assert_eq!(Some(turn.id.as_str()), params["turn"]["id"].as_str());
assert!(turn.items.is_empty(), "final items must not be repeated");
finished_threads.push(thread_id);
}
_ => {}
}
}
for thread_id in ["root", "child", "grandchild"] {
assert!(text_by_thread[thread_id].contains("nested-ok"));
assert!(finished_threads.contains(&thread_id));
}
assert!(activity_count > 0);
}
#[test]
fn managed_command_outlives_its_root_turn_and_reports_native_termination() {
let capture = capture();
let notifications = capture["terminalNotifications"].as_array().unwrap();
let Event::ItemStart {
item: Item::ToolCall(started),
..
} = events::from_notification("item/started", notifications[0]["params"].clone())
else {
panic!("expected a running command item");
};
assert_eq!(started.status, ToolStatus::InProgress);
let Event::TurnEnd { turn } =
events::from_notification("turn/completed", notifications[1]["params"].clone())
else {
panic!("expected the root turn to finish before the command");
};
assert_eq!(turn.status, crate::turn::Status::Completed);
let notification = ¬ifications[2];
let params = ¬ification["params"];
let Event::ItemEnd {
item: Item::ToolCall(call),
..
} = events::from_notification("item/completed", params.clone())
else {
panic!("expected a mapped command item");
};
assert_eq!(call.id, started.id);
assert_eq!(call.status, ToolStatus::Failed);
let ToolDetail::Command {
command, exit_code, ..
} = call.detail
else {
panic!("expected command details");
};
assert!(command.contains("sleep 60"));
assert_eq!(exit_code, Some(-1));
assert_eq!(
params["item"]["processId"],
capture["terminalList"]["data"][0]["processId"]
);
}