helper.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, P>(event: E, payload: P) -> Self
  32. where
  33. E: Eq + Hash + Debug + Clone + Display,
  34. P: std::convert::Into<Payload>,
  35. {
  36. init_sdk();
  37. Self {
  38. request: DispatchRequest::new(event, payload.into()),
  39. }
  40. }
  41. // #[allow(dead_code)]
  42. // pub fn bytes_payload<T>(mut self, payload: T) -> Self
  43. // where
  44. // T: serde::Serialize,
  45. // {
  46. // let bytes: Vec<u8> = bincode::serialize(&payload).unwrap();
  47. // self.request = self.request.payload(Payload::Bytes(bytes));
  48. // self
  49. // }
  50. //
  51. // #[allow(dead_code)]
  52. // pub fn protobuf_payload<T>(mut self, payload: T) -> Self
  53. // where
  54. // T: ::protobuf::Message,
  55. // {
  56. // let bytes: Vec<u8> = payload.write_to_bytes().unwrap();
  57. // self.request = self.request.payload(Payload::Bytes(bytes));
  58. // self
  59. // }
  60. #[allow(dead_code)]
  61. pub async fn async_send(self) -> EventResponse {
  62. let resp = async_send(self.request).await;
  63. dbg!(&resp);
  64. resp
  65. }
  66. #[allow(dead_code)]
  67. pub fn sync_send(self) -> EventResponse {
  68. let resp = sync_send(self.request);
  69. dbg!(&resp);
  70. resp
  71. }
  72. }