reovim-server 0.14.4

Reovim server - the editing engine
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
use super::*;

fn make_record(level: Level, message: &str) -> Record<'_> {
    Record::builder(level)
        .message(message)
        .module_path("test::module")
        .file("test.rs")
        .line(42)
        .build()
}

#[test]
fn test_push_and_tail() {
    let buffer = DebugRingBuffer::with_capacity(4096);

    buffer.push(&make_record(Level::Info, "message 1"));
    buffer.push(&make_record(Level::Warn, "message 2"));
    buffer.push(&make_record(Level::Error, "message 3"));

    let recent = buffer.tail(2);
    assert_eq!(recent.len(), 2);
    assert_eq!(recent[0].message, "message 3"); // Newest first
    assert_eq!(recent[1].message, "message 2");
}

#[test]
fn test_entries_order() {
    let buffer = DebugRingBuffer::with_capacity(4096);

    buffer.push(&make_record(Level::Info, "first"));
    buffer.push(&make_record(Level::Info, "second"));
    buffer.push(&make_record(Level::Info, "third"));

    let all = buffer.entries();
    assert_eq!(all.len(), 3);
    assert_eq!(all[0].message, "first"); // Oldest first
    assert_eq!(all[1].message, "second");
    assert_eq!(all[2].message, "third");
}

#[test]
fn test_wrap_around() {
    // Small buffer that will definitely wrap
    let buffer = DebugRingBuffer::with_capacity(200);

    // Push enough entries to force wrap
    for i in 0..20 {
        buffer.push(&make_record(Level::Info, &format!("message {i}")));
    }

    let stats = buffer.stats();
    assert!(stats.bytes_used <= 200, "Should not exceed capacity");
    assert!(stats.dropped > 0, "Should have dropped entries");
    assert!(stats.total_logged == 20, "Should have logged 20 total");
}

#[test]
fn test_string_interning() {
    let buffer = DebugRingBuffer::with_capacity(4096);

    // Push multiple entries with same target/file
    for i in 0..10 {
        buffer.push(&make_record(Level::Info, &format!("message {i}")));
    }

    let stats = buffer.stats();
    assert_eq!(stats.interned_targets, 1, "Should intern target once");
    assert_eq!(stats.interned_files, 1, "Should intern file once");
}

#[test]
fn test_message_truncation() {
    let buffer = DebugRingBuffer::with_capacity(64 * 1024);

    // Create a message longer than MAX_MESSAGE_LEN
    let long_message = "x".repeat(MAX_MESSAGE_LEN + 1000);
    buffer.push(&make_record(Level::Info, &long_message));

    let entries = buffer.entries();
    assert_eq!(entries.len(), 1);
    assert!(
        entries[0].message.len() <= MAX_MESSAGE_LEN + 20, // +20 for truncation marker
        "Message should be truncated"
    );
    assert!(entries[0].message.ends_with("...[truncated]"), "Should have truncation marker");
}

#[test]
fn test_dump_formatting() {
    let buffer = DebugRingBuffer::with_capacity(4096);

    buffer.push(&make_record(Level::Error, "test error"));
    buffer.push(&make_record(Level::Info, "test info"));

    let dump = buffer.dump();
    assert!(dump.contains("Debug Ring Buffer Dump"));
    assert!(dump.contains("test error"));
    assert!(dump.contains("test info"));
    assert!(dump.contains("ERROR"));
    assert!(dump.contains("INFO"));
}

#[test]
fn test_try_dump() {
    let buffer = DebugRingBuffer::with_capacity(4096);
    buffer.push(&make_record(Level::Info, "test"));

    // Should succeed when no one holds the lock
    let dump = buffer.try_dump();
    assert!(dump.is_some());
    assert!(dump.unwrap().contains("test"));
}

#[test]
fn test_stats() {
    let buffer = DebugRingBuffer::with_capacity(1024);

    buffer.push(&make_record(Level::Info, "test"));

    let stats = buffer.stats();
    assert_eq!(stats.capacity_bytes, 1024);
    assert!(stats.bytes_used > 0);
    assert_eq!(stats.entry_count, 1);
    assert_eq!(stats.total_logged, 1);
    assert_eq!(stats.dropped, 0);
}

