reovim-module-vim 0.14.3

Vim policy module for reovim - keybindings and behavior
Documentation
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use {
    reovim_driver_annotation::AnnotationSourceRegistry,
    reovim_driver_command::{CommandHandlerStore, CommandProvider},
    reovim_driver_input::{KeybindingStore, ModeInfoStore, ModeProviderRegistry, ResolverRegistry},
    reovim_kernel::api::v1::{Module, ModuleContext, OptionScope, OptionValue, ProbeResult},
};

use crate::{VimMode, VimModule, bindings, commands, operators, visual};

#[test]
fn test_vim_module_id() {
    let module = VimModule::new();
    assert_eq!(module.id().as_str(), "vim");
}

#[test]
fn test_vim_module_name() {
    let module = VimModule::new();
    assert_eq!(module.name(), "Vim");
}

#[test]
fn test_vim_module_version() {
    let module = VimModule::new();
    let version = module.version();
    assert_eq!(version.major, 0);
    assert_eq!(version.minor, 9);
}

#[test]
fn test_vim_keybindings_not_empty() {
    let module = VimModule::new();
    let bindings = module.keybindings();
    assert!(!bindings.is_empty(), "Vim module should provide keybindings");
    // Should have at least 100 bindings across all modes
    assert!(bindings.len() > 100, "Vim module should have many keybindings");
}

#[test]
fn test_normal_mode_bindings() {
    let bindings = bindings::normal::bindings();
    assert!(!bindings.is_empty(), "Normal mode should have bindings");
    // Verify basic navigation keys exist
    assert!(bindings.iter().any(|b| b.keys == "h"), "Normal mode should have 'h' binding");
    assert!(bindings.iter().any(|b| b.keys == "j"), "Normal mode should have 'j' binding");
    assert!(bindings.iter().any(|b| b.keys == "k"), "Normal mode should have 'k' binding");
    assert!(bindings.iter().any(|b| b.keys == "l"), "Normal mode should have 'l' binding");
}

#[test]
fn test_insert_mode_bindings() {
    let bindings = bindings::insert::bindings();
    assert!(!bindings.is_empty(), "Insert mode should have bindings");
    // Verify escape key exists
    assert!(
        bindings.iter().any(|b| b.keys == "<Esc>"),
        "Insert mode should have Escape binding"
    );
}

#[test]
fn test_visual_mode_bindings() {
    let bindings = bindings::visual::bindings();
    assert!(!bindings.is_empty(), "Visual mode should have bindings");
}

#[test]
fn test_operator_modes_bindings() {
    let bindings = bindings::operator_modes::all_operator_bindings();
    assert!(!bindings.is_empty(), "Operator modes should have bindings");
}

#[test]
fn test_commandline_mode_bindings() {
    let bindings = bindings::commandline::bindings();
    assert!(!bindings.is_empty(), "Commandline mode should have bindings");
}

#[test]
fn test_all_bindings_aggregation() {
    let all = bindings::all();
    let normal = bindings::normal::bindings();
    let insert = bindings::insert::bindings();
    let visual = bindings::visual::bindings();
    let operator_modes = bindings::operator_modes::all_operator_bindings();
    let cmdline = bindings::commandline::bindings();
    let window = bindings::window::bindings();

    let expected_total = normal.len()
        + insert.len()
        + visual.len()
        + operator_modes.len()
        + cmdline.len()
        + window.len();
    assert_eq!(all.len(), expected_total, "all() should aggregate all mode bindings");
}

// ========================================================================
// Epic #445: Line number option tests
// ========================================================================

#[test]
fn test_line_number_options_registered() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();

    let result = module.init(&ctx);
    assert!(
        matches!(result, ProbeResult::Success),
        "Vim module should initialize successfully"
    );

    // Verify 'number' option is registered
    assert!(ctx.kernel.options.contains("number"), "'number' option should be registered");

    // Verify 'relativenumber' option is registered
    assert!(
        ctx.kernel.options.contains("relativenumber"),
        "'relativenumber' option should be registered"
    );
}

