event_builder.rs 3.0 KB

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