helper.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // use crate::helper::*;
  2. use crate::helper::{spawn_server, TestServer};
  3. use actix_web::web::Data;
  4. use flowy_document::{
  5. entities::doc::QueryDocParams,
  6. services::doc::edit_doc_context::EditDocContext as ClientEditDocContext,
  7. };
  8. use flowy_net::config::ServerConfig;
  9. use flowy_test::{workspace::ViewTest, FlowyTest};
  10. use std::sync::Arc;
  11. use tokio::time::{sleep, Duration};
  12. pub struct DocumentTest {
  13. server: TestServer,
  14. flowy_test: FlowyTest,
  15. }
  16. #[derive(Clone)]
  17. pub enum DocScript {
  18. SendText(usize, &'static str),
  19. AssertClient(&'static str),
  20. AssertServer(&'static str),
  21. }
  22. impl DocumentTest {
  23. pub async fn new() -> Self {
  24. let server = spawn_server().await;
  25. let server_config = ServerConfig::new(&server.host, "http", "ws");
  26. let flowy_test = FlowyTest::setup_with(server_config);
  27. Self { server, flowy_test }
  28. }
  29. pub async fn run_scripts(self, scripts: Vec<DocScript>) {
  30. init_user(&self.flowy_test).await;
  31. let DocumentTest { server, flowy_test } = self;
  32. run_scripts(server, flowy_test, scripts).await;
  33. sleep(Duration::from_secs(5)).await;
  34. }
  35. }
  36. pub async fn run_scripts(server: TestServer, flowy_test: FlowyTest, scripts: Vec<DocScript>) {
  37. let client_edit_context = create_doc(&flowy_test).await;
  38. let doc_id = client_edit_context.doc_id.clone();
  39. for script in scripts {
  40. match script {
  41. DocScript::SendText(index, s) => {
  42. client_edit_context.insert(index, s);
  43. },
  44. DocScript::AssertClient(s) => {
  45. let json = client_edit_context.doc_json();
  46. assert_eq(s, &json);
  47. },
  48. DocScript::AssertServer(s) => {
  49. sleep(Duration::from_millis(100)).await;
  50. let pool = server.pg_pool.clone();
  51. let edit_context = server
  52. .app_ctx
  53. .doc_biz
  54. .manager
  55. .get(&doc_id, Data::new(pool))
  56. .await
  57. .unwrap()
  58. .unwrap();
  59. let json = edit_context.doc_json();
  60. assert_eq(s, &json);
  61. },
  62. }
  63. }
  64. std::mem::forget(flowy_test);
  65. }
  66. fn assert_eq(expect: &str, receive: &str) {
  67. if expect != receive {
  68. log::error!("expect: {}", expect);
  69. log::error!("but receive: {}", receive);
  70. }
  71. assert_eq!(expect, receive);
  72. }
  73. async fn create_doc(flowy_test: &FlowyTest) -> Arc<ClientEditDocContext> {
  74. let view_test = ViewTest::new(flowy_test).await;
  75. let doc_id = view_test.view.id.clone();
  76. let user_session = flowy_test.sdk.user_session.clone();
  77. let flowy_document = flowy_test.sdk.flowy_document.clone();
  78. let edit_context = flowy_document
  79. .open(QueryDocParams { doc_id }, user_session.db_pool().unwrap())
  80. .await
  81. .unwrap();
  82. edit_context
  83. }
  84. async fn init_user(flowy_test: &FlowyTest) {
  85. let _ = flowy_test.sign_up().await;
  86. let user_session = flowy_test.sdk.user_session.clone();
  87. user_session.init_user().await.unwrap();
  88. }