use std::path::PathBuf;
use pretty_assertions::assert_eq;
use rho_protocol::{self as wire, BackgroundStatus, JobId, JobKind, JobState, JobSummary};
use serde_json::{json, Value};
use super::{child_options, run_ended};
fn model_ref(provider: &str, model: &str) -> wire::ModelRef {
wire::ModelRef {
provider: provider.into(),
model: model.into(),
}
}
#[test]
fn a_childs_selects_offer_only_their_current_value() {
let options = wire::SessionOptions {
model: wire::ModelOptions {
auth: "api-key".into(),
current: model_ref("openai", "gpt-4"),
available: vec![
wire::ModelChoice {
model: model_ref("xai", "grok-3"),
label: "Grok 3".into(),
},
wire::ModelChoice {
model: model_ref("openai", "gpt-4"),
label: "gpt-4".into(),
},
],
},
reasoning: None,
fast_mode: Some(wire::FastModeOptions { enabled: false }),
permission_mode: wire::PermissionModeOptions {
current: wire::PermissionMode::Auto,
available: vec![wire::PermissionMode::Plan, wire::PermissionMode::Auto],
},
commands: Vec::new(),
added_dirs: Vec::new(),
};
let update = serde_json::to_value(child_options(&options).unwrap()).unwrap();
let offered: Vec<(Value, Value)> = update["configOptions"]
.as_array()
.unwrap()
.iter()
.map(|option| {
let values: Vec<&Value> = option["options"]
.as_array()
.unwrap()
.iter()
.map(|choice| &choice["value"])
.collect();
(option["currentValue"].clone(), json!(values))
})
.collect();
assert_eq!(
(update["sessionUpdate"].clone(), offered),
(
json!("config_option_update"),
vec![
(json!("auto"), json!(["auto"])),
(json!("openai/gpt-4"), json!(["openai/gpt-4"])),
(json!("off"), json!(["off"])),
]
)
);
}
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,
}
}
#[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}"
);
}
}