| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 | use std::ops::Deref;use assert_json_diff::assert_json_eq;use collab::core::collab::MutexCollab;use collab::core::origin::CollabOrigin;use collab::preclude::updates::decoder::Decode;use collab::preclude::{merge_updates_v1, JsonValue, Update};use collab_define::CollabType;use flowy_database2::entities::{DatabasePB, DatabaseViewIdPB, RepeatedDatabaseSnapshotPB};use flowy_database2::event_map::DatabaseEvent::*;use flowy_folder2::entities::ViewPB;use flowy_test::event_builder::EventBuilder;use crate::util::FlowySupabaseTest;pub struct FlowySupabaseDatabaseTest {  pub uuid: String,  inner: FlowySupabaseTest,}impl FlowySupabaseDatabaseTest {  #[allow(dead_code)]  pub async fn new_with_user(uuid: String) -> Option<Self> {    let inner = FlowySupabaseTest::new()?;    inner.supabase_sign_up_with_uuid(&uuid, None).await.unwrap();    Some(Self { uuid, inner })  }  pub async fn new_with_new_user() -> Option<Self> {    let inner = FlowySupabaseTest::new()?;    let uuid = uuid::Uuid::new_v4().to_string();    let _ = inner.supabase_sign_up_with_uuid(&uuid, None).await.unwrap();    Some(Self { uuid, inner })  }  pub async fn create_database(&self) -> (ViewPB, DatabasePB) {    let current_workspace = self.inner.get_current_workspace().await;    let view = self      .inner      .create_grid(        ¤t_workspace.workspace.id,        "my database".to_string(),        vec![],      )      .await;    let database = self.inner.get_database(&view.id).await;    (view, database)  }  pub async fn get_collab_json(&self, database_id: &str) -> JsonValue {    let database_editor = self      .database_manager      .get_database(database_id)      .await      .unwrap();    // let address = Arc::into_raw(database_editor.clone());    let database = database_editor.get_mutex_database().lock();    database.get_mutex_collab().to_json_value()  }  pub async fn get_database_snapshots(&self, view_id: &str) -> RepeatedDatabaseSnapshotPB {    EventBuilder::new(self.inner.deref().clone())      .event(GetDatabaseSnapshots)      .payload(DatabaseViewIdPB {        value: view_id.to_string(),      })      .async_send()      .await      .parse::<RepeatedDatabaseSnapshotPB>()  }  pub async fn get_database_collab_update(&self, database_id: &str) -> Vec<u8> {    let cloud_service = self.database_manager.get_cloud_service().clone();    let remote_updates = cloud_service      .get_collab_update(database_id, CollabType::Database)      .await      .unwrap();    if remote_updates.is_empty() {      return vec![];    }    let updates = remote_updates      .iter()      .map(|update| update.as_ref())      .collect::<Vec<&[u8]>>();    merge_updates_v1(&updates).unwrap()  }}pub fn assert_database_collab_content(  database_id: &str,  collab_update: &[u8],  expected: JsonValue,) {  let collab = MutexCollab::new(CollabOrigin::Server, database_id, vec![]);  collab.lock().with_origin_transact_mut(|txn| {    let update = Update::decode_v1(collab_update).unwrap();    txn.apply_update(update);  });  let json = collab.to_json_value();  assert_json_eq!(json, expected);}impl Deref for FlowySupabaseDatabaseTest {  type Target = FlowySupabaseTest;  fn deref(&self) -> &Self::Target {    &self.inner  }}
 |