event_builder.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. use crate::FlowySDKTest;
  2. use flowy_user::{entities::UserProfilePB, errors::FlowyError};
  3. use lib_dispatch::prelude::{
  4. AFPluginDispatcher, AFPluginEventResponse, AFPluginFromBytes, AFPluginRequest, StatusCode, ToBytes, *,
  5. };
  6. use std::{
  7. convert::TryFrom,
  8. fmt::{Debug, Display},
  9. hash::Hash,
  10. marker::PhantomData,
  11. sync::Arc,
  12. };
  13. pub type FolderEventBuilder = EventBuilder<FlowyError>;
  14. impl FolderEventBuilder {
  15. pub fn new(sdk: FlowySDKTest) -> Self {
  16. EventBuilder::test(TestContext::new(sdk))
  17. }
  18. pub fn user_profile(&self) -> &Option<UserProfilePB> {
  19. &self.user_profile
  20. }
  21. }
  22. pub type UserModuleEventBuilder = FolderEventBuilder;
  23. #[derive(Clone)]
  24. pub struct EventBuilder<E> {
  25. context: TestContext,
  26. user_profile: Option<UserProfilePB>,
  27. err_phantom: PhantomData<E>,
  28. }
  29. impl<E> EventBuilder<E>
  30. where
  31. E: AFPluginFromBytes + Debug,
  32. {
  33. fn test(context: TestContext) -> Self {
  34. Self {
  35. context,
  36. user_profile: None,
  37. err_phantom: PhantomData,
  38. }
  39. }
  40. pub fn payload<P>(mut self, payload: P) -> Self
  41. where
  42. P: ToBytes,
  43. {
  44. match payload.into_bytes() {
  45. Ok(bytes) => {
  46. let module_request = self.get_request();
  47. self.context.request = Some(module_request.payload(bytes))
  48. }
  49. Err(e) => {
  50. log::error!("Set payload failed: {:?}", e);
  51. }
  52. }
  53. self
  54. }
  55. pub fn event<Event>(mut self, event: Event) -> Self
  56. where
  57. Event: Eq + Hash + Debug + Clone + Display,
  58. {
  59. self.context.request = Some(AFPluginRequest::new(event));
  60. self
  61. }
  62. pub fn sync_send(mut self) -> Self {
  63. let request = self.get_request();
  64. let resp = AFPluginDispatcher::sync_send(self.dispatch(), request);
  65. self.context.response = Some(resp);
  66. self
  67. }
  68. pub async fn async_send(mut self) -> Self {
  69. let request = self.get_request();
  70. let resp = AFPluginDispatcher::async_send(self.dispatch(), request).await;
  71. self.context.response = Some(resp);
  72. self
  73. }
  74. pub fn parse<R>(self) -> R
  75. where
  76. R: AFPluginFromBytes,
  77. {
  78. let response = self.get_response();
  79. match response.clone().parse::<R, E>() {
  80. Ok(Ok(data)) => data,
  81. Ok(Err(e)) => {
  82. panic!(
  83. "Parser {:?} failed: {:?}, response {:?}",
  84. std::any::type_name::<R>(),
  85. e,
  86. response
  87. )
  88. }
  89. Err(e) => panic!(
  90. "Dispatch {:?} failed: {:?}, response {:?}",
  91. std::any::type_name::<R>(),
  92. e,
  93. response
  94. ),
  95. }
  96. }
  97. pub fn error(self) -> E {
  98. let response = self.get_response();
  99. assert_eq!(response.status_code, StatusCode::Err);
  100. <AFPluginData<E>>::try_from(response.payload).unwrap().into_inner()
  101. }
  102. pub fn assert_error(self) -> Self {
  103. // self.context.assert_error();
  104. self
  105. }
  106. pub fn assert_success(self) -> Self {
  107. // self.context.assert_success();
  108. self
  109. }
  110. fn dispatch(&self) -> Arc<AFPluginDispatcher> {
  111. self.context.sdk.dispatcher()
  112. }
  113. fn get_response(&self) -> AFPluginEventResponse {
  114. self.context
  115. .response
  116. .as_ref()
  117. .expect("must call sync_send first")
  118. .clone()
  119. }
  120. fn get_request(&mut self) -> AFPluginRequest {
  121. self.context.request.take().expect("must call event first")
  122. }
  123. }
  124. #[derive(Clone)]
  125. pub struct TestContext {
  126. pub sdk: FlowySDKTest,
  127. request: Option<AFPluginRequest>,
  128. response: Option<AFPluginEventResponse>,
  129. }
  130. impl TestContext {
  131. pub fn new(sdk: FlowySDKTest) -> Self {
  132. Self {
  133. sdk,
  134. request: None,
  135. response: None,
  136. }
  137. }
  138. }