seq-repl 5.6.0

TUI REPL for the Seq programming language with IR visualization
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use super::*;

#[test]
fn test_app_creation() -> Result<(), String> {
    let app = App::new()?;
    assert_eq!(app.editor.status(), "NORMAL");
    assert!(!app.should_quit);
    Ok(())
}

#[test]
fn test_mode_switching() -> Result<(), String> {
    let mut app = App::new()?;

    // i enters insert mode
    app.handle_key(KeyEvent::from(KeyCode::Char('i')));
    assert_eq!(app.editor.status(), "INSERT");

    // Esc returns to normal
    app.handle_key(KeyEvent::from(KeyCode::Esc));
    assert_eq!(app.editor.status(), "NORMAL");
    Ok(())
}

#[test]
fn test_insert_mode_typing() -> Result<(), String> {
    let mut app = App::new()?;
    app.handle_key(KeyEvent::from(KeyCode::Char('i')));

    app.handle_key(KeyEvent::from(KeyCode::Char('h')));
    app.handle_key(KeyEvent::from(KeyCode::Char('i')));

    assert_eq!(app.repl_state.input, "hi");
    Ok(())
}

#[test]
fn test_normal_mode_navigation() -> Result<(), String> {
    let mut app = App::new()?;

    // Type "hello" in insert mode
    app.handle_key(KeyEvent::from(KeyCode::Char('i')));
    for c in "hello".chars() {
        app.handle_key(KeyEvent::from(KeyCode::Char(c)));
    }
    app.handle_key(KeyEvent::from(KeyCode::Esc));

    // Now in normal mode at position 4 (esc moves left one from end)
    // Move to start with 0
    app.handle_key(KeyEvent::from(KeyCode::Char('0')));
    assert_eq!(app.repl_state.cursor, 0);

    // l moves right
    app.handle_key(KeyEvent::from(KeyCode::Char('l')));
    assert_eq!(app.repl_state.cursor, 1);

    // h moves left
    app.handle_key(KeyEvent::from(KeyCode::Char('h')));
    assert_eq!(app.repl_state.cursor, 0);

    // $ goes to end (ON last char, not past it - vim normal mode behavior)
    app.handle_key(KeyEvent::from(KeyCode::Char('$')));
    assert_eq!(app.repl_state.cursor, 4); // 'o' is at index 4
    Ok(())
}

#[test]
fn test_history_navigation() -> Result<(), String> {
    let mut app = App::new()?;

    // Add some history entries manually
    app.repl_state
        .add_entry(HistoryEntry::new("first").with_output("1"));
    app.repl_state
        .add_entry(HistoryEntry::new("second").with_output("2"));

    // Up arrow goes up in history (to most recent)
    app.handle_key(KeyEvent::from(KeyCode::Up));
    assert_eq!(app.repl_state.input, "second");

    // Up again goes to older entry
    app.handle_key(KeyEvent::from(KeyCode::Up));
    assert_eq!(app.repl_state.input, "first");

    // Down goes back to newer entry
    app.handle_key(KeyEvent::from(KeyCode::Down));
    assert_eq!(app.repl_state.input, "second");
    Ok(())
}

#[test]
fn test_jk_history_navigation() -> Result<(), String> {
    let mut app = App::new()?;

    // Add some history entries
    app.repl_state
        .add_entry(HistoryEntry::new("first").with_output("1"));
    app.repl_state
        .add_entry(HistoryEntry::new("second").with_output("2"));

    // In normal mode (default), k on single line goes to history
    app.handle_key(KeyEvent::from(KeyCode::Char('k')));
    assert_eq!(app.repl_state.input, "second");

    // k again goes to older entry
    app.handle_key(KeyEvent::from(KeyCode::Char('k')));
    assert_eq!(app.repl_state.input, "first");

    // j goes back to newer entry
    app.handle_key(KeyEvent::from(KeyCode::Char('j')));
    assert_eq!(app.repl_state.input, "second");

    // j again returns to empty input (current line)
    app.handle_key(KeyEvent::from(KeyCode::Char('j')));
    assert_eq!(app.repl_state.input, "");
    Ok(())
}

