event_builder.rs 3.5 KB

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