#[test]
fn test_line_number_option_aliases() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    // Verify short alias 'nu' resolves to 'number'
    assert_eq!(
        ctx.kernel.options.resolve_name("nu"),
        Some("number".to_string()),
        "'nu' should be an alias for 'number'"
    );

    // Verify short alias 'rnu' resolves to 'relativenumber'
    assert_eq!(
        ctx.kernel.options.resolve_name("rnu"),
        Some("relativenumber".to_string()),
        "'rnu' should be an alias for 'relativenumber'"
    );
}

#[test]
fn test_line_number_option_defaults() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    // Both options should default to false
    let number_val = ctx.kernel.options.get_global("number");
    assert_eq!(number_val, Some(OptionValue::bool(false)), "'number' should default to false");

    let rnu_val = ctx.kernel.options.get_global("relativenumber");
    assert_eq!(
        rnu_val,
        Some(OptionValue::bool(false)),
        "'relativenumber' should default to false"
    );
}

#[test]
fn test_line_number_option_scope() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    // Both options should have Window scope
    let number_spec = ctx.kernel.options.get_spec("number").unwrap();
    assert_eq!(number_spec.scope, OptionScope::Window, "'number' should have Window scope");

    let rnu_spec = ctx.kernel.options.get_spec("relativenumber").unwrap();
    assert_eq!(rnu_spec.scope, OptionScope::Window, "'relativenumber' should have Window scope");
}

// ========================================================================
// Epic #570: Vim behavior options (#573)
// ========================================================================

#[test]
fn test_vim_manifest_options_count() {
    let manifest =
        reovim_driver_manifest::PersonalityManifest::parse(crate::VIM_MANIFEST_TOML).unwrap();
    // 9 options: number, relativenumber, scrolloff, sidescrolloff,
    // ignorecase, smartcase, hlsearch, incsearch, wrapscan
    assert_eq!(manifest.options.len(), 9);
}

#[test]
fn test_vim_options_registered_after_init() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    let expected = [
        "scrolloff",
        "sidescrolloff",
        "ignorecase",
        "smartcase",
        "hlsearch",
        "incsearch",
        "wrapscan",
    ];
    for name in &expected {
        assert!(ctx.kernel.options.contains(name), "'{name}' should be registered");
    }
}

#[test]
fn test_vim_options_aliases() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    let aliases = [
        ("so", "scrolloff"),
        ("siso", "sidescrolloff"),
        ("ic", "ignorecase"),
        ("scs", "smartcase"),
        ("hls", "hlsearch"),
        ("is", "incsearch"),
        ("ws", "wrapscan"),
    ];
    for (short, full) in &aliases {
        assert_eq!(
            ctx.kernel.options.resolve_name(short),
            Some(full.to_string()),
            "'{short}' should resolve to '{full}'"
        );
    }
}

#[test]
fn test_vim_options_defaults() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    assert_eq!(ctx.kernel.options.get_global("scrolloff"), Some(OptionValue::int(0)));
    assert_eq!(ctx.kernel.options.get_global("sidescrolloff"), Some(OptionValue::int(0)));
    assert_eq!(ctx.kernel.options.get_global("ignorecase"), Some(OptionValue::bool(false)));
    assert_eq!(ctx.kernel.options.get_global("smartcase"), Some(OptionValue::bool(false)));
    assert_eq!(ctx.kernel.options.get_global("hlsearch"), Some(OptionValue::bool(false)));
    assert_eq!(ctx.kernel.options.get_global("incsearch"), Some(OptionValue::bool(false)));
    assert_eq!(ctx.kernel.options.get_global("wrapscan"), Some(OptionValue::bool(true)));
}

#[test]
fn test_vim_options_ownership() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    let vim_options = ctx.kernel.options.list_by_module(&crate::VIM_MODULE);
    // 7 vim behavior options + 2 line number options = 9
    assert_eq!(vim_options.len(), 9);
}

#[test]
fn test_vim_init_fails_on_duplicate_option() {
    use reovim_kernel::api::v1::OptionSpec;

    let ctx = ModuleContext::default();

    // Pre-register one of our options to trigger a conflict
    let _ = ctx.kernel.options.register(OptionSpec::new(
        "scrolloff",
        "Already taken",
        OptionValue::int(1),
    ));

    let mut module = VimModule::new();
    let result = module.init(&ctx);
    assert!(matches!(result, ProbeResult::Failed(_)), "init should fail on duplicate option");
}