#[test]
fn test_jk_multiline_navigation() -> Result<(), String> {
    let mut app = App::new()?;

    // Add history entries: oldest first, then multi-line, then newest
    app.repl_state
        .add_entry(HistoryEntry::new("oldest").with_output("1"));
    app.repl_state
        .add_entry(HistoryEntry::new("line1\nline2\nline3").with_output("2"));

    // Navigate to the multi-line history entry
    app.handle_key(KeyEvent::from(KeyCode::Char('k'))); // Go to newest (multi-line)
    assert_eq!(app.repl_state.input, "line1\nline2\nline3");

    // Cursor should be at end of last line after history navigation
    // j on last line should go forward in history (to current empty input)
    app.handle_key(KeyEvent::from(KeyCode::Char('j')));
    assert_eq!(app.repl_state.input, "");

    // k goes back to multi-line
    app.handle_key(KeyEvent::from(KeyCode::Char('k')));
    assert_eq!(app.repl_state.input, "line1\nline2\nline3");

    // Now test navigation within the multi-line buffer
    // Go to start of last line with 0
    app.handle_key(KeyEvent::from(KeyCode::Char('0')));

    // k should move up within buffer (to line2), not to history
    app.handle_key(KeyEvent::from(KeyCode::Char('k')));
    assert_eq!(app.repl_state.input, "line1\nline2\nline3"); // Still same input

    // k again moves to line1 (now on first line)
    app.handle_key(KeyEvent::from(KeyCode::Char('k')));
    assert_eq!(app.repl_state.input, "line1\nline2\nline3"); // Still same input

    // k on first line goes to older history
    app.handle_key(KeyEvent::from(KeyCode::Char('k')));
    assert_eq!(app.repl_state.input, "oldest");

    Ok(())
}

#[test]
fn test_jk_with_unicode() -> Result<(), String> {
    let mut app = App::new()?;

    // Add history with emoji
    app.repl_state
        .add_entry(HistoryEntry::new("hello 👋").with_output("1"));

    // k should navigate to history without panicking
    app.handle_key(KeyEvent::from(KeyCode::Char('k')));
    assert_eq!(app.repl_state.input, "hello 👋");

    // j should navigate back
    app.handle_key(KeyEvent::from(KeyCode::Char('j')));
    assert_eq!(app.repl_state.input, "");

    Ok(())
}

#[test]
fn test_jk_empty_input() -> Result<(), String> {
    let mut app = App::new()?;

    // Add history so we have somewhere to navigate
    app.repl_state
        .add_entry(HistoryEntry::new("history").with_output("1"));

    // With empty input, j/k should work without panic
    assert_eq!(app.repl_state.input, "");
    app.handle_key(KeyEvent::from(KeyCode::Char('k'))); // Should go to history
    assert_eq!(app.repl_state.input, "history");

    app.handle_key(KeyEvent::from(KeyCode::Char('j'))); // Should return to empty
    assert_eq!(app.repl_state.input, "");

    Ok(())
}

#[test]
fn test_jk_insert_mode_no_history() -> Result<(), String> {
    let mut app = App::new()?;

    // Add history
    app.repl_state
        .add_entry(HistoryEntry::new("history").with_output("1"));

    // Enter insert mode
    app.handle_key(KeyEvent::from(KeyCode::Char('i')));
    assert_eq!(app.editor.status(), "INSERT");

    // j/k in insert mode should NOT navigate history - they should be passed
    // to vim-line which handles them as cursor movement (no-op on single line)
    app.handle_key(KeyEvent::from(KeyCode::Char('j')));
    assert_eq!(app.repl_state.input, "j"); // j inserted as text

    app.handle_key(KeyEvent::from(KeyCode::Char('k')));
    assert_eq!(app.repl_state.input, "jk"); // k inserted as text

    Ok(())
}

#[test]
fn test_quit_command() -> Result<(), String> {
    let mut app = App::new()?;
    // Ctrl+Q quits the application
    app.handle_key(KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL));
    assert!(app.should_quit);
    Ok(())
}

#[test]
fn test_repl_command() -> Result<(), String> {
    let mut app = App::new()?;
    app.handle_key(KeyEvent::from(KeyCode::Char('i')));
    app.handle_key(KeyEvent::from(KeyCode::Char(':')));
    app.handle_key(KeyEvent::from(KeyCode::Char('q')));
    app.handle_key(KeyEvent::from(KeyCode::Enter));
    assert!(app.should_quit);
    Ok(())
}

#[test]
fn test_word_effect_lookup() -> Result<(), String> {
    let app = App::new()?;
    // Stack manipulation
    assert!(app.get_word_effect("dup").is_some());
    assert!(app.get_word_effect("swap").is_some());

    // Integer arithmetic - long and symbolic forms
    assert!(app.get_word_effect("i.add").is_some());
    assert!(app.get_word_effect("i.+").is_some());
    assert!(app.get_word_effect("i.multiply").is_some());
    assert!(app.get_word_effect("i.*").is_some());

    // Integer comparisons
    assert!(app.get_word_effect("i.<").is_some());
    assert!(app.get_word_effect("i.=").is_some());

    // Float arithmetic - long and symbolic forms
    assert!(app.get_word_effect("f.add").is_some());
    assert!(app.get_word_effect("f.*").is_some());

    // Float comparisons
    assert!(app.get_word_effect("f.<").is_some());

    // Unknown word
    assert!(app.get_word_effect("unknown").is_none());
    Ok(())
}

