arf-console 0.6.0

A cross-platform R console written in Rust
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use super::*;
use ratatui::style::Color;

/// Helper: render markdown and collect line text (without styling).
fn render_plain(input: &str) -> Vec<String> {
    render_markdown(input, None, None)
        .iter()
        .map(|line| {
            line.spans
                .iter()
                .map(|s| s.content.as_ref())
                .collect::<String>()
        })
        .collect()
}

#[test]
fn plain_text() {
    let lines = render_plain("Hello world");
    assert_eq!(lines, vec!["Hello world"]);
}

#[test]
fn heading_prefix() {
    let lines = render_plain("# Title\n\nBody");
    assert_eq!(lines, vec!["# Title", "", "Body"]);
}

#[test]
fn heading_levels() {
    let lines = render_plain("## Sub\n\n### Third");
    assert_eq!(lines, vec!["## Sub", "", "### Third"]);
}

#[test]
fn emphasis_and_strong() {
    let lines = render_markdown("*em* **strong**", None, None);
    assert_eq!(lines.len(), 1);
    // Check that there are separate spans with appropriate styles
    let spans = &lines[0].spans;
    assert!(spans.len() >= 2);
}

#[test]
fn inline_code() {
    let lines = render_markdown("Use `print()`", None, None);
    assert_eq!(lines.len(), 1);
    let text: String = lines[0].spans.iter().map(|s| s.content.as_ref()).collect();
    assert_eq!(text, "Use print()");
}

#[test]
fn code_block() {
    let input = "```r\nx <- 1\ny <- 2\n```";
    let lines = render_plain(input);
    assert!(lines.contains(&"x <- 1".to_string()));
    assert!(lines.contains(&"y <- 2".to_string()));
}

#[test]
fn unordered_list() {
    let input = "- one\n- two\n- three";
    let lines = render_plain(input);
    assert_eq!(lines, vec!["- one", "- two", "- three"]);
}

#[test]
fn ordered_list() {
    let input = "1. first\n2. second\n3. third";
    let lines = render_plain(input);
    assert_eq!(lines, vec!["1. first", "2. second", "3. third"]);
}

#[test]
fn nested_list() {
    let input = "- outer\n  - inner";
    let lines = render_plain(input);
    assert_eq!(lines, vec!["- outer", "  - inner"]);
}

#[test]
fn blockquote() {
    let input = "> quoted text";
    let lines = render_plain(input);
    assert_eq!(lines, vec!["> quoted text"]);
}

#[test]
fn simple_table() {
    let input = "| A | B |\n|---|---|\n| 1 | 2 |";
    let lines = render_plain(input);
    // Should have header, separator, data row
    assert_eq!(lines.len(), 3);
    assert!(lines[0].contains("A"));
    assert!(lines[0].contains("B"));
    assert!(lines[1].contains("-"));
    assert!(lines[2].contains("1"));
    assert!(lines[2].contains("2"));
}

#[test]
fn link_rendering() {
    let input = "[click here](https://example.com)";
    let lines = render_plain(input);
    assert_eq!(lines.len(), 1);
    assert!(lines[0].contains("click here"));
    assert!(lines[0].contains("https://example.com"));
}

fn link_document(input: &str, width: usize) -> RenderedMarkdown {
    render_markdown_document(input, None, Some(width), |_| LinkDisplay::LabelOnly)
}

fn fragment_text(document: &RenderedMarkdown, fragment: &LinkFragment) -> String {
    let text: String = document.lines[fragment.line]
        .spans
        .iter()
        .map(|span| span.content.as_ref())
        .collect();
    text[fragment.byte_range.clone()].to_owned()
}

#[test]
fn semantic_links_keep_distinct_identities_and_raw_destinations() {
    let document = link_document(
        "[one](x-r-help:base/%/%) and [**two**](x-r-help:base/%/%) plus [`code`](https://example.com?q=1#part)",
        80,
    );
    assert_eq!(document.links.len(), 3);
    assert_eq!(document.links[0].destination, "x-r-help:base/%/%");
    assert_eq!(document.links[1].destination, document.links[0].destination);
    assert_eq!(
        document.links[2].destination,
        "https://example.com?q=1#part"
    );
    for (index, label, range) in [(0, "one", 0..3), (1, "two", 8..11), (2, "code", 17..21)] {
        let link = &document.links[index];
        assert_eq!(link.label, label);
        assert_eq!(link.text_ranges.len(), 1);
        assert_eq!(link.text_ranges[0].range, range);
        assert_eq!(link.fragments.len(), 1);
        assert_eq!(link.fragments[0].columns, range);
        assert_eq!(fragment_text(&document, &link.fragments[0]), label);
    }
    let spans = &document.lines[0].spans;
    let bold = spans.iter().find(|span| span.content == "two").unwrap();
    assert!(
        bold.style
            .add_modifier
            .contains(ratatui::style::Modifier::BOLD)
    );
    assert!(
        bold.style
            .add_modifier
            .contains(ratatui::style::Modifier::UNDERLINED)
    );
    let code = spans.iter().find(|span| span.content == "code").unwrap();
    assert!(
        code.style
            .add_modifier
            .contains(ratatui::style::Modifier::UNDERLINED)
    );
}

