hive-agent-mcp: test the tool output rendering and the in-agent socket dial
This commit is contained in:
parent
4fe54c87c8
commit
9412bb568a
1 changed files with 268 additions and 1 deletions
|
|
@ -465,7 +465,274 @@ pub fn annotate_retries(mut s: String, retries: u32) -> String {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::dial_socket;
|
||||
use super::*;
|
||||
use hive_sh4re::inbox::LooseEnd;
|
||||
use serde_json::json;
|
||||
|
||||
fn loose_end(value: serde_json::Value) -> LooseEnd {
|
||||
serde_json::from_value(value).expect("a loose end")
|
||||
}
|
||||
|
||||
fn todo(id: i64) -> LooseEnd {
|
||||
loose_end(json!({
|
||||
"kind": "todo", "id": id, "subsystem": "bash", "summary": format!("task {id}"),
|
||||
"age_seconds": 5,
|
||||
}))
|
||||
}
|
||||
|
||||
fn agent_meta(extra: &serde_json::Value) -> hive_core_agent_sock::Response {
|
||||
let mut meta = json!({"kind": "agent_meta", "name": "a", "running": true});
|
||||
meta.as_object_mut()
|
||||
.unwrap()
|
||||
.extend(extra.as_object().unwrap().clone());
|
||||
serde_json::from_value(meta).expect("an agent meta")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_reply_names_the_tool_and_what_went_wrong() {
|
||||
let refused = hive_core_agent_sock::Response::Err {
|
||||
message: "no such agent".to_owned(),
|
||||
};
|
||||
assert_eq!(reply_err(Ok(refused), "t"), "t failed: no such agent");
|
||||
let odd = reply_err(Ok(hive_core_agent_sock::Response::Ok), "t");
|
||||
assert!(odd.starts_with("t unexpected response: "), "{odd}");
|
||||
let down = reply_err(Err(anyhow::anyhow!("socket gone")), "t");
|
||||
assert_eq!(down, "t transport error: socket gone");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ack_is_the_ok_message_on_ok_and_the_failure_otherwise() {
|
||||
let ok = format_ack(
|
||||
Ok(hive_core_agent_sock::Response::Ok),
|
||||
"send",
|
||||
"sent".into(),
|
||||
);
|
||||
assert_eq!(ok, "sent");
|
||||
let err = format_ack(
|
||||
Ok(hive_core_agent_sock::Response::Err {
|
||||
message: "full".to_owned(),
|
||||
}),
|
||||
"send",
|
||||
"sent".into(),
|
||||
);
|
||||
assert_eq!(err, "send failed: full");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_loose_ends_is_said_in_words() {
|
||||
assert_eq!(render_loose_ends(&[]), "(no loose ends)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_kind_of_loose_end_renders_its_own_line() {
|
||||
let out = render_loose_ends(&[
|
||||
loose_end(json!({
|
||||
"kind": "reminder", "id": 3, "owner": "me", "message": "check ci",
|
||||
"due_at": "2026-01-02T03:04:05Z", "age_seconds": 60,
|
||||
})),
|
||||
loose_end(json!({"kind": "pending_messages", "count": 2})),
|
||||
loose_end(json!({
|
||||
"kind": "todo", "id": 9, "subsystem": "forge", "subsystem_key": "77",
|
||||
"summary": "review", "source": "PR 5", "age_seconds": 7,
|
||||
})),
|
||||
]);
|
||||
assert!(out.starts_with("3 loose end(s):\n"), "{out}");
|
||||
assert!(
|
||||
out.contains(
|
||||
"- reminder #3 (me, scheduled 60s ago, due_at=2026-01-02 03:04:05 UTC): check ci\n"
|
||||
),
|
||||
"{out}"
|
||||
);
|
||||
assert!(out.contains("- 2 inbox message(s) queued;"), "{out}");
|
||||
assert!(
|
||||
out.contains(
|
||||
"- todo #9 [forge 77, 7s old]: review — PR 5 (cancel_loose_end kind:\"todo\" id:9 to clear)\n"
|
||||
),
|
||||
"{out}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_todo_without_key_or_source_leaves_those_parts_out() {
|
||||
let out = render_loose_ends(&[todo(1)]);
|
||||
assert!(
|
||||
out.contains(
|
||||
"- todo #1 [bash, 5s old]: task 1 (cancel_loose_end kind:\"todo\" id:1 to clear)\n"
|
||||
),
|
||||
"{out}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unread_matrix_lists_each_room_line_or_points_at_the_tools() {
|
||||
let with = render_loose_ends(&[loose_end(json!({
|
||||
"kind": "unread_matrix", "rooms": 2, "summary": "- a: 1 unread\n- b: 3 unread",
|
||||
}))]);
|
||||
assert!(
|
||||
with.contains(
|
||||
"- unread matrix messages in 2 room(s):\n - a: 1 unread\n - b: 3 unread\n use list_rooms + read_room"
|
||||
),
|
||||
"{with}"
|
||||
);
|
||||
let bare = render_loose_ends(&[loose_end(json!({"kind": "unread_matrix", "rooms": 1}))]);
|
||||
assert!(
|
||||
bare.contains("- unread matrix messages in 1 room(s) — use list_rooms + read_room"),
|
||||
"{bare}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn more_todos_than_the_cap_are_counted_not_rendered() {
|
||||
let todos: Vec<LooseEnd> = (1..=45).map(todo).collect();
|
||||
let out = render_loose_ends(&todos);
|
||||
assert!(out.contains(": task 40 "), "{out}");
|
||||
assert!(!out.contains(": task 41 "), "{out}");
|
||||
assert!(
|
||||
out.contains("- 5 more todo(s) not shown — clear reviewed ids with mark_todos_done"),
|
||||
"{out}"
|
||||
);
|
||||
// Control: at the cap exactly, nothing is hidden.
|
||||
let exact = render_loose_ends(&todos[..MAX_RENDERED_TODOS]);
|
||||
assert!(!exact.contains("not shown"), "{exact}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_single_unread_message_shows_its_sender_and_body_and_others_their_count() {
|
||||
assert_eq!(format_matrix_summary(&[]), "");
|
||||
let one = MatrixRoomUnread {
|
||||
label: "ops".into(),
|
||||
count: 1,
|
||||
last_body: Some("hi".into()),
|
||||
last_sender: Some("@bob:x".into()),
|
||||
};
|
||||
let many = MatrixRoomUnread {
|
||||
label: "chat".into(),
|
||||
count: 4,
|
||||
last_body: Some("ignored".into()),
|
||||
last_sender: Some("@al:x".into()),
|
||||
};
|
||||
// A lone message with no sender known falls back to the count.
|
||||
let nameless = MatrixRoomUnread {
|
||||
label: "x".into(),
|
||||
count: 1,
|
||||
last_body: None,
|
||||
last_sender: None,
|
||||
};
|
||||
assert_eq!(
|
||||
format_matrix_summary(&[one, many, nameless]),
|
||||
"- ops: @bob:x: hi\n- chat: 4 unread\n- x: 1 unread"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_meta_always_shows_the_identity_and_only_the_set_optionals() {
|
||||
let bare = format_agent_meta(Ok(agent_meta(&json!({}))));
|
||||
assert_eq!(
|
||||
bare,
|
||||
"name: a\nhyperhive_rev: <unknown>\nrunning: yes\nstatus: <none>"
|
||||
);
|
||||
let full = format_agent_meta(Ok(agent_meta(&json!({
|
||||
"running": false, "hyperhive_rev": "abc", "hive_name": "pr1ma",
|
||||
"swarm_name": "constellation", "status_text": "busy",
|
||||
"matrix_accounts": [
|
||||
{"name": "main", "user_id": "@a:x", "homeserver": "https://x"},
|
||||
{"name": "alt", "homeserver": "https://y"},
|
||||
],
|
||||
}))));
|
||||
assert!(full.contains("running: no"), "{full}");
|
||||
assert!(
|
||||
full.contains("\nhive_name: pr1ma\nswarm_name: constellation\n"),
|
||||
"{full}"
|
||||
);
|
||||
// No `status_set_at`: the status has no age.
|
||||
assert!(full.contains("\nstatus: busy\n"), "{full}");
|
||||
assert!(
|
||||
full.ends_with("matrix_accounts:\n main (@a:x) on https://x\n alt (?) on https://y"),
|
||||
"{full}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_status_set_long_ago_says_how_long() {
|
||||
// Epoch second 0 is decades back, whatever the clock reads now.
|
||||
let aged = format_agent_meta(Ok(agent_meta(
|
||||
&json!({"status_text": "old", "status_set_at": 0}),
|
||||
)));
|
||||
assert!(aged.contains("status: old (set "), "{aged}");
|
||||
assert!(aged.ends_with("d ago)"), "{aged}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_meta_lookup_is_reported_under_the_tool_name() {
|
||||
let out = format_agent_meta(Err(anyhow::anyhow!("down")));
|
||||
assert_eq!(out, "get_agent_meta transport error: down");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ages_pick_the_largest_whole_unit() {
|
||||
let ages = [0, 59, 60, 3599, 3600, 86_399, 86_400, 200_000].map(format_age_secs);
|
||||
assert_eq!(ages, ["0s", "59s", "1m", "59m", "1h", "23h", "1d", "2d"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_retry_note_is_added_only_after_a_retry_and_agrees_in_number() {
|
||||
assert_eq!(annotate_retries("done".into(), 0), "done");
|
||||
let once = annotate_retries("done".into(), 1);
|
||||
assert!(once.contains("needed 1 retry —"), "{once}");
|
||||
let twice = annotate_retries("done".into(), 3);
|
||||
assert!(twice.contains("needed 3 retries —"), "{twice}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_socket_that_answers_is_decoded() {
|
||||
use tokio::io::{AsyncBufReadExt as _, AsyncWriteExt as _, BufReader};
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"hive-agent-mcp-answer-test-{}-{}.sock",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos()
|
||||
));
|
||||
let listener = tokio::net::UnixListener::bind(&path).unwrap();
|
||||
let server = tokio::spawn(async move {
|
||||
let (conn, _) = listener.accept().await.unwrap();
|
||||
let mut conn = BufReader::new(conn);
|
||||
let mut line = String::new();
|
||||
conn.read_line(&mut line).await.unwrap();
|
||||
conn.get_mut()
|
||||
.write_all(b"{\"kind\":\"acked\",\"count\":2}\n")
|
||||
.await
|
||||
.unwrap();
|
||||
line
|
||||
});
|
||||
|
||||
let resp = dial_socket(
|
||||
&path,
|
||||
&hive_agent_sock::Request::MarkTodosDone { ids: vec![1, 2] },
|
||||
std::time::Duration::from_secs(5),
|
||||
)
|
||||
.await;
|
||||
let sent = server.await.unwrap();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
|
||||
assert!(
|
||||
matches!(resp, Some(hive_agent_sock::Response::Acked { count: 2 })),
|
||||
"{resp:?}"
|
||||
);
|
||||
assert!(
|
||||
sent.contains("mark_todos_done") && sent.contains("[1,2]"),
|
||||
"{sent}"
|
||||
);
|
||||
// A path nothing listens on is `None`, not an error.
|
||||
let gone = dial_socket(
|
||||
&std::env::temp_dir().join("hive-agent-mcp-no-such.sock"),
|
||||
&hive_agent_sock::Request::ListReminders,
|
||||
std::time::Duration::from_millis(100),
|
||||
)
|
||||
.await;
|
||||
assert!(gone.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_socket_that_accepts_and_never_answers_is_given_up_on() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue