reovim-driver-session 0.14.4

Session driver for reovim - provides traits for session management
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
use super::*;

/// Verify the trait is object-safe (can use `dyn EmptySessionHandler`).
#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn trait_is_object_safe() {
    struct TestHandler;

    impl EmptySessionHandler for TestHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn id(&self) -> &'static str {
            "test:handler"
        }
        fn description(&self) -> &'static str {
            "Test handler"
        }
    }

    // This compiles if trait is object-safe
    let _: Box<dyn EmptySessionHandler> = Box::new(TestHandler);
}

#[test]
fn action_variants_constructible() {
    let create = EmptySessionAction::CreateBuffer {
        name: Some("test".to_string()),
        content: "hello".to_string(),
    };
    assert!(matches!(create, EmptySessionAction::CreateBuffer { .. }));

    let none = EmptySessionAction::None;
    assert!(matches!(none, EmptySessionAction::None));
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn default_priority_is_100() {
    struct TestHandler;

    impl EmptySessionHandler for TestHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn id(&self) -> &'static str {
            "test:handler"
        }
        fn description(&self) -> &'static str {
            "Test handler"
        }
    }

    let handler = TestHandler;
    assert_eq!(handler.priority(), 100);
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn custom_priority() {
    struct HighPriorityHandler;

    impl EmptySessionHandler for HighPriorityHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn priority(&self) -> u32 {
            10
        }
        fn id(&self) -> &'static str {
            "test:high-priority"
        }
        fn description(&self) -> &'static str {
            "High priority handler"
        }
    }

    let handler = HighPriorityHandler;
    assert_eq!(handler.priority(), 10);
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_creates_buffer_with_content() {
    struct ContentHandler;

    impl EmptySessionHandler for ContentHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::CreateBuffer {
                name: Some("welcome.txt".to_string()),
                content: "Welcome to reovim!".to_string(),
            }
        }
        fn id(&self) -> &'static str {
            "test:content"
        }
        fn description(&self) -> &'static str {
            "Content handler"
        }
    }

    let handler = ContentHandler;
    let ctx = EmptySessionContext {
        session_id: 1,
        file_args: &[],
        cwd: Path::new("/tmp"),
    };
    let action = handler.handle(&ctx);
    if let EmptySessionAction::CreateBuffer { name, content } = action {
        assert_eq!(name, Some("welcome.txt".to_string()));
        assert_eq!(content, "Welcome to reovim!");
    } else {
        panic!("expected CreateBuffer");
    }
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_creates_unnamed_buffer() {
    struct ScratchHandler;

    impl EmptySessionHandler for ScratchHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::CreateBuffer {
                name: None,
                content: String::new(),
            }
        }
        fn id(&self) -> &'static str {
            "test:scratch"
        }
        fn description(&self) -> &'static str {
            "Scratch buffer handler"
        }
    }

    let handler = ScratchHandler;
    let ctx = EmptySessionContext {
        session_id: 1,
        file_args: &[],
        cwd: Path::new("/tmp"),
    };
    let action = handler.handle(&ctx);
    if let EmptySessionAction::CreateBuffer { name, content } = action {
        assert!(name.is_none());
        assert!(content.is_empty());
    } else {
        panic!("expected CreateBuffer");
    }
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_with_file_args_returns_none() {
    struct ConditionalHandler;

    impl EmptySessionHandler for ConditionalHandler {
        fn handle(&self, ctx: &EmptySessionContext) -> EmptySessionAction {
            if ctx.file_args.is_empty() {
                EmptySessionAction::CreateBuffer {
                    name: None,
                    content: String::new(),
                }
            } else {
                EmptySessionAction::None
            }
        }
        fn id(&self) -> &'static str {
            "test:conditional"
        }
        fn description(&self) -> &'static str {
            "Conditional handler"
        }
    }

    let handler = ConditionalHandler;

    // With file args, should return None
    let files = vec!["file.txt".to_string()];
    let ctx = EmptySessionContext {
        session_id: 1,
        file_args: &files,
        cwd: Path::new("/tmp"),
    };
    assert!(matches!(handler.handle(&ctx), EmptySessionAction::None));

    // Without file args, should create buffer
    let ctx = EmptySessionContext {
        session_id: 1,
        file_args: &[],
        cwd: Path::new("/tmp"),
    };
    assert!(matches!(handler.handle(&ctx), EmptySessionAction::CreateBuffer { .. }));
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_id_and_description() {
    struct TestHandler;

    impl EmptySessionHandler for TestHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn id(&self) -> &'static str {
            "test-module:my-handler"
        }
        fn description(&self) -> &'static str {
            "A test handler for unit tests"
        }
    }

    let handler = TestHandler;
    assert_eq!(handler.id(), "test-module:my-handler");
    assert_eq!(handler.description(), "A test handler for unit tests");
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn empty_session_context_debug() {
    let ctx = EmptySessionContext {
        session_id: 42,
        file_args: &[],
        cwd: Path::new("/home/user"),
    };
    let debug = format!("{ctx:?}");
    assert!(debug.contains("EmptySessionContext"));
    assert!(debug.contains("42"));
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn empty_session_action_debug() {
    let action = EmptySessionAction::None;
    let debug = format!("{action:?}");
    assert!(debug.contains("None"));

    let action = EmptySessionAction::CreateBuffer {
        name: Some("test".to_string()),
        content: "hello".to_string(),
    };
    let debug = format!("{action:?}");
    assert!(debug.contains("CreateBuffer"));
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn empty_session_action_clone() {
    let action = EmptySessionAction::CreateBuffer {
        name: Some("test".to_string()),
        content: "content".to_string(),
    };
    #[allow(clippy::redundant_clone)]
    let cloned = action.clone();
    if let EmptySessionAction::CreateBuffer { name, content } = cloned {
        assert_eq!(name, Some("test".to_string()));
        assert_eq!(content, "content");
    } else {
        panic!("expected CreateBuffer");
    }
}

#[test]
fn empty_session_action_clone_none() {
    let action = EmptySessionAction::None;
    #[allow(clippy::redundant_clone)]
    let cloned = action.clone();
    assert!(matches!(cloned, EmptySessionAction::None));
}

#[test]
fn empty_session_context_fields() {
    let files = vec!["a.rs".to_string(), "b.rs".to_string()];
    let ctx = EmptySessionContext {
        session_id: 99,
        file_args: &files,
        cwd: Path::new("/home/user/project"),
    };
    assert_eq!(ctx.session_id, 99);
    assert_eq!(ctx.file_args.len(), 2);
    assert_eq!(ctx.file_args[0], "a.rs");
    assert_eq!(ctx.cwd, Path::new("/home/user/project"));
}

#[test]
fn empty_session_context_empty_file_args() {
    let ctx = EmptySessionContext {
        session_id: 1,
        file_args: &[],
        cwd: Path::new("/"),
    };
    assert!(ctx.file_args.is_empty());
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_multiple_on_same_context() {
    struct FirstHandler;
    impl EmptySessionHandler for FirstHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn priority(&self) -> u32 {
            50
        }
        fn id(&self) -> &'static str {
            "test:first"
        }
        fn description(&self) -> &'static str {
            "First handler"
        }
    }

    struct SecondHandler;
    impl EmptySessionHandler for SecondHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::CreateBuffer {
                name: None,
                content: String::new(),
            }
        }
        fn priority(&self) -> u32 {
            100
        }
        fn id(&self) -> &'static str {
            "test:second"
        }
        fn description(&self) -> &'static str {
            "Second handler"
        }
    }

    let ctx = EmptySessionContext {
        session_id: 1,
        file_args: &[],
        cwd: Path::new("/tmp"),
    };

    let first = FirstHandler;
    let second = SecondHandler;

    // First returns None, second returns CreateBuffer
    assert!(matches!(first.handle(&ctx), EmptySessionAction::None));
    assert!(matches!(second.handle(&ctx), EmptySessionAction::CreateBuffer { .. }));

    // Priority ordering
    assert!(first.priority() < second.priority());
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_with_zero_priority() {
    struct ZeroPriority;
    impl EmptySessionHandler for ZeroPriority {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn priority(&self) -> u32 {
            0
        }
        fn id(&self) -> &'static str {
            "test:zero"
        }
        fn description(&self) -> &'static str {
            "Zero priority"
        }
    }

    let handler = ZeroPriority;
    assert_eq!(handler.priority(), 0);
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn create_buffer_with_none_name_and_content() {
    let action = EmptySessionAction::CreateBuffer {
        name: None,
        content: "some content here".to_string(),
    };
    if let EmptySessionAction::CreateBuffer { name, content } = action {
        assert!(name.is_none());
        assert!(!content.is_empty());
    } else {
        panic!("expected CreateBuffer");
    }
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_send_sync_bounds() {
    fn assert_send_sync<T: Send + Sync + 'static>() {}

    struct StaticHandler;
    impl EmptySessionHandler for StaticHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn id(&self) -> &'static str {
            "test:static"
        }
        fn description(&self) -> &'static str {
            "Static handler"
        }
    }

    assert_send_sync::<StaticHandler>();
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_uses_cwd_from_context() {
    struct CwdHandler;
    impl EmptySessionHandler for CwdHandler {
        fn handle(&self, ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::CreateBuffer {
                name: Some(ctx.cwd.display().to_string()),
                content: String::new(),
            }
        }
        fn id(&self) -> &'static str {
            "test:cwd"
        }
        fn description(&self) -> &'static str {
            "CWD handler"
        }
    }

    let handler = CwdHandler;
    let ctx = EmptySessionContext {
        session_id: 1,
        file_args: &[],
        cwd: Path::new("/home/user/project"),
    };
    let action = handler.handle(&ctx);
    if let EmptySessionAction::CreateBuffer { name, .. } = action {
        assert_eq!(name, Some("/home/user/project".to_string()));
    } else {
        panic!("expected CreateBuffer");
    }
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_uses_session_id_from_context() {
    struct SessionIdHandler;
    impl EmptySessionHandler for SessionIdHandler {
        fn handle(&self, ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::CreateBuffer {
                name: Some(format!("session-{}", ctx.session_id)),
                content: String::new(),
            }
        }
        fn id(&self) -> &'static str {
            "test:session-id"
        }
        fn description(&self) -> &'static str {
            "Session ID handler"
        }
    }

    let handler = SessionIdHandler;
    let ctx = EmptySessionContext {
        session_id: 42,
        file_args: &[],
        cwd: Path::new("/tmp"),
    };
    let action = handler.handle(&ctx);
    if let EmptySessionAction::CreateBuffer { name, .. } = action {
        assert_eq!(name, Some("session-42".to_string()));
    } else {
        panic!("expected CreateBuffer");
    }
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn empty_session_context_debug_with_file_args() {
    let files = vec!["file1.rs".to_string(), "file2.rs".to_string()];
    let ctx = EmptySessionContext {
        session_id: 10,
        file_args: &files,
        cwd: Path::new("/tmp"),
    };
    let debug = format!("{ctx:?}");
    assert!(debug.contains("10"));
    assert!(debug.contains("file1.rs"));
    assert!(debug.contains("file2.rs"));
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_with_max_priority() {
    struct MaxPriorityHandler;
    impl EmptySessionHandler for MaxPriorityHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn priority(&self) -> u32 {
            u32::MAX
        }
        fn id(&self) -> &'static str {
            "test:max-priority"
        }
        fn description(&self) -> &'static str {
            "Max priority handler"
        }
    }

    let handler = MaxPriorityHandler;
    assert_eq!(handler.priority(), u32::MAX);
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn create_buffer_action_debug_with_none_name() {
    let action = EmptySessionAction::CreateBuffer {
        name: None,
        content: String::new(),
    };
    let debug = format!("{action:?}");
    assert!(debug.contains("CreateBuffer"));
    assert!(debug.contains("None"));
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_description_is_not_empty() {
    struct DescribedHandler;
    impl EmptySessionHandler for DescribedHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn id(&self) -> &'static str {
            "test:described"
        }
        fn description(&self) -> &'static str {
            "A handler with a description"
        }
    }

    let handler = DescribedHandler;
    assert!(!handler.description().is_empty());
    assert!(handler.description().contains("description"));
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn handler_id_follows_naming_convention() {
    struct WellNamedHandler;
    impl EmptySessionHandler for WellNamedHandler {
        fn handle(&self, _ctx: &EmptySessionContext) -> EmptySessionAction {
            EmptySessionAction::None
        }
        fn id(&self) -> &'static str {
            "module:handler-name"
        }
        fn description(&self) -> &'static str {
            "Well named handler"
        }
    }

    let handler = WellNamedHandler;
    assert!(handler.id().contains(':'));
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn clone_create_buffer_with_long_content() {
    let action = EmptySessionAction::CreateBuffer {
        name: Some("long-content".to_string()),
        content: "a".repeat(10000),
    };
    #[allow(clippy::redundant_clone)]
    let cloned = action.clone();
    if let EmptySessionAction::CreateBuffer { name, content } = cloned {
        assert_eq!(name, Some("long-content".to_string()));
        assert_eq!(content.len(), 10000);
    } else {
        panic!("expected CreateBuffer");
    }
}