use super::{clause, failure};
use crate::git_tree::terminal::settled;
use crate::steps_view::records::STDERR_FILE;
use std::path::Path;
const FAILED: &[u8] = br#"{"type":"message_start","v":1,"role":"assistant"}
{"type":"error","status":401,"message":"Unauthorized"}
{"type":"end"}
"#;
const WHOLE: &[u8] = br#"{"type":"message_start","v":1,"role":"assistant"}
{"type":"finish","reason":"stop"}
{"type":"end"}
"#;
fn read(step: &Path, response: &[u8]) -> Option<String> {
failure(step, response, settled(response))
}
#[test]
fn an_in_band_error_is_the_event_line_itself() {
let dir = tempfile::TempDir::new().unwrap();
let said = read(dir.path(), FAILED).expect("the call failed");
assert!(said.contains(r#""status":401"#), "{said}");
assert!(said.contains("Unauthorized"), "{said}");
}
#[test]
fn an_adapter_that_died_before_the_contract_says_so_from_its_stderr() {
let dir = tempfile::TempDir::new().unwrap();
std::fs::write(
dir.path().join(STDERR_FILE),
"bz: no credential for provider row \"work\"\n",
)
.unwrap();
let said = read(dir.path(), b"").expect("the call failed");
assert!(said.contains("no credential for provider row"), "{said}");
}
#[test]
fn a_complete_tail_is_no_failure_and_reads_no_stderr() {
let dir = tempfile::TempDir::new().unwrap();
std::fs::write(dir.path().join(STDERR_FILE), "stale words\n").unwrap();
assert_eq!(read(dir.path(), WHOLE), None);
}
#[test]
fn a_killed_call_with_no_words_is_no_sentence() {
let dir = tempfile::TempDir::new().unwrap();
assert_eq!(read(dir.path(), b"{\"type\":\"delta\"}\n"), None);
}
#[test]
fn the_clause_is_the_providers_message_when_the_evidence_is_an_event() {
assert_eq!(
clause(r#"{"type":"error","status":401,"message":"Unauthorized"}"#),
"Unauthorized"
);
}
#[test]
fn an_event_with_no_message_says_itself() {
let raw = r#"{"type":"error","status":503}"#;
assert_eq!(clause(raw), raw);
let blank = r#"{"type":"error","message":" "}"#;
assert_eq!(clause(blank), blank);
}
#[test]
fn plain_stderr_gives_its_first_line_capped() {
assert_eq!(
clause("\n\nbz: malformed config\n at line 3\n"),
"bz: malformed config"
);
let long = "x".repeat(400);
assert_eq!(clause(&long).chars().count(), super::CLAUSE_CAP);
assert!(
clause(&long).ends_with('\u{2026}'),
"one word, cut where it runs out"
);
}
#[test]
fn a_cut_clause_is_marked_and_ends_on_a_word() {
let said = "model: not-a-real-model-xyz; `not-a-real-model-xyz` is not in \
the model cache; run `bz --list-models` to refresh or enable partial matching";
let cut = clause(said);
assert!(cut.chars().count() <= super::CLAUSE_CAP, "{cut}");
assert!(
cut.ends_with("or\u{2026}"),
"marked, and on a word boundary: {cut}"
);
assert!(
!cut.contains("enab"),
"no half word survives the cut: {cut}"
);
}
#[test]
fn a_clause_that_fits_carries_no_mark() {
let said = "bz: no credential for provider `anthropic`";
assert_eq!(clause(said), said);
}
#[test]
fn empty_evidence_is_an_empty_clause() {
assert_eq!(clause(" \n"), "");
}