helper.rs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // use crate::helper::*;
  2. use crate::helper::{spawn_server, TestServer};
  3. use flowy_document::{
  4. entities::doc::{DocDelta, QueryDocParams},
  5. module::FlowyDocument,
  6. services::doc::edit_doc_context::EditDocContext,
  7. };
  8. use flowy_net::config::ServerConfig;
  9. use flowy_ot::core::Delta;
  10. use flowy_test::{workspace::ViewTest, FlowyTest, FlowyTestSDK};
  11. use flowy_user::services::user::UserSession;
  12. use std::{str::FromStr, sync::Arc};
  13. use tokio::time::{interval, Duration};
  14. pub struct DocumentTest {
  15. server: TestServer,
  16. flowy_test: FlowyTest,
  17. flowy_document: Arc<FlowyDocument>,
  18. user_session: Arc<UserSession>,
  19. edit_context: Arc<EditDocContext>,
  20. }
  21. #[derive(Clone)]
  22. pub enum DocScript {
  23. SendText(&'static str),
  24. SendBinary(Vec<u8>),
  25. }
  26. impl DocumentTest {
  27. pub async fn new() -> Self {
  28. let server = spawn_server().await;
  29. let server_config = ServerConfig::new(&server.host, "http", "ws");
  30. let flowy_test = FlowyTest::setup_with(server_config);
  31. init_user(&flowy_test).await;
  32. let edit_context = create_doc(&flowy_test).await;
  33. let user_session = flowy_test.sdk.user_session.clone();
  34. let flowy_document = flowy_test.sdk.flowy_document.clone();
  35. Self {
  36. server,
  37. flowy_test,
  38. flowy_document,
  39. user_session,
  40. edit_context,
  41. }
  42. }
  43. pub async fn run_scripts(self, scripts: Vec<DocScript>) {
  44. for script in scripts {
  45. match script {
  46. DocScript::SendText(s) => {
  47. let delta = Delta::from_str(s).unwrap();
  48. let data = delta.to_json();
  49. let doc_delta = DocDelta {
  50. doc_id: self.edit_context.doc_id.clone(),
  51. data,
  52. };
  53. self.flowy_document.apply_doc_delta(doc_delta).await;
  54. },
  55. DocScript::SendBinary(_bytes) => {},
  56. }
  57. }
  58. std::mem::forget(self);
  59. let mut interval = interval(Duration::from_secs(5));
  60. interval.tick().await;
  61. interval.tick().await;
  62. }
  63. }
  64. async fn create_doc(flowy_test: &FlowyTest) -> Arc<EditDocContext> {
  65. let view_test = ViewTest::new(flowy_test).await;
  66. let doc_id = view_test.view.id.clone();
  67. let user_session = flowy_test.sdk.user_session.clone();
  68. let flowy_document = flowy_test.sdk.flowy_document.clone();
  69. let edit_context = flowy_document
  70. .open(QueryDocParams { doc_id }, user_session.db_pool().unwrap())
  71. .await
  72. .unwrap();
  73. edit_context
  74. }
  75. async fn init_user(flowy_test: &FlowyTest) {
  76. let _ = flowy_test.sign_up().await;
  77. let user_session = flowy_test.sdk.user_session.clone();
  78. user_session.init_user().await.unwrap();
  79. }