#![deny(unsafe_code)]
use super::*;
fn read(json: &str) -> Option<Summary> {
Atif.read(&crate::json::parse(json.as_bytes()).expect("fixture parses"))
}
fn step(id: u32, source: &str, message: &str, rest: &str) -> String {
format!(
r#"{{"step_id":{id},"source":"{source}","timestamp":"2026-04-23T10:55:31.123456+00:00",
"message":"{message}"{}{rest}}}"#,
if rest.is_empty() { "" } else { "," }
)
}
#[test]
fn the_documents_other_keys_are_a_session_row() {
let sum = read(
r#"{"schema_version":"ATIF-v1.7","session_id":"sxs_abc",
"agent":{"name":"opencode","version":"1.2.3","model_name":"vendor/model"}}"#,
)
.expect("recognised");
assert_eq!((sum.class, sum.who), (Class::Message, Who::System));
assert_eq!(sum.actor, "session");
assert_eq!(sum.time, None);
assert_eq!(sum.what, "ATIF-v1.7 \u{b7} opencode 1.2.3 \u{b7} vendor/model \u{b7} sxs_abc");
}
#[test]
fn a_stranger_at_the_top_level_is_left_to_the_generic_tree() {
assert!(read(r#"{"title":"notes","items":[1,2]}"#).is_none());
assert!(read("[1,2,3]").is_none());
assert!(read("42").is_none());
}
#[test]
fn a_partial_envelope_still_reads() {
let sum = read(r#"{"session_id":"sxs_1"}"#).expect("recognised");
assert_eq!(sum.what, "sxs_1");
let sum = read(r#"{"schema_version":"ATIF-v1.7","agent":{}}"#).expect("recognised");
assert_eq!(sum.what, "ATIF-v1.7 \u{b7} agent");
}
#[test]
fn a_step_that_says_something_is_a_message() {
let sum = read(&step(2, "user", "build me a reader\\nplease", "")).expect("recognised");
assert_eq!((sum.class, sum.who), (Class::Message, Who::User));
assert_eq!(sum.actor, "user");
assert_eq!(sum.time.as_deref(), Some("10:55"));
assert_eq!(sum.what, "build me a reader please");
assert_eq!(sum.calls, 0);
}
#[test]
fn an_agent_step_is_the_assistant() {
let sum = read(&step(3, "agent", "Here is the plan.", "")).expect("recognised");
assert_eq!((sum.class, sum.who), (Class::Message, Who::Assistant));
assert_eq!(sum.actor, "assistant");
}
#[test]
fn a_message_collapses_its_calls_to_a_count() {
let sum = read(&step(
4,
"agent",
"Checking the tree.",
r#""reasoning_content":"weighing it up",
"tool_calls":[
{"function_name":"bash","arguments":{"command":"git status"},"tool_call_id":"c1"},
{"function_name":"read","arguments":{"filePath":"/a/b.rs"},"tool_call_id":"c2"}]"#,
))
.expect("recognised");
assert_eq!(sum.class, Class::Message);
assert_eq!(sum.what, "Checking the tree. \u{b7} thinking \u{b7} 2 tool calls");
assert_eq!(sum.calls, 2);
let one = read(&step(
5,
"agent",
"Looking.",
r#""tool_calls":[{"function_name":"bash","arguments":{"command":"ls"}}]"#,
))
.expect("recognised");
assert_eq!(one.what, "Looking. \u{b7} 1 tool call");
}
#[test]
fn a_blank_message_is_mechanics_not_speech() {
let sum = read(&step(6, "agent", " ", r#""reasoning_content":"hm""#)).expect("recognised");
assert_eq!(sum.class, Class::Step);
assert_eq!(sum.what, "thinking");
}
#[test]
fn a_call_names_its_argument_and_its_answer() {
let sum = read(&step(
7,
"agent",
"",
r#""tool_calls":[{"function_name":"bash","arguments":{"command":"make -j8"},
"tool_call_id":"c1"}],
"observation":{"results":[{"source_call_id":"c1","content":"a\nb\nc"}]}"#,
))
.expect("recognised");
assert_eq!((sum.class, sum.who), (Class::Step, Who::Tool));
assert_eq!(sum.actor, "tool");
assert_eq!(sum.what, "bash(make -j8) \u{2192} 3 lines");
assert_eq!(sum.calls, 1);
assert_eq!(sum.time.as_deref(), Some("10:55"));
}
#[test]
fn the_argument_shown_is_the_one_that_says_what_happened() {
let one = |args: &str| {
read(&step(
8,
"agent",
"",
&format!(r#""tool_calls":[{{"function_name":"t","arguments":{args}}}]"#),
))
.expect("recognised")
.what
};
assert_eq!(one(r#"{"path":"/x","pattern":"*.rs"}"#), "t(*.rs)");
assert_eq!(one(r#"{"filePath":"/x/y","limit":40}"#), "t(/x/y)");
assert_eq!(one(r#"{"url":"https://x/y"}"#), "t(https://x/y)");
assert_eq!(one(r#"{"query":"who"}"#), "t(who)");
assert_eq!(one(r#"{"todos":[{"text":"a"}]}"#), "t()");
assert_eq!(one(r#"{"command":41}"#), "t()");
assert_eq!(one("{}"), "t()");
assert_eq!(one(r#""{\"command\":\"ls -l\"}""#), "t(ls -l)");
assert_eq!(one(r#""not json at all""#), "t()");
}
#[test]
fn adjacent_identical_entries_collapse_to_a_count() {
let sum = read(&step(
9,
"agent",
"",
r#""tool_calls":[
{"function_name":"bash","arguments":{"command":"pkg-config --exists onig"}},
{"function_name":"bash","arguments":{"command":"pkg-config --exists onig"}},
{"function_name":"read","arguments":{"filePath":"/a"}}]"#,
))
.expect("recognised");
assert_eq!(
sum.what,
"bash(pkg-config --exists onig) \u{d7}2 \u{b7} read(/a)"
);
assert_eq!(sum.calls, 3);
}
#[test]
fn a_thought_and_the_call_it_led_to_share_a_row() {
let sum = read(&step(
10,
"agent",
"",
r#""reasoning_content":"try pkg-config",
"tool_calls":[{"function_name":"bash","arguments":{"command":"ls"}}]"#,
))
.expect("recognised");
assert_eq!(sum.what, "thinking \u{b7} bash(ls)");
}
#[test]
fn an_orphan_result_is_counted_and_an_unanswered_call_is_bare() {
let sum = read(&step(
11,
"agent",
"",
r#""tool_calls":[{"function_name":"bash","arguments":{"command":"ls"},
"tool_call_id":"c1"}],
"observation":{"results":[{"source_call_id":"zz","content":"x"}]}"#,
))
.expect("recognised");
assert_eq!(sum.what, "bash(ls) \u{b7} 1 result");
let only = read(&step(
12,
"agent",
"",
r#""observation":{"results":[{"source_call_id":"a","content":"x"},
{"source_call_id":"b","content":"y"}]}"#,
))
.expect("recognised");
assert_eq!(only.what, "2 results");
assert_eq!((only.who, only.actor.as_str()), (Who::Assistant, "assistant"));
}
#[test]
fn a_result_says_how_much_came_back() {
let one = |content: &str| {
read(&step(
13,
"agent",
"",
&format!(
r#""tool_calls":[{{"function_name":"t","arguments":{{"command":"c"}},
"tool_call_id":"c1"}}],
"observation":{{"results":[{{"source_call_id":"c1","content":{content}}}]}}"#
),
))
.expect("recognised")
.what
};
assert_eq!(one(r#""a\nb""#), "t(c) \u{2192} 2 lines");
assert_eq!(one(r#""hello""#), "t(c) \u{2192} 5 bytes");
assert_eq!(one(r#""""#), "t(c) \u{2192} empty");
assert_eq!(one("null"), "t(c) \u{2192} ok", "null reads as absent, as it does everywhere here");
assert_eq!(one(r#"{"a":1}"#), "t(c) \u{2192} 7 bytes");
}
#[test]
fn the_opening_step_has_no_clock_and_none_is_invented() {
let sum = read(r#"{"step_id":1,"source":"user","message":"do the thing"}"#).expect("read");
assert_eq!(sum.time, None);
assert_eq!((sum.class, sum.who), (Class::Message, Who::User));
assert_eq!(sum.what, "do the thing");
}
#[test]
fn null_and_empty_mean_the_same_as_absent() {
for rest in [
r#""tool_calls":null,"observation":null,"reasoning_content":null"#,
r#""tool_calls":[],"observation":{},"reasoning_content":"""#,
r#""tool_calls":[],"observation":{"results":[]}"#,
] {
let sum = read(&step(14, "agent", "", rest)).expect("recognised");
assert_eq!(sum.what, "(no content)", "{rest}");
assert_eq!(sum.calls, 0);
}
}
#[test]
fn a_broken_timestamp_is_no_clock_rather_than_a_wrong_one() {
for ts in [r#""2026-04-23T10:€z""#, r#""nope""#, r#""""#, "12345", "null"] {
let json = format!(r#"{{"step_id":1,"source":"agent","message":"x","timestamp":{ts}}}"#);
assert_eq!(read(&json).expect("recognised").time, None, "{ts}");
}
}
#[test]
fn an_unknown_source_still_reads_and_a_scalar_step_does_not() {
let sum = read(r#"{"step_id":9,"source":"orchestrator","message":"hi"}"#).expect("read");
assert_eq!((sum.who, sum.actor.as_str()), (Who::Assistant, "assistant"));
assert!(read(r#"{"note":"stray"}"#).is_none());
}
#[test]
fn a_huge_step_is_still_one_line() {
let long = "x".repeat(4000);
let sum = read(&step(
15,
"agent",
&long,
&format!(r#""tool_calls":[{{"function_name":"bash","arguments":{{"command":"{long}"}}}}]"#),
))
.expect("recognised");
assert!(crate::render::str_width(&sum.what) <= EXCERPT + ARG + 32, "{}", sum.what);
assert!(sum.what.contains('\u{2026}'));
}
#[test]
fn the_dialect_says_its_records_are_the_steps_of_a_document() {
assert_eq!(Atif.records_at(), RecordsAt::Member("steps"));
assert_eq!(Atif.name(), "atif");
assert!(super::super::exists("atif"));
assert!(super::super::list_text().contains("atif"));
}
#[test]
fn a_message_carries_its_text_as_a_body_and_a_step_does_not() {
let sum = read(&step(3, "user", "first line\\nsecond line", "")).expect("recognised");
let body = sum.body.expect("a message has a body");
assert_eq!(body.head, "first line\nsecond line");
assert_eq!(body.lines, 2);
assert!(body.whole());
assert_eq!(body.at, vec![crate::lens::Step::Key("message")]);
let mechanics = read(&step(
4,
"agent",
"",
r#""tool_calls":[{"function_name":"bash","arguments":{"command":"ls"}}]"#,
))
.expect("recognised");
assert!(mechanics.body.is_none(), "mechanics stay one line");
assert!(read(r#"{"schema_version":"ATIF-v1.7"}"#).expect("session").body.is_none());
}
#[test]
fn a_huge_message_is_kept_as_a_head_and_a_path() {
let long = "y".repeat(40_000);
let sum = read(&step(9, "user", &long, "")).expect("recognised");
let body = sum.body.expect("a body");
assert_eq!(body.head.len(), crate::lens::BODY_KEEP, "a head, not the message");
assert_eq!(body.bytes, 40_000);
assert!(!body.whole(), "the head is not the whole message");
let record = crate::json::parse(step(9, "user", &long, "").as_bytes()).expect("fixture");
assert_eq!(body.text_in(Some(&record)).len(), 40_000);
assert_eq!(body.text_in(None).len(), crate::lens::BODY_KEEP, "no record, no more than the head");
assert!(body.head.len() + body.at.len() * 16 < 2 * crate::lens::BODY_KEEP);
}