unicode.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //! Tests for Unicode support and correct cursor transformation.
  2. pub mod common;
  3. use anyhow::Result;
  4. use common::*;
  5. use log::info;
  6. use operational_transform::OperationSeq;
  7. use rustpad_server::{server, ServerConfig};
  8. use serde_json::json;
  9. #[tokio::test]
  10. async fn test_unicode_length() -> Result<()> {
  11. pretty_env_logger::try_init().ok();
  12. let filter = server(ServerConfig::default());
  13. expect_text(&filter, "unicode", "").await;
  14. let mut client = connect(&filter, "unicode").await?;
  15. let msg = client.recv().await?;
  16. assert_eq!(msg, json!({ "Identity": 0 }));
  17. let mut operation = OperationSeq::default();
  18. operation.insert("h🎉e🎉l👨‍👨‍👦‍👦lo");
  19. let msg = json!({
  20. "Edit": {
  21. "revision": 0,
  22. "operation": operation
  23. }
  24. });
  25. info!("sending ClientMsg {}", msg);
  26. client.send(&msg).await;
  27. let msg = client.recv().await?;
  28. assert_eq!(
  29. msg,
  30. json!({
  31. "History": {
  32. "start": 0,
  33. "operations": [
  34. { "id": 0, "operation": ["h🎉e🎉l👨‍👨‍👦‍👦lo"] }
  35. ]
  36. }
  37. })
  38. );
  39. info!("testing that text length is equal to number of Unicode code points...");
  40. let mut operation = OperationSeq::default();
  41. operation.delete(14);
  42. let msg = json!({
  43. "Edit": {
  44. "revision": 1,
  45. "operation": operation
  46. }
  47. });
  48. info!("sending ClientMsg {}", msg);
  49. client.send(&msg).await;
  50. let msg = client.recv().await?;
  51. assert_eq!(
  52. msg,
  53. json!({
  54. "History": {
  55. "start": 1,
  56. "operations": [
  57. { "id": 0, "operation": [-14] }
  58. ]
  59. }
  60. })
  61. );
  62. expect_text(&filter, "unicode", "").await;
  63. Ok(())
  64. }
  65. #[tokio::test]
  66. async fn test_multiple_operations() -> Result<()> {
  67. pretty_env_logger::try_init().ok();
  68. let filter = server(ServerConfig::default());
  69. expect_text(&filter, "unicode", "").await;
  70. let mut client = connect(&filter, "unicode").await?;
  71. let msg = client.recv().await?;
  72. assert_eq!(msg, json!({ "Identity": 0 }));
  73. let mut operation = OperationSeq::default();
  74. operation.insert("🎉😍𒀇👨‍👨‍👦‍👦"); // Emoticons and Cuneiform
  75. let msg = json!({
  76. "Edit": {
  77. "revision": 0,
  78. "operation": operation
  79. }
  80. });
  81. info!("sending ClientMsg {}", msg);
  82. client.send(&msg).await;
  83. let msg = client.recv().await?;
  84. assert_eq!(
  85. msg,
  86. json!({
  87. "History": {
  88. "start": 0,
  89. "operations": [
  90. { "id": 0, "operation": ["🎉😍𒀇👨‍👨‍👦‍👦"] }
  91. ]
  92. }
  93. })
  94. );
  95. let mut operation = OperationSeq::default();
  96. operation.insert("👯‍♂️");
  97. operation.retain(3);
  98. operation.insert("𐅣𐅤𐅥"); // Ancient Greek numbers
  99. operation.retain(7);
  100. let msg = json!({
  101. "Edit": {
  102. "revision": 1,
  103. "operation": operation
  104. }
  105. });
  106. info!("sending ClientMsg {}", msg);
  107. client.send(&msg).await;
  108. let msg = client.recv().await?;
  109. assert_eq!(
  110. msg,
  111. json!({
  112. "History": {
  113. "start": 1,
  114. "operations": [
  115. { "id": 0, "operation": ["👯‍♂️", 3, "𐅣𐅤𐅥", 7] }
  116. ]
  117. }
  118. })
  119. );
  120. expect_text(&filter, "unicode", "👯‍♂️🎉😍𒀇𐅣𐅤𐅥👨‍👨‍👦‍👦").await;
  121. let mut operation = OperationSeq::default();
  122. operation.retain(2);
  123. operation.insert("h̷̙̤̏͊̑̍̆̃̉͝ĕ̶̠̌̓̃̓̽̃̚l̸̥̊̓̓͝͠l̸̨̠̣̟̥͠ỏ̴̳̖̪̟̱̰̥̞̙̏̓́͗̽̀̈́͛͐̚̕͝͝ ̶̡͍͙͚̞͙̣̘͙̯͇̙̠̀w̷̨̨̪͚̤͙͖̝͕̜̭̯̝̋̋̿̿̀̾͛̐̏͘͘̕͝ǒ̴̙͉͈̗̖͍̘̥̤̒̈́̒͠r̶̨̡̢̦͔̙̮̦͖͔̩͈̗̖̂̀l̶̡̢͚̬̤͕̜̀͛̌̈́̈́͑͋̈̍̇͊͝͠ď̵̛̛̯͕̭̩͖̝̙͎̊̏̈́̎͊̐̏͊̕͜͝͠͝"); // Lots of ligatures
  124. operation.retain(8);
  125. let msg = json!({
  126. "Edit": {
  127. "revision": 1,
  128. "operation": operation
  129. }
  130. });
  131. info!("sending ClientMsg {}", msg);
  132. client.send(&msg).await;
  133. let msg = client.recv().await?;
  134. assert_eq!(
  135. msg,
  136. json!({
  137. "History": {
  138. "start": 2,
  139. "operations": [
  140. { "id": 0, "operation": [6, "h̷̙̤̏͊̑̍̆̃̉͝ĕ̶̠̌̓̃̓̽̃̚l̸̥̊̓̓͝͠l̸̨̠̣̟̥͠ỏ̴̳̖̪̟̱̰̥̞̙̏̓́͗̽̀̈́͛͐̚̕͝͝ ̶̡͍͙͚̞͙̣̘͙̯͇̙̠̀w̷̨̨̪͚̤͙͖̝͕̜̭̯̝̋̋̿̿̀̾͛̐̏͘͘̕͝ǒ̴̙͉͈̗̖͍̘̥̤̒̈́̒͠r̶̨̡̢̦͔̙̮̦͖͔̩͈̗̖̂̀l̶̡̢͚̬̤͕̜̀͛̌̈́̈́͑͋̈̍̇͊͝͠ď̵̛̛̯͕̭̩͖̝̙͎̊̏̈́̎͊̐̏͊̕͜͝͠͝", 11] }
  141. ]
  142. }
  143. })
  144. );
  145. expect_text(&filter, "unicode", "👯‍♂️🎉😍h̷̙̤̏͊̑̍̆̃̉͝ĕ̶̠̌̓̃̓̽̃̚l̸̥̊̓̓͝͠l̸̨̠̣̟̥͠ỏ̴̳̖̪̟̱̰̥̞̙̏̓́͗̽̀̈́͛͐̚̕͝͝ ̶̡͍͙͚̞͙̣̘͙̯͇̙̠̀w̷̨̨̪͚̤͙͖̝͕̜̭̯̝̋̋̿̿̀̾͛̐̏͘͘̕͝ǒ̴̙͉͈̗̖͍̘̥̤̒̈́̒͠r̶̨̡̢̦͔̙̮̦͖͔̩͈̗̖̂̀l̶̡̢͚̬̤͕̜̀͛̌̈́̈́͑͋̈̍̇͊͝͠ď̵̛̛̯͕̭̩͖̝̙͎̊̏̈́̎͊̐̏͊̕͜͝͠͝𒀇𐅣𐅤𐅥👨‍👨‍👦‍👦").await;
  146. Ok(())
  147. }
  148. #[tokio::test]
  149. async fn test_unicode_cursors() -> Result<()> {
  150. pretty_env_logger::try_init().ok();
  151. let filter = server(ServerConfig::default());
  152. let mut client = connect(&filter, "unicode").await?;
  153. assert_eq!(client.recv().await?, json!({ "Identity": 0 }));
  154. let mut operation = OperationSeq::default();
  155. operation.insert("🎉🎉🎉");
  156. let msg = json!({
  157. "Edit": {
  158. "revision": 0,
  159. "operation": operation
  160. }
  161. });
  162. info!("sending ClientMsg {}", msg);
  163. client.send(&msg).await;
  164. client.recv().await?;
  165. let cursors = json!({
  166. "cursors": [0, 1, 2, 3],
  167. "selections": [[0, 1], [2, 3]]
  168. });
  169. client.send(&json!({ "CursorData": cursors })).await;
  170. let cursors_resp = json!({
  171. "UserCursor": {
  172. "id": 0,
  173. "data": cursors
  174. }
  175. });
  176. assert_eq!(client.recv().await?, cursors_resp);
  177. let mut client2 = connect(&filter, "unicode").await?;
  178. assert_eq!(client2.recv().await?, json!({ "Identity": 1 }));
  179. client2.recv().await?;
  180. assert_eq!(client2.recv().await?, cursors_resp);
  181. let msg = json!({
  182. "Edit": {
  183. "revision": 0,
  184. "operation": ["🎉"]
  185. }
  186. });
  187. client2.send(&msg).await;
  188. let mut client3 = connect(&filter, "unicode").await?;
  189. assert_eq!(client3.recv().await?, json!({ "Identity": 2 }));
  190. client3.recv().await?;
  191. let transformed_cursors_resp = json!({
  192. "UserCursor": {
  193. "id": 0,
  194. "data": {
  195. "cursors": [1, 2, 3, 4],
  196. "selections": [[1, 2], [3, 4]]
  197. }
  198. }
  199. });
  200. assert_eq!(client3.recv().await?, transformed_cursors_resp);
  201. Ok(())
  202. }