rich_rust 0.2.1

A Rust port of Python's Rich library for beautiful terminal output
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
//! End-to-end tests for input length limiting in interactive prompts.
//!
//! These tests verify that:
//! 1. Normal input within limits works
//! 2. Input at exact limit works
//! 3. Input exceeding limit fails with proper error
//! 4. UTF-8 boundary cases are handled correctly
//! 5. All prompt types (Prompt, Select, Confirm) respect limits
//! 6. Error messages are clear and informative
//!
//! Run with: cargo test --test e2e_input_limits -- --nocapture

mod common;

use common::init_test_logging;
use rich_rust::interactive::{Confirm, PromptError, Select};
use rich_rust::prelude::*;
use std::io::Cursor;
use tracing::info;

/// Helper: create a console configured as interactive (terminal).
fn interactive_console() -> Console {
    Console::builder()
        .width(80)
        .force_terminal(true)
        .markup(true)
        .build()
}

/// Helper: create a BufRead reader from a string.
fn input(s: &str) -> Cursor<Vec<u8>> {
    Cursor::new(s.as_bytes().to_vec())
}

/// Helper: create a BufRead reader from raw bytes.
fn input_bytes(bytes: Vec<u8>) -> Cursor<Vec<u8>> {
    Cursor::new(bytes)
}

/// Helper: create a separator line for logging.
fn separator() -> &'static str {
    "--------------------------------------------------"
}

// =============================================================================
// TEST SECTION 1: Basic Input Length Limits
// =============================================================================

/// Test: Input well under the limit is accepted.
#[test]
fn test_input_well_under_limit() {
    init_test_logging();
    info!("TEST: Input well under limit");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 1024;
    let short_input = "hello\n"; // 6 bytes

    info!(
        input_len = short_input.len(),
        limit = limit,
        "Testing short input"
    );

    let prompt = Prompt::new("Name").max_length(limit);
    let mut reader = input(short_input);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_ok(), "Short input should be accepted");
    assert_eq!(result.unwrap(), "hello");
    info!("Short input accepted correctly");
}

/// Test: Input exactly at the limit is accepted.
#[test]
fn test_input_exactly_at_limit() {
    init_test_logging();
    info!("TEST: Input exactly at limit");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 6; // "ab\n" is 3 bytes, "hello\n" is 6 bytes
    let exact_input = "hello\n"; // exactly 6 bytes

    info!(
        input_len = exact_input.len(),
        limit = limit,
        "Testing exact-limit input"
    );

    let prompt = Prompt::new("Input").max_length(limit);
    let mut reader = input(exact_input);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_ok(), "Exact-limit input should be accepted");
    assert_eq!(result.unwrap(), "hello");
    info!("[PASS] Exact-limit input accepted correctly");
}

/// Test: Input one byte over the limit is rejected.
#[test]
fn test_input_one_byte_over_limit() {
    init_test_logging();
    info!("TEST: Input one byte over limit");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 5; // "hello\n" is 6 bytes, limit is 5
    let over_input = "hello\n";

    info!(
        input_len = over_input.len(),
        limit = limit,
        "Testing over-limit input"
    );

    let prompt = Prompt::new("Input").max_length(limit);
    let mut reader = input(over_input);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_err(), "Over-limit input should be rejected");
    let err = result.unwrap_err();
    assert!(err.is_input_too_long());
    assert_eq!(err.input_limit(), Some(limit));
    info!("[PASS] Over-limit input rejected with InputTooLong error");
}

/// Test: Very long input is rejected without excessive memory allocation.
#[test]
fn test_very_long_input_rejected() {
    init_test_logging();
    info!("TEST: Very long input (simulate attack)");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 1024;
    // Create 100KB of input (should fail fast, not allocate all of it)
    let long_input: String = "x".repeat(100_000) + "\n";

    info!(
        input_len = long_input.len(),
        limit = limit,
        "Testing very long input"
    );

    let prompt = Prompt::new("Input").max_length(limit);
    let mut reader = input(&long_input);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_err(), "Very long input should be rejected");
    let err = result.unwrap_err();
    assert!(err.is_input_too_long());
    info!("[PASS] Very long input rejected (memory protected)");
}

