use tempfile::tempdir;
use super::render::painted;
use super::{AGENT, write_file};
use crate::git_tree::{AgentState, Framing};
use crate::steps_view::{NO_RESPONSE, StepTab, Wound, build, latest_wound};
fn write_repro(ws: &std::path::Path, seq: &str) {
write_file(ws, seq, "request.json", br#"{"model":"opus"}"#);
write_file(ws, seq, "response.json", b"");
}
const BZ_REFUSAL: &str = "bz: no workspace in this environment — providers, sign-ins and the model \
cache belong to a workspace, and there is nothing shared to fall back to. Run this inside a yog \
workspace, or focus one in yog.";
#[test]
fn a_driver_that_wrote_nothing_is_a_wound_not_a_quiet_step() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_repro(ws, "001");
let view = build(ws, AGENT, AgentState::Stopped);
let step = &view.steps[0];
assert!(
step.wound.wounded(),
"zero-byte response + no meta is the wound"
);
assert_eq!(step.attempts, 0);
assert_eq!(step.tokens.total_tokens(), 0);
assert_eq!(step.framing, Framing::Killed);
assert!(latest_wound(&view).wounded());
}
#[test]
fn an_absent_response_with_no_meta_is_the_same_wound() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_file(ws, "001", "request.json", b"{}");
assert!(
build(ws, AGENT, AgentState::Stopped).steps[0]
.wound
.wounded()
);
}
#[test]
fn a_step_with_bytes_or_a_settled_meta_is_not_a_wound() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_file(
ws,
"001",
"response.json",
b"{\"type\":\"finish\",\"reason\":\"stop\"}\n{\"type\":\"end\"}\n",
);
write_file(ws, "002", "response.json", b"");
write_file(ws, "002", "meta.json", b"not json at all");
write_file(ws, "002", "stderr.log", BZ_REFUSAL.as_bytes());
let view = build(ws, AGENT, AgentState::Stopped);
assert_eq!(view.steps[0].wound, Wound::None);
assert_eq!(view.steps[1].wound, Wound::None);
assert!(!latest_wound(&view).wounded());
assert_eq!(
latest_wound(&build(ws, "ghost", AgentState::Stopped)),
Wound::None
);
}
#[test]
fn a_live_drivers_newest_step_is_a_call_in_flight_not_a_wound() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_repro(ws, "001");
for state in [AgentState::Live, AgentState::InFlight] {
let view = build(ws, AGENT, state);
assert!(!view.steps[0].wound.wounded());
assert!(!latest_wound(&view).wounded());
}
for state in [AgentState::Quiescent, AgentState::Stopped] {
assert!(build(ws, AGENT, state).steps[0].wound.wounded());
}
}
#[test]
fn a_live_driver_excuses_only_its_newest_step() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_repro(ws, "001");
write_repro(ws, "002");
let view = build(ws, AGENT, AgentState::InFlight);
assert!(
view.steps[0].wound.wounded(),
"where the conversation died stays rendered after a resume"
);
assert!(!view.steps[1].wound.wounded());
}
#[test]
fn the_wound_carries_the_adapters_own_words_off_the_steps_stderr_log() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_repro(ws, "001");
write_file(ws, "001", "stderr.log", BZ_REFUSAL.as_bytes());
let wound = latest_wound(&build(ws, AGENT, AgentState::Stopped));
assert_eq!(wound, Wound::Spoke(BZ_REFUSAL.to_owned()));
let sentence = wound.banner();
assert!(sentence.contains(NO_RESPONSE), "the class: {sentence}");
assert!(
sentence.contains("no workspace in this environment"),
"the reason, in words, not a pointer at somewhere to look: {sentence}"
);
assert!(
sentence.contains("stderr.log"),
"where the rest is: {sentence}"
);
}
#[test]
fn a_wound_with_an_empty_stderr_log_says_so_rather_than_inventing_a_cause() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_repro(ws, "001");
write_file(ws, "001", "stderr.log", b"");
assert_eq!(
build(ws, AGENT, AgentState::Stopped).steps[0].wound,
Wound::Mute
);
write_file(ws, "001", "stderr.log", b" \n\n ");
let wound = latest_wound(&build(ws, AGENT, AgentState::Stopped));
assert_eq!(wound, Wound::Mute);
let sentence = wound.banner();
assert!(
sentence.contains(NO_RESPONSE),
"still the class: {sentence}"
);
assert!(
sentence.contains("nothing on disk says why"),
"the honest end of the trail: {sentence}"
);
let bare = tempdir().unwrap();
write_repro(bare.path(), "001");
assert_eq!(
build(bare.path(), AGENT, AgentState::Stopped).steps[0].wound,
Wound::Mute
);
assert_eq!(Wound::None.banner(), "");
assert_eq!(Wound::default(), Wound::None);
}
#[test]
fn a_chatty_adapter_is_quoted_by_its_tail_not_in_full() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_repro(ws, "001");
let mut log = "noise line\n".repeat(600);
log.push_str("one\ntwo\nthree\n");
write_file(ws, "001", "stderr.log", log.as_bytes());
let wound = latest_wound(&build(ws, AGENT, AgentState::Stopped));
assert_eq!(wound, Wound::Spoke("one\ntwo\nthree".to_owned()));
}
#[test]
fn the_wound_reaches_the_paint_output_in_place_of_the_quiet_badge() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_repro(ws, "001");
let text = painted(
&build(ws, AGENT, AgentState::Stopped),
None,
None,
StepTab::Meta,
);
assert!(text.contains(NO_RESPONSE), "got:\n{text}");
assert!(text.contains('✖'), "the wound badge, not the ash ■: {text}");
assert!(!text.contains('■'), "got:\n{text}");
}