From 008c8905c07d9531002cc42588332f1b83e69ff8 Mon Sep 17 00:00:00 2001 From: Jeremy Massel <1123407+jkmassel@users.noreply.github.com> Date: Tue, 14 Oct 2025 10:34:29 -0600 Subject: [PATCH 1/2] Add support bot e2e tests --- wp_api/src/wp_com/support_bots.rs | 2 +- wp_com_e2e/src/main.rs | 13 +++- wp_com_e2e/src/support_bot_tests.rs | 93 +++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 wp_com_e2e/src/support_bot_tests.rs diff --git a/wp_api/src/wp_com/support_bots.rs b/wp_api/src/wp_com/support_bots.rs index fb941c387..6dc68eecc 100644 --- a/wp_api/src/wp_com/support_bots.rs +++ b/wp_api/src/wp_com/support_bots.rs @@ -117,7 +117,7 @@ pub struct BotMessageSummary { pub created_at: WpGmtDateTime, } -#[derive(Debug, PartialEq, Eq, Deserialize, uniffi::Record)] +#[derive(Debug, Default, PartialEq, Eq, Deserialize, uniffi::Record)] pub struct GetBotConversationParams { // The number of the page to retrieve, limited to 100. #[uniffi(default = None)] diff --git a/wp_com_e2e/src/main.rs b/wp_com_e2e/src/main.rs index 9cdbb6209..8842e28d4 100644 --- a/wp_com_e2e/src/main.rs +++ b/wp_com_e2e/src/main.rs @@ -1,12 +1,13 @@ use anyhow::Result; use async_trait::async_trait; use clap::{Parser, Subcommand}; -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use wp_api::{prelude::*, wp_com::client::WpComApiClient}; mod me_tests; mod oauth2_tests; mod sites_tests; +mod support_bot_tests; mod support_eligibility_test; mod support_tickets_test; @@ -47,6 +48,13 @@ async fn main() -> Result<(), anyhow::Error> { token, allow_writes, } => { + // Writes to bots can take a while, so we need to increase the timeout + let test_timeout = if allow_writes { + Duration::from_secs(60) + } else { + Duration::from_secs(10) + }; + let delegate = WpApiClientDelegate { auth_provider: WpAuthenticationProvider::static_with_auth( WpAuthentication::Bearer { @@ -54,7 +62,7 @@ async fn main() -> Result<(), anyhow::Error> { }, ) .into(), - request_executor: Arc::new(ReqwestRequestExecutor::default()), + request_executor: Arc::new(ReqwestRequestExecutor::new(false, test_timeout)), middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()), app_notifier: Arc::new(EmptyAppNotifier), }; @@ -84,6 +92,7 @@ async fn run_tests( me_tests::me_test(client).await?; oauth2_tests::oauth2_test(client, token.clone()).await?; sites_tests::sites_test(client).await?; + support_bot_tests::support_bots_test(client, allow_writes).await?; support_eligibility_test::support_eligibility_test(client).await?; support_tickets_test::support_tickets_test(client, allow_writes).await?; Ok(()) diff --git a/wp_com_e2e/src/support_bot_tests.rs b/wp_com_e2e/src/support_bot_tests.rs new file mode 100644 index 000000000..6672cca83 --- /dev/null +++ b/wp_com_e2e/src/support_bot_tests.rs @@ -0,0 +1,93 @@ +use std::collections::HashMap; + +use wp_api::wp_com::{ + client::WpComApiClient, + support_bots::{ + AddMessageToBotConversationParams, BotConversation, BotId, ChatId, + CreateBotConversationParams, GetBotConversationParams, + }, +}; + +pub async fn support_bots_test(client: &WpComApiClient, allow_writes: bool) -> anyhow::Result<()> { + let bot_id = BotId("jetpack-chat-mobile".to_string()); + println!("== Support Bots Test =="); + + let conversations = client + .support_bots() + .get_bot_converation_list(&bot_id) + .await? + .data; + + println!( + "✅ Fetch Conversation List: Found {} conversations", + conversations.len() + ); + + for conversation in conversations { + let chat_id = ChatId(conversation.chat_id); + + if let Err(e) = client + .support_bots() + .get_bot_conversation(&bot_id, &chat_id, &GetBotConversationParams::default()) + .await + { + println!( + "❌ Fetch Conversation: {} Error: {}", + conversation.chat_id, e + ); + return Err(e.into()); + } else { + println!("✅ Fetch Conversation: {}", conversation.chat_id); + } + } + + if allow_writes { + let new_conversation = create_conversation(client, &bot_id).await?; + let chat_id = ChatId(new_conversation.chat_id); + add_message_to_conversation(client, &bot_id, chat_id).await?; + } + + Ok(()) +} + +async fn create_conversation( + client: &WpComApiClient, + bot_id: &BotId, +) -> anyhow::Result { + let new_conversation = client + .support_bots() + .create_bot_conversation( + bot_id, + &CreateBotConversationParams { + message: "This is a test – it can be deleted without replying.".to_string(), + user_id: None, + }, + ) + .await? + .data; + + println!("✅ Create Conversation"); + + Ok(new_conversation) +} + +async fn add_message_to_conversation( + client: &WpComApiClient, + bot_id: &BotId, + conversation_id: ChatId, +) -> anyhow::Result<()> { + client + .support_bots() + .add_message_to_bot_conversation( + bot_id, + &conversation_id, + &AddMessageToBotConversationParams { + message: "Test Message".to_string(), + context: HashMap::new(), + }, + ) + .await?; + + println!("✅ Add Message to Conversation"); + Ok(()) +} From 35435d892f67c0ff0f8baebe4baad11d78fbf2f6 Mon Sep 17 00:00:00 2001 From: Jeremy Massel <1123407+jkmassel@users.noreply.github.com> Date: Tue, 14 Oct 2025 10:59:09 -0600 Subject: [PATCH 2/2] Make oauth2 tests work regardless of client_id --- wp_com_e2e/src/main.rs | 4 +--- wp_com_e2e/src/me_tests.rs | 19 +++++++++++++++---- wp_com_e2e/src/oauth2_tests.rs | 15 --------------- 3 files changed, 16 insertions(+), 22 deletions(-) delete mode 100644 wp_com_e2e/src/oauth2_tests.rs diff --git a/wp_com_e2e/src/main.rs b/wp_com_e2e/src/main.rs index 8842e28d4..4353d2a33 100644 --- a/wp_com_e2e/src/main.rs +++ b/wp_com_e2e/src/main.rs @@ -5,7 +5,6 @@ use std::{sync::Arc, time::Duration}; use wp_api::{prelude::*, wp_com::client::WpComApiClient}; mod me_tests; -mod oauth2_tests; mod sites_tests; mod support_bot_tests; mod support_eligibility_test; @@ -89,8 +88,7 @@ async fn run_tests( token: String, allow_writes: bool, ) -> Result<(), anyhow::Error> { - me_tests::me_test(client).await?; - oauth2_tests::oauth2_test(client, token.clone()).await?; + me_tests::me_test(client, token.clone()).await?; sites_tests::sites_test(client).await?; support_bot_tests::support_bots_test(client, allow_writes).await?; support_eligibility_test::support_eligibility_test(client).await?; diff --git a/wp_com_e2e/src/me_tests.rs b/wp_com_e2e/src/me_tests.rs index 5f294f1af..756cc05d6 100644 --- a/wp_com_e2e/src/me_tests.rs +++ b/wp_com_e2e/src/me_tests.rs @@ -1,11 +1,22 @@ -use wp_api::wp_com::client::WpComApiClient; +use wp_api::wp_com::{client::WpComApiClient, oauth2::TokenValidationParameters}; -pub async fn me_test(client: &WpComApiClient) -> anyhow::Result<()> { +pub async fn me_test(client: &WpComApiClient, token: String) -> anyhow::Result<()> { println!("== Current User Info Test =="); - client.me().get().await?; - + let user_info = client.me().get().await?.data; println!("✅ Get Current User Info"); + if let Some(client_id) = user_info.token_client_id { + println!("== OAuth 2 Token Test =="); + client + .oauth2() + .fetch_info(&TokenValidationParameters { + client_id: client_id.to_string(), + token: token.clone(), + }) + .await?; + println!("✅ Get OAuth 2 Token Info"); + } + Ok(()) } diff --git a/wp_com_e2e/src/oauth2_tests.rs b/wp_com_e2e/src/oauth2_tests.rs deleted file mode 100644 index d96e86572..000000000 --- a/wp_com_e2e/src/oauth2_tests.rs +++ /dev/null @@ -1,15 +0,0 @@ -use wp_api::wp_com::{client::WpComApiClient, oauth2::TokenValidationParameters}; - -pub async fn oauth2_test(client: &WpComApiClient, token: String) -> anyhow::Result<()> { - println!("== OAuth 2 Token Test =="); - client - .oauth2() - .fetch_info(&TokenValidationParameters { - client_id: "11".to_string(), - token, - }) - .await?; - println!("✅ Get OAuth 2 Token Info"); - - Ok(()) -}