Skip to content

Commit d180729

Browse files
committed
make a grpc/http test client
1 parent 38557b4 commit d180729

25 files changed

+189
-263
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xmtp_api/src/test_utils.rs

+39-7
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,30 @@ pub use wasm::*;
9090
#[cfg(not(target_arch = "wasm32"))]
9191
mod not_wasm {
9292
use super::*;
93+
use xmtp_proto::api_client::ApiBuilder;
9394
use xmtp_proto::xmtp::mls::api::v1::WelcomeMessage;
9495
#[derive(Clone)]
9596
pub struct ApiClient;
97+
pub struct MockApiBuilder;
98+
99+
impl ApiBuilder for MockApiBuilder {
100+
type Output = ApiClient;
101+
type Error = MockError;
102+
103+
fn set_libxmtp_version(&mut self, version: String) -> Result<(), Self::Error> {
104+
Ok(())
105+
}
106+
fn set_app_version(&mut self, version: String) -> Result<(), Self::Error> {
107+
Ok(())
108+
}
109+
fn set_host(&mut self, host: String) {
110+
}
111+
fn set_payer(&mut self, _host: String) {}
112+
fn set_tls(&mut self, tls: bool) {}
113+
async fn build(self) -> Result<Self::Output, Self::Error> {
114+
Ok(ApiClient)
115+
}
116+
}
96117

97118
mock! {
98119
pub ApiClient { }
@@ -142,12 +163,23 @@ mod not_wasm {
142163
-> Result<VerifySmartContractWalletSignaturesResponse, MockError>;
143164
}
144165

145-
#[async_trait::async_trait]
146166
impl XmtpTestClient for ApiClient {
147-
type Builder = ApiClient;
148-
async fn create_local() -> Self { ApiClient }
149-
async fn create_dev() -> Self { ApiClient }
167+
type Builder = MockApiBuilder;
168+
fn create_local() -> MockApiBuilder { MockApiBuilder }
169+
fn create_dev() -> MockApiBuilder { MockApiBuilder }
170+
}
171+
/*
172+
impl ApiBuilder for ApiClient {
173+
type Output = ApiClient;
174+
type Error = MockError;
175+
fn set_libxmtp_version(&mut self, version: String) -> Result<(), Self::Error>;
176+
fn set_app_version(&mut self, version: String) -> Result<(), Self::Error>;
177+
fn set_host(&mut self, host: String);
178+
fn set_payer(&mut self, _host: String);
179+
fn set_tls(&mut self, tls: bool);
180+
async fn build(self) -> Result<Self::Output, Self::Error>;
150181
}
182+
*/
151183
}
152184
}
153185

@@ -208,9 +240,9 @@ mod wasm {
208240

209241
#[async_trait::async_trait(?Send)]
210242
impl XmtpTestClient for ApiClient {
211-
type Builder = ApiClient;
212-
async fn create_local() -> Self { ApiClient }
213-
async fn create_dev() -> Self { ApiClient }
243+
type Builder = ();
244+
fn create_local() -> () { () }
245+
fn create_dev() -> () { () }
214246
}
215247
}
216248
}

xmtp_api_d14n/Cargo.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ prost-types.workspace = true
1414
xmtp_common.workspace = true
1515
xmtp_proto = { workspace = true, features = ["convert"] }
1616

17+
[dev-dependencies]
18+
xmtp_proto = { workspace = true, features = ["convert", "test-utils"] }
19+
xmtp_common = { workspace = true, features = ["test-utils"] }
20+
wasm-bindgen-test.workspace = true
21+
1722
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
18-
xmtp_api_grpc.workspace = true
23+
xmtp_api_grpc = { workspace = true, features = ["test-utils"] }
1924
xmtp_api_http.workspace = true
2025
tokio.workspace = true
2126

@@ -24,5 +29,7 @@ xmtp_api_http.workspace = true
2429
tokio.workspace = true
2530

2631
[features]
32+
default = ["grpc-api"]
2733
http-api = ["xmtp_proto/http-api"]
2834
grpc-api = ["xmtp_proto/grpc-api"]
35+
test-utils = []

