use chrono::{Duration, Utc};
use fauna_api::authentification::{self, ApiKey, CredentialsOrApiKey};
use fauna_api::calendar::{self, Answer, Draft};
use fauna_api::chats::{self, CatchUpOptions, OwnerKind, SearchOptions, UnreadOptions};
use fauna_api::groups;
use fauna_api::parrot::generative::Author;
use fauna_api::parrot::{self, chat::CreateChatOptions};
const ROUTE: &str = "http://localhost:32862";
fn key(id: &str) -> CredentialsOrApiKey {
CredentialsOrApiKey::ApiKey(ApiKey {
id: id.to_string(),
..Default::default()
})
}
#[tokio::test]
#[ignore = "needs the local Fauna stack on :32862"]
async fn agents_keep_up_and_plan_through_keys() {
fauna_api::set_route(ROUTE);
parrot::set_route(ROUTE);
let premium = authentification::authorize_token(
&authentification::authorize("premium@faunakit.com", "password")
.await
.expect("premium signs in"),
)
.await
.expect("premium credentials");
let enterprise = authentification::authorize_token(
&authentification::authorize("enterprise@faunakit.com", "password")
.await
.expect("enterprise signs in"),
)
.await
.expect("enterprise credentials");
let group = groups::create(&premium, "Rust agents")
.await
.expect("group created");
let invite = groups::invite(
&premium,
&group.id,
"enterprise@faunakit.com",
groups::InviteOptions {
notify: Some(false),
},
)
.await
.expect("enterprise invited");
groups::accept_invite(&enterprise, &invite)
.await
.expect("enterprise joins");
let chat = parrot::chat::create_chat(
&CredentialsOrApiKey::Credentials(premium.clone()),
Some("Agents"),
Some(&group),
CreateChatOptions {
human: Some(true),
..Default::default()
},
)
.await
.expect("people only chat created");
let mine = key("sk-PREMIUM");
let theirs = key("sk-ENTERPRISE");
chats::catch_up(
&theirs,
CatchUpOptions {
min_trust: Some(0.0),
max: Some(500),
},
)
.await
.expect("enterprise's agent starts caught up");
chats::post(&mine, &chat, "Rust says the build is green.")
.await
.expect("premium's agent posts");
let unread = chats::unread(&theirs, UnreadOptions::default())
.await
.expect("unread");
assert!(unread.chats.iter().any(|c| c.id == chat && c.unread == 1));
let listed = chats::list(&theirs).await.expect("shared chats");
let shared = listed
.iter()
.find(|c| c.id == chat)
.expect("the chat is shared");
assert_eq!(shared.owner.kind, OwnerKind::Group);
assert_eq!(shared.owner.trust, None);
let caught = chats::catch_up(&theirs, CatchUpOptions::default())
.await
.expect("catch up");
let message = caught
.chats
.iter()
.find(|c| c.id == chat)
.and_then(|c| c.messages.first())
.expect("the post arrives");
assert_eq!(message.text, "Rust says the build is green.");
assert!(matches!(
&message.author,
Some(Author::ApiKey(key)) if key.name.as_deref() == Some("premium.fauna.com")
));
let found = chats::search(&theirs, "build green", SearchOptions::default())
.await
.expect("search");
assert!(found.iter().any(|f| f.chat.id == chat));
let start = Utc::now() + Duration::days(3);
let event = calendar::create(
&mine,
Draft {
title: "Rust review".to_string(),
start,
end: start + Duration::hours(1),
all_day: None,
time_zone: Some("America/New_York".to_string()),
place: None,
notes: None,
recurrence: None,
reminder: Some(0),
attendees: Some(vec!["enterprise@faunakit.com".to_string()]),
webhook: None,
},
None,
)
.await
.expect("event created");
assert_eq!(event.start.timestamp(), start.timestamp());
let answered = calendar::respond(&theirs, &event.id, Answer::Yes)
.await
.expect("enterprise answers");
assert_eq!(answered.response, Some(Answer::Yes));
calendar::remove(&mine, &event.id)
.await
.expect("event removed");
groups::remove(&premium, &group.id)
.await
.expect("group deleted");
}