event_builder.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.parse::<R, E>() {
  78. Ok(Ok(data)) => data,
  79. Ok(Err(e)) => {
  80. panic!("Parser {:?} failed: {:?}", std::any::type_name::<R>(), e)
  81. }
  82. Err(e) => panic!(
  83. "Internal error: {:?}, parser {:?} failed",
  84. e,
  85. std::any::type_name::<R>(),
  86. ),
  87. }
  88. }
  89. pub fn error(self) -> E {
  90. let response = self.get_response();
  91. assert_eq!(response.status_code, StatusCode::Err);
  92. <Data<E>>::try_from(response.payload).unwrap().into_inner()
  93. }
  94. pub fn assert_error(self) -> Self {
  95. // self.context.assert_error();
  96. self
  97. }
  98. pub fn assert_success(self) -> Self {
  99. // self.context.assert_success();
  100. self
  101. }
  102. fn dispatch(&self) -> Arc<EventDispatcher> {
  103. self.context.sdk.dispatcher()
  104. }
  105. fn get_response(&self) -> EventResponse {
  106. self.context
  107. .response
  108. .as_ref()
  109. .expect("must call sync_send first")
  110. .clone()
  111. }
  112. fn get_request(&mut self) -> ModuleRequest {
  113. self.context.request.take().expect("must call event first")
  114. }
  115. }
  116. #[derive(Clone)]
  117. pub struct TestContext {
  118. pub sdk: FlowySDKTest,
  119. request: Option<ModuleRequest>,
  120. response: Option<EventResponse>,
  121. }
  122. impl TestContext {
  123. pub fn new(sdk: FlowySDKTest) -> Self {
  124. Self {
  125. sdk,
  126. request: None,
  127. response: None,
  128. }
  129. }
  130. }