helper.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use std::ops::Deref;
  2. use assert_json_diff::assert_json_eq;
  3. use collab::core::collab::MutexCollab;
  4. use collab::core::origin::CollabOrigin;
  5. use collab::preclude::updates::decoder::Decode;
  6. use collab::preclude::{merge_updates_v1, JsonValue, Update};
  7. use collab_define::CollabType;
  8. use flowy_database2::entities::{DatabasePB, DatabaseViewIdPB, RepeatedDatabaseSnapshotPB};
  9. use flowy_database2::event_map::DatabaseEvent::*;
  10. use flowy_folder2::entities::ViewPB;
  11. use flowy_test::event_builder::EventBuilder;
  12. use crate::util::FlowySupabaseTest;
  13. pub struct FlowySupabaseDatabaseTest {
  14. pub uuid: String,
  15. inner: FlowySupabaseTest,
  16. }
  17. impl FlowySupabaseDatabaseTest {
  18. #[allow(dead_code)]
  19. pub async fn new_with_user(uuid: String) -> Option<Self> {
  20. let inner = FlowySupabaseTest::new()?;
  21. inner
  22. .third_party_sign_up_with_uuid(&uuid, None)
  23. .await
  24. .unwrap();
  25. Some(Self { uuid, inner })
  26. }
  27. pub async fn new_with_new_user() -> Option<Self> {
  28. let inner = FlowySupabaseTest::new()?;
  29. let uuid = uuid::Uuid::new_v4().to_string();
  30. let _ = inner
  31. .third_party_sign_up_with_uuid(&uuid, None)
  32. .await
  33. .unwrap();
  34. Some(Self { uuid, inner })
  35. }
  36. pub async fn create_database(&self) -> (ViewPB, DatabasePB) {
  37. let current_workspace = self.inner.get_current_workspace().await;
  38. let view = self
  39. .inner
  40. .create_grid(
  41. &current_workspace.workspace.id,
  42. "my database".to_string(),
  43. vec![],
  44. )
  45. .await;
  46. let database = self.inner.get_database(&view.id).await;
  47. (view, database)
  48. }
  49. pub async fn get_collab_json(&self, database_id: &str) -> JsonValue {
  50. let database_editor = self
  51. .database_manager
  52. .get_database(database_id)
  53. .await
  54. .unwrap();
  55. // let address = Arc::into_raw(database_editor.clone());
  56. let database = database_editor.get_mutex_database().lock();
  57. database.get_mutex_collab().to_json_value()
  58. }
  59. pub async fn get_database_snapshots(&self, view_id: &str) -> RepeatedDatabaseSnapshotPB {
  60. EventBuilder::new(self.inner.deref().clone())
  61. .event(GetDatabaseSnapshots)
  62. .payload(DatabaseViewIdPB {
  63. value: view_id.to_string(),
  64. })
  65. .async_send()
  66. .await
  67. .parse::<RepeatedDatabaseSnapshotPB>()
  68. }
  69. pub async fn get_database_collab_update(&self, database_id: &str) -> Vec<u8> {
  70. let cloud_service = self.database_manager.get_cloud_service().clone();
  71. let remote_updates = cloud_service
  72. .get_collab_update(database_id, CollabType::Database)
  73. .await
  74. .unwrap();
  75. if remote_updates.is_empty() {
  76. return vec![];
  77. }
  78. let updates = remote_updates
  79. .iter()
  80. .map(|update| update.as_ref())
  81. .collect::<Vec<&[u8]>>();
  82. merge_updates_v1(&updates).unwrap()
  83. }
  84. }
  85. pub fn assert_database_collab_content(
  86. database_id: &str,
  87. collab_update: &[u8],
  88. expected: JsonValue,
  89. ) {
  90. let collab = MutexCollab::new(CollabOrigin::Server, database_id, vec![]);
  91. collab.lock().with_origin_transact_mut(|txn| {
  92. let update = Update::decode_v1(collab_update).unwrap();
  93. txn.apply_update(update);
  94. });
  95. let json = collab.to_json_value();
  96. assert_json_eq!(json, expected);
  97. }
  98. impl Deref for FlowySupabaseDatabaseTest {
  99. type Target = FlowySupabaseTest;
  100. fn deref(&self) -> &Self::Target {
  101. &self.inner
  102. }
  103. }