#[test]
fn test_concurrent_access() {
    use std::{sync::Arc, thread};

    let buffer = Arc::new(DebugRingBuffer::with_capacity(8192));
    let mut handles = vec![];

    // Spawn multiple writers
    for t in 0..4 {
        let buf = Arc::clone(&buffer);
        handles.push(thread::spawn(move || {
            for i in 0..100 {
                let msg = format!("thread {t} message {i}");
                let record = Record::builder(Level::Info)
                    .message(&msg)
                    .module_path("test")
                    .file("test.rs")
                    .line(1)
                    .build();
                buf.push(&record);
            }
        }));
    }

    // Spawn readers
    for _ in 0..2 {
        let buf = Arc::clone(&buffer);
        handles.push(thread::spawn(move || {
            for _ in 0..50 {
                let _ = buf.tail(10);
                let _ = buf.stats();
            }
        }));
    }

    for handle in handles {
        handle.join().expect("thread panicked");
    }

    let stats = buffer.stats();
    assert!(stats.total_logged > 0, "Should have logged entries");
}

#[test]
fn test_default() {
    let buffer = DebugRingBuffer::default();
    let stats = buffer.stats();
    assert_eq!(stats.capacity_bytes, DEFAULT_CAPACITY);
}

#[test]
fn test_debug_impl() {
    let buffer = DebugRingBuffer::with_capacity(1024);
    buffer.push(&make_record(Level::Info, "test"));

    let debug = format!("{buffer:?}");
    assert!(debug.contains("DebugRingBuffer"));
    assert!(debug.contains("capacity_bytes"));
    assert!(debug.contains("1024"));
}

#[test]
fn test_intern_table() {
    let mut table = InternTable::new();

    // First intern
    let id1 = table.intern("hello");
    assert_eq!(id1, 0);

    // Same string returns same ID
    let id2 = table.intern("hello");
    assert_eq!(id2, 0);

    // Different string gets new ID
    let id3 = table.intern("world");
    assert_eq!(id3, 1);

    // Lookup works
    assert_eq!(table.get(0), "hello");
    assert_eq!(table.get(1), "world");
    assert_eq!(table.get(99), ""); // Invalid ID returns empty
}

#[test]
fn test_sequence_numbers() {
    let buffer = DebugRingBuffer::with_capacity(4096);

    buffer.push(&make_record(Level::Info, "first"));
    buffer.push(&make_record(Level::Info, "second"));
    buffer.push(&make_record(Level::Info, "third"));

    let entries = buffer.entries();
    assert_eq!(entries[0].seq, 0);
    assert_eq!(entries[1].seq, 1);
    assert_eq!(entries[2].seq, 2);
}

#[test]
fn test_timestamps_increase() {
    let buffer = DebugRingBuffer::with_capacity(4096);

    buffer.push(&make_record(Level::Info, "first"));
    std::thread::sleep(std::time::Duration::from_millis(10));
    buffer.push(&make_record(Level::Info, "second"));

    let entries = buffer.entries();
    assert!(entries[1].timestamp_us > entries[0].timestamp_us, "Timestamps should increase");
}

#[test]
fn test_bytes_used() {
    let buffer = DebugRingBuffer::with_capacity(4096);
    assert_eq!(buffer.bytes_used(), 0);

    buffer.push(&make_record(Level::Info, "test message"));
    assert!(buffer.bytes_used() > 0);
}

#[test]
fn test_already_initialized_error() {
    let err = AlreadyInitialized;
    assert_eq!(format!("{err}"), "debug ring buffer already initialized");
}

#[test]
fn test_intern_table_full() {
    let mut table = InternTable::new();

    // Fill the table to MAX_INTERN_ENTRIES
    for i in 0..MAX_INTERN_ENTRIES {
        let id = table.intern(&format!("string_{i}"));
        assert_eq!(id, u16::try_from(i).unwrap());
    }

    // Next intern should return sentinel value 0
    let overflow_id = table.intern("overflow");
    assert_eq!(overflow_id, 0);

    // Original string 0 should still work
    assert_eq!(table.get(0), "string_0");
}