#[test]
fn test_ctrl_c_quits() -> Result<(), String> {
    let mut app = App::new()?;
    let key = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
    app.handle_key(key);
    assert!(app.should_quit);
    Ok(())
}

#[test]
fn test_tab_completion() -> Result<(), String> {
    let mut app = App::new()?;

    // Enter insert mode and type "du"
    app.handle_key(KeyEvent::from(KeyCode::Char('i')));
    app.handle_key(KeyEvent::from(KeyCode::Char('d')));
    app.handle_key(KeyEvent::from(KeyCode::Char('u')));

    // Verify input and cursor are correct
    assert_eq!(app.repl_state.input, "du");
    assert_eq!(app.repl_state.cursor, 2);

    // Press Tab to trigger completion
    app.handle_key(KeyEvent::from(KeyCode::Tab));

    // Should show completions with "dup" matching
    assert!(
        app.completions.is_visible(),
        "Completions should be visible after Tab"
    );
    assert!(
        !app.completions.items().is_empty(),
        "Should have completions for 'du'"
    );
    assert!(
        app.completions.items().iter().any(|c| c.label == "dup"),
        "Should include 'dup' in completions"
    );

    Ok(())
}

#[test]
fn test_tab_completion_empty_prefix() -> Result<(), String> {
    let mut app = App::new()?;

    // Press Tab with empty input
    app.handle_key(KeyEvent::from(KeyCode::Tab));

    // Should not show completions, should show status message
    assert!(
        !app.completions.is_visible(),
        "Should not show completions for empty prefix"
    );
    assert!(
        app.status_message
            .as_ref()
            .is_some_and(|m| m.contains("type a prefix")),
        "Should show 'type a prefix' message"
    );

    Ok(())
}

#[test]
fn test_search_mode_enter_and_exit() -> Result<(), String> {
    let mut app = App::new()?;

    // Add history
    app.repl_state
        .add_entry(HistoryEntry::new("first entry").with_output("1"));
    app.repl_state
        .add_entry(HistoryEntry::new("second entry").with_output("2"));

    // In normal mode, '/' enters search mode
    assert!(!app.search_mode);
    app.handle_key(KeyEvent::from(KeyCode::Char('/')));
    assert!(app.search_mode);
    assert!(
        app.status_message
            .as_ref()
            .is_some_and(|m| m.starts_with("/")),
        "Status should show search prompt"
    );

    // Esc exits search mode
    app.handle_key(KeyEvent::from(KeyCode::Esc));
    assert!(!app.search_mode);

    Ok(())
}

#[test]
fn test_search_mode_filtering() -> Result<(), String> {
    let mut app = App::new()?;
    app.repl_state.history.clear(); // Clear any loaded history

    // Add history with different content
    app.repl_state
        .add_entry(HistoryEntry::new("dup swap").with_output("1"));
    app.repl_state
        .add_entry(HistoryEntry::new("drop").with_output("2"));
    app.repl_state
        .add_entry(HistoryEntry::new("dup dup").with_output("3"));

    // Enter search mode
    app.handle_key(KeyEvent::from(KeyCode::Char('/')));
    assert!(app.search_mode);

    // Type "dup" to search
    app.handle_key(KeyEvent::from(KeyCode::Char('d')));
    app.handle_key(KeyEvent::from(KeyCode::Char('u')));
    app.handle_key(KeyEvent::from(KeyCode::Char('p')));

    // Should have 2 matches (entries containing "dup")
    assert_eq!(app.search_matches.len(), 2);
    // Preview shows most recent match
    assert_eq!(app.repl_state.input, "dup dup");
    assert!(
        app.status_message
            .as_ref()
            .is_some_and(|m| m.contains("/dup") && m.contains("(1/2)")),
        "Status should show search pattern and match count"
    );

    Ok(())
}

#[test]
fn test_search_mode_accept() -> Result<(), String> {
    let mut app = App::new()?;

    // Add history
    app.repl_state
        .add_entry(HistoryEntry::new("first").with_output("1"));
    app.repl_state
        .add_entry(HistoryEntry::new("second").with_output("2"));

    // Enter search mode and search for "first"
    app.handle_key(KeyEvent::from(KeyCode::Char('/')));
    for c in "first".chars() {
        app.handle_key(KeyEvent::from(KeyCode::Char(c)));
    }

    // Preview should already show the match
    assert_eq!(app.repl_state.input, "first");

    // Press Enter to accept
    app.handle_key(KeyEvent::from(KeyCode::Enter));

    // Should be out of search mode with input kept
    assert!(!app.search_mode);
    assert_eq!(app.repl_state.input, "first");

    Ok(())
}

