| e071e91a Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
mod common; |
| 6 |
|
| 7 |
use common::{add_agent, setup, Serving}; |
| 8 |
use serde_json::{json, Value}; |
| 9 |
use spectra_core::keys::KeyPair; |
| 10 |
use spectra_mcp::client::Client; |
| 11 |
|
| 12 |
async fn get(c: &Client, path: &str) -> Value { |
| 13 |
let r = c.send("GET", path, &[], vec![]).await.unwrap(); |
| 14 |
assert_eq!(r.status, 200, "{path}: {}", String::from_utf8_lossy(&r.body)); |
| 15 |
r.json().unwrap() |
| 16 |
} |
| 17 |
|
| 18 |
#[tokio::test] |
| 19 |
async fn every_endpoint_for_an_agent_and_the_owner() { |
| 20 |
let dir = setup(); |
| 21 |
let d = dir.path(); |
| 22 |
let claude = add_agent(d, "claude-oxygen", &[]); |
| 23 |
let srv = Serving::start(d); |
| 24 |
let a = Client::new(&srv.socket, Some(claude)); |
| 25 |
let owner = Client::new(&srv.socket, Some(KeyPair::load_home(&d.join("owner")).unwrap())); |
| 26 |
let anon = Client::new(&srv.socket, None); |
| 27 |
|
| 28 |
assert_eq!(get(&anon, "/api/v1/health").await["ok"], true); |
| 29 |
assert_eq!(get(&anon, "/api/v1/version").await["ledger_format"], 1); |
| 30 |
let r = anon.send("GET", "/api/v1/agents", &[], vec![]).await.unwrap(); |
| 31 |
assert_eq!(r.status, 401); |
| 32 |
assert_eq!(r.header("content-type"), Some("application/problem+json")); |
| 33 |
assert_eq!(r.json().unwrap()["status"], 401); |
| 34 |
|
| 35 |
let b = get(&a, "/api/v1/brief").await; |
| 36 |
assert_eq!(b["you"], "claude-oxygen · Andy McCall's agent"); |
| 37 |
assert_eq!(b["project"], "oxygen"); |
| 38 |
assert_eq!(get(&a, "/api/v1/agents").await[0]["display"], "claude-oxygen · Andy McCall's agent"); |
| 39 |
assert_eq!(get(&a, "/api/v1/projects").await[0]["name"], "oxygen"); |
| 40 |
let chans = get(&a, "/api/v1/channels").await; |
| 41 |
assert!(chans.as_array().unwrap().iter().any(|c| c["display"] == "#general")); |
| 42 |
|
| 43 |
|
| 44 |
let mut first = String::new(); |
| 45 |
for i in 0..5 { |
| 46 |
let r = a |
| 47 |
.send( |
| 48 |
"POST", |
| 49 |
"/api/v1/channels/general/messages", |
| 50 |
&[], |
| 51 |
serde_json::to_vec(&json!({ "body": format!("note {i} for @andy") })).unwrap(), |
| 52 |
) |
| 53 |
.await |
| 54 |
.unwrap(); |
| 55 |
assert_eq!(r.status, 201, "{}", String::from_utf8_lossy(&r.body)); |
| 56 |
if i == 0 { |
| 57 |
first = r.json().unwrap()["id"].as_str().unwrap().to_string(); |
| 58 |
} |
| 59 |
} |
| 60 |
let p1 = get(&a, "/api/v1/channels/general/messages?limit=2").await; |
| 61 |
assert_eq!(p1["items"].as_array().unwrap().len(), 2); |
| 62 |
let r = a |
| 63 |
.send( |
| 64 |
"POST", |
| 65 |
"/api/v1/channels/general/messages", |
| 66 |
&[], |
| 67 |
serde_json::to_vec(&json!({ "body": "a reply", "reply_to": first })).unwrap(), |
| 68 |
) |
| 69 |
.await |
| 70 |
.unwrap(); |
| 71 |
assert_eq!(r.status, 201); |
| 72 |
let t = get(&a, &format!("/api/v1/threads/{first}")).await; |
| 73 |
assert_eq!(t["items"].as_array().unwrap().len(), 2); |
| 74 |
let r = a.send("POST", "/api/v1/channels/system/messages", &[], serde_json::to_vec(&json!({ "body": "x" })).unwrap()).await.unwrap(); |
| 75 |
assert_eq!((r.status, r.json().unwrap()["status"].as_u64()), (400, Some(400)), "#system refuses, as a problem"); |
| 76 |
|
| 77 |
|
| 78 |
let inbox = get(&owner, "/api/v1/inbox").await; |
| 79 |
assert_eq!(inbox.as_array().unwrap().len(), 5); |
| 80 |
let ids: Vec<Value> = inbox.as_array().unwrap().iter().map(|i| i["id"].clone()).collect(); |
| 81 |
let r = owner.send("POST", "/api/v1/inbox/ack", &[], serde_json::to_vec(&json!({ "ids": ids })).unwrap()).await.unwrap(); |
| 82 |
assert_eq!(r.json().unwrap()["read"], 5); |
| 83 |
assert_eq!(get(&owner, "/api/v1/inbox").await.as_array().unwrap().len(), 0); |
| 84 |
assert_eq!(get(&a, "/api/v1/search?q=note&limit=3").await.as_array().unwrap().len(), 3); |
| 85 |
|
| 86 |
|
| 87 |
let r = a.send("GET", "/api/v1/projects", &[], vec![]).await.unwrap(); |
| 88 |
let tag = r.header("etag").unwrap().to_string(); |
| 89 |
assert_eq!(a.send("GET", "/api/v1/projects", &[("if-none-match", tag)], vec![]).await.unwrap().status, 304); |
| 90 |
|
| 91 |
|
| 92 |
assert_eq!(a.send("GET", "/api/v1/events", &[], vec![]).await.unwrap().status, 403); |
| 93 |
let ev = get(&owner, "/api/v1/events?limit=5").await; |
| 94 |
assert_eq!(ev["items"][0]["kind"], "identity.owner"); |
| 95 |
assert_eq!(ev["next"], 5); |
| 96 |
let posted = get(&owner, "/api/v1/events?kind=message.posted&limit=50").await; |
| 97 |
assert_eq!(posted["items"].as_array().unwrap().len(), 6); |
| 98 |
assert!(posted["items"].as_array().unwrap().iter().all(|e| e["method"] == "signature" && e["assurance"] == 2)); |
| 99 |
let stream = tokio::spawn({ |
| 100 |
let owner = Client::new(&srv.socket, Some(KeyPair::load_home(&d.join("owner")).unwrap())); |
| 101 |
async move { owner.send("GET", "/api/v1/events?follow=1&max=1&kind=message.posted", &[], vec![]).await.unwrap() } |
| 102 |
}); |
| 103 |
tokio::time::sleep(std::time::Duration::from_millis(300)).await; |
| 104 |
a.send("POST", "/api/v1/channels/general/messages", &[], serde_json::to_vec(&json!({ "body": "streamed" })).unwrap()).await.unwrap(); |
| 105 |
let s = tokio::time::timeout(std::time::Duration::from_secs(10), stream).await.expect("the stream ends after max events").unwrap(); |
| 106 |
assert!(String::from_utf8_lossy(&s.body).contains("\"kind\":\"message.posted\""), "{}", String::from_utf8_lossy(&s.body)); |
| 107 |
srv.stop(); |
| 108 |
} |