#[test]
fn test_eviction_edge_cases() {
    // Very small buffer to test edge cases in eviction
    let buffer = DebugRingBuffer::with_capacity(100);

    // Push one entry
    buffer.push(&make_record(Level::Info, "first"));
    let stats1 = buffer.stats();
    assert_eq!(stats1.entry_count, 1);

    // Push entries until we trigger eviction
    for i in 0..10 {
        buffer.push(&make_record(Level::Info, &format!("msg{i}")));
    }

    let stats2 = buffer.stats();
    assert!(stats2.bytes_used <= 100);
    assert!(stats2.dropped > 0);
}

#[test]
fn test_eviction_with_large_entry() {
    let buffer = DebugRingBuffer::with_capacity(200);

    // Add several entries
    for i in 0..5 {
        buffer.push(&make_record(Level::Info, &format!("entry_{i}")));
    }

    // Add a large entry that will force multiple evictions
    let large_msg = "x".repeat(150);
    buffer.push(&make_record(Level::Info, &large_msg));

    let stats = buffer.stats();
    assert!(stats.bytes_used <= 200);
    assert!(stats.dropped > 0);
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[test]
fn test_global_init_and_access() {
    use std::sync::Mutex;

    // Use a mutex to ensure this test doesn't interfere with others
    static TEST_LOCK: Mutex<()> = Mutex::new(());
    let _guard = TEST_LOCK.lock().unwrap();

    // try_debug_ring should return None if not initialized
    if try_debug_ring().is_none() {
        // Initialize with custom capacity
        let result = init_debug_ring_with_capacity(2048);
        if result.is_ok() {
            // Access the ring
            let ring = debug_ring();
            ring.push(&make_record(Level::Info, "test"));

            let stats = ring.stats();
            assert_eq!(stats.capacity_bytes, 2048);

            // Try to initialize again - should fail
            let result2 = init_debug_ring_with_capacity(4096);
            assert!(result2.is_err());
            assert_eq!(result2.unwrap_err(), AlreadyInitialized);

            // try_debug_ring should now return Some
            assert!(try_debug_ring().is_some());
        }
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[test]
fn test_debug_ring_uninitialized_returns_none_via_try() {
    // OnceLock is global state shared across tests, so we cannot reliably
    // test the panic path of debug_ring() (another test may have initialized it).
    // Instead, verify that try_debug_ring() returns a consistent result:
    // either Some (already initialized by another test) or None (first test to run).
    let result = try_debug_ring();
    if result.is_none() {
        // Not yet initialized: verify try_debug_ring consistently returns None
        assert!(try_debug_ring().is_none());
    } else {
        // Already initialized by another test: verify debug_ring() works
        let ring = debug_ring();
        assert!(ring.stats().capacity_bytes > 0);
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[test]
fn test_init_debug_ring_default_capacity() {
    use std::sync::Mutex;

    static TEST_LOCK: Mutex<()> = Mutex::new(());
    let _guard = TEST_LOCK.lock().unwrap();

    if try_debug_ring().is_none() {
        let result = init_debug_ring();
        if result.is_ok() {
            let ring = debug_ring();
            let stats = ring.stats();
            assert_eq!(stats.capacity_bytes, DEFAULT_CAPACITY);
        }
    }
}

#[test]
fn test_already_initialized_implements_error() {
    let err = AlreadyInitialized;
    let err_trait: &dyn std::error::Error = &err;
    let _ = err_trait;
    assert_eq!(err, AlreadyInitialized);
}

#[test]
fn test_intern_table_len() {
    let mut table = InternTable::new();
    assert_eq!(table.len(), 0);

    table.intern("first");
    assert_eq!(table.len(), 1);

    table.intern("first"); // Same string
    assert_eq!(table.len(), 1);

    table.intern("second");
    assert_eq!(table.len(), 2);
}

#[test]
fn test_eviction_empty_buffer() {
    let buffer = DebugRingBuffer::with_capacity(50);

    // Push one large entry that exceeds capacity
    let large = "x".repeat(100);
    buffer.push(&make_record(Level::Info, &large));

    // Should handle gracefully - might end up empty if entry too large
    let stats = buffer.stats();
    assert!(stats.bytes_used <= 50 || stats.entry_count == 1);
}