use std::time::Duration;
use tempfile::tempdir;
use super::{AGENT, write_file};
use crate::app::Cadence;
use crate::git_tree::AgentState;
use crate::steps_view::{Wound, build, latest_wound};
fn just_sent(ws: &std::path::Path) {
write_file(ws, "001", "request.json", br#"{"model":"opus"}"#);
write_file(ws, "001", "response.json", b"");
}
const LATER: i64 = 4_000_000_000;
#[test]
fn a_call_younger_than_the_window_is_in_flight_not_a_wound() {
let dir = tempdir().unwrap();
let ws = dir.path();
just_sent(ws);
let now = i64::try_from(
std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs(),
)
.unwrap();
let grace = Cadence::default().wound_grace();
assert!(
!latest_wound(&build(ws, AGENT, AgentState::Stopped, now, grace)).wounded(),
"the cached liveness has not had time to say a driver took the lock"
);
assert!(
latest_wound(&build(ws, AGENT, AgentState::Stopped, LATER, grace)).wounded(),
"past the window the honest wound is stated — delayed, never dropped"
);
}
#[test]
fn a_step_with_no_call_start_is_not_excused() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_file(ws, "001", "response.json", b"");
assert!(
latest_wound(&build(ws, AGENT, AgentState::Stopped, 0, Duration::MAX)).wounded(),
"an unreadable stamp never hides a wound, however wide the window"
);
}
#[test]
fn a_settled_refusal_never_waits() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_file(ws, "001", "request.json", br#"{"model":"opus"}"#);
write_file(
ws,
"001",
"response.json",
b"{\"type\":\"error\",\"status\":401,\"message\":\"Unauthorized: check credentials\"}\n{\"type\":\"end\"}\n",
);
let wound = latest_wound(&build(ws, AGENT, AgentState::Stopped, 0, Duration::MAX));
assert!(
matches!(wound, Wound::Refused(_)),
"stated at once, inside any window: {wound:?}"
);
}