#[test]
fn link_columns_distinguish_unicode_width_from_bytes() {
    let document = link_document("前🎉 [日本 **bold** `x` e\u{301}](opaque:target)", 80);
    let link = &document.links[0];
    assert_eq!(link.label, "日本 bold x e\u{301}");
    assert_eq!(
        link.fragments,
        vec![LinkFragment {
            line: 0,
            byte_range: 8..25,
            columns: 5..18,
        }]
    );
    assert_eq!(fragment_text(&document, &link.fragments[0]), link.label);
}

#[test]
fn wrapped_link_ranges_keep_identity_across_widths() {
    let input = "lead [alpha beta needle gamma](opaque:first) tail [end](opaque:second)";
    for width in [0, 1, 6, 12, 80] {
        let document = link_document(input, width);
        assert_eq!(document.links.len(), 2);
        let link = &document.links[0];
        assert_eq!(link.label, "alpha beta needle gamma");
        assert_eq!(link.destination, "opaque:first");
        assert_eq!(link.text_ranges[0].range, 5..28);
        assert_eq!(document.links[1].label, "end");
        for fragment in &link.fragments {
            let text = fragment_text(&document, fragment);
            assert!(!text.is_empty());
            assert!(link.label.contains(&text));
            assert_eq!(fragment.columns.end - fragment.columns.start, text.len());
        }
        if width == 12 {
            assert_eq!(
                link.fragments,
                vec![
                    LinkFragment {
                        line: 0,
                        byte_range: 5..10,
                        columns: 5..10
                    },
                    LinkFragment {
                        line: 1,
                        byte_range: 0..11,
                        columns: 0..11
                    },
                    LinkFragment {
                        line: 2,
                        byte_range: 0..5,
                        columns: 0..5
                    },
                ]
            );
        }
    }
}

#[test]
fn link_ranges_exclude_list_and_blockquote_prefixes() {
    let document = link_document("> - [alpha beta needle gamma](opaque:target)", 14);
    let link = &document.links[0];
    assert_eq!(link.label, "alpha beta needle gamma");
    assert_eq!(link.text_ranges[0].range, 4..27);
    assert!(link.fragments.len() > 1);
    for fragment in &link.fragments {
        assert!(fragment.columns.start >= 4);
        assert!(link.label.contains(&fragment_text(&document, fragment)));
    }
}

#[test]
fn table_links_keep_ranges_through_cell_breaks_and_wrapping() {
    let input = "| 前 | Link |\n|---|---|\n| 日本語 | [alpha **beta**<br>needle gamma](opaque:target) |\n| 日本 | [other](opaque:target) |";
    for width in [18, 26, 80] {
        let document = link_document(input, width);
        assert_eq!(document.links.len(), 2);
        let link = &document.links[0];
        assert_eq!(link.label, "alpha beta\nneedle gamma");
        assert_eq!(link.text_ranges.len(), 2);
        assert!(link.fragments.len() >= 2);
        for fragment in &link.fragments {
            let text = fragment_text(&document, fragment);
            assert!(link.label.contains(&text));
            assert!(!text.contains('|'));
        }
        if width == 80 {
            assert_eq!(
                link.fragments[0],
                LinkFragment {
                    line: 2,
                    byte_range: 14..24,
                    columns: 11..21,
                }
            );
            assert_eq!(
                link.fragments[1],
                LinkFragment {
                    line: 3,
                    byte_range: 11..23,
                    columns: 11..23,
                }
            );
        }
        assert_eq!(document.links[1].label, "other");
    }
}

#[test]
fn link_identity_survives_explicit_line_breaks() {
    let document = link_document("[alpha  \nbeta<br>gamma](opaque:target)", 80);
    let link = &document.links[0];
    assert_eq!(link.label, "alpha\nbeta\ngamma");
    assert_eq!(link.text_ranges.len(), 3);
    assert_eq!(link.fragments.len(), 3);
    let labels = link
        .fragments
        .iter()
        .map(|f| fragment_text(&document, f))
        .collect::<Vec<_>>();
    assert_eq!(labels, ["alpha", "beta", "gamma"]);
}

