helper.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. use flowy_sdk::module::build_modules;
  2. pub use flowy_sdk::*;
  3. use flowy_sys::prelude::*;
  4. use std::{
  5. fmt::{Debug, Display},
  6. fs,
  7. hash::Hash,
  8. sync::Once,
  9. };
  10. static INIT: Once = Once::new();
  11. pub fn init_sdk() {
  12. let root_dir = root_dir();
  13. INIT.call_once(|| {
  14. FlowySDK::init_log(&root_dir);
  15. });
  16. FlowySDK::init(&root_dir);
  17. }
  18. fn root_dir() -> String {
  19. let mut path = fs::canonicalize(".").unwrap();
  20. path.push("tests/temp/flowy/");
  21. let path_str = path.to_str().unwrap().to_string();
  22. if !std::path::Path::new(&path).exists() {
  23. std::fs::create_dir_all(path).unwrap();
  24. }
  25. path_str
  26. }
  27. pub struct EventTester {
  28. request: DispatchRequest,
  29. }
  30. impl EventTester {
  31. pub fn new<E>(event: E, payload: Payload) -> Self
  32. where
  33. E: Eq + Hash + Debug + Clone + Display,
  34. {
  35. init_sdk();
  36. Self {
  37. request: DispatchRequest::new(event, payload),
  38. }
  39. }
  40. #[allow(dead_code)]
  41. pub fn bytes_payload<T>(mut self, payload: T) -> Self
  42. where
  43. T: serde::Serialize,
  44. {
  45. let bytes: Vec<u8> = bincode::serialize(&payload).unwrap();
  46. self.request = self.request.payload(Payload::Bytes(bytes));
  47. self
  48. }
  49. #[allow(dead_code)]
  50. pub fn protobuf_payload<T>(mut self, payload: T) -> Self
  51. where
  52. T: ::protobuf::Message,
  53. {
  54. let bytes: Vec<u8> = payload.write_to_bytes().unwrap();
  55. self.request = self.request.payload(Payload::Bytes(bytes));
  56. self
  57. }
  58. #[allow(dead_code)]
  59. pub async fn async_send(self) -> EventResponse {
  60. let resp = async_send(self.request).await;
  61. dbg!(&resp);
  62. resp
  63. }
  64. #[allow(dead_code)]
  65. pub fn sync_send(self) -> EventResponse {
  66. let resp = sync_send(self.request);
  67. dbg!(&resp);
  68. resp
  69. }
  70. }