undo_redo_test.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. pub mod helper;
  2. use crate::helper::{TestOp::*, *};
  3. use flowy_ot::{client::RECORD_THRESHOLD, core::Interval};
  4. #[test]
  5. fn history_undo_insert() {
  6. let ops = vec![
  7. Insert(0, "123", 0),
  8. Undo(0),
  9. AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
  10. ];
  11. OpTester::new().run_script_with_newline(ops);
  12. }
  13. #[test]
  14. fn history_undo_insert2() {
  15. let ops = vec![
  16. Insert(0, "123", 0),
  17. Wait(RECORD_THRESHOLD),
  18. Insert(0, "456", 0),
  19. Undo(0),
  20. AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
  21. Undo(0),
  22. AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
  23. ];
  24. OpTester::new().run_script_with_newline(ops);
  25. }
  26. #[test]
  27. fn history_redo_insert() {
  28. let ops = vec![
  29. Insert(0, "123", 0),
  30. AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
  31. Undo(0),
  32. AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
  33. Redo(0),
  34. AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
  35. ];
  36. OpTester::new().run_script_with_newline(ops);
  37. }
  38. #[test]
  39. fn history_redo_insert_with_lagging() {
  40. let ops = vec![
  41. Insert(0, "123", 0),
  42. Wait(RECORD_THRESHOLD),
  43. Insert(0, "456", 3),
  44. Wait(RECORD_THRESHOLD),
  45. AssertStr(0, "123456\n"),
  46. AssertOpsJson(0, r#"[{"insert":"123456\n"}]"#),
  47. Undo(0),
  48. AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
  49. Redo(0),
  50. AssertOpsJson(0, r#"[{"insert":"123456\n"}]"#),
  51. Undo(0),
  52. AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
  53. ];
  54. OpTester::new().run_script_with_newline(ops);
  55. }
  56. #[test]
  57. fn history_undo_attributes() {
  58. let ops = vec![
  59. Insert(0, "123", 0),
  60. Bold(0, Interval::new(0, 3), true),
  61. Undo(0),
  62. AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
  63. ];
  64. OpTester::new().run_script_with_newline(ops);
  65. }
  66. #[test]
  67. fn history_undo_attributes_with_lagging() {
  68. let ops = vec![
  69. Insert(0, "123", 0),
  70. Wait(RECORD_THRESHOLD),
  71. Bold(0, Interval::new(0, 3), true),
  72. Undo(0),
  73. AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
  74. ];
  75. OpTester::new().run_script_with_newline(ops);
  76. }
  77. #[test]
  78. fn history_redo_attributes() {
  79. let ops = vec![
  80. Insert(0, "123", 0),
  81. Bold(0, Interval::new(0, 3), true),
  82. Undo(0),
  83. AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
  84. Redo(0),
  85. AssertOpsJson(
  86. 0,
  87. r#" [{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"}]"#,
  88. ),
  89. ];
  90. OpTester::new().run_script_with_newline(ops);
  91. }
  92. #[test]
  93. fn history_redo_attributes_with_lagging() {
  94. let ops = vec![
  95. Insert(0, "123", 0),
  96. Wait(RECORD_THRESHOLD),
  97. Bold(0, Interval::new(0, 3), true),
  98. Undo(0),
  99. AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
  100. Redo(0),
  101. AssertOpsJson(
  102. 0,
  103. r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"}]"#,
  104. ),
  105. ];
  106. OpTester::new().run_script_with_newline(ops);
  107. }
  108. #[test]
  109. fn history_undo_delete() {
  110. let ops = vec![
  111. Insert(0, "123", 0),
  112. AssertOpsJson(0, r#"[{"insert":"123"}]"#),
  113. Delete(0, Interval::new(0, 3)),
  114. AssertOpsJson(0, r#"[]"#),
  115. Undo(0),
  116. AssertOpsJson(0, r#"[{"insert":"123"}]"#),
  117. ];
  118. OpTester::new().run_script(ops);
  119. }
  120. #[test]
  121. fn history_undo_delete2() {
  122. let ops = vec![
  123. Insert(0, "123", 0),
  124. Bold(0, Interval::new(0, 3), true),
  125. Delete(0, Interval::new(0, 1)),
  126. AssertOpsJson(
  127. 0,
  128. r#"[
  129. {"insert":"23","attributes":{"bold":"true"}},
  130. {"insert":"\n"}]
  131. "#,
  132. ),
  133. Undo(0),
  134. AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
  135. ];
  136. OpTester::new().run_script_with_newline(ops);
  137. }
  138. #[test]
  139. fn history_undo_delete2_with_lagging() {
  140. let ops = vec![
  141. Insert(0, "123", 0),
  142. Wait(RECORD_THRESHOLD),
  143. Bold(0, Interval::new(0, 3), true),
  144. Wait(RECORD_THRESHOLD),
  145. Delete(0, Interval::new(0, 1)),
  146. AssertOpsJson(
  147. 0,
  148. r#"[
  149. {"insert":"23","attributes":{"bold":"true"}},
  150. {"insert":"\n"}]
  151. "#,
  152. ),
  153. Undo(0),
  154. AssertOpsJson(
  155. 0,
  156. r#"[
  157. {"insert":"123","attributes":{"bold":"true"}},
  158. {"insert":"\n"}]
  159. "#,
  160. ),
  161. ];
  162. OpTester::new().run_script_with_newline(ops);
  163. }
  164. #[test]
  165. fn history_redo_delete() {
  166. let ops = vec![
  167. Insert(0, "123", 0),
  168. Wait(RECORD_THRESHOLD),
  169. Delete(0, Interval::new(0, 3)),
  170. AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
  171. Undo(0),
  172. Redo(0),
  173. AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
  174. ];
  175. OpTester::new().run_script_with_newline(ops);
  176. }
  177. #[test]
  178. fn history_undo_replace() {
  179. let ops = vec![
  180. Insert(0, "123", 0),
  181. Bold(0, Interval::new(0, 3), true),
  182. Replace(0, Interval::new(0, 2), "ab"),
  183. AssertOpsJson(
  184. 0,
  185. r#"[
  186. {"insert":"ab"},
  187. {"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}]
  188. "#,
  189. ),
  190. Undo(0),
  191. AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
  192. ];
  193. OpTester::new().run_script_with_newline(ops);
  194. }
  195. #[test]
  196. fn history_undo_replace_with_lagging() {
  197. let ops = vec![
  198. Insert(0, "123", 0),
  199. Wait(RECORD_THRESHOLD),
  200. Bold(0, Interval::new(0, 3), true),
  201. Wait(RECORD_THRESHOLD),
  202. Replace(0, Interval::new(0, 2), "ab"),
  203. AssertOpsJson(
  204. 0,
  205. r#"[
  206. {"insert":"ab"},
  207. {"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}]
  208. "#,
  209. ),
  210. Undo(0),
  211. AssertOpsJson(
  212. 0,
  213. r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"}]"#,
  214. ),
  215. ];
  216. OpTester::new().run_script_with_newline(ops);
  217. }
  218. #[test]
  219. fn history_redo_replace() {
  220. let ops = vec![
  221. Insert(0, "123", 0),
  222. Bold(0, Interval::new(0, 3), true),
  223. Replace(0, Interval::new(0, 2), "ab"),
  224. Undo(0),
  225. Redo(0),
  226. AssertOpsJson(
  227. 0,
  228. r#"[
  229. {"insert":"ab"},
  230. {"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}]
  231. "#,
  232. ),
  233. ];
  234. OpTester::new().run_script_with_newline(ops);
  235. }
  236. #[test]
  237. fn history_undo_add_header() {
  238. let ops = vec![
  239. Insert(0, "123456", 0),
  240. Header(0, Interval::new(0, 6), 1, true),
  241. Insert(0, "\n", 3),
  242. Insert(0, "\n", 4),
  243. Undo(0),
  244. Redo(0),
  245. AssertOpsJson(
  246. 0,
  247. r#"[{"insert":"123"},{"insert":"\n\n","attributes":{"header":"1"}},{"insert":"456"},{"insert":"\n","attributes":{"header":"1"}}]"#,
  248. ),
  249. ];
  250. OpTester::new().run_script_with_newline(ops);
  251. }
  252. #[test]
  253. fn history_undo_add_link() {
  254. let site = "https://appflowy.io";
  255. let ops = vec![
  256. Insert(0, site, 0),
  257. Wait(RECORD_THRESHOLD),
  258. Link(0, Interval::new(0, site.len()), site, true),
  259. Undo(0),
  260. AssertOpsJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
  261. Redo(0),
  262. AssertOpsJson(
  263. 0,
  264. r#"[{"insert":"https://appflowy.io","attributes":{"link":"https://appflowy.io"}},{"insert":"\n"}]"#,
  265. ),
  266. ];
  267. OpTester::new().run_script_with_newline(ops);
  268. }