#[test]
fn link_fallback_deduplicates_urls_and_excludes_destinations_from_ranges() {
    for source in [
        "<https://example.com>",
        "[https://example.com](https://example.com)",
    ] {
        assert_eq!(render_plain(source), ["https://example.com"]);
    }
    let document = render_markdown_with_text("[label](https://example.com)", None, None);
    assert_eq!(document.links[0].label, "label");
    assert_eq!(
        fragment_text(&document, &document.links[0].fragments[0]),
        "label"
    );
    assert_eq!(document.text[0].text, "label (https://example.com)");
    // The generic renderer does not interpret help targets.
    assert_eq!(
        render_plain("[mean](x-r-help:mean)"),
        ["mean (x-r-help:mean)"]
    );
    assert_eq!(
        render_markdown("[label](https://example.com)", None, None),
        document.lines,
    );
}

#[test]
fn heading_links_and_empty_labels_have_valid_metadata() {
    let document = link_document(
        "# [Heading](opaque:h)\n\n[](opaque:empty)\n\n```\n[code](opaque:c)\n```",
        1,
    );
    assert_eq!(document.links.len(), 2);
    assert_eq!(document.links[0].fragments[0].columns, 2..9);
    assert_eq!(document.links[1].destination, "opaque:empty");
    assert!(document.links[1].fragments.is_empty());
}

#[test]
fn horizontal_rule() {
    let input = "before\n\n---\n\nafter";
    let lines = render_plain(input);
    assert!(lines.contains(&"———".to_string()));
}

#[test]
fn table_with_br_tags() {
    let input = "| Arg | Desc |\n|---|---|\n| x | first<br>second |";
    let lines = render_plain(input);
    // The cell with <br> should produce two visual rows
    assert!(lines.len() >= 4); // header + separator + 2 data lines
    // First data row has "first"
    assert!(lines[2].contains("first"));
    // Second data row has "second"
    assert!(lines[3].contains("second"));
}

#[test]
fn inline_html_br_outside_table() {
    let input = "line one<br>line two";
    let lines = render_plain(input);
    assert_eq!(lines, vec!["line one", "line two"]);
}

#[test]
fn r_code_block_syntax_highlight() {
    let input = "```r\nx <- 42\n```";
    let lines = render_markdown(input, None, None);
    // Should produce one line: "x <- 42"
    let text: String = lines
        .iter()
        .flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
        .collect();
    assert!(text.contains("x <- 42"));

    // Find the line containing "x <- 42"
    let code_line = lines.iter().find(|l| {
        let t: String = l.spans.iter().map(|s| s.content.as_ref()).collect();
        t.contains("<-")
    });
    assert!(code_line.is_some(), "Should have a line with <-");
    let code_line = code_line.unwrap();

    // With syntax highlighting, should have multiple spans (not a single dim span)
    assert!(
        code_line.spans.len() >= 3,
        "R code should be tokenized into multiple spans, got {}",
        code_line.spans.len()
    );

    // The operator "<-" should have Yellow foreground
    let op_span = code_line
        .spans
        .iter()
        .find(|s| s.content.as_ref().contains("<-"));
    assert!(op_span.is_some(), "Should have an <- operator span");
    assert_eq!(
        op_span.unwrap().style.fg,
        Some(Color::Yellow),
        "Operator <- should be Yellow"
    );

    // The number "42" should have LightMagenta foreground
    let num_span = code_line.spans.iter().find(|s| s.content.as_ref() == "42");
    assert!(num_span.is_some(), "Should have a 42 number span");
    assert_eq!(
        num_span.unwrap().style.fg,
        Some(Color::LightMagenta),
        "Number 42 should be LightMagenta"
    );
}

#[test]
fn non_r_code_block_uses_dim_style() {
    let input = "```python\nprint('hello')\n```";
    let lines = render_markdown(input, None, None);
    // Should produce one line with dim style (not syntax highlighted)
    let code_line = lines.iter().find(|l| {
        let t: String = l.spans.iter().map(|s| s.content.as_ref()).collect();
        t.contains("print")
    });
    assert!(code_line.is_some());
    let code_line = code_line.unwrap();
    // Non-R code blocks get a single dim span per line
    assert_eq!(
        code_line.spans.len(),
        1,
        "Non-R code should be a single span"
    );
    assert!(
        code_line.spans[0]
            .style
            .add_modifier
            .contains(ratatui::style::Modifier::DIM),
        "Non-R code should use DIM style"
    );
}

