use pretty_assertions::assert_eq;
use rho_protocol::display::{ToolBody, ToolCard, ToolCardStatus, ToolFact, ToolFamily, ToolHeader};
use serde_json::{json, Value};
use super::{finished_card, preview_card};
const ID: &str = "1a2b3c4d-0000-4000-8000-000000000000";
fn card(status: ToolCardStatus, verb: &str, target: &str) -> ToolCard {
ToolCard::new(
status,
ToolFamily::Default,
ToolHeader::status_first(verb, target),
)
}
fn done(verb: &str, target: &str) -> ToolCard {
card(ToolCardStatus::Ok, verb, target)
}
fn meta(text: &str) -> ToolFact {
ToolFact::Meta { text: text.into() }
}
fn lines(lines: &[&str]) -> ToolBody {
ToolBody::Lines(lines.iter().map(|line| (*line).to_owned()).collect())
}
fn pretty(value: Value) -> String {
serde_json::to_string_pretty(&value).unwrap()
}
#[test]
fn preview_names_the_target_in_the_running_tense() {
let running = ToolCardStatus::Running;
for (case, arguments, status, want) in [
(
"list",
json!({"action": "list"}),
running,
card(running, "listing", "sessions"),
),
(
"new",
json!({"action": "new", "workspace": "/w", "prompt": "Fix the fold\nthen test"}),
running,
card(running, "starting", "\"Fix the fold\""),
),
(
"wait",
json!({"action": "wait", "session": ID}),
running,
card(running, "waiting on", "1a2b3c4d"),
),
(
"interrupted wait",
json!({"action": "wait", "session": ID}),
ToolCardStatus::Interrupted,
card(ToolCardStatus::Interrupted, "waiting on", "1a2b3c4d"),
),
(
"live read with a null anchor",
json!({"action": "read", "session": ID, "anchor": null}),
running,
card(running, "reading", "1a2b3c4d"),
),
(
"history read",
json!({"action": "read", "session": "f18996c5aa", "anchor": "m3"}),
running,
card(running, "reading", "history"),
),
(
"search",
json!({"action": "search", "query": "quartz"}),
running,
card(running, "searching", "\"quartz\""),
),
(
"unknown action",
json!({"action": "teleport"}),
running,
ToolCard::new(
running,
ToolFamily::Default,
ToolHeader::call("sessions", None),
),
),
] {
assert_eq!(preview_card(&arguments, status), want, "{case}");
}
}
#[test]
fn finished_control_calls_name_the_session_and_the_outcome() {
let session = |action: &str| json!({"action": action, "session": ID});
let titled = pretty(json!({"session": ID, "title": "Fix fold jump"}));
let steered = pretty(json!({"id": "steer-1", "session": ID}));
let told = format!("{titled}\nrho tells you when this turn ends or asks a question.");
let list = pretty(json!({
"sessions": [
{"session": ID, "workspace": "/src/rho", "state": "running", "opened_by": "agent",
"title": "Fix fold jump", "updated_at": 1},
{"session": "9f8e7d6c-1", "workspace": "/src/rho", "state": "waiting_for_input",
"opened_by": "agent", "updated_at": 1,
"worktree": {"branch": "rho/fold", "path": "/t/fold", "origin": "/src/rho"}},
{"session": "5a5a5a5a-2", "workspace": "/src/ui", "state": "running",
"opened_by": "person", "title": "Theme pass", "updated_at": 1},
],
"more": true,
}));
let new = pretty(json!({
"session": ID, "workspace": "/src/rho",
"worktree": {"branch": "rho/fold", "path": "/t/fold", "origin": "/src/rho"},
}));
let read = pretty(json!({
"session": ID, "title": "Fix fold jump", "text": "user: hi\nassistant: hello",
"start": 0, "end": 26, "total_chars": 90, "next_start": 26,
}));
let answered = pretty(json!({
"session": ID, "title": "Fix fold jump", "state": "completed", "done": true,
"outcome": {"type": "answered", "text": "\nFixed it.\nTests pass.", "stop": "end_turn"},
}));
let asks = pretty(json!({
"session": ID, "state": "waiting_for_input", "done": true,
"inputs": [{"id": 7, "ask": {"kind": "question", "title": "Pick a fix",
"reason": "Two fixes pass the tests.", "questions": [{
"id": "fix", "prompt": "Which fix?", "header": "Fix", "selection": "one",
"allow_other": false, "required": true,
"default": {"values": ["clamp"], "style": "selected"},
"choices": [{"value": "clamp", "label": "Clamp"}, {"value": "skip", "label": "Skip"}],
}]}}],
}));
let failed = pretty(json!({
"session": ID, "state": "failed", "done": true,
"outcome": {"type": "failed", "error": {"kind": "provider", "message": "rate limited\nretry later"}},
}));
let running = pretty(json!({"session": ID, "state": "running", "done": false}));
let long = "it keeps the jump bounded ".repeat(6);
let long = long.trim_end().to_owned();
let wait_for = json!({"action": "wait", "session": ID, "timeout_secs": 50});
let cases = [
(
"list",
json!({"action": "list", "workspace": "/src/rho", "hide_idle": true}),
list.as_str(),
true,
done("listed", "3 sessions")
.with_facts(vec![
meta("2 running · 1 needs input · more not listed"),
meta("in rho · idle hidden"),
])
.with_body(lines(&[
"running 1a2b3c4d Fix fold jump rho",
"needs input 9f8e7d6c untitled worktree rho/fold",
"running 5a5a5a5a Theme pass ui",
])),
),
(
"empty list",
json!({"action": "list"}),
r#"{"sessions": [], "more": false}"#,
true,
done("listed", "no sessions"),
),
(
"new",
json!({"action": "new", "workspace": "/src/rho", "prompt": "Fix the fold\nthen test",
"agent": "worker", "model": "opus", "worktree": true}),
new.as_str(),
true,
done("started", "\"Fix the fold\"")
.with_facts(vec![
meta("1a2b3c4d · rho · worktree rho/fold"),
meta("as worker · opus"),
])
.with_body(lines(&["Fix the fold", "then test"])),
),
(
"prompt, titled, one line with a double space",
json!({"action": "prompt", "session": ID, "prompt": "Rebase please"}),
told.as_str(),
true,
done("prompted", "Fix fold jump").with_facts(vec![meta("\"Rebase please\"")]),
),
(
"steer, untitled, through the text alias",
json!({"action": "steer", "session": ID, "text": "Stop\nand rebase"}),
steered.as_str(),
true,
done("steered", "1a2b3c4d")
.with_facts(vec![meta("\"Stop\"")])
.with_body(lines(&["Stop", "and rebase"])),
),
(
"cancel",
session("cancel"),
titled.as_str(),
true,
done("cancelled", "Fix fold jump").with_facts(vec![meta("its turn was asked to stop")]),
),
(
"answer",
json!({"action": "answer", "session": ID, "request": 7,
"answers": {"fix": ["clamp"], "why": ["it", "fits"]}}),
titled.as_str(),
true,
done("answered", "Fix fold jump").with_facts(vec![meta("fix: clamp · why: it, fits")]),
),
(
"answer too long for its line",
json!({"action": "answer", "session": ID, "request": 7,
"answers": {"fix": ["clamp"], "why": [long.as_str()]}}),
titled.as_str(),
true,
done("answered", "Fix fold jump")
.with_facts(vec![meta("fix: clamp · why: it keeps the jump bounded it keeps the jump bounded it keeps the jump bounded it keeps the jump bound…")])
.with_body(ToolBody::Lines(vec!["fix: clamp".into(), format!("why: {long}")])),
),
(
"decline",
json!({"action": "answer", "session": ID, "request": 7, "decline": true}),
titled.as_str(),
true,
done("answered", "Fix fold jump").with_facts(vec![meta("declined")]),
),
(
"live read",
session("read"),
read.as_str(),
true,
done("read", "Fix fold jump")
.with_facts(vec![meta("transcript · chars 0–26 of 90 · more available")])
.with_body(lines(&["user: hi", "assistant: hello"])),
),
(
"wait, answered",
session("wait"),
answered.as_str(),
true,
done("waited", "Fix fold jump · completed")
.with_facts(vec![meta("Fixed it.")])
.with_body(lines(&["Fixed it.", "Tests pass."])),
),
(
"wait, asks a question",
session("wait"),
asks.as_str(),
true,
done("waited", "1a2b3c4d · needs an answer")
.with_facts(vec![meta("asks \"Pick a fix\" · 1 question · request 7")])
.with_body(lines(&[
"Two fixes pass the tests.",
"Fix: Which fix?",
" • Clamp (default)",
" • Skip",
])),
),
(
"wait, failed",
session("wait"),
failed.as_str(),
true,
done("waited", "1a2b3c4d · failed")
.with_facts(vec![ToolFact::Error {
text: "rate limited".into(),
}])
.with_body(lines(&["rate limited", "retry later"])),
),
(
"wait, still running",
wait_for.clone(),
running.as_str(),
true,
done("waited", "1a2b3c4d · still running")
.with_facts(vec![meta("no answer yet after 50s")]),
),
(
"failure",
session("prompt"),
"no session 1a2b3c4d is known to the rho daemon",
false,
card(ToolCardStatus::Error, "prompted", "1a2b3c4d").with_facts(vec![
ToolFact::Error {
text: "no session 1a2b3c4d is known to the rho daemon".into(),
},
]),
),
(
"unreadable result",
session("cancel"),
"{}",
true,
done("cancelled", "1a2b3c4d").with_body(lines(&["{}"])),
),
];
for (case, arguments, content, ok, want) in cases {
assert_eq!(finished_card(&arguments, content, ok), want, "{case}");
}
}
#[test]
fn finished_history_reads_name_the_session_not_the_handle() {
let arguments = json!({"action": "read", "session": "f18996c5aa", "anchor": "m3"});
let read = |id: Option<&str>| {
let mut read = json!({
"index": {"skipped_files": 0, "omitted_records": 0},
"session": "f18996c5aa", "anchor": "m3", "role": "user", "text": "quartz",
"start": 0, "end": 6, "total_chars": 6, "next_start": null, "next_anchor": null,
"previous_anchor": null, "omitted_blocks": 0,
});
if let Some(id) = id {
read["id"] = json!(id);
}
serde_json::to_string(&read).unwrap()
};
let recall = serde_json::to_string(&json!({
"note": "untrusted", "recall_id": "r00000000000000ab", "tool_call_id": "call-1",
"ok": false, "text": "boom", "start": 0, "end": 4, "total_chars": 9, "next_start": 4,
}))
.unwrap();
let read_body = |first: &str| lines(&[first, "anchor: m3", "", "quartz"]);
let cases = [
(
"read",
arguments.clone(),
read(Some(ID)),
done("read", "history 1a2b3c4d")
.with_facts(vec![meta("user · chars 0–6 of 6")])
.with_body(read_body(&format!("session: {ID}"))),
),
(
"read saved before reads gave an id",
arguments,
read(None),
done("read", "history")
.with_facts(vec![meta("user · chars 0–6 of 6")])
.with_body(read_body("handle: f18996c5aa")),
),
(
"recall",
json!({"action": "recall", "recall_id": "r00000000000000ab"}),
recall,
done("recalled", "r00000000000000ab")
.with_facts(vec![meta(
"chars 0–4 of 9 · more available · the original call failed",
)])
.with_body(lines(&["boom"])),
),
];
for (case, arguments, content, want) in cases {
assert_eq!(finished_card(&arguments, &content, true), want, "{case}");
}
}