event_builder.rs 3.8 KB

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