helper.rs 1.9 KB

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