event_builder.rs 3.5 KB

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