use agent_client_protocol::v1::schema as v1;
use pretty_assertions::assert_eq;
use rho_protocol::{SessionId, SubagentRef};
use serde_json::{json, Value};
use super::{linked_children, notification};
use crate::app::acp::markers;
fn v2_json(update: v1::SessionUpdate) -> Option<Value> {
notification(v1::SessionNotification::new("s", update))
.unwrap()
.map(|notification| serde_json::to_value(notification.update).unwrap())
}
#[test]
fn v1_updates_take_their_v2_shapes() {
let cases = [
(
"status-only tool update keeps the rest of the card",
v1::SessionUpdate::ToolCallUpdate(v1::ToolCallUpdate::new(
"call-1",
v1::ToolCallUpdateFields::new().status(v1::ToolCallStatus::Completed),
)),
Some(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call-1",
"status": "completed",
})),
),
(
"a location keeps its 1-based line",
v1::SessionUpdate::ToolCallUpdate(v1::ToolCallUpdate::new(
"call-1",
v1::ToolCallUpdateFields::new().locations(vec![
v1::ToolCallLocation::new("/work/notes.txt").line(1),
v1::ToolCallLocation::new("/work/new.txt"),
]),
)),
Some(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call-1",
"locations": [
{"path": "/work/notes.txt", "line": 1},
{"path": "/work/new.txt"},
],
})),
),
(
"a new tool call is its first update",
v1::SessionUpdate::ToolCall(
v1::ToolCall::new("call-2", "Read notes.txt")
.kind(v1::ToolKind::Read)
.status(v1::ToolCallStatus::Pending),
),
Some(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call-2",
"title": "Read notes.txt",
"kind": "read",
"status": "pending",
"content": [],
"locations": [],
})),
),
(
"todo plan",
v1::SessionUpdate::Plan(v1::Plan::new(vec![v1::PlanEntry::new(
"write tests",
v1::PlanEntryPriority::Medium,
v1::PlanEntryStatus::InProgress,
)])),
Some(json!({
"sessionUpdate": "plan_update",
"plan": {
"type": "items",
"planId": "rho-todos",
"entries": [
{"content": "write tests", "priority": "medium", "status": "in_progress"},
],
},
})),
),
(
"a cleared todo list removes the plan",
v1::SessionUpdate::Plan(v1::Plan::new(Vec::new())),
Some(json!({"sessionUpdate": "plan_removed", "planId": "rho-todos"})),
),
(
"a call a cancel interrupted is cancelled, without Rho's marker",
v1::SessionUpdate::ToolCallUpdate(
v1::ToolCallUpdate::new(
"call-1",
v1::ToolCallUpdateFields::new().status(v1::ToolCallStatus::Failed),
)
.meta(serde_json::Map::from_iter([(
"rho/toolCancelled".to_owned(),
json!(true),
)])),
),
Some(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call-1",
"status": "cancelled",
})),
),
(
"command input hint",
v1::SessionUpdate::AvailableCommandsUpdate(v1::AvailableCommandsUpdate::new(vec![
v1::AvailableCommand::new("compact", "Compact the context").input(
v1::AvailableCommandInput::Unstructured(v1::UnstructuredCommandInput::new(
"instructions",
)),
),
])),
Some(json!({
"sessionUpdate": "available_commands_update",
"availableCommands": [{
"type": "command",
"name": "compact",
"description": "Compact the context",
"input": {"type": "text", "hint": "instructions"},
}],
})),
),
(
"mode changes travel as config options",
v1::SessionUpdate::CurrentModeUpdate(v1::CurrentModeUpdate::new("auto")),
None,
),
];
for (label, update, expected) in cases {
assert_eq!(v2_json(update), expected, "{label}");
}
}
#[test]
fn config_options_use_v2_ids() {
let option = v1::SessionConfigOption::select(
"model",
"Model",
"grok",
vec![v1::SessionConfigSelectGroup::new(
"xai",
"xAI",
vec![v1::SessionConfigSelectOption::new("grok", "Grok")],
)],
)
.category(v1::SessionConfigOptionCategory::Model);
let update = v1::SessionUpdate::ConfigOptionUpdate(v1::ConfigOptionUpdate::new(vec![option]));
assert_eq!(
v2_json(update),
Some(json!({
"sessionUpdate": "config_option_update",
"configOptions": [{
"configId": "model",
"name": "Model",
"category": "model",
"type": "select",
"currentValue": "grok",
"options": [{
"groupId": "xai",
"name": "xAI",
"options": [{"value": "grok", "name": "Grok"}],
}],
}],
}))
);
}
#[cfg(unix)]
#[test]
fn marked_diffs_become_delete_and_move_changes() {
let marked = |diff: v1::Diff, key: &str, value: Value| {
v1::ToolCallContent::Diff(diff.meta(serde_json::Map::from_iter([(key.to_owned(), value)])))
};
let deleted = |path: &str, old: &str| {
marked(
v1::Diff::new(path, "").old_text(old.to_owned()),
"rho/fileDeleted",
json!(true),
)
};
let diffs = |content: Vec<v1::ToolCallContent>| {
v1::SessionUpdate::ToolCallUpdate(v1::ToolCallUpdate::new(
"call-5",
v1::ToolCallUpdateFields::new().content(content),
))
};
let cases = [
(
"a move is one move change, its source's deletion folded in",
diffs(vec![
deleted("/work/a.txt", "old\n"),
marked(
v1::Diff::new("/work/b.txt", "new\n"),
"rho/movedFrom",
json!("/work/a.txt"),
),
]),
Some(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call-5",
"content": [{
"type": "diff",
"changes": [{
"operation": "move",
"oldPath": "/work/a.txt",
"path": "/work/b.txt",
"fileType": "text",
}],
"patch": {
"format": "git_patch",
"text": "diff --git /work/a.txt /work/b.txt\nrename from /work/a.txt\nrename to /work/b.txt\n--- /work/a.txt\n+++ /work/b.txt\n@@ -1 +1 @@\n-old\n+new\n",
},
}],
})),
),
(
"an unmarked deletion and new file stay apart",
diffs(vec![
deleted("/work/a.txt", "x\n"),
v1::ToolCallContent::Diff(v1::Diff::new("/work/b.txt", "x\n")),
]),
Some(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call-5",
"content": [
{
"type": "diff",
"changes": [{"operation": "delete", "path": "/work/a.txt", "fileType": "text"}],
"patch": {
"format": "git_patch",
"text": "diff --git /work/a.txt /work/a.txt\ndeleted file mode 100644\n--- /work/a.txt\n+++ /dev/null\n@@ -1 +0,0 @@\n-x\n",
},
},
{
"type": "diff",
"changes": [{"operation": "add", "path": "/work/b.txt", "fileType": "text"}],
"patch": {
"format": "git_patch",
"text": "diff --git /work/b.txt /work/b.txt\nnew file mode 100644\n--- /dev/null\n+++ /work/b.txt\n@@ -0,0 +1 @@\n+x\n",
},
},
],
})),
),
(
"a deleted file is a delete change, without Rho's marker",
v1::SessionUpdate::ToolCallUpdate(v1::ToolCallUpdate::new(
"call-3",
v1::ToolCallUpdateFields::new().content(vec![v1::ToolCallContent::Diff(
v1::Diff::new("/work/old.txt", "")
.old_text("bye\n".to_owned())
.meta(serde_json::Map::from_iter([(
"rho/fileDeleted".to_owned(),
json!(true),
)])),
)]),
)),
Some(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call-3",
"content": [{
"type": "diff",
"changes": [{"operation": "delete", "path": "/work/old.txt", "fileType": "text"}],
"patch": {
"format": "git_patch",
"text": "diff --git /work/old.txt /work/old.txt\ndeleted file mode 100644\n--- /work/old.txt\n+++ /dev/null\n@@ -1 +0,0 @@\n-bye\n",
},
}],
})),
),
(
"an emptied file stays a modify",
v1::SessionUpdate::ToolCallUpdate(v1::ToolCallUpdate::new(
"call-4",
v1::ToolCallUpdateFields::new().content(vec![v1::ToolCallContent::Diff(
v1::Diff::new("/work/old.txt", "").old_text("bye\n".to_owned()),
)]),
)),
Some(json!({
"sessionUpdate": "tool_call_update",
"toolCallId": "call-4",
"content": [{
"type": "diff",
"changes": [{"operation": "modify", "path": "/work/old.txt", "fileType": "text"}],
"patch": {
"format": "git_patch",
"text": "diff --git /work/old.txt /work/old.txt\n--- /work/old.txt\n+++ /work/old.txt\n@@ -1 +0,0 @@\n-bye\n",
},
}],
})),
),
];
for (label, update, expected) in cases {
assert_eq!(v2_json(update), expected, "{label}");
}
}
#[test]
fn links_name_their_child_and_its_session() {
let link = |child: &SubagentRef| {
v1::ToolCallContent::Content(
v1::Content::new(v1::ContentBlock::from("")).meta(markers::subagent(child)),
)
};
let with_session = SubagentRef::new("run-1", Some(SessionId::new("child-1")));
let without = SubagentRef::new("run-2", None);
let cases = [
(
"a started call links its child with its session",
v1::SessionUpdate::ToolCall(
v1::ToolCall::new("call-1", "worker").content(vec![link(&with_session)]),
),
vec![with_session.clone()],
),
(
"an update links a child on another runtime by its run",
v1::SessionUpdate::ToolCallUpdate(v1::ToolCallUpdate::new(
"call-2",
v1::ToolCallUpdateFields::new().content(vec![
v1::ToolCallContent::Content(v1::Content::new(v1::ContentBlock::from("done"))),
link(&without),
]),
)),
vec![without],
),
(
"an update that leaves the content as it was links nothing",
v1::SessionUpdate::ToolCallUpdate(v1::ToolCallUpdate::new(
"call-3",
v1::ToolCallUpdateFields::new().status(v1::ToolCallStatus::Completed),
)),
vec![],
),
];
for (case, update, expected) in cases {
assert_eq!(linked_children(&update), expected, "{case}");
}
}