use std::path::PathBuf;
use pretty_assertions::assert_eq;
use rho_protocol::{
self as wire, BackgroundStatus, ChildInfo, ChildRole, JobId, JobKind, JobState, JobSummary,
};
use super::{run_ended, started_by, StartedChild};
fn job(id: &str, kind: JobKind, session: &str, state: JobState) -> JobSummary {
JobSummary {
id: JobId::new(id),
kind,
state,
workspace: PathBuf::from("/work"),
parent: None,
session: Some(wire::SessionId::new(session)),
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,
}
}
#[test]
fn a_childs_end_is_its_runs_job_state() {
use JobKind::{Session, Subagent};
use JobState::{Cancelled, Completed, Failed, Interrupted, Running, TimedOut};
let cases = [
(
"a completed run",
vec![job("j", Subagent, "c", Completed)],
Some(BackgroundStatus::Completed),
),
(
"a run that timed out failed",
vec![job("j", Subagent, "c", TimedOut)],
Some(BackgroundStatus::Failed),
),
(
"a cancelled run",
vec![job("j", Subagent, "c", Cancelled)],
Some(BackgroundStatus::Cancelled),
),
(
"an interrupted run ended unseen",
vec![job("j", Subagent, "c", Interrupted)],
Some(BackgroundStatus::Unknown),
),
(
"a run under way has not ended",
vec![job("j", Subagent, "c", Running)],
None,
),
(
"the latest run of the session counts, and no session job",
vec![
job("old", Subagent, "c", Failed),
job("new", Subagent, "c", Completed),
job("turns", Session, "c", Cancelled),
],
Some(BackgroundStatus::Completed),
),
(
"another session's run says nothing",
vec![job("j", Subagent, "other", Completed)],
None,
),
];
for (case, jobs, expected) in cases {
assert_eq!(
run_ended(&jobs, &wire::SessionId::new("c")),
expected,
"{case}"
);
}
}
fn child(
id: &str,
parent: &str,
role: ChildRole,
session: &str,
title: Option<&str>,
) -> JobSummary {
let info = ChildInfo::new(role);
JobSummary {
parent: Some(JobId::new(parent)),
child: Some(match title {
Some(title) => info.titled(title),
None => info,
}),
..job(id, JobKind::Session, session, JobState::Idle)
}
}
#[test]
fn a_resume_finds_the_sessions_its_session_started() {
let jobs = [
job(
"parent-job",
JobKind::Session,
"parent",
JobState::Completed,
),
job("other-job", JobKind::Session, "other", JobState::Completed),
child(
"a",
"parent-job",
ChildRole::Started,
"titled",
Some("Fix the build"),
),
child("b", "parent-job", ChildRole::Started, "untitled", None),
JobSummary {
kind: JobKind::Subagent,
..child(
"c",
"parent-job",
ChildRole::Delegated,
"delegated",
Some("task"),
)
},
child(
"d",
"other-job",
ChildRole::Started,
"elsewhere",
Some("Not ours"),
),
child("e", "c", ChildRole::Started, "nested", None),
];
assert_eq!(
started_by(&jobs, &wire::SessionId::new("parent")),
vec![
StartedChild {
session: wire::SessionId::new("titled"),
title: Some("Fix the build".to_owned()),
},
StartedChild {
session: wire::SessionId::new("untitled"),
title: None,
},
StartedChild {
session: wire::SessionId::new("nested"),
title: None,
},
]
);
}