fix: infinite loop when getting ancestor for orphan view (#5375)

This commit is contained in:
Lucas.Xu 2024-05-21 10:47:31 +08:00 committed by GitHub
parent c67e266174
commit 24b3d69860
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 0 deletions

View File

@ -139,6 +139,24 @@ impl EventIntegrationTest {
}
}
/// Create orphan views in the folder.
/// Orphan view: the parent_view_id equal to the view_id
/// Normally, the orphan view will be created in nested database
pub async fn create_orphan_view(&self, name: &str, view_id: &str, layout: ViewLayoutPB) {
let payload = CreateOrphanViewPayloadPB {
name: name.to_string(),
desc: "".to_string(),
layout,
view_id: view_id.to_string(),
initial_data: vec![],
};
EventBuilder::new(self.clone())
.event(FolderEvent::CreateOrphanView)
.payload(payload)
.async_send()
.await;
}
pub fn get_folder_data(&self) -> FolderData {
let mutex_folder = self.appflowy_core.folder_manager.get_mutex_folder().clone();
let folder_lock_guard = mutex_folder.read();
@ -240,6 +258,18 @@ impl EventIntegrationTest {
.await
.parse::<ViewPB>()
}
pub async fn get_view_ancestors(&self, view_id: &str) -> Vec<ViewPB> {
EventBuilder::new(self.clone())
.event(FolderEvent::GetViewAncestors)
.payload(ViewIdPB {
value: view_id.to_string(),
})
.async_send()
.await
.parse::<RepeatedViewPB>()
.items
}
}
pub struct ViewTest {

View File

@ -1,6 +1,8 @@
use collab_folder::ViewLayout;
use event_integration_test::EventIntegrationTest;
use flowy_folder::entities::icon::{ViewIconPB, ViewIconTypePB};
use flowy_folder::entities::ViewLayoutPB;
use crate::folder::local_test::script::FolderScript::*;
use crate::folder::local_test::script::FolderTest;
@ -331,3 +333,17 @@ async fn move_view_event_test() {
assert_eq!(after_view_ids[0], view_ids[1]);
assert_eq!(after_view_ids[1], view_ids[0]);
}
#[tokio::test]
async fn create_orphan_child_view_and_get_its_ancestors_test() {
let test = EventIntegrationTest::new_anon().await;
let name = "Orphan View";
let view_id = "20240521";
test
.create_orphan_view(name, view_id, ViewLayoutPB::Grid)
.await;
let ancestors = test.get_view_ancestors(view_id).await;
assert_eq!(ancestors.len(), 1);
assert_eq!(ancestors[0].name, "Orphan View");
assert_eq!(ancestors[0].id, view_id);
}

View File

@ -530,6 +530,10 @@ impl FolderManager {
while let Some(view) =
self.with_folder(|| None, |folder| folder.views.get_view(&parent_view_id))
{
// If the view is already in the ancestors list, then break the loop
if ancestors.iter().any(|v: &ViewPB| v.id == view.id) {
break;
}
ancestors.push(view_pb_without_child_views(view.as_ref().clone()));
parent_view_id = view.parent_view_id.clone();
}