Skip to content

Commit

Permalink
Fix bsky-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Nov 26, 2024
1 parent 5bad0a1 commit 04fd7dc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 67 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bsky-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ unicode-segmentation = { version = "1.11.0", optional = true }
trait-variant.workspace = true

[dev-dependencies]
atrium-common.workspace = true
ipld-core.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

Expand Down
54 changes: 45 additions & 9 deletions bsky-sdk/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct BskyAgent<T = ReqwestClient, S = MemorySessionStore>
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
inner: Arc<AtpAgent<S, T>>,
}
Expand All @@ -50,6 +51,7 @@ pub struct BskyAgent<T, S = MemorySessionStore>
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
inner: Arc<AtpAgent<S, T>>,
}
Expand All @@ -67,6 +69,7 @@ impl<T, S> BskyAgent<T, S>
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
/// Get the agent's current state as a [`Config`].
pub async fn to_config(&self) -> Config {
Expand Down Expand Up @@ -249,6 +252,7 @@ impl<T, S> Deref for BskyAgent<T, S>
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
type Target = AtpAgent<S, T>;

Expand All @@ -258,30 +262,62 @@ where
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use super::*;
use atrium_api::agent::atp_agent::AtpSession;
use atrium_api::{
agent::atp_agent::AtpSession, com::atproto::server::create_session::OutputData,
};
use atrium_common::store::Store;
use thiserror::Error;

#[derive(Error, Debug)]
#[error("mock session store error")]
pub struct MockSessionError;

#[derive(Clone)]
struct NoopStore;
pub struct MockSessionStore;

impl Store<(), AtpSession> for MockSessionStore {
type Error = MockSessionError;

impl AtpSessionStore for NoopStore {
async fn get_session(&self) -> Option<AtpSession> {
async fn get(&self, _: &()) -> core::result::Result<Option<AtpSession>, Self::Error> {
Ok(Some(
OutputData {
access_jwt: String::from("access"),
active: None,
did: "did:fake:handle.test".parse().expect("invalid did"),
did_doc: None,
email: None,
email_auth_factor: None,
email_confirmed: None,
handle: "handle.test".parse().expect("invalid handle"),
refresh_jwt: String::from("refresh"),
status: None,
}
.into(),
))
}
async fn set(&self, _: (), _: AtpSession) -> core::result::Result<(), Self::Error> {
unimplemented!()
}
async fn set_session(&self, _: AtpSession) {
async fn del(&self, _: &()) -> core::result::Result<(), Self::Error> {
unimplemented!()
}
async fn clear_session(&self) {
async fn clear(&self) -> core::result::Result<(), Self::Error> {
unimplemented!()
}
}

impl AtpSessionStore for MockSessionStore {}

#[cfg(feature = "default-client")]
#[tokio::test]
async fn clone_agent() {
let agent =
BskyAgent::builder().store(NoopStore).build().await.expect("failed to build agent");
let agent = BskyAgent::builder()
.store(MockSessionStore)
.build()
.await
.expect("failed to build agent");
let cloned = agent.clone();

agent.configure_endpoint(String::from("https://example.com"));
Expand Down
32 changes: 4 additions & 28 deletions bsky-sdk/src/agent/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct BskyAtpAgentBuilder<T, S = MemorySessionStore>
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
config: Config,
store: S,
Expand All @@ -35,6 +36,7 @@ impl<T, S> BskyAtpAgentBuilder<T, S>
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
/// Set the configuration for the agent.
pub fn config(mut self, config: Config) -> Self {
Expand All @@ -47,6 +49,7 @@ where
pub fn store<S0>(self, store: S0) -> BskyAtpAgentBuilder<T, S0>
where
S0: AtpSessionStore + Send + Sync,
S0::Error: std::error::Error + Send + Sync + 'static,
{
BskyAtpAgentBuilder { config: self.config, store, client: self.client }
}
Expand Down Expand Up @@ -105,34 +108,7 @@ impl Default for BskyAtpAgentBuilder<ReqwestClient, MemorySessionStore> {
#[cfg(test)]
mod tests {
use super::*;
use atrium_api::agent::atp_agent::AtpSession;
use atrium_api::com::atproto::server::create_session::OutputData;

fn session() -> AtpSession {
OutputData {
access_jwt: String::new(),
active: None,
did: "did:fake:handle.test".parse().expect("invalid did"),
did_doc: None,
email: None,
email_auth_factor: None,
email_confirmed: None,
handle: "handle.test".parse().expect("invalid handle"),
refresh_jwt: String::new(),
status: None,
}
.into()
}

struct MockSessionStore;

impl AtpSessionStore for MockSessionStore {
async fn get_session(&self) -> Option<AtpSession> {
Some(session())
}
async fn set_session(&self, _: AtpSession) {}
async fn clear_session(&self) {}
}
use crate::agent::tests::MockSessionStore;

#[cfg(feature = "default-client")]
#[tokio::test]
Expand Down
35 changes: 5 additions & 30 deletions bsky-sdk/src/record.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Record operations.
mod agent;

use std::future::Future;

use crate::error::{Error, Result};
use crate::BskyAgent;
use atrium_api::agent::atp_agent::store::AtpSessionStore;
Expand All @@ -11,12 +9,14 @@ use atrium_api::com::atproto::repo::{
};
use atrium_api::types::{Collection, LimitedNonZeroU8, TryIntoUnknown};
use atrium_api::xrpc::XrpcClient;
use std::future::Future;

#[cfg_attr(not(target_arch = "wasm32"), trait_variant::make(Send))]
pub trait Record<T, S>
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
fn list(
agent: &BskyAgent<T, S>,
Expand Down Expand Up @@ -46,6 +46,7 @@ macro_rules! record_impl {
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
async fn list(
agent: &BskyAgent<T, S>,
Expand Down Expand Up @@ -163,6 +164,7 @@ macro_rules! record_impl {
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
async fn list(
agent: &BskyAgent<T, S>,
Expand Down Expand Up @@ -273,10 +275,7 @@ record_impl!(
#[cfg(test)]
mod tests {
use super::*;
use crate::agent::BskyAtpAgentBuilder;
use crate::tests::FAKE_CID;
use atrium_api::agent::atp_agent::AtpSession;
use atrium_api::com::atproto::server::create_session::OutputData;
use crate::{agent::tests::MockSessionStore, agent::BskyAtpAgentBuilder, tests::FAKE_CID};
use atrium_api::types::string::Datetime;
use atrium_api::xrpc::http::{Request, Response};
use atrium_api::xrpc::types::Header;
Expand Down Expand Up @@ -319,30 +318,6 @@ mod tests {
}
}

struct MockSessionStore;

impl AtpSessionStore for MockSessionStore {
async fn get_session(&self) -> Option<AtpSession> {
Some(
OutputData {
access_jwt: String::from("access"),
active: None,
did: "did:fake:handle.test".parse().expect("invalid did"),
did_doc: None,
email: None,
email_auth_factor: None,
email_confirmed: None,
handle: "handle.test".parse().expect("invalid handle"),
refresh_jwt: String::from("refresh"),
status: None,
}
.into(),
)
}
async fn set_session(&self, _: AtpSession) {}
async fn clear_session(&self) {}
}

#[tokio::test]
async fn actor_profile() -> Result<()> {
let agent = BskyAtpAgentBuilder::new(MockClient).store(MockSessionStore).build().await?;
Expand Down
1 change: 1 addition & 0 deletions bsky-sdk/src/record/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl<T, S> BskyAgent<T, S>
where
T: XrpcClient + Send + Sync,
S: AtpSessionStore + Send + Sync,
S::Error: std::error::Error + Send + Sync + 'static,
{
/// Create a record with various types of data.
/// For example, the Record families defined in [`KnownRecord`](atrium_api::record::KnownRecord) are supported.
Expand Down

0 comments on commit 04fd7dc

Please sign in to comment.