use super::*;
#[test]
fn select_eot_only_keeps_only_the_last_agent_message() {
let t = mk_turn_agents(
0,
Some("ask"),
&["let me look", "still working", "the final answer"],
0,
);
let lane = select_agent_messages(&t, &cfg());
assert_eq!(lane.len(), 1);
match &lane[0] {
AgentRender::Kept(a) => assert_eq!(a.unit.text, "the final answer"),
_ => panic!("expected the last message kept"),
}
}
#[test]
fn select_one_message_turn_keeps_it_as_last() {
let t = mk_turn_agents(0, Some("ask"), &["let me look into this"], 0);
let lane = select_agent_messages(&t, &rich_cfg());
assert_eq!(lane.len(), 1);
assert!(matches!(lane[0], AgentRender::Kept(_)));
}
#[test]
fn select_short_run_keeps_all_under_threshold() {
let texts: Vec<String> = (0..6).map(|i| format!("let me step {i}")).collect();
let refs: Vec<&str> = texts.iter().map(String::as_str).collect();
let t = mk_turn_agents(0, Some("ask"), &refs, 0);
let lane = select_agent_messages(&t, &rich_cfg());
assert_eq!(lane.len(), 6, "6 <= threshold 6 → keep all");
assert!(lane.iter().all(|r| matches!(r, AgentRender::Kept(_))));
}
#[test]
fn select_rich_first_kept_and_sudden_rich_middle_survives() {
let t = mk_turn_agents(
0,
Some("ask"),
&[
"found the root cause already", "let me try the next thing", "now i will check another", "12 passed 3 failed in src/x.rs:9", "let me write it up", "next i continue here", "now let me finalize", "the final committed answer", ],
0,
);
let lane = select_agent_messages(&t, &rich_cfg());
let kept: Vec<&str> = lane
.iter()
.filter_map(|r| match r {
AgentRender::Kept(a) => Some(a.unit.text.as_str()),
_ => None,
})
.collect();
assert_eq!(
kept,
vec![
"found the root cause already",
"12 passed 3 failed in src/x.rs:9",
"the final committed answer",
]
);
let placeholders = lane
.iter()
.filter(|r| matches!(r, AgentRender::Placeholder(_)))
.count();
assert_eq!(
placeholders, 2,
"two contiguous declaration runs → two placeholders"
);
}
#[test]
fn select_all_middles_droppable_makes_one_placeholder() {
let t = mk_turn_agents(
0,
Some("ask"),
&[
"the opening plan is to refactor", "let me a",
"let me b",
"let me c",
"let me d",
"let me e",
"let me f",
"the final answer here", ],
0,
);
let lane = select_agent_messages(&t, &rich_cfg());
let spans: Vec<&PlaceholderSpan> = lane
.iter()
.filter_map(|r| match r {
AgentRender::Placeholder(s) => Some(s),
_ => None,
})
.collect();
assert_eq!(
spans.len(),
1,
"one contiguous dropped run → one placeholder"
);
assert_eq!(spans[0].messages, 6, "the six middle declarations collapse");
}
#[test]
fn select_no_keep_first_collapses_a_declaration_first() {
let mut c = rich_cfg();
c.keep_first = false;
let t = mk_turn_agents(
0,
Some("ask"),
&[
"let me look into this", "let me b",
"let me c",
"let me d",
"let me e",
"let me f",
"the final answer", ],
0,
);
let lane = select_agent_messages(&t, &c);
match &lane[0] {
AgentRender::Placeholder(s) => assert!(s.messages >= 1),
_ => panic!("the declaration first must collapse with --no-keep-first: {lane:?}"),
}
let kept_lane = select_agent_messages(&t, &rich_cfg());
assert!(matches!(kept_lane[0], AgentRender::Kept(_)));
}
#[test]
fn select_all_mode_keeps_every_message_no_placeholder() {
let texts: Vec<String> = (0..10).map(|i| format!("let me step {i}")).collect();
let refs: Vec<&str> = texts.iter().map(String::as_str).collect();
let t = mk_turn_agents(0, Some("ask"), &refs, 0);
let mut c = rich_cfg();
c.mode = AgentMsgMode::All;
let lane = select_agent_messages(&t, &c);
assert_eq!(lane.len(), 10);
assert!(lane.iter().all(|r| matches!(r, AgentRender::Kept(_))));
}
#[test]
fn select_fusion_message_kept_whole_and_char_capped_later() {
let fused = "root cause confirmed in src/x.rs:42 — now let me write the fix";
let t = mk_turn_agents(
0,
Some("ask"),
&[
"let me a", "let me b", "let me c", "let me d", "let me e", "let me f", fused, "done",
],
0,
);
let lane = select_agent_messages(&t, &rich_cfg());
let kept: Vec<&str> = lane
.iter()
.filter_map(|r| match r {
AgentRender::Kept(a) => Some(a.unit.text.as_str()),
_ => None,
})
.collect();
assert!(
kept.contains(&fused),
"the fused finding survives Stage 1: {kept:?}"
);
let u = unit(Role::Assistant, 1, fused, 0);
let r = render_unit_body(&u, None);
assert!(
!r.truncated,
"this fused body is under ASST_CAP so it renders whole"
);
}
#[test]
fn trigger_boundary_six_keeps_all_seven_filters() {
let c = rich_cfg();
let six: Vec<&str> = vec![
"let me a", "let me b", "let me c", "let me d", "let me e", "let me f",
];
let t6 = mk_turn_agents(0, Some("ask"), &six, 0);
let lane6 = select_agent_messages(&t6, &c);
assert_eq!(lane6.len(), 6, "6 > 6 is false → keep all");
assert!(lane6.iter().all(|r| matches!(r, AgentRender::Kept(_))));
let seven: Vec<&str> = vec![
"let me a", "let me b", "let me c", "let me d", "let me e", "let me f", "let me g",
];
let t7 = mk_turn_agents(0, Some("ask"), &seven, 0);
let lane7 = select_agent_messages(&t7, &c);
let kept = lane7
.iter()
.filter(|r| matches!(r, AgentRender::Kept(_)))
.count();
let phs = lane7
.iter()
.filter(|r| matches!(r, AgentRender::Placeholder(_)))
.count();
assert_eq!(kept, 2, "first + last kept under filtering");
assert_eq!(
phs, 1,
"the 5 middle declarations collapse into one placeholder"
);
}
#[test]
fn eot_only_selection_is_byte_identical_to_single_eot_render() {
let t = mk_turn_agents(
3,
Some("the ask"),
&["let me look", "found 12 things", "the final reply"],
0,
);
let mut eot_lines: Vec<String> = Vec::new();
render_turn_text(&t, SelSides::AssistantOnly, &cfg(), None, &mut |s| {
eot_lines.push(s)
});
let last = t.assistant_eot().unwrap();
let mut single: Vec<String> = Vec::new();
emit_unit_text(last, None, &mut |s| single.push(s));
assert_eq!(eot_lines, single, "EotOnly == single-EOT render");
assert_eq!(
turn_cost(&t, SelSides::AssistantOnly, &cfg()),
unit_cost(last)
);
}
fn resend_pair_turn(addendum: &str) -> TurnSlice {
let first = format!(
"the committed answer runs long enough to clear the eighty-char fingerprint gate {}",
"and keeps going for a while"
);
let second = format!("{first}{addendum}");
mk_turn_agents(0, Some("ask"), &[&first, &second], 0)
}
#[test]
fn a_later_message_carrying_the_whole_earlier_body_supersedes_it() {
let t = resend_pair_turn(" plus the addendum the hook asked for");
let lane = select_agent_messages(&t, &{
let mut c = longest_cfg();
c.mode = AgentMsgMode::All;
c
});
assert_eq!(lane.len(), 2, "both messages still occupy a lane slot");
let (msg, by_line) = match &lane[0] {
AgentRender::Superseded { msg, by_line } => (msg, *by_line),
other => panic!("the earlier message must be superseded, got {other:?}"),
};
assert_eq!(msg.unit.line_no, t.agents[0].unit.line_no);
assert_eq!(
by_line, t.agents[1].unit.line_no,
"the LATER message survives"
);
assert!(
matches!(&lane[1], AgentRender::Kept(a) if a.unit.line_no == t.agents[1].unit.line_no),
"the survivor is rendered in full"
);
let line = superseded_line(msg, by_line);
assert_eq!(
line,
format!(
"△ L{} [superseded by the same-prefix re-send at L{}, {} chars]",
msg.unit.line_no, by_line, msg.unit.full_chars
)
);
assert_eq!(superseded_cost(msg, by_line), line.chars().count() + 1);
assert!(
superseded_cost(msg, by_line) < unit_cost(&msg.unit),
"the marker must be cheaper than the body it replaces"
);
}
#[test]
fn a_shared_prefix_without_containment_is_not_a_resend() {
let head =
"the committed answer runs long enough to clear the eighty-char fingerprint gate here";
let first = format!("{head} and then says ALPHA about the first branch");
let second = format!("{head} and then says BETA about the second branch");
let t = mk_turn_agents(0, Some("ask"), &[&first, &second], 0);
let lane = select_agent_messages(&t, &{
let mut c = longest_cfg();
c.mode = AgentMsgMode::All;
c
});
assert!(
lane.iter().all(|r| matches!(r, AgentRender::Kept(_))),
"a divergent pair is not a re-send: {lane:?}"
);
}
#[test]
fn a_body_under_the_fingerprint_length_is_never_a_resend() {
let t = mk_turn_agents(0, Some("ask"), &["Done.", "Done."], 0);
let lane = select_agent_messages(&t, &{
let mut c = longest_cfg();
c.mode = AgentMsgMode::All;
c
});
assert_eq!(lane.len(), 2);
assert!(
lane.iter().all(|r| matches!(r, AgentRender::Kept(_))),
"{lane:?}"
);
assert!(resend_head(&t.agents[0].unit).is_none());
let long = unit(Role::Assistant, 1, &"z".repeat(DEDUP_PREFIX), 0);
assert_eq!(
resend_head(&long).map(|h| h.chars().count()),
Some(DEDUP_PREFIX),
"exactly at the threshold the head is the full fingerprint width"
);
}
#[test]
fn a_resend_whose_survivor_was_folded_keeps_both_bodies_out_of_one_marker() {
let t = resend_pair_turn(" plus an addendum");
let lane = select_agent_messages(&t, &cfg());
assert_eq!(lane.len(), 1);
assert!(matches!(lane[0], AgentRender::Kept(_)), "{lane:?}");
}
#[test]
fn three_same_prefix_messages_supersede_onto_the_last() {
let head =
"the committed answer runs long enough to clear the eighty-char fingerprint gate here";
let a = head.to_string();
let b = format!("{head} plus one");
let c = format!("{head} plus one plus two");
let t = mk_turn_agents(0, Some("ask"), &[&a, &b, &c], 0);
let lane = select_agent_messages(&t, &{
let mut cfg = longest_cfg();
cfg.mode = AgentMsgMode::All;
cfg
});
let last_line = t.agents[2].unit.line_no;
let marked: Vec<usize> = lane
.iter()
.filter_map(|r| match r {
AgentRender::Superseded { by_line, .. } => Some(*by_line),
_ => None,
})
.collect();
assert_eq!(marked, vec![last_line, last_line], "{lane:?}");
}
#[test]
fn the_lane_cost_equals_what_a_superseded_lane_emits() {
let t = resend_pair_turn(" plus the addendum");
let c = {
let mut c = longest_cfg();
c.mode = AgentMsgMode::All;
c
};
let mut emitted = String::new();
for entry in select_agent_messages(&t, &c) {
match entry {
AgentRender::Kept(a) => emit_unit_text(&a.unit, None, &mut |s| {
emitted.push_str(&s);
emitted.push('\n');
}),
AgentRender::Placeholder(s) => {
for line in agent_placeholder_lines(&s) {
emitted.push_str(&line);
emitted.push('\n');
}
}
AgentRender::Superseded { msg, by_line } => {
emitted.push_str(&superseded_line(msg, by_line));
emitted.push('\n');
}
}
}
assert_eq!(emitted.chars().count(), assistant_lane_cost(&t, &c));
}