//! The JSON API (spec §18) against the real daemon: every endpoint, signed //! by an agent and by the owner; problems for refusals; paging; ETags; the //! event log as the owner's; and the live stream. mod common; use common::{add_agent, setup, Serving}; use serde_json::{json, Value}; use spectra_core::keys::KeyPair; use spectra_mcp::client::Client; async fn get(c: &Client, path: &str) -> Value { let r = c.send("GET", path, &[], vec![]).await.unwrap(); assert_eq!(r.status, 200, "{path}: {}", String::from_utf8_lossy(&r.body)); r.json().unwrap() } #[tokio::test] async fn every_endpoint_for_an_agent_and_the_owner() { let dir = setup(); let d = dir.path(); let claude = add_agent(d, "claude-oxygen", &[]); let srv = Serving::start(d); let a = Client::new(&srv.socket, Some(claude)); let owner = Client::new(&srv.socket, Some(KeyPair::load_home(&d.join("owner")).unwrap())); let anon = Client::new(&srv.socket, None); assert_eq!(get(&anon, "/api/v1/health").await["ok"], true); assert_eq!(get(&anon, "/api/v1/version").await["ledger_format"], 1); let r = anon.send("GET", "/api/v1/agents", &[], vec![]).await.unwrap(); assert_eq!(r.status, 401); assert_eq!(r.header("content-type"), Some("application/problem+json")); assert_eq!(r.json().unwrap()["status"], 401); let b = get(&a, "/api/v1/brief").await; assert_eq!(b["you"], "claude-oxygen · Andy McCall's agent"); assert_eq!(b["project"], "oxygen"); assert_eq!(get(&a, "/api/v1/agents").await[0]["display"], "claude-oxygen · Andy McCall's agent"); assert_eq!(get(&a, "/api/v1/projects").await[0]["name"], "oxygen"); let chans = get(&a, "/api/v1/channels").await; assert!(chans.as_array().unwrap().iter().any(|c| c["display"] == "#general")); // Post, page, and read a thread. let mut first = String::new(); for i in 0..5 { let r = a .send( "POST", "/api/v1/channels/general/messages", &[], serde_json::to_vec(&json!({ "body": format!("note {i} for @andy") })).unwrap(), ) .await .unwrap(); assert_eq!(r.status, 201, "{}", String::from_utf8_lossy(&r.body)); if i == 0 { first = r.json().unwrap()["id"].as_str().unwrap().to_string(); } } let p1 = get(&a, "/api/v1/channels/general/messages?limit=2").await; assert_eq!(p1["items"].as_array().unwrap().len(), 2); let r = a .send( "POST", "/api/v1/channels/general/messages", &[], serde_json::to_vec(&json!({ "body": "a reply", "reply_to": first })).unwrap(), ) .await .unwrap(); assert_eq!(r.status, 201); let t = get(&a, &format!("/api/v1/threads/{first}")).await; assert_eq!(t["items"].as_array().unwrap().len(), 2); let r = a.send("POST", "/api/v1/channels/system/messages", &[], serde_json::to_vec(&json!({ "body": "x" })).unwrap()).await.unwrap(); assert_eq!((r.status, r.json().unwrap()["status"].as_u64()), (400, Some(400)), "#system refuses, as a problem"); // The owner's inbox, acked; search. let inbox = get(&owner, "/api/v1/inbox").await; assert_eq!(inbox.as_array().unwrap().len(), 5); let ids: Vec = inbox.as_array().unwrap().iter().map(|i| i["id"].clone()).collect(); let r = owner.send("POST", "/api/v1/inbox/ack", &[], serde_json::to_vec(&json!({ "ids": ids })).unwrap()).await.unwrap(); assert_eq!(r.json().unwrap()["read"], 5); assert_eq!(get(&owner, "/api/v1/inbox").await.as_array().unwrap().len(), 0); assert_eq!(get(&a, "/api/v1/search?q=note&limit=3").await.as_array().unwrap().len(), 3); // ETags: the same answer twice is a 304. let r = a.send("GET", "/api/v1/projects", &[], vec![]).await.unwrap(); let tag = r.header("etag").unwrap().to_string(); assert_eq!(a.send("GET", "/api/v1/projects", &[("if-none-match", tag)], vec![]).await.unwrap().status, 304); // The log is the owner's; it pages; the stream follows. assert_eq!(a.send("GET", "/api/v1/events", &[], vec![]).await.unwrap().status, 403); let ev = get(&owner, "/api/v1/events?limit=5").await; assert_eq!(ev["items"][0]["kind"], "identity.owner"); assert_eq!(ev["next"], 5); let posted = get(&owner, "/api/v1/events?kind=message.posted&limit=50").await; assert_eq!(posted["items"].as_array().unwrap().len(), 6); assert!(posted["items"].as_array().unwrap().iter().all(|e| e["method"] == "signature" && e["assurance"] == 2)); let stream = tokio::spawn({ let owner = Client::new(&srv.socket, Some(KeyPair::load_home(&d.join("owner")).unwrap())); async move { owner.send("GET", "/api/v1/events?follow=1&max=1&kind=message.posted", &[], vec![]).await.unwrap() } }); tokio::time::sleep(std::time::Duration::from_millis(300)).await; a.send("POST", "/api/v1/channels/general/messages", &[], serde_json::to_vec(&json!({ "body": "streamed" })).unwrap()).await.unwrap(); let s = tokio::time::timeout(std::time::Duration::from_secs(10), stream).await.expect("the stream ends after max events").unwrap(); assert!(String::from_utf8_lossy(&s.body).contains("\"kind\":\"message.posted\""), "{}", String::from_utf8_lossy(&s.body)); srv.stop(); }