#[test]
fn test_search_mode_cancel_restores_input() -> Result<(), String> {
    let mut app = App::new()?;

    // Add history
    app.repl_state
        .add_entry(HistoryEntry::new("history entry").with_output("1"));

    // Type something first
    app.handle_key(KeyEvent::from(KeyCode::Char('i')));
    for c in "my input".chars() {
        app.handle_key(KeyEvent::from(KeyCode::Char(c)));
    }
    app.handle_key(KeyEvent::from(KeyCode::Esc));
    assert_eq!(app.repl_state.input, "my input");

    // Enter search mode
    app.handle_key(KeyEvent::from(KeyCode::Char('/')));
    for c in "history".chars() {
        app.handle_key(KeyEvent::from(KeyCode::Char(c)));
    }

    // Preview shows match
    assert_eq!(app.repl_state.input, "history entry");

    // Cancel with Esc - should restore original
    app.handle_key(KeyEvent::from(KeyCode::Esc));
    assert!(!app.search_mode);
    assert_eq!(app.repl_state.input, "my input");

    Ok(())
}

#[test]
fn test_search_mode_navigate_matches() -> Result<(), String> {
    let mut app = App::new()?;
    app.repl_state.history.clear(); // Clear any loaded history

    // Add history with same prefix
    app.repl_state
        .add_entry(HistoryEntry::new("test1").with_output("1"));
    app.repl_state
        .add_entry(HistoryEntry::new("test2").with_output("2"));
    app.repl_state
        .add_entry(HistoryEntry::new("test3").with_output("3"));

    // Enter search mode and search for "test"
    app.handle_key(KeyEvent::from(KeyCode::Char('/')));
    for c in "test".chars() {
        app.handle_key(KeyEvent::from(KeyCode::Char(c)));
    }

    // Should have 3 matches, starting at index 0 (most recent first: test3)
    assert_eq!(app.search_matches.len(), 3);
    assert_eq!(app.search_match_index, 0);
    // Preview shows first match
    assert_eq!(app.repl_state.input, "test3");

    // Tab goes to next match
    app.handle_key(KeyEvent::from(KeyCode::Tab));
    assert_eq!(app.search_match_index, 1);
    assert_eq!(app.repl_state.input, "test2");

    // Shift+Tab goes to previous match
    app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::SHIFT));
    assert_eq!(app.search_match_index, 0);
    assert_eq!(app.repl_state.input, "test3");

    // Shift+Tab wraps around to last match
    app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::SHIFT));
    assert_eq!(app.search_match_index, 2);
    assert_eq!(app.repl_state.input, "test1");

    Ok(())
}

#[test]
fn test_search_mode_backspace() -> Result<(), String> {
    let mut app = App::new()?;
    app.repl_state.history.clear(); // Clear any loaded history

    // Add history
    app.repl_state
        .add_entry(HistoryEntry::new("dup").with_output("1"));
    app.repl_state
        .add_entry(HistoryEntry::new("drop").with_output("2"));

    // Enter search mode
    app.handle_key(KeyEvent::from(KeyCode::Char('/')));

    // Type "dup"
    for c in "dup".chars() {
        app.handle_key(KeyEvent::from(KeyCode::Char(c)));
    }
    assert_eq!(app.search_pattern, "dup");
    assert_eq!(app.search_matches.len(), 1);

    // Backspace removes last char
    app.handle_key(KeyEvent::from(KeyCode::Backspace));
    assert_eq!(app.search_pattern, "du");

    // With "du", should match both "dup" (d-u-p) - wait, "drop" doesn't match "du"
    // Actually only "dup" matches "du"
    assert_eq!(app.search_matches.len(), 1);

    Ok(())
}

#[test]
fn test_search_mode_case_insensitive() -> Result<(), String> {
    let mut app = App::new()?;
    app.repl_state.history.clear(); // Clear any loaded history

    // Add history with mixed case
    app.repl_state
        .add_entry(HistoryEntry::new("DUP").with_output("1"));
    app.repl_state
        .add_entry(HistoryEntry::new("Dup").with_output("2"));
    app.repl_state
        .add_entry(HistoryEntry::new("dup").with_output("3"));

    // Enter search mode and search for lowercase "dup"
    app.handle_key(KeyEvent::from(KeyCode::Char('/')));
    for c in "dup".chars() {
        app.handle_key(KeyEvent::from(KeyCode::Char(c)));
    }

    // Should match all 3 (case insensitive)
    assert_eq!(app.search_matches.len(), 3);

    Ok(())
}

#[test]
fn test_search_not_in_insert_mode() -> Result<(), String> {
    let mut app = App::new()?;

    // Enter insert mode
    app.handle_key(KeyEvent::from(KeyCode::Char('i')));
    assert_eq!(app.editor.status(), "INSERT");

    // '/' in insert mode should insert '/', not enter search mode
    app.handle_key(KeyEvent::from(KeyCode::Char('/')));
    assert!(!app.search_mode);
    assert_eq!(app.repl_state.input, "/");

    Ok(())
}