@@ -390,9 +390,10 @@ async fn perform_loadfile(
390390 message: EditorMessageContents :: Result ( Ok ( ResultOkTypes :: Void ) )
391391 }
392392 ) ;
393+ server_id += MESSAGE_ID_INCREMENT ;
393394
394395 if has_toc {
395- server_id + 2.0 * MESSAGE_ID_INCREMENT
396+ server_id + MESSAGE_ID_INCREMENT
396397 } else {
397398 server_id
398399 }
@@ -1232,3 +1233,182 @@ async fn test_4_core(
12321233
12331234 Ok ( ( ) )
12341235}
1236+
1237+ make_test ! ( test_5, test_5_core) ;
1238+
1239+ async fn test_5_core (
1240+ codechat_server : CodeChatEditorServer ,
1241+ driver_ref : & WebDriver ,
1242+ test_dir : & Path ,
1243+ ) -> Result < ( ) , WebDriverError > {
1244+ let path = canonicalize ( test_dir. join ( "test.py" ) ) . unwrap ( ) ;
1245+ let path_str = path. to_str ( ) . unwrap ( ) . to_string ( ) ;
1246+ let version = 0.0 ;
1247+ let orig_text = indoc ! (
1248+ "
1249+ # Test.
1250+ #
1251+ # ```graphviz
1252+ # digraph g {
1253+ # A -> B
1254+ # }
1255+ # ```
1256+ #
1257+ # Test.
1258+ test()
1259+ "
1260+ )
1261+ . to_string ( ) ;
1262+ let server_id = perform_loadfile (
1263+ & codechat_server,
1264+ test_dir,
1265+ "test.py" ,
1266+ Some ( ( orig_text. clone ( ) , version) ) ,
1267+ false ,
1268+ 6.0 ,
1269+ )
1270+ . await ;
1271+
1272+ // Target the iframe containing the Client.
1273+ select_codechat_iframe ( driver_ref) . await ;
1274+
1275+ // Focus it.
1276+ let contents_css = ".CodeChat-CodeMirror .CodeChat-doc-contents" ;
1277+ let doc_block_contents = driver_ref. find ( By :: Css ( contents_css) ) . await . unwrap ( ) ;
1278+ doc_block_contents. click ( ) . await . unwrap ( ) ;
1279+ // The click produces an updated cursor/scroll location after an autosave
1280+ // delay.
1281+ let mut client_id = INITIAL_CLIENT_MESSAGE_ID ;
1282+ assert_eq ! (
1283+ codechat_server. get_message_timeout( TIMEOUT ) . await . unwrap( ) ,
1284+ EditorMessage {
1285+ id: client_id,
1286+ message: EditorMessageContents :: Update ( UpdateMessageContents {
1287+ file_path: path_str. clone( ) ,
1288+ contents: None ,
1289+ cursor_position: Some ( 1 ) ,
1290+ scroll_position: Some ( 1.0 )
1291+ } )
1292+ }
1293+ ) ;
1294+ client_id += MESSAGE_ID_INCREMENT ;
1295+
1296+ // Refind it, since it's now switched with a TinyMCE editor.
1297+ let tinymce_contents = driver_ref. find ( By :: Id ( "TinyMCE-inst" ) ) . await . unwrap ( ) ;
1298+ // Make an edit.
1299+ tinymce_contents. send_keys ( "foo" ) . await . unwrap ( ) ;
1300+
1301+ // Verify the updated text.
1302+ //
1303+ // Update the version from the value provided by the client, which varies
1304+ // randomly.
1305+ let msg = codechat_server. get_message_timeout ( TIMEOUT ) . await . unwrap ( ) ;
1306+ let client_version = get_version ( & msg) ;
1307+ assert_eq ! (
1308+ msg,
1309+ EditorMessage {
1310+ id: client_id,
1311+ message: EditorMessageContents :: Update ( UpdateMessageContents {
1312+ file_path: path_str. clone( ) ,
1313+ contents: Some ( CodeChatForWeb {
1314+ metadata: SourceFileMetadata {
1315+ mode: "python" . to_string( ) ,
1316+ } ,
1317+ source: CodeMirrorDiffable :: Diff ( CodeMirrorDiff {
1318+ doc: vec![
1319+ StringDiff {
1320+ from: 0 ,
1321+ to: Some ( 8 ) ,
1322+ insert: "# fooTest.\n " . to_string( ) ,
1323+ } ,
1324+ StringDiff {
1325+ from: 24 ,
1326+ to: Some ( 55 , ) ,
1327+ insert: "# digraph g { A -> B }\n " . to_string( ) ,
1328+ }
1329+ ] ,
1330+ doc_blocks: vec![ ] ,
1331+ version,
1332+ } ) ,
1333+ version: client_version,
1334+ } ) ,
1335+ cursor_position: Some ( 1 ) ,
1336+ scroll_position: Some ( 1.0 )
1337+ } )
1338+ }
1339+ ) ;
1340+ let version = client_version;
1341+ codechat_server. send_result ( client_id, None ) . await . unwrap ( ) ;
1342+ client_id += MESSAGE_ID_INCREMENT ;
1343+
1344+ // The Server sends the Client a wrapped version of the text; the Client
1345+ // replies with a Result(Ok).
1346+ assert_eq ! (
1347+ codechat_server. get_message_timeout( TIMEOUT ) . await . unwrap( ) ,
1348+ EditorMessage {
1349+ id: server_id,
1350+ message: EditorMessageContents :: Result ( Ok ( ResultOkTypes :: Void ) )
1351+ }
1352+ ) ;
1353+ //server_id += MESSAGE_ID_INCREMENT;
1354+
1355+ // Send new text, which turns into a diff.
1356+ let ide_id = codechat_server
1357+ . send_message_update_plain ( path_str. clone ( ) , Some ( ( orig_text, version) ) , Some ( 1 ) , None )
1358+ . await
1359+ . unwrap ( ) ;
1360+ assert_eq ! (
1361+ codechat_server. get_message_timeout( TIMEOUT ) . await . unwrap( ) ,
1362+ EditorMessage {
1363+ id: ide_id,
1364+ message: EditorMessageContents :: Result ( Ok ( ResultOkTypes :: Void ) )
1365+ }
1366+ ) ;
1367+
1368+ // Make another edit, to push any corrupted text back.
1369+ tinymce_contents. send_keys ( "bar" ) . await . unwrap ( ) ;
1370+ // Verify the updated text.
1371+ //
1372+ // Update the version from the value provided by the client, which varies
1373+ // randomly.
1374+ let msg = codechat_server. get_message_timeout ( TIMEOUT ) . await . unwrap ( ) ;
1375+ let client_version = get_version ( & msg) ;
1376+ assert_eq ! (
1377+ msg,
1378+ EditorMessage {
1379+ id: client_id,
1380+ message: EditorMessageContents :: Update ( UpdateMessageContents {
1381+ file_path: path_str. clone( ) ,
1382+ contents: Some ( CodeChatForWeb {
1383+ metadata: SourceFileMetadata {
1384+ mode: "python" . to_string( ) ,
1385+ } ,
1386+ source: CodeMirrorDiffable :: Diff ( CodeMirrorDiff {
1387+ doc: vec![
1388+ StringDiff {
1389+ from: 0 ,
1390+ to: Some ( 8 ) ,
1391+ insert: "# Tesbart.\n " . to_string( ) ,
1392+ } ,
1393+ StringDiff {
1394+ from: 24 ,
1395+ to: Some ( 55 , ) ,
1396+ insert: "# digraph g { A -> B }\n " . to_string( ) ,
1397+ }
1398+ ] ,
1399+ doc_blocks: vec![ ] ,
1400+ version,
1401+ } ) ,
1402+ version: client_version,
1403+ } ) ,
1404+ cursor_position: Some ( 1 ) ,
1405+ scroll_position: Some ( 1.0 )
1406+ } )
1407+ }
1408+ ) ;
1409+ //let version = client_version;
1410+ codechat_server. send_result ( client_id, None ) . await . unwrap ( ) ;
1411+ //client_id += MESSAGE_ID_INCREMENT;
1412+
1413+ Ok ( ( ) )
1414+ }
0 commit comments