#[test]
fn r_code_block_multiline() {
    let input = "```r\nif (TRUE) {\n  print(x)\n}\n```";
    let lines = render_markdown(input, None, None);
    let texts: Vec<String> = lines
        .iter()
        .map(|l| l.spans.iter().map(|s| s.content.as_ref()).collect())
        .collect();
    assert!(texts.iter().any(|t| t.contains("if")));
    assert!(texts.iter().any(|t| t.contains("print")));

    // The "if" keyword should be highlighted
    let if_line = lines
        .iter()
        .find(|l| l.spans.iter().any(|s| s.content.as_ref() == "if"));
    assert!(if_line.is_some());
    let if_span = if_line
        .unwrap()
        .spans
        .iter()
        .find(|s| s.content.as_ref() == "if");
    assert_eq!(
        if_span.unwrap().style.fg,
        Some(Color::LightBlue),
        "Keyword 'if' should be LightBlue"
    );
}

#[test]
fn r_code_block_with_comments() {
    let input = "```r\n# A comment\nx <- 1\n```";
    let lines = render_markdown(input, None, None);
    // Comment line should be DarkGray
    let comment_line = lines.iter().find(|l| {
        l.spans
            .iter()
            .any(|s| s.content.as_ref().contains("# A comment"))
    });
    assert!(comment_line.is_some());
    let comment_span = comment_line
        .unwrap()
        .spans
        .iter()
        .find(|s| s.content.as_ref().contains("# A comment"));
    assert_eq!(
        comment_span.unwrap().style.fg,
        Some(Color::DarkGray),
        "Comment should be DarkGray"
    );
}

#[test]
fn untagged_code_block_with_default_r() {
    // Code blocks without a language tag should use the default language
    let input = "```\nx <- 42\n```";
    // Without default: no highlighting (dim style)
    let lines_no_default = render_markdown(input, None, None);
    let code_line = lines_no_default.iter().find(|l| {
        let t: String = l.spans.iter().map(|s| s.content.as_ref()).collect();
        t.contains("<-")
    });
    assert!(code_line.is_some());
    // Should be a single dim span (no tokenization)
    assert_eq!(code_line.unwrap().spans.len(), 1);

    // With default "r": should get syntax highlighting
    let lines_r = render_markdown(input, Some("r"), None);
    let code_line = lines_r.iter().find(|l| {
        let t: String = l.spans.iter().map(|s| s.content.as_ref()).collect();
        t.contains("<-")
    });
    assert!(code_line.is_some());
    let code_line = code_line.unwrap();
    // Should be tokenized into multiple spans
    assert!(
        code_line.spans.len() >= 3,
        "Default R should tokenize untagged code blocks, got {} spans",
        code_line.spans.len()
    );
    // Operator should be Yellow
    let op_span = code_line
        .spans
        .iter()
        .find(|s| s.content.as_ref().contains("<-"));
    assert_eq!(op_span.unwrap().style.fg, Some(Color::Yellow));
}

#[test]
fn explicit_lang_overrides_default() {
    // Explicit language tag should take precedence over default
    let input = "```python\nprint('hello')\n```";
    let lines = render_markdown(input, Some("r"), None);
    let code_line = lines.iter().find(|l| {
        let t: String = l.spans.iter().map(|s| s.content.as_ref()).collect();
        t.contains("print")
    });
    assert!(code_line.is_some());
    // Python code block should NOT be R-highlighted, should be dim
    assert_eq!(
        code_line.unwrap().spans.len(),
        1,
        "Explicit python tag should not use R highlighting even with default_code_lang=r"
    );
}

#[test]
fn empty_input() {
    let lines = render_plain("");
    assert!(lines.is_empty());
}

#[test]
fn paragraphs_separated_by_blank_line() {
    let input = "First paragraph.\n\nSecond paragraph.";
    let lines = render_plain(input);
    assert_eq!(lines, vec!["First paragraph.", "", "Second paragraph."]);
}

// ── wrap_width ─────────────────────────────────────────────────────

/// Helper: render with wrapping and collect plain text.
fn render_wrapped(input: &str, width: usize) -> Vec<String> {
    render_markdown(input, None, Some(width))
        .iter()
        .map(|line| {
            line.spans
                .iter()
                .map(|s| s.content.as_ref())
                .collect::<String>()
        })
        .collect()
}

#[test]
fn wrap_long_paragraph() {
    let input = "Hello world, this is a long paragraph that should wrap.";
    let lines = render_wrapped(input, 20);
    assert!(lines.len() > 1, "Should wrap into multiple lines");
    for line in &lines {
        assert!(
            unicode_width::UnicodeWidthStr::width(line.as_str()) <= 20,
            "Line too wide: {:?} ({})",
            line,
            unicode_width::UnicodeWidthStr::width(line.as_str())
        );
    }
}

