use client_api_test_util::TestClient; use collab_entity::CollabType; use database_entity::dto::{AFAccessLevel, AFRole}; use std::time::Duration; use tokio::time::sleep; #[tokio::test] async fn viewing_document_editing_users_test() { let collab_type = CollabType::Document; let mut owner = TestClient::new_user().await; let mut guest = TestClient::new_user().await; let workspace_id = owner.workspace_id().await; owner .add_workspace_member(&workspace_id, &guest, AFRole::Member) .await; let object_id = owner .create_and_edit_collab(&workspace_id, collab_type.clone()) .await; let owner_uid = owner.uid().await; let clients = owner.get_connect_users(&object_id).await; assert_eq!(clients.len(), 1); assert_eq!(clients[0], owner_uid); owner .add_collab_member( &workspace_id, &object_id, &guest, AFAccessLevel::ReadAndWrite, ) .await; guest .open_collab(&workspace_id, &object_id, collab_type) .await; guest.wait_object_sync_complete(&object_id).await.unwrap(); // after guest open the collab, it will emit an awareness that contains the user id of guest. // This awareness will be sent to the server. Server will broadcast the awareness to all the clients // that are connected to the collab. let guest_uid = guest.uid().await; let mut clients: Vec = owner.get_connect_users(&object_id).await; clients.sort(); let mut expected_clients = [owner_uid, guest_uid]; expected_clients.sort(); assert_eq!(clients.len(), 2); assert_eq!(clients, expected_clients); // simulate the guest close the collab guest.clean_awareness_state(&object_id); // sleep 2 second to make sure the awareness observe callback is called sleep(Duration::from_secs(2)).await; guest.wait_object_sync_complete(&object_id).await.unwrap(); let clients = owner.get_connect_users(&object_id).await; assert_eq!(clients.len(), 1); assert_eq!(clients[0], owner_uid); // simulate the guest open the collab again guest.emit_awareness_state(&object_id); sleep(Duration::from_secs(2)).await; guest.wait_object_sync_complete(&object_id).await.unwrap(); guest.wait_object_sync_complete(&object_id).await.unwrap(); let clients = owner.get_connect_users(&object_id).await; assert_eq!(clients.len(), 2); }