xmtp_api_d14n/src/endpoints/d14n/get_inbox_ids.rs

+3-35
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ impl Endpoint for GetInboxIds {
4848
}
4949
}
5050

51-
#[cfg(all(test, not(target_arch = "wasm32")))]
51+
#[cfg(test)]
5252
mod test {
5353
use crate::d14n::GetInboxIds;
54-
use xmtp_proto::traits::Query;
54+
use xmtp_proto::prelude::*;
5555
use xmtp_proto::xmtp::xmtpv4::message_api::GetInboxIdsResponse;
5656

5757
#[test]
@@ -61,41 +61,9 @@ mod test {
6161
println!("{}", pnq);
6262
}
6363

64-
#[cfg(feature = "grpc-api")]
6564
#[tokio::test]
6665
async fn test_get_inbox_ids() {
67-
use crate::d14n::GetInboxIds;
68-
use xmtp_api_grpc::grpc_client::GrpcClient;
69-
use xmtp_api_grpc::LOCALHOST_ADDRESS;
70-
use xmtp_proto::api_client::ApiBuilder;
71-
use xmtp_proto::traits::Query;
72-
73-
let mut client = GrpcClient::builder();
74-
client.set_app_version("0.0.0".into()).unwrap();
75-
client.set_tls(false);
76-
client.set_host(LOCALHOST_ADDRESS.to_string());
77-
let client = client.build().await.unwrap();
78-
79-
let endpoint = GetInboxIds::builder()
80-
.addresses(vec!["".to_string()])
81-
.build()
82-
.unwrap();
83-
84-
//todo: fix later when it was implemented
85-
let result = endpoint.query(&client).await;
86-
assert!(result.is_err());
87-
}
88-
89-
#[cfg(feature = "http-api")]
90-
#[tokio::test]
91-
async fn test_get_inbox_ids_http() {
92-
use xmtp_proto::api_client::ApiBuilder;
93-
94-
let mut client = XmtpHttpApiClient::builder();
95-
client.set_app_version("0.0.0".into()).unwrap();
96-
client.set_libxmtp_version("0.0.0".into()).unwrap();
97-
client.set_tls(true);
98-
client.set_host(LOCALHOST_ADDRESS.to_string());
66+
let client = crate::TestClient::create_local();
9967
let client = client.build().await.unwrap();
10068

10169
let endpoint = GetInboxIds::builder()

xmtp_api_d14n/src/endpoints/d14n/publish_client_envelopes.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ impl Endpoint for PublishClientEnvelopes {
3939
}
4040
}
4141

42-
#[cfg(all(test, not(target_arch = "wasm32")))]
42+
#[cfg(test)]
4343
mod test {
44+
use super::*;
45+
use xmtp_proto::prelude::*;
46+
4447
#[test]
4548
fn test_file_descriptor() {
4649
use xmtp_proto::xmtp::xmtpv4::payer_api::{
@@ -51,20 +54,11 @@ mod test {
5154
println!("{}", pnq);
5255
}
5356

54-
#[cfg(feature = "grpc-api")]
5557
#[tokio::test]
5658
async fn test_get_inbox_ids() {
57-
use crate::d14n::PublishClientEnvelopes;
58-
use xmtp_api_grpc::grpc_client::GrpcClient;
59-
use xmtp_api_grpc::LOCALHOST_ADDRESS;
60-
use xmtp_proto::api_client::ApiBuilder;
61-
use xmtp_proto::traits::Query;
6259
use xmtp_proto::xmtp::xmtpv4::envelopes::ClientEnvelope;
6360

64-
let mut client = GrpcClient::builder();
65-
client.set_app_version("0.0.0".into()).unwrap();
66-
client.set_tls(false);
67-
client.set_host(LOCALHOST_ADDRESS.to_string());
61+
let client = crate::TestClient::create_local();
6862
let client = client.build().await.unwrap();
6963

7064
let endpoint = PublishClientEnvelopes::builder()

xmtp_api_d14n/src/endpoints/d14n/query_envelopes.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,24 @@ impl Endpoint for QueryEnvelopes {
8181
}
8282
}
8383

84-
#[cfg(all(test, not(target_arch = "wasm32")))]
84+
#[cfg(test)]
8585
mod test {
86+
use xmtp_proto::prelude::*;
87+
use super::*;
88+
use wasm_bindgen_test::wasm_bindgen_test;
89+
8690
#[test]
8791
fn test_file_descriptor() {
8892
use xmtp_proto::xmtp::xmtpv4::message_api::{QueryEnvelopesRequest, FILE_DESCRIPTOR_SET};
8993
let pnq = crate::path_and_query::<QueryEnvelopesRequest>(FILE_DESCRIPTOR_SET);
9094
println!("{}", pnq);
9195
}
9296

93-
#[cfg(feature = "grpc-api")]
94-
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
95-
#[cfg(not(target_arch = "wasm32"))]
97+
#[wasm_bindgen_test(unsupported = tokio::test)]
9698
async fn test_get_inbox_ids() {
9799
use crate::d14n::QueryEnvelopes;
98-
use xmtp_api_grpc::grpc_client::GrpcClient;
99-
use xmtp_api_grpc::LOCALHOST_ADDRESS;
100-
use xmtp_proto::api_client::ApiBuilder;
101-
use xmtp_proto::traits::Query;
102-
use xmtp_proto::xmtp::xmtpv4::message_api::EnvelopesQuery;
103-
104-
let mut client = GrpcClient::builder();
105-
client.set_app_version("0.0.0".into()).unwrap();
106-
client.set_tls(false);
107-
client.set_host(LOCALHOST_ADDRESS.to_string());
100+
101+
let client = crate::TestClient::create_local();
108102
let client = client.build().await.unwrap();
109103

110104
let endpoint = QueryEnvelopes::builder()

xmtp_api_d14n/src/endpoints/v3/identity/get_identity_updates_v2.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,20 @@ impl Endpoint for GetIdentityUpdatesV2 {
3636
}
3737
}
3838

39-
#[cfg(all(test, not(target_arch = "wasm32")))]
39+
#[cfg(test)]
4040
mod test {
41+
use super::*;
42+
use xmtp_proto::prelude::*;
4143

4244
#[test]
4345
fn test_file_descriptor() {
44-
use xmtp_proto::xmtp::identity::api::v1::{GetIdentityUpdatesRequest, FILE_DESCRIPTOR_SET};
4546
let pnq = crate::path_and_query::<GetIdentityUpdatesRequest>(FILE_DESCRIPTOR_SET);
4647
println!("{}", pnq);
4748
}
4849

49-
#[cfg(feature = "grpc-api")]
5050
#[tokio::test]
5151
async fn test_get_identity_updates_v2() {
52-
use crate::v3::GetIdentityUpdatesV2;
53-
use xmtp_api_grpc::grpc_client::GrpcClient;
54-
use xmtp_api_grpc::LOCALHOST_ADDRESS;
55-
use xmtp_proto::api_client::ApiBuilder;
56-
use xmtp_proto::traits::Query;
57-
use xmtp_proto::xmtp::identity::api::v1::{
58-
get_identity_updates_request::Request, GetIdentityUpdatesResponse,
59-
};
60-
61-
let mut client = GrpcClient::builder();
62-
client.set_app_version("0.0.0".into()).unwrap();
63-
client.set_tls(false);
64-
client.set_host(LOCALHOST_ADDRESS.to_string());
52+
let client = crate::TestClient::create_local();
6553
let client = client.build().await.unwrap();
6654
let endpoint = GetIdentityUpdatesV2::builder()
6755
.requests(vec![Request {

xmtp_api_d14n/src/endpoints/v3/identity/get_inbox_ids.rs

+4-39
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ impl Endpoint for GetInboxIds {
4646
}
4747
}
4848

49-
#[cfg(all(test, not(target_arch = "wasm32")))]
49+
#[cfg(test)]
5050
mod test {
51-
use crate::v3::GetInboxIds;
52-
use xmtp_proto::traits::Query;
51+
use super::*;
52+
use xmtp_proto::prelude::*;
5353
use xmtp_proto::xmtp::identity::api::v1::GetInboxIdsResponse;
5454

5555
#[test]
@@ -60,44 +60,9 @@ mod test {
6060
println!("{}", pnq);
6161
}
6262

63-
#[cfg(feature = "grpc-api")]
6463
#[tokio::test]
65-
#[ignore]
6664
async fn test_get_inbox_ids() {
67-
use crate::v3::identity::GetInboxIds;
68-
use xmtp_api_grpc::grpc_client::GrpcClient;
69-
use xmtp_api_grpc::LOCALHOST_ADDRESS;
70-
use xmtp_proto::api_client::ApiBuilder;
71-
use xmtp_proto::traits::Query;
72-
use xmtp_proto::xmtp::identity::api::v1::GetInboxIdsResponse;
73-
74-
let mut client = GrpcClient::builder();
75-
client.set_app_version("0.0.0".into()).unwrap();
76-
client.set_tls(false);
77-
client.set_host(LOCALHOST_ADDRESS.to_string());
78-
let client = client.build().await.unwrap();
79-
80-
let endpoint = GetInboxIds::builder()
81-
.addresses(vec!["".to_string()])
82-
.build()
83-
.unwrap();
84-
85-
let result: GetInboxIdsResponse = endpoint.query(&client).await.unwrap();
86-
assert_eq!(result.responses.len(), 0);
87-
}
88-
89-
#[cfg(feature = "http-api")]
90-
#[tokio::test]
91-
async fn test_get_inbox_ids_http() {
92-
use xmtp_api_http::XmtpHttpApiClient;
93-
use xmtp_api_http::LOCALHOST_ADDRESS;
94-
use xmtp_proto::api_client::ApiBuilder;
95-
96-
let mut client = XmtpHttpApiClient::builder();
97-
client.set_app_version("0.0.0".into()).unwrap();
98-
client.set_libxmtp_version("0.0.0".into()).unwrap();
99-
client.set_tls(true);
100-
client.set_host(LOCALHOST_ADDRESS.to_string());
65+
let client = crate::TestClient::create_local();
10166
let client = client.build().await.unwrap();
10267

10368
let endpoint = GetInboxIds::builder()

xmtp_api_d14n/src/endpoints/v3/identity/publish_identity_update.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ impl Endpoint for PublishIdentityUpdate {
3838
}
3939
}
4040

41-
#[cfg(all(test, not(target_arch = "wasm32")))]
41+
#[cfg(test)]
4242
mod test {
43+
use xmtp_proto::prelude::*;
44+
use super::*;
4345

4446
#[test]
4547
fn test_file_descriptor() {
@@ -50,22 +52,12 @@ mod test {
5052
println!("{}", pnq);
5153
}
5254

53-
#[cfg(feature = "grpc-api")]
5455
#[tokio::test]
5556
async fn test_publish_identity_update() {
56-
use crate::v3::PublishIdentityUpdate;
57-
use xmtp_api_grpc::grpc_client::GrpcClient;
58-
use xmtp_api_grpc::{GrpcError, LOCALHOST_ADDRESS};
5957
use xmtp_common::time::now_ns;
60-
use xmtp_proto::api_client::ApiBuilder;
61-
use xmtp_proto::traits::Query;
62-
use xmtp_proto::xmtp::identity::api::v1::PublishIdentityUpdateResponse;
63-
6458
use xmtp_proto::xmtp::identity::associations::IdentityUpdate;
65-
let mut client = GrpcClient::builder();
66-
client.set_app_version("0.0.0".into()).unwrap();
67-
client.set_tls(false);
68-
client.set_host(LOCALHOST_ADDRESS.to_string());
59+
60+
let client = crate::TestClient::create_local();
6961
let client = client.build().await.unwrap();
7062
let endpoint = PublishIdentityUpdate::builder()
7163
.identity_update(IdentityUpdate {
@@ -76,7 +68,7 @@ mod test {
7668
.build()
7769
.unwrap();
7870

79-
let _: Result<PublishIdentityUpdateResponse, xmtp_proto::traits::ApiError<GrpcError>> =
71+
let _: Result<PublishIdentityUpdateResponse, _> =
8072
endpoint.query(&client).await;
8173
}
8274
}

0 commit comments

Comments
 (0)