builder.rs 3.7 KB

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