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