use agent_ask::{
build_question, cid_of, create_app, generate_keypair, verify_artifact, AppState,
BuildQuestionOpts, Store,
};
use axum::body::Body;
use axum::http::Request;
use http_body_util::BodyExt;
use serde_json::Value;
use tower::util::ServiceExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let store = Store::open(":memory:")?;
let kp = generate_keypair();
let app = create_app(AppState::new(store));
let q = build_question(
&kp,
BuildQuestionOpts {
title: "Why CIDv1 raw+sha256?".into(),
body: "So any JCS-compatible runtime computes the same id.".into(),
tags: vec!["meta".into(), "cid".into()],
..Default::default()
},
)?;
let expected_cid = cid_of(&q)?;
println!("author = {}...", &kp.did[..24]);
println!("cid = {expected_cid}");
let bytes = serde_json::to_vec(&q)?;
let res = app
.clone()
.oneshot(
Request::post("/questions")
.header("content-type", "application/json")
.header("content-length", bytes.len().to_string())
.body(Body::from(bytes))?,
)
.await?;
let status = res.status();
let body: Value = serde_json::from_slice(&res.into_body().collect().await?.to_bytes())?;
println!("POST /questions -> {status} {body}");
assert_eq!(status, 201);
assert_eq!(body["cid"].as_str(), Some(expected_cid.as_str()));
let get = app
.oneshot(
Request::get(format!("/artifact/{expected_cid}"))
.body(Body::empty())?,
)
.await?;
let fetched: Value = serde_json::from_slice(&get.into_body().collect().await?.to_bytes())?;
assert_eq!(cid_of(&fetched)?, expected_cid, "CID drift — JCS bytes differ");
let v = verify_artifact(&fetched);
assert!(v.ok, "verify failed: {:?}", v.errors);
println!("GET /artifact/{}... -> verified={}", &expected_cid[..14], v.ok);
println!("ok");
Ok(())
}