// =============================================================================
// TEST SECTION 2: UTF-8 Boundary Cases
// =============================================================================

/// Test: Multi-byte UTF-8 characters within limit.
#[test]
fn test_utf8_multibyte_within_limit() {
    init_test_logging();
    info!("TEST: Multi-byte UTF-8 within limit");
    info!("{}", separator());

    let console = interactive_console();
    // "世界\n" = 3 + 3 + 1 = 7 bytes
    let utf8_input = "世界\n";
    let limit = 10;

    info!(
        input = utf8_input.trim(),
        byte_len = utf8_input.len(),
        char_count = utf8_input.chars().count(),
        limit = limit,
        "Testing UTF-8 input"
    );

    let prompt = Prompt::new("City").max_length(limit);
    let mut reader = input(utf8_input);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(
        result.is_ok(),
        "UTF-8 input within limit should be accepted"
    );
    assert_eq!(result.unwrap(), "世界");
    info!("[PASS] Multi-byte UTF-8 accepted correctly");
}

/// Test: Multi-byte UTF-8 crossing limit fails cleanly.
#[test]
fn test_utf8_crossing_limit() {
    init_test_logging();
    info!("TEST: UTF-8 crossing byte limit");
    info!("{}", separator());

    let console = interactive_console();
    // "世界\n" = 7 bytes, limit at 6 cuts into a character
    let utf8_input = "世界\n";
    let limit = 6;

    info!(
        input = utf8_input.trim(),
        byte_len = utf8_input.len(),
        limit = limit,
        "Testing UTF-8 at byte boundary"
    );

    let prompt = Prompt::new("City").max_length(limit);
    let mut reader = input(utf8_input);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_err(), "UTF-8 crossing limit should be rejected");
    let err = result.unwrap_err();
    assert!(err.is_input_too_long());
    info!("[PASS] UTF-8 boundary handled correctly - rejected before invalid split");
}

/// Test: Emoji input near limit.
#[test]
fn test_emoji_input_within_limit() {
    init_test_logging();
    info!("TEST: Emoji input near limit");
    info!("{}", separator());

    let console = interactive_console();
    // "👋🌍\n" = 4 + 4 + 1 = 9 bytes
    let emoji_input = "👋🌍\n";
    let limit = 10;

    info!(
        input = emoji_input.trim(),
        byte_len = emoji_input.len(),
        limit = limit,
        "Testing emoji input"
    );

    let prompt = Prompt::new("Greeting").max_length(limit);
    let mut reader = input(emoji_input);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_ok(), "Emoji within limit should be accepted");
    assert_eq!(result.unwrap(), "👋🌍");
    info!("[PASS] Emoji input handled correctly");
}

/// Test: Mixed ASCII and UTF-8.
#[test]
fn test_mixed_ascii_utf8() {
    init_test_logging();
    info!("TEST: Mixed ASCII and UTF-8");
    info!("{}", separator());

    let console = interactive_console();
    // "Hi 世界!\n" = 2 + 1 + 6 + 1 + 1 = 11 bytes
    let mixed_input = "Hi 世界!\n";
    let limit = 15;

    info!(
        input = mixed_input.trim(),
        byte_len = mixed_input.len(),
        limit = limit,
        "Testing mixed encoding"
    );

    let prompt = Prompt::new("Message").max_length(limit);
    let mut reader = input(mixed_input);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(
        result.is_ok(),
        "Mixed input within limit should be accepted"
    );
    assert_eq!(result.unwrap(), "Hi 世界!");
    info!("[PASS] Mixed ASCII/UTF-8 handled correctly");
}

/// Test: Invalid UTF-8 returns validation error.
#[test]
fn test_invalid_utf8_rejected() {
    init_test_logging();
    info!("TEST: Invalid UTF-8 handling");
    info!("{}", separator());

    let console = interactive_console();
    // Invalid UTF-8 sequence followed by newline
    let invalid_bytes = vec![0xff, 0xfe, b'\n'];
    let limit = 100;

    info!(
        byte_len = invalid_bytes.len(),
        limit = limit,
        "Testing invalid UTF-8 bytes"
    );

    let prompt = Prompt::new("Data").max_length(limit);
    let mut reader = input_bytes(invalid_bytes);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_err(), "Invalid UTF-8 should be rejected");
    // Should be a Validation error, not InputTooLong
    let err = result.unwrap_err();
    assert!(
        !err.is_input_too_long(),
        "Invalid UTF-8 should not be InputTooLong error"
    );
    info!("[PASS] Invalid UTF-8 rejected with validation error");
}