#[test]
fn wrap_code_block_not_wrapped() {
    let input = "```\nthis is a very long code line that should not be wrapped at all ever\n```";
    let lines = render_wrapped(input, 20);
    let code_text: String = lines
        .iter()
        .find(|l| l.contains("this is"))
        .cloned()
        .unwrap_or_default();
    assert!(code_text.len() > 20, "Code block should NOT be wrapped");
}

#[test]
fn wrap_table_cells() {
    let input = "| Arg | Description |\n|---|---|\n| x | A very long description that should be wrapped within the cell |";
    let lines = render_wrapped(input, 40);
    // The table should have more rows than the raw 3 (header + sep + 1 data row)
    // because the long description wraps.
    assert!(
        lines.len() > 3,
        "Table cell should wrap: got {} lines: {:?}",
        lines.len(),
        lines
    );
    // All lines should fit within the wrap width
    for line in &lines {
        let w = unicode_width::UnicodeWidthStr::width(line.as_str());
        assert!(w <= 40, "Table line too wide ({} > 40): {:?}", w, line);
    }
}

#[test]
fn wrap_table_narrow_preserves_content() {
    let input = "| Name | Value |\n|---|---|\n| foo | bar |";
    // Wide enough that no wrapping is needed
    let lines_wide = render_wrapped(input, 80);
    // Narrow — may wrap but should still contain all content
    let lines_narrow = render_wrapped(input, 30);
    let all_text_wide: String = lines_wide.join("");
    let all_text_narrow: String = lines_narrow.join("");
    assert!(all_text_narrow.contains("foo"));
    assert!(all_text_narrow.contains("bar"));
    assert!(all_text_wide.contains("foo"));
    assert!(all_text_wide.contains("bar"));
}

#[test]
fn wrap_prose_but_not_code_block() {
    // Integration test: a document with both a paragraph and a code block.
    // The paragraph should wrap; the code block should not.
    let input = "This is a long paragraph that will be wrapped at the given width.\n\n```\ncode_line_that_must_not_be_wrapped_ever()\n```";
    let lines = render_wrapped(input, 30);

    // Paragraph lines should all fit within 30 columns and actually wrap
    let code_start = lines
        .iter()
        .position(|l| l.contains("code_line"))
        .expect("Should contain the code line");
    assert!(
        code_start > 2,
        "Paragraph should have wrapped into multiple lines, got {}",
        code_start
    );
    for (i, line) in lines.iter().enumerate() {
        if i < code_start && !line.is_empty() {
            let w = unicode_width::UnicodeWidthStr::width(line.as_str());
            assert!(
                w <= 30,
                "Paragraph line should wrap (line {}: {} cols): {:?}",
                i,
                w,
                line
            );
        }
    }

    // Code block line should NOT be wrapped (wider than 30)
    let code_line = &lines[code_start];
    assert!(
        code_line.contains("code_line_that_must_not_be_wrapped_ever()"),
        "Code block should remain intact"
    );
}

#[test]
fn distribute_column_widths_empty() {
    let empty: Vec<usize> = vec![];
    assert_eq!(distribute_column_widths(&[], 10), empty);
}

#[test]
fn distribute_column_widths_single_column() {
    // Single wide column gets clamped to available
    assert_eq!(distribute_column_widths(&[20], 10), vec![10]);
}

#[test]
fn distribute_column_widths_all_fit() {
    // All columns fit within their fair share — no redistribution needed
    assert_eq!(distribute_column_widths(&[3, 3, 3], 12), vec![3, 3, 3]);
}

#[test]
fn distribute_column_widths_one_wide() {
    // One wide column, two narrow. Narrow keep natural, wide gets the rest.
    let result = distribute_column_widths(&[2, 20, 3], 15);
    assert_eq!(result[0], 2); // narrow, kept
    assert_eq!(result[2], 3); // narrow, kept
    assert_eq!(result[1], 10); // wide, gets 15 - 2 - 3 = 10
    assert_eq!(result.iter().sum::<usize>(), 15);
}

#[test]
fn distribute_column_widths_available_less_than_n() {
    // available < n_cols: first `available` columns get 1, rest get 0
    let result = distribute_column_widths(&[5, 10, 15], 2);
    assert_eq!(result, vec![1, 1, 0]);
    assert!(result.iter().sum::<usize>() <= 2);
}

#[test]
fn distribute_column_widths_available_equals_n() {
    // available == n_cols: each column gets exactly 1
    let result = distribute_column_widths(&[5, 10, 15], 3);
    assert_eq!(result, vec![1, 1, 1]);
}