at main 216 lines 6.2 kB view raw
1//! WASM browser tests for weaver-editor-browser. 2//! 3//! Run with: `wasm-pack test --headless --firefox` or `--chrome` 4 5use wasm_bindgen_test::*; 6 7wasm_bindgen_test_configure!(run_in_browser); 8 9use weaver_editor_browser::{ 10 BeforeInputContext, BeforeInputResult, InputType, Platform, Range, handle_beforeinput, 11 parse_browser_input_type, platform, 12}; 13use weaver_editor_core::{EditorDocument, EditorRope, PlainEditor, UndoableBuffer}; 14 15type TestEditor = PlainEditor<UndoableBuffer<EditorRope>>; 16 17fn make_editor(content: &str) -> TestEditor { 18 let rope = EditorRope::from_str(content); 19 let buf = UndoableBuffer::new(rope, 100); 20 PlainEditor::new(buf) 21} 22 23fn test_platform() -> Platform { 24 Platform { 25 ios: false, 26 mac: false, 27 android: false, 28 chrome: false, 29 safari: false, 30 gecko: false, 31 webkit_version: None, 32 chrome_version: None, 33 mobile: false, 34 } 35} 36 37// === InputType parsing tests === 38 39#[wasm_bindgen_test] 40fn test_parse_insert_text() { 41 assert_eq!( 42 parse_browser_input_type("insertText"), 43 InputType::InsertText 44 ); 45} 46 47#[wasm_bindgen_test] 48fn test_parse_delete_backward() { 49 assert_eq!( 50 parse_browser_input_type("deleteContentBackward"), 51 InputType::DeleteContentBackward 52 ); 53} 54 55#[wasm_bindgen_test] 56fn test_parse_unknown() { 57 match parse_browser_input_type("unknownType") { 58 InputType::Unknown(s) => assert_eq!(s, "unknownType"), 59 _ => panic!("Expected Unknown variant"), 60 } 61} 62 63// === Platform detection tests === 64 65#[wasm_bindgen_test] 66fn test_platform_detection() { 67 let plat = platform(); 68 // Just verify it returns something without panicking. 69 // Actual values depend on the browser running the test. 70 let _ = plat.mac; 71 let _ = plat.safari; 72 let _ = plat.chrome; 73 let _ = plat.gecko; 74 let _ = plat.android; 75 let _ = plat.ios; 76 let _ = plat.mobile; 77} 78 79// === BeforeInput handler tests === 80 81#[wasm_bindgen_test] 82fn test_handle_insert_text() { 83 let mut editor = make_editor("hello"); 84 editor.set_cursor_offset(5); 85 let plat = test_platform(); 86 87 let ctx = BeforeInputContext { 88 input_type: InputType::InsertText, 89 data: Some(" world".to_string()), 90 target_range: None, 91 is_composing: false, 92 platform: &plat, 93 }; 94 95 let result = handle_beforeinput(&mut editor, &ctx, Range::caret(5)); 96 assert!(matches!(result, BeforeInputResult::Handled)); 97 assert_eq!(editor.content_string(), "hello world"); 98} 99 100#[wasm_bindgen_test] 101fn test_handle_delete_backward() { 102 let mut editor = make_editor("hello"); 103 editor.set_cursor_offset(5); 104 let plat = test_platform(); 105 106 let ctx = BeforeInputContext { 107 input_type: InputType::DeleteContentBackward, 108 data: None, 109 target_range: None, 110 is_composing: false, 111 platform: &plat, 112 }; 113 114 let result = handle_beforeinput(&mut editor, &ctx, Range::caret(5)); 115 assert!(matches!(result, BeforeInputResult::Handled)); 116 assert_eq!(editor.content_string(), "hell"); 117} 118 119#[wasm_bindgen_test] 120fn test_handle_composition_passthrough() { 121 let mut editor = make_editor("hello"); 122 let plat = test_platform(); 123 124 let ctx = BeforeInputContext { 125 input_type: InputType::InsertText, 126 data: Some("x".to_string()), 127 target_range: None, 128 is_composing: true, // During composition 129 platform: &plat, 130 }; 131 132 let result = handle_beforeinput(&mut editor, &ctx, Range::caret(5)); 133 assert!(matches!(result, BeforeInputResult::PassThrough)); 134 // Document unchanged during composition passthrough. 135 assert_eq!(editor.content_string(), "hello"); 136} 137 138#[wasm_bindgen_test] 139fn test_handle_undo_redo() { 140 let mut editor = make_editor("hello"); 141 editor.set_cursor_offset(5); 142 let plat = test_platform(); 143 144 // Insert text first. 145 let insert_ctx = BeforeInputContext { 146 input_type: InputType::InsertText, 147 data: Some(" world".to_string()), 148 target_range: None, 149 is_composing: false, 150 platform: &plat, 151 }; 152 handle_beforeinput(&mut editor, &insert_ctx, Range::caret(5)); 153 assert_eq!(editor.content_string(), "hello world"); 154 155 // Undo. 156 let undo_ctx = BeforeInputContext { 157 input_type: InputType::HistoryUndo, 158 data: None, 159 target_range: None, 160 is_composing: false, 161 platform: &plat, 162 }; 163 let result = handle_beforeinput(&mut editor, &undo_ctx, Range::caret(11)); 164 assert!(matches!(result, BeforeInputResult::Handled)); 165 assert_eq!(editor.content_string(), "hello"); 166 167 // Redo. 168 let redo_ctx = BeforeInputContext { 169 input_type: InputType::HistoryRedo, 170 data: None, 171 target_range: None, 172 is_composing: false, 173 platform: &plat, 174 }; 175 let result = handle_beforeinput(&mut editor, &redo_ctx, Range::caret(5)); 176 assert!(matches!(result, BeforeInputResult::Handled)); 177 assert_eq!(editor.content_string(), "hello world"); 178} 179 180#[wasm_bindgen_test] 181fn test_handle_insert_paragraph() { 182 let mut editor = make_editor("hello"); 183 editor.set_cursor_offset(5); 184 let plat = test_platform(); 185 186 let ctx = BeforeInputContext { 187 input_type: InputType::InsertParagraph, 188 data: None, 189 target_range: None, 190 is_composing: false, 191 platform: &plat, 192 }; 193 194 let result = handle_beforeinput(&mut editor, &ctx, Range::caret(5)); 195 assert!(matches!(result, BeforeInputResult::Handled)); 196 // InsertParagraph inserts double newline. 197 assert!(editor.content_string().contains("\n\n")); 198} 199 200#[wasm_bindgen_test] 201fn test_handle_selection_delete() { 202 let mut editor = make_editor("hello world"); 203 let plat = test_platform(); 204 205 let ctx = BeforeInputContext { 206 input_type: InputType::DeleteContentBackward, 207 data: None, 208 target_range: Some(Range::new(5, 11)), // Select " world" 209 is_composing: false, 210 platform: &plat, 211 }; 212 213 let result = handle_beforeinput(&mut editor, &ctx, Range::new(5, 11)); 214 assert!(matches!(result, BeforeInputResult::Handled)); 215 assert_eq!(editor.content_string(), "hello"); 216}