// =============================================================================
// TEST SECTION 3: Select and Confirm Prompts
// =============================================================================

/// Test: Select prompt respects max_length.
#[test]
fn test_select_max_length_exceeded() {
    init_test_logging();
    info!("TEST: Select prompt max_length exceeded");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 5;
    // Very long input for a selection
    let long_input = "this is way too long for a selection\n";

    info!(
        input_len = long_input.len(),
        limit = limit,
        "Testing Select with long input"
    );

    let select = Select::new("Choose")
        .choices(["Option A", "Option B", "Option C"])
        .max_length(limit);
    let mut reader = input(long_input);
    let result = select.ask_from(&console, &mut reader);

    assert!(result.is_err(), "Long Select input should be rejected");
    let err = result.unwrap_err();
    assert!(err.is_input_too_long());
    info!("[PASS] Select rejects oversized input");
}

/// Test: Select prompt accepts valid short input.
#[test]
fn test_select_max_length_valid() {
    init_test_logging();
    info!("TEST: Select prompt valid input");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 100;
    let short_input = "1\n"; // Select option 1

    info!(
        input_len = short_input.len(),
        limit = limit,
        "Testing Select with short input"
    );

    let select = Select::new("Choose")
        .choices(["Option A", "Option B", "Option C"])
        .max_length(limit);
    let mut reader = input(short_input);
    let result = select.ask_from(&console, &mut reader);

    assert!(result.is_ok(), "Short Select input should be accepted");
    assert_eq!(result.unwrap(), "Option A"); // "1" selects first option
    info!("[PASS] Select accepts valid input");
}

/// Test: Confirm prompt respects max_length with absurd input.
#[test]
fn test_confirm_max_length_exceeded() {
    init_test_logging();
    info!("TEST: Confirm prompt max_length exceeded");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 10;
    // Absurd input for a yes/no confirmation
    let absurd_input = "yesyesyesyesyesyes\n";

    info!(
        input_len = absurd_input.len(),
        limit = limit,
        "Testing Confirm with absurd input"
    );

    let confirm = Confirm::new("Continue?").max_length(limit);
    let mut reader = input(absurd_input);
    let result = confirm.ask_from(&console, &mut reader);

    assert!(result.is_err(), "Absurd Confirm input should be rejected");
    let err = result.unwrap_err();
    assert!(err.is_input_too_long());
    info!("[PASS] Confirm rejects absurd input");
}

/// Test: Confirm prompt accepts valid yes/no.
#[test]
fn test_confirm_max_length_valid() {
    init_test_logging();
    info!("TEST: Confirm prompt valid input");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 100;

    for (inp, expected) in [
        ("y\n", true),
        ("n\n", false),
        ("yes\n", true),
        ("no\n", false),
    ] {
        info!(input = inp.trim(), expected = expected, "Testing Confirm");

        let confirm = Confirm::new("Continue?").max_length(limit);
        let mut reader = input(inp);
        let result = confirm.ask_from(&console, &mut reader);

        assert!(result.is_ok(), "Valid Confirm input should be accepted");
        assert_eq!(result.unwrap(), expected);
    }
    info!("[PASS] Confirm accepts valid yes/no inputs");
}

// =============================================================================
// TEST SECTION 4: Error Message Clarity
// =============================================================================

/// Test: InputTooLong error contains limit and received values.
#[test]
fn test_error_contains_both_values() {
    init_test_logging();
    info!("TEST: Error message contains limit and received");
    info!("{}", separator());

    let err = PromptError::InputTooLong {
        limit: 100,
        received: 250,
    };

    let msg = err.to_string();
    info!(error_message = %msg, "Checking error message content");

    assert!(msg.contains("100"), "Error should mention limit");
    assert!(msg.contains("250"), "Error should mention received bytes");
    assert!(
        msg.contains("limit") || msg.contains("bytes"),
        "Error should be descriptive"
    );
    info!("[PASS] Error message is clear and informative");
}