// ========================================================================
// AnnotationSource registration test
// ========================================================================

#[test]
fn test_annotation_source_registered() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    // Verify AnnotationSourceRegistry is created and has a source
    let registry = ctx.services.get::<AnnotationSourceRegistry>();
    assert!(registry.is_some(), "AnnotationSourceRegistry should be created");

    let registry = registry.unwrap();
    assert_eq!(registry.len(), 1, "Should have 1 annotation source registered");
}

// ========================================================================
// VimModule Default and exit tests
// ========================================================================

#[test]
fn test_vim_module_default() {
    let module = VimModule;
    assert_eq!(module.id().as_str(), "vim");
    assert_eq!(module.name(), "Vim");
}

#[test]
fn test_vim_module_exit() {
    let mut module = VimModule::new();
    let result = module.exit();
    assert!(result.is_ok(), "exit() should succeed");
}

#[test]
fn test_vim_module_const_new() {
    const MODULE: VimModule = VimModule::new();
    assert_eq!(MODULE.name(), "Vim");
}

// ========================================================================
// CommandProvider tests
// ========================================================================

#[test]
fn test_command_handlers_not_empty() {
    let module = VimModule::new();
    let handlers = module.command_handlers();
    assert!(!handlers.is_empty(), "Should have command handlers");
}

#[test]
fn test_command_handlers_count() {
    let module = VimModule::new();
    let handlers = module.command_handlers();
    // mode_commands() + visual_commands() + operator_commands()
    let mode_count = commands::mode_commands().len();
    let visual_count = visual::visual_commands().len();
    let operator_count = operators::operator_commands().len();
    let expected = mode_count + visual_count + operator_count;
    assert_eq!(
        handlers.len(),
        expected,
        "command_handlers should aggregate mode + visual + operator commands"
    );
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn test_command_handlers_all_have_ids() {
    let module = VimModule::new();
    let handlers = module.command_handlers();
    for handler in &handlers {
        let id = handler.id();
        assert_eq!(id.module().as_str(), "vim", "handler '{}' should be in vim module", id.name());
    }
}

// ========================================================================
// Init registration counts
// ========================================================================

#[test]
fn test_init_registers_resolvers() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    let resolver_registry = ctx.services.get::<ResolverRegistry>();
    assert!(resolver_registry.is_some(), "ResolverRegistry should exist after init");
    let resolver_registry = resolver_registry.unwrap();
    // Should have: normal, insert, replace, delete, yank, change, commandline, window,
    // visual, visual-line, visual-block, lowercase, uppercase, toggle-case = 14
    assert_eq!(resolver_registry.len(), 14, "Should register 14 resolvers");
}

#[test]
fn test_init_registers_modes() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    let mode_store = ctx.services.get::<ModeInfoStore>();
    assert!(mode_store.is_some(), "ModeInfoStore should exist after init");
    let mode_store = mode_store.unwrap();
    assert_eq!(mode_store.len(), VimMode::ALL.len(), "Should register all VimMode variants");
}

#[test]
fn test_init_registers_keybindings() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    let keybinding_store = ctx.services.get::<KeybindingStore>();
    assert!(keybinding_store.is_some(), "KeybindingStore should exist after init");
    let keybinding_store = keybinding_store.unwrap();
    let all_bindings = bindings::all();
    // Keybindings = vim core bindings only (#700: manifest keybindings migrated to in-crate adapters)
    assert_eq!(
        keybinding_store.len(),
        all_bindings.len(),
        "Should register all vim core keybindings"
    );
}

#[test]
fn test_init_registers_commands() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    let command_store = ctx.services.get::<CommandHandlerStore>();
    assert!(command_store.is_some(), "CommandHandlerStore should exist after init");
    let command_store = command_store.unwrap();
    let expected = module.command_handlers().len();
    assert_eq!(command_store.len(), expected, "Should register all command handlers");
}

#[test]
fn test_init_registers_mode_bridge_store() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    let bridge_store = ctx
        .services
        .get::<reovim_driver_manifest::ModeBridgeStore>();
    assert!(bridge_store.is_some(), "ModeBridgeStore should exist after init");
    let bridge_store = bridge_store.unwrap();
    assert_eq!(bridge_store.bridges().len(), 2, "Should have 2 mode bridges");
    assert_eq!(bridge_store.find_parent("snippet:navigating"), Some("vim:insert"));
    assert_eq!(bridge_store.find_parent("range-finder:jump-input"), Some("vim:normal"));
}

