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 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 { let inner = FlowySupabaseTest::new()?; inner.sign_up_with_uuid(&uuid).await; Some(Self { uuid, inner }) } pub async fn new_with_new_user() -> Option { let inner = FlowySupabaseTest::new()?; let uuid = uuid::Uuid::new_v4().to_string(); let _ = inner.sign_up_with_uuid(&uuid).await; 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::() } pub async fn get_collab_update(&self, database_id: &str) -> Vec { let cloud_service = self.database_manager.get_cloud_service().clone(); let remote_updates = cloud_service.get_collab_update(database_id).await.unwrap(); if remote_updates.is_empty() { return vec![]; } let updates = remote_updates .iter() .map(|update| update.as_ref()) .collect::>(); 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_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 } }