/// Test: Error helper methods work correctly.
#[test]
fn test_error_helper_methods() {
    init_test_logging();
    info!("TEST: Error helper methods");
    info!("{}", separator());

    // InputTooLong case
    let too_long = PromptError::InputTooLong {
        limit: 64,
        received: 128,
    };
    assert!(too_long.is_input_too_long());
    assert_eq!(too_long.input_limit(), Some(64));
    info!("[PASS] InputTooLong helpers work");

    // Other error types should return false/None
    let eof = PromptError::Eof;
    assert!(!eof.is_input_too_long());
    assert_eq!(eof.input_limit(), None);

    let not_interactive = PromptError::NotInteractive;
    assert!(!not_interactive.is_input_too_long());
    assert_eq!(not_interactive.input_limit(), None);

    info!("[PASS] Non-InputTooLong errors return correct values");
}

/// Test: Error is Debug-printable for logging.
#[test]
fn test_error_debug_format() {
    init_test_logging();
    info!("TEST: Error Debug format");
    info!("{}", separator());

    let err = PromptError::InputTooLong {
        limit: 512,
        received: 1024,
    };

    let debug_str = format!("{:?}", err);
    info!(debug_output = %debug_str, "Debug format output");

    assert!(
        debug_str.contains("InputTooLong"),
        "Debug should show variant name"
    );
    assert!(debug_str.contains("512"), "Debug should show limit");
    assert!(debug_str.contains("1024"), "Debug should show received");
    info!("[PASS] Error Debug format is useful for logging");
}

// =============================================================================
// TEST SECTION 5: Default Limit Behavior
// =============================================================================

/// Test: Prompt without explicit max_length uses default (64 KiB).
#[test]
fn test_default_limit_used() {
    init_test_logging();
    info!("TEST: Default limit behavior");
    info!("{}", separator());

    let console = interactive_console();
    // Input under 64 KiB should work with default limit
    let normal_input = "This is a normal sized input\n";

    info!(
        input_len = normal_input.len(),
        "Testing with default limit (64 KiB)"
    );

    let prompt = Prompt::new("Message"); // No explicit max_length
    let mut reader = input(normal_input);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(
        result.is_ok(),
        "Normal input should work with default 64 KiB limit"
    );
    info!("[PASS] Default limit allows normal input");
}

// =============================================================================
// TEST SECTION 6: Edge Cases
// =============================================================================

/// Test: Empty input with allow_empty under limit.
#[test]
fn test_empty_input_with_limit() {
    init_test_logging();
    info!("TEST: Empty input with limit");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 100;

    let prompt = Prompt::new("Optional").allow_empty(true).max_length(limit);
    let mut reader = input("\n");
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_ok(), "Empty input should be allowed");
    assert_eq!(result.unwrap(), "");
    info!("[PASS] Empty input works with limit");
}

/// Test: Input with only whitespace under limit.
#[test]
fn test_whitespace_input_with_limit() {
    init_test_logging();
    info!("TEST: Whitespace input with limit");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 100;

    let prompt = Prompt::new("Input").allow_empty(true).max_length(limit);
    let mut reader = input("   \n");
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_ok(), "Whitespace input should be accepted");
    // Trailing whitespace is trimmed
    assert_eq!(result.unwrap(), "");
    info!("[PASS] Whitespace input handled correctly");
}

/// Test: Multiple lines (only first line read).
#[test]
fn test_multiline_input_first_line_only() {
    init_test_logging();
    info!("TEST: Multiline input reads first line only");
    info!("{}", separator());

    let console = interactive_console();
    let limit = 100;
    let multiline = "first\nsecond\nthird\n";

    let prompt = Prompt::new("Line").max_length(limit);
    let mut reader = input(multiline);
    let result = prompt.ask_from(&console, &mut reader);

    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "first");
    info!("[PASS] Only first line is read");
}