script.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. use crate::grid::grid_editor::GridEditorTest;
  2. use flowy_http_model::revision::Revision;
  3. use flowy_revision::{RevisionSnapshot, REVISION_WRITE_INTERVAL_IN_MILLIS};
  4. use flowy_sync::client_grid::{GridOperations, GridRevisionPad};
  5. use grid_rev_model::FieldRevision;
  6. use std::time::Duration;
  7. use tokio::time::sleep;
  8. pub enum SnapshotScript {
  9. WriteSnapshot,
  10. #[allow(dead_code)]
  11. AssertSnapshot {
  12. rev_id: i64,
  13. expected: Option<RevisionSnapshot>,
  14. },
  15. AssertSnapshotContent {
  16. snapshot: RevisionSnapshot,
  17. expected: String,
  18. },
  19. CreateField {
  20. field_rev: FieldRevision,
  21. },
  22. DeleteField {
  23. field_rev: FieldRevision,
  24. },
  25. }
  26. pub struct GridSnapshotTest {
  27. inner: GridEditorTest,
  28. pub current_snapshot: Option<RevisionSnapshot>,
  29. pub current_revision: Option<Revision>,
  30. }
  31. impl GridSnapshotTest {
  32. pub async fn new() -> Self {
  33. let editor_test = GridEditorTest::new_table().await;
  34. Self {
  35. inner: editor_test,
  36. current_snapshot: None,
  37. current_revision: None,
  38. }
  39. }
  40. pub fn grid_id(&self) -> String {
  41. self.view_id.clone()
  42. }
  43. pub async fn grid_pad(&self) -> GridRevisionPad {
  44. self.editor.grid_pad().read().await.clone()
  45. }
  46. pub async fn run_scripts(&mut self, scripts: Vec<SnapshotScript>) {
  47. for script in scripts {
  48. self.run_script(script).await;
  49. }
  50. }
  51. pub async fn get_latest_snapshot(&self) -> Option<RevisionSnapshot> {
  52. self.editor.rev_manager().read_snapshot(None).await.unwrap()
  53. }
  54. pub async fn run_script(&mut self, script: SnapshotScript) {
  55. let rev_manager = self.editor.rev_manager();
  56. match script {
  57. SnapshotScript::WriteSnapshot => {
  58. sleep(Duration::from_millis(2 * REVISION_WRITE_INTERVAL_IN_MILLIS)).await;
  59. rev_manager.generate_snapshot().await;
  60. self.current_snapshot = rev_manager.read_snapshot(None).await.unwrap();
  61. }
  62. SnapshotScript::AssertSnapshot { rev_id, expected } => {
  63. let snapshot = rev_manager.read_snapshot(Some(rev_id)).await.unwrap();
  64. assert_eq!(snapshot, expected);
  65. }
  66. SnapshotScript::AssertSnapshotContent { snapshot, expected } => {
  67. let operations = GridOperations::from_bytes(snapshot.data).unwrap();
  68. let pad = GridRevisionPad::from_operations(operations).unwrap();
  69. assert_eq!(pad.json_str().unwrap(), expected);
  70. }
  71. SnapshotScript::CreateField { field_rev } => {
  72. self.editor.create_new_field_rev(field_rev).await.unwrap();
  73. let current_rev_id = rev_manager.rev_id();
  74. self.current_revision = rev_manager.get_revision(current_rev_id).await;
  75. }
  76. SnapshotScript::DeleteField { field_rev } => {
  77. self.editor.delete_field(&field_rev.id).await.unwrap();
  78. }
  79. }
  80. }
  81. }
  82. impl std::ops::Deref for GridSnapshotTest {
  83. type Target = GridEditorTest;
  84. fn deref(&self) -> &Self::Target {
  85. &self.inner
  86. }
  87. }
  88. impl std::ops::DerefMut for GridSnapshotTest {
  89. fn deref_mut(&mut self) -> &mut Self::Target {
  90. &mut self.inner
  91. }
  92. }