#[test]
fn test_vim_manifest_has_no_key_conflicts() {
    let manifest =
        reovim_driver_manifest::PersonalityManifest::parse(crate::VIM_MANIFEST_TOML).unwrap();
    let conflicts = manifest.detect_conflicts();
    assert!(
        conflicts.is_empty(),
        "vim.toml should have no key conflicts, found: {conflicts:?}"
    );
}

#[test]
fn test_vim_manifest_modes_are_valid() {
    let manifest =
        reovim_driver_manifest::PersonalityManifest::parse(crate::VIM_MANIFEST_TOML).unwrap();
    // Validate against VimModule's registered modes
    let known_modes = &[
        ("vim", "normal"),
        ("vim", "insert"),
        ("vim", "visual"),
        ("vim", "operator-pending"),
    ];
    let warnings = manifest.validate_modes(known_modes);
    assert!(
        warnings.is_empty(),
        "vim.toml should only reference valid vim modes, found: {warnings:?}"
    );
}

#[test]
fn test_init_registers_mode_provider() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    module.init(&ctx);

    let mode_registry = ctx.services.get::<ModeProviderRegistry>();
    assert!(mode_registry.is_some(), "ModeProviderRegistry should exist after init");
}

#[test]
fn test_init_returns_success() {
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    let result = module.init(&ctx);
    assert!(matches!(result, ProbeResult::Success));
}

#[test]
fn test_init_idempotent_options() {
    // Second init should fail because options already registered
    let mut module = VimModule::new();
    let ctx = ModuleContext::default();
    let result1 = module.init(&ctx);
    assert!(matches!(result1, ProbeResult::Success));

    let result2 = module.init(&ctx);
    // Second init may fail on option registration - this is expected behavior
    // The important thing is it doesn't panic
    let _ = result2;
}

#[test]
fn test_vim_module_version_patch() {
    let module = VimModule::new();
    let version = module.version();
    assert_eq!(version.patch, 0);
}

// ========================================================================
// Binding category tests
// ========================================================================

#[test]
fn test_normal_mode_has_motion_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("motion")),
        "Normal mode should have motion bindings"
    );
}

#[test]
fn test_normal_mode_has_operator_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("operator")),
        "Normal mode should have operator bindings"
    );
}

#[test]
fn test_normal_mode_has_mode_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("mode")),
        "Normal mode should have mode bindings"
    );
}

#[test]
fn test_normal_mode_has_edit_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("edit")),
        "Normal mode should have edit bindings"
    );
}

#[test]
fn test_normal_mode_has_history_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("history")),
        "Normal mode should have history bindings"
    );
}

#[test]
fn test_normal_mode_has_clipboard_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("clipboard")),
        "Normal mode should have clipboard bindings"
    );
}

#[test]
fn test_normal_mode_has_scroll_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("scroll")),
        "Normal mode should have scroll bindings"
    );
}

#[test]
fn test_normal_mode_has_search_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("search")),
        "Normal mode should have search bindings"
    );
}

#[test]
fn test_normal_mode_has_window_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("window")),
        "Normal mode should have window bindings"
    );
}

#[test]
fn test_normal_mode_has_mark_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("mark")),
        "Normal mode should have mark bindings"
    );
}

#[test]
fn test_normal_mode_has_session_category() {
    let bindings = bindings::normal::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("session")),
        "Normal mode should have session bindings"
    );
}

#[test]
fn test_insert_mode_has_completion_category() {
    let bindings = bindings::insert::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("completion")),
        "Insert mode should have completion bindings"
    );
}

#[test]
fn test_visual_mode_has_textobject_category() {
    let bindings = bindings::visual::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("textobject")),
        "Visual mode should have textobject bindings"
    );
}

#[test]
fn test_visual_mode_has_selection_category() {
    let bindings = bindings::visual::bindings();
    assert!(
        bindings.iter().any(|b| b.category == Some("selection")),
        "Visual mode should have selection bindings"
    );
}