2023-07-05 20:57:09 +08:00
|
|
|
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};
|
2023-07-29 09:46:24 +08:00
|
|
|
use collab_plugins::cloud_storage::CollabType;
|
2023-07-05 20:57:09 +08:00
|
|
|
|
|
|
|
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 {
|
2023-07-14 13:37:13 +08:00
|
|
|
#[allow(dead_code)]
|
2023-07-05 20:57:09 +08:00
|
|
|
pub async fn new_with_user(uuid: String) -> Option<Self> {
|
|
|
|
let inner = FlowySupabaseTest::new()?;
|
2023-07-29 09:46:24 +08:00
|
|
|
inner
|
|
|
|
.third_party_sign_up_with_uuid(&uuid, None)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-07-05 20:57:09 +08:00
|
|
|
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();
|
2023-07-29 09:46:24 +08:00
|
|
|
let _ = inner
|
|
|
|
.third_party_sign_up_with_uuid(&uuid, None)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-07-05 20:57:09 +08:00
|
|
|
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>()
|
|
|
|
}
|
|
|
|
|
2023-07-29 09:46:24 +08:00
|
|
|
pub async fn get_database_collab_update(&self, database_id: &str) -> Vec<u8> {
|
2023-07-05 20:57:09 +08:00
|
|
|
let cloud_service = self.database_manager.get_cloud_service().clone();
|
2023-07-29 09:46:24 +08:00
|
|
|
let remote_updates = cloud_service
|
|
|
|
.get_collab_update(database_id, CollabType::Database)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-07-05 20:57:09 +08:00
|
|
|
|
|
|
|
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![]);
|
2023-08-12 17:36:31 +08:00
|
|
|
collab.lock().with_origin_transact_mut(|txn| {
|
2023-07-05 20:57:09 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|