use std::time::Duration;
use tempfile::tempdir;
use super::{AGENT, write_file};
use crate::app::Cadence;
use crate::git_tree::{AgentState, Framing};
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:?}"
);
}
#[test]
fn a_step_being_filled_reads_in_flight_and_a_cut_one_still_reads_killed() {
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\":\"message_start\"}\n{\"type\":\"delta\"}\n",
);
let grace = Cadence::default().wound_grace();
let framing = |state, now| build(ws, AGENT, state, now, grace).steps[0].framing;
assert_eq!(framing(AgentState::Live, LATER), Framing::InFlight);
assert_eq!(framing(AgentState::InFlight, LATER), Framing::InFlight);
let now = i64::try_from(
std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs(),
)
.unwrap();
assert_eq!(framing(AgentState::Stopped, now), Framing::InFlight);
assert_eq!(framing(AgentState::Stopped, LATER), Framing::Killed);
}
#[test]
fn only_the_newest_step_is_ever_read_as_in_flight() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_file(
ws,
"001",
"response.json",
b"{\"type\":\"message_start\"}\n",
);
write_file(ws, "002", "request.json", br#"{"model":"opus"}"#);
write_file(
ws,
"002",
"response.json",
b"{\"type\":\"message_start\"}\n",
);
let view = build(
ws,
AGENT,
AgentState::Live,
LATER,
Cadence::default().wound_grace(),
);
assert_eq!(view.steps[0].framing, Framing::Killed);
assert_eq!(view.steps[1].framing, Framing::InFlight);
}
#[test]
fn a_settled_step_keeps_its_framing_under_a_live_driver() {
let dir = tempdir().unwrap();
let ws = dir.path();
write_file(
ws,
"001",
"response.json",
b"{\"type\":\"finish\",\"reason\":\"stop\"}\n{\"type\":\"end\"}\n",
);
let view = build(
ws,
AGENT,
AgentState::Live,
LATER,
Cadence::default().wound_grace(),
);
assert_eq!(view.steps[0].framing, Framing::Complete);
}