use super::*;
fn auq_answer(uuid: &str, parent: &str, tool_use_id: &str, ts: &str) -> String {
format!(
r#"{{"type":"user","uuid":"{uuid}","parentUuid":"{parent}","timestamp":"{ts}","toolUseResult":{{"answers":{{"Pick one":"A"}}}},"message":{{"role":"user","content":[{{"type":"tool_result","tool_use_id":"{tool_use_id}","content":"(answer recorded)"}}]}}}}"#
)
}
fn two_calls(uuid: &str, parent: &str, ts: &str) -> String {
format!(
r#"{{"type":"assistant","uuid":"{uuid}","parentUuid":"{parent}","timestamp":"{ts}","message":{{"role":"assistant","id":"m1","content":[{{"type":"tool_use","id":"t1","name":"AskUserQuestion","input":{{}}}},{{"type":"tool_use","id":"t2","name":"Read","input":{{}}}}]}}}}"#
)
}
#[test]
fn the_survivor_of_a_draft_is_an_opener_and_not_merely_the_newest_record_sharing_its_parent() {
let r = recs(&[
&u("u0", "", "chart the lagoon", "2026-06-07T05:00:00.000Z"),
&two_calls("a0", "u0", "2026-06-07T05:00:05.000Z"),
&u(
"d1",
"a0",
"sound the reef margn",
"2026-06-07T05:01:00.000Z",
),
&auq_answer("q1", "a0", "t1", "2026-06-07T05:01:30.000Z"),
r#"{"type":"user","uuid":"c0","parentUuid":"a0","timestamp":"2026-06-07T05:02:00.000Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t2","content":"soundings"}]}}"#,
&a("a2", "c0", "sounding", "2026-06-07T05:02:05.000Z"),
]);
let c = Chain::build(&r, None);
let kind = c.kind(2).expect("the recalled prompt has a kind");
assert!(
matches!(kind, Kind::Draft { .. }),
"nothing ever answered it: {kind:?}"
);
assert_eq!(
kind.survivor(),
Some(3),
"the answered question, not the plain carrier written after it"
);
}
#[test]
fn the_chains_own_child_wins_over_the_last_opener_sharing_the_parent() {
let r = recs(&[
&u("u0", "", "chart the lagoon", "2026-06-07T05:00:00.000Z"),
&two_calls("a0", "u0", "2026-06-07T05:00:05.000Z"),
&u(
"d1",
"a0",
"sound the reef margn",
"2026-06-07T05:01:00.000Z",
),
&auq_answer("q1", "a0", "t1", "2026-06-07T05:01:30.000Z"),
&auq_answer("q2", "a0", "t2", "2026-06-07T05:02:00.000Z"),
&a("a2", "q1", "sounding", "2026-06-07T05:02:05.000Z"),
]);
let c = Chain::build(&r, None);
let kind = c.kind(2).expect("the recalled prompt has a kind");
assert_eq!(
kind.survivor(),
Some(3),
"the branch the walk took, not the later opener under the same parent"
);
}
#[test]
fn the_survivor_is_named_from_the_chains_own_child_only_when_that_child_opens_a_turn() {
let r = recs(&[
&u("u0", "", "chart the lagoon", "2026-06-07T05:00:00.000Z"),
r#"{"type":"assistant","uuid":"a0","parentUuid":"u0","timestamp":"2026-06-07T05:00:05.000Z","message":{"role":"assistant","id":"m1","content":[{"type":"tool_use","id":"t1","name":"Read","input":{}}]}}"#,
&u("d1", "a0", "sound the reef", "2026-06-07T05:01:00.000Z"),
r#"{"type":"user","uuid":"c0","parentUuid":"a0","timestamp":"2026-06-07T05:02:00.000Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"soundings"}]}}"#,
&a("a2", "c0", "read them", "2026-06-07T05:02:05.000Z"),
]);
let c = Chain::build(&r, None);
let kind = c.kind(2).expect("the abandoned prompt has a kind");
assert_eq!(
kind.survivor(),
None,
"no opener replaced it, so none is named: {kind:?}"
);
}
#[test]
fn a_replay_copy_below_an_opener_does_not_make_it_a_rewound_turn() {
let r = recs(&[
&u("u0", "", "chart the lagoon", "2026-06-07T05:00:00.000Z"),
&a("a0", "u0", "charting", "2026-06-07T05:00:05.000Z"),
&u(
"d1",
"a0",
"sound the reef margn",
"2026-06-07T05:01:00.000Z",
),
&a("dup", "d1", "a replay copy", "2026-06-07T05:01:05.000Z"),
&u(
"u2",
"a0",
"sound the reef margin",
"2026-06-07T05:02:00.000Z",
),
&a("dup", "u2", "its survivor", "2026-06-07T05:02:05.000Z"),
]);
let c = Chain::build(&r, None);
let kind = c.kind(2).expect("the recalled prompt has a kind");
assert!(
matches!(kind, Kind::Draft { .. }),
"no assistant record the map still holds hangs below it: {kind:?}"
);
assert_eq!(c.drafts, 1);
assert_eq!(c.rewound_turns, 0);
}
#[test]
fn every_child_of_one_parent_is_indexed_not_just_the_first() {
let r = recs(&[
&u("u0", "", "chart the lagoon", "2026-06-07T05:00:00.000Z"),
&a("a0", "u0", "charting", "2026-06-07T05:00:05.000Z"),
&u(
"d1",
"a0",
"sound the reef margn",
"2026-06-07T05:01:00.000Z",
),
&a("r1", "d1", "an answer", "2026-06-07T05:01:02.000Z"),
&att("x1", "d1", "2026-06-07T05:01:03.000Z"),
&att("x2", "d1", "2026-06-07T05:01:05.000Z"),
&u(
"u2",
"a0",
"sound the reef margin",
"2026-06-07T05:02:00.000Z",
),
&a("a2", "u2", "sounding", "2026-06-07T05:02:05.000Z"),
]);
let c = Chain::build(&r, None);
let kind = c.kind(2).expect("the rewound prompt has a kind");
assert!(
matches!(kind, Kind::Rewound { .. }),
"the reply is the FIRST of the opener's three children: {kind:?}"
);
assert_eq!(kind.survivor(), Some(6));
assert_eq!(c.rewound_turns, 1);
assert_eq!(c.drafts, 0);
}
#[test]
fn the_relink_re_points_the_anchors_other_children_at_the_preserved_tail() {
let r = recs(&[
&u("u0", "", "dropped prompt", "2026-06-07T05:00:00.000Z"),
&a("a0", "u0", "dropped reply", "2026-06-07T05:00:05.000Z"),
&u("k1", "a0", "kept prompt", "2026-06-07T05:05:00.000Z"),
&a("k2", "k1", "kept reply", "2026-06-07T05:05:05.000Z"),
r#"{"type":"system","subtype":"compact_boundary","uuid":"b1","parentUuid":null,"timestamp":"2026-06-07T05:10:00.000Z","compactMetadata":{"trigger":"auto","preservedMessages":{"anchorUuid":"a0","uuids":["k1","k2"]}}}"#,
&att("x1", "a0", "2026-06-07T05:11:00.000Z"),
]);
let c = Chain::build(&r, None);
assert_eq!(c.leaf_index, Some(3), "the preserved tail is the leaf");
let s = survivals(&c, r.len());
assert_eq!(&s[0..2], ["pre-cut", "pre-cut"], "the cut still cuts");
assert_eq!(s[2], "live");
assert_eq!(s[3], "live");
assert_eq!(
s[5], "live",
"the re-pointed attachment hangs off the leaf and comes with it"
);
}