use std::path::PathBuf;
use pretty_assertions::assert_eq;
use rho_protocol::{
Ask, InputAsker, InputRequest, InputRequestId, JobId, JobKind, JobLive, JobState, JobSummary,
ModelRef, SessionId,
};
use super::rail_jobs;
const NOW: u64 = 1_000;
fn job(id: &str, kind: JobKind, parent: Option<&str>) -> JobSummary {
JobSummary {
id: JobId::new(id),
kind,
state: JobState::Running,
workspace: PathBuf::from("/work"),
parent: parent.map(JobId::new),
session: None,
run: None,
workflow_run: None,
result_pending: false,
created_at: 0,
updated_at: 0,
error: None,
queue: None,
live: None,
grant: None,
grant_limit: None,
worktree: None,
worktree_stale: false,
child: None,
}
}
fn session(id: &str, session: &str) -> JobSummary {
JobSummary {
session: Some(SessionId::new(session)),
..job(id, JobKind::Session, None)
}
}
fn run(id: &str, parent: &str, state: JobState, updated_at: u64) -> JobSummary {
JobSummary {
state,
updated_at,
run: Some(format!("run-{id}")),
..job(id, JobKind::Subagent, Some(parent))
}
}
#[test]
fn the_rail_takes_the_sessions_live_and_just_ended_runs() {
let jobs = [
run("early", "mine", JobState::Running, 0),
run("node", "workflow", JobState::Running, 0),
job("workflow", JobKind::Workflow, Some("mine")),
JobSummary {
state: JobState::Completed,
updated_at: NOW - 60,
result_pending: true,
..job("owed", JobKind::Workflow, Some("mine"))
},
JobSummary {
state: JobState::Completed,
updated_at: NOW - 60,
..job("delivered", JobKind::Workflow, Some("mine"))
},
session("mine", "s1"),
session("theirs", "s2"),
run("live", "mine", JobState::Running, 0),
run("queued", "mine", JobState::Queued, 0),
run("old", "mine", JobState::Completed, NOW - 60),
run("fresh", "mine", JobState::Failed, NOW - 3),
run("foreign", "theirs", JobState::Running, 0),
job("unnamed", JobKind::Subagent, Some("mine")),
];
let rail = |kind| rail_jobs(&jobs, "s1", kind, NOW);
assert_eq!(
(
rail(JobKind::Subagent)
.into_iter()
.filter_map(|job| job.run.as_deref())
.collect::<Vec<_>>(),
rail(JobKind::Workflow)
.into_iter()
.map(|job| job.id.as_str())
.collect::<Vec<_>>(),
),
(
vec!["run-early", "run-live", "run-queued", "run-fresh"],
vec!["workflow", "owed"],
)
);
}
#[test]
fn the_rail_takes_the_sessions_it_started_while_their_turns_run() {
let started = |id: &str, state, updated_at| JobSummary {
state,
updated_at,
session: Some(SessionId::new(format!("session-{id}"))),
..job(id, JobKind::Session, Some("mine"))
};
let jobs = [
session("mine", "s1"),
started("working", JobState::Running, 0),
started("asking", JobState::WaitingForInput, 0),
started("waiting", JobState::Queued, 0),
started("just_idle", JobState::Idle, NOW - 3),
started("long_idle", JobState::Idle, NOW - 60),
started("long_done", JobState::Completed, NOW - 60),
JobSummary {
live: Some(JobLive {
title: None,
model: ModelRef {
provider: "xai".into(),
model: "grok-fixture".into(),
},
turn_elapsed_ms: None,
usage: None,
inputs: vec![InputRequest {
id: InputRequestId(1),
ask: Ask::Unknown,
tool_call: None,
timeout: None,
asker: InputAsker::Subagent,
subagent: None,
}],
}),
..started("idle_asking", JobState::Idle, NOW - 60)
},
run("idle_run", "mine", JobState::Idle, NOW - 60),
];
assert_eq!(
(
rail_jobs(&jobs, "s1", JobKind::Session, NOW)
.into_iter()
.map(|job| job.id.as_str())
.collect::<Vec<_>>(),
rail_jobs(&jobs, "s1", JobKind::Subagent, NOW)
.into_iter()
.map(|job| job.id.as_str())
.collect::<Vec<_>>(),
),
(
vec!["working", "asking", "waiting", "just_idle", "idle_asking"],
vec!["idle_run"],
)
);
}