event_builder.rs 2.8 KB

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