seer-cli 0.51.0

Interactive CLI for Seer domain name utilities
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! DNS Records lens — Records tab (tab 0), DNSSEC tab (tab 1), Compare tab
//! (tab 2), Trace tab (tab 3).
//!
//! Records renders a dig-style query result: the response's status line,
//! the ANSWER section (the CNAME chain under its real owners, then the
//! records), and for a response without answers why not — NXDOMAIN, NODATA,
//! a referral or a server failure — with the AUTHORITY records the server
//! sent, and a note when the resolver answered the name itself. The outcome
//! wording is `seer_core::output::dig`'s, shared with `seer dig`, and every
//! remote string goes through `sanitize_line`, as in the CLI formatters.
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Cell, Paragraph, Row, Table};
use ratatui::Frame;
use seer_core::output::dig::{self as wording, Tone};
use seer_core::output::sanitize_line;
use seer_core::{DnsQueryResult, DnsRecord, DnsStatus};

use crate::tui::action::LensData;
use crate::tui::filter;
use crate::tui::lenses::{compare, dnssec, trace};
use crate::tui::panes::{DnsState, Panes};
use crate::tui::theme::Theme;
use crate::tui::widgets::{panel, row_style, scroll_to, stack, wrap, Band};

#[allow(clippy::too_many_arguments)]
pub fn render(
    f: &mut Frame,
    area: Rect,
    theme: &Theme,
    tab: usize,
    data: &LensData,
    filter: &str,
    focused: bool,
    sel: usize,
    panes: &Panes,
) {
    match tab {
        1 => dnssec::render(f, area, theme, data),
        2 => compare::render(f, area, theme, data),
        3 => trace::render(f, area, theme, data, focused, sel),
        _ => records(f, area, theme, data, filter, focused, sel, &panes.dns),
    }
}

/// The color of a response code: green for NOERROR, yellow for NXDOMAIN,
/// red for a server failure (SERVFAIL, REFUSED, …) — `seer dig`'s scheme.
pub(super) fn status_color(theme: &Theme, status: DnsStatus) -> Color {
    match wording::tone(status) {
        Tone::Answered => theme.green,
        Tone::Negative => theme.yellow,
        Tone::Failed => theme.red,
    }
}

/// The color of a verdict line (`wording::verdict`): yellow for a negative
/// answer, red when the server failed to answer at all.
pub(super) fn verdict_color(theme: &Theme, status: DnsStatus) -> Color {
    match wording::tone(status) {
        Tone::Failed => theme.red,
        Tone::Answered | Tone::Negative => theme.yellow,
    }
}

/// `● STATUS`, bold in the status's color — the head of a status line.
pub(super) fn status_spans(theme: &Theme, status: DnsStatus) -> [Span<'static>; 2] {
    let style = Style::default().fg(status_color(theme, status));
    [
        Span::styled("● ", style),
        Span::styled(status.to_string(), style.add_modifier(Modifier::BOLD)),
    ]
}

/// A table of DNS records in dig's column order — owner, TTL, type, data —
/// under `section` (`ANSWER`, `AUTHORITY`). Each record comes with whether
/// it is an answer proper, whose data is highlighted, rather than a CNAME
/// hop or an authority record. `selected` is the highlighted row, if any.
pub(super) fn record_table<'r>(
    theme: &Theme,
    section: &'static str,
    records: impl IntoIterator<Item = (&'r DnsRecord, bool)>,
    selected: Option<usize>,
) -> Table<'static> {
    let header =
        Row::new([section, "TTL", "TYPE", "DATA"]).style(Style::default().fg(theme.overlay0));
    let rows: Vec<Row> = records
        .into_iter()
        .enumerate()
        .map(|(i, (record, answer))| {
            let data = if answer { theme.green } else { theme.subtext };
            Row::new([
                Cell::from(sanitize_line(&record.name)),
                Cell::from(record.ttl.to_string()).style(Style::default().fg(theme.overlay)),
                Cell::from(record.record_type.as_str()).style(Style::default().fg(theme.lavender)),
                Cell::from(sanitize_line(&record.data.to_string()))
                    .style(Style::default().fg(data)),
            ])
            .style(row_style(theme, selected == Some(i)))
        })
        .collect();
    Table::new(
        rows,
        [
            Constraint::Percentage(30),
            Constraint::Length(7),
            Constraint::Length(8),
            Constraint::Min(0),
        ],
    )
    .header(header)
    .column_spacing(1)
}

/// The Records tab.
#[allow(clippy::too_many_arguments)]
fn records(
    f: &mut Frame,
    area: Rect,
    theme: &Theme,
    data: &LensData,
    filter: &str,
    focused: bool,
    sel: usize,
    dns: &DnsState,
) {
    let LensData::Dig(result) = data else { return };
    let title = format!(
        "dig · {} records · {}",
        result.record_type,
        sanitize_line(&result.name)
    );
    let inner = panel::render(f, area, theme, &title, theme.sky, focused);

    // Layout: nameserver chips + hint + status line + the response
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Min(0),
        ])
        .split(inner);

    // Nameserver chip row
    let mut ns_spans: Vec<Span> = Vec::new();
    for (i, label) in dns.slot_labels().into_iter().enumerate() {
        if i > 0 {
            ns_spans.push(Span::raw(" "));
        }
        let style = if i == dns.ns_idx {
            Style::default().fg(theme.base).bg(theme.sky)
        } else {
            Style::default().fg(theme.subtext).bg(theme.surface0)
        };
        ns_spans.push(Span::styled(format!(" {label} "), style));
    }
    f.render_widget(Paragraph::new(Line::from(ns_spans)), chunks[0]);

    // Hint line
    f.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled("s ", Style::default().fg(theme.overlay0)),
            Span::styled("nameserver  ", Style::default().fg(theme.subtext)),
            Span::styled("/ ", Style::default().fg(theme.overlay0)),
            Span::styled("filter", Style::default().fg(theme.subtext)),
        ])),
        chunks[1],
    );

    f.render_widget(Paragraph::new(status_line(theme, result)), chunks[2]);
    response(f, chunks[3], theme, result, filter, focused.then_some(sel));
}

/// dig's header line: `● NOERROR  flags qr rd ra  server 1.1.1.1  time 12 ms`.
/// An answer no server gave has no header flags (see `DnsQueryResult::flags`),
/// so the field is left out rather than shown empty; `server default` is the
/// default upstream, `server none` a name the resolver answered itself.
fn status_line(theme: &Theme, result: &DnsQueryResult) -> Line<'static> {
    let label = |text: &'static str| Span::styled(text, Style::default().fg(theme.overlay0));
    let value = |text: String| Span::styled(text, Style::default().fg(theme.text));
    let mut spans = Vec::from(status_spans(theme, result.status));
    if !result.flags.is_empty() {
        spans.push(label("  flags "));
        spans.push(value(sanitize_line(&result.flags.join(" "))));
    }
    spans.push(label("  server "));
    spans.push(value(result.server.as_deref().map_or_else(
        || wording::unnamed_server(result).to_string(),
        sanitize_line,
    )));
    spans.push(label("  time "));
    spans.push(value(format!("{} ms", result.query_time_ms)));
    Line::from(spans)
}

/// One band of the response, top to bottom.
enum Section {
    /// The ANSWER table: the visible (filtered) rows, scrolled to `selected`.
    Answers,
    /// Why there is no answer (`wording::query_verdict`), pre-wrapped to
    /// the area's width — a referral's names the zone and can run long.
    Verdict(Vec<String>),
    /// The AUTHORITY records (the SOA of a negative answer, a referral's NS
    /// records).
    Authority,
    /// The local-answer or wildcard note, pre-wrapped to the area's width,
    /// and its color.
    Note(Vec<String>, Color),
}

/// The response below the status line, stacked from the top: the answers,
/// the verdict when there are none, the authority records, and the
/// local-answer or wildcard note. Only the answer table shrinks (and
/// scrolls) when they do not all fit, so the verdict and the notes are never
/// cut.
fn response(
    f: &mut Frame,
    area: Rect,
    theme: &Theme,
    result: &DnsQueryResult,
    filter: &str,
    selected: Option<usize>,
) {
    let mut sections = Vec::new();
    if !result.answers.is_empty() {
        sections.push(Section::Answers);
    }
    if let Some(verdict) = wording::query_verdict(result, sanitize_line) {
        sections.push(Section::Verdict(wrap(&verdict, area.width)));
    }
    if !result.authority.is_empty() {
        sections.push(Section::Authority);
    }
    if result.answered_locally {
        sections.push(Section::Note(
            wrap(wording::LOCAL_NOTE, area.width),
            theme.yellow,
        ));
    }
    if let Some(probe) = &result.wildcard {
        if let Some(note) = wording::wildcard_note(probe, sanitize_line(&probe.probe_name)) {
            // A likely-synthesized answer is the one worth a warning.
            let color = if probe.matches_answer {
                theme.yellow
            } else {
                theme.subtext
            };
            sections.push(Section::Note(
                wrap(&format!("Wildcard: {note}"), area.width),
                color,
            ));
        }
    }

    let rows = filter::dig_rows(result, filter).count();
    let height = |lines: usize| u16::try_from(lines).unwrap_or(u16::MAX);
    let layout: Vec<Band> = sections
        .iter()
        .map(|section| match section {
            // The column header, then the visible rows; it scrolls.
            Section::Answers => Band::shrinking(height(rows + 1), 0),
            Section::Verdict(lines) => Band::fixed(height(lines.len())),
            Section::Authority => Band::fixed(height(result.authority.len() + 1)),
            Section::Note(lines, _) => Band::fixed(height(lines.len())),
        })
        .collect();
    let bands = stack(area, &layout);

    for (section, band) in sections.into_iter().zip(bands) {
        match section {
            Section::Answers => {
                let rows = filter::dig_rows(result, filter);
                let table = record_table(theme, "ANSWER", rows, selected);
                let mut state = scroll_to(selected);
                f.render_stateful_widget(table, band, &mut state);
            }
            Section::Verdict(lines) => f.render_widget(
                colored_lines(lines, verdict_color(theme, result.status)),
                band,
            ),
            Section::Authority => f.render_widget(
                record_table(
                    theme,
                    "AUTHORITY",
                    result.authority.iter().map(|r| (r, false)),
                    None,
                ),
                band,
            ),
            Section::Note(lines, color) => f.render_widget(colored_lines(lines, color), band),
        }
    }
}

/// Pre-wrapped lines in one color.
fn colored_lines(lines: Vec<String>, color: Color) -> Paragraph<'static> {
    let lines: Vec<Line> = lines
        .into_iter()
        .map(|line| Line::from(Span::styled(line, Style::default().fg(color))))
        .collect();
    Paragraph::new(lines)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::payload::fixtures;
    use crate::tui::test_util::{render_buffer, render_lines, render_text};
    use seer_core::dns::{RecordData, RecordType};
    use seer_core::WildcardProbe;

    fn draw(data: &LensData, panes: &Panes, filter: &str, sel: Option<usize>) -> String {
        let theme = Theme::frappe();
        render_lines(100, 16, |f| {
            render(
                f,
                f.area(),
                &theme,
                0,
                data,
                filter,
                sel.is_some(),
                sel.unwrap_or(0),
                panes,
            );
        })
    }

    fn dig_data(result: DnsQueryResult) -> LensData {
        LensData::Dig(Box::new(result))
    }

    /// `www.seer.test` → `shop.seer.test` → `edge.cdn.test` → two addresses.
    fn chained() -> DnsQueryResult {
        DnsQueryResult {
            server: Some("1.1.1.1".into()),
            ..fixtures::dig(
                RecordType::A,
                vec![
                    fixtures::cname("www.seer.test", "shop.seer.test."),
                    fixtures::cname("shop.seer.test", "edge.cdn.test."),
                    fixtures::a("edge.cdn.test", "192.0.2.7"),
                    fixtures::a("edge.cdn.test", "192.0.2.8"),
                ],
            )
        }
    }

    fn soa(zone: &str) -> DnsRecord {
        DnsRecord {
            name: zone.into(),
            record_type: RecordType::SOA,
            ttl: 900,
            data: RecordData::SOA {
                mname: format!("ns1.{zone}."),
                rname: format!("hostmaster.{zone}."),
                serial: 2024010101,
                refresh: 7200,
                retry: 3600,
                expire: 1209600,
                minimum: 300,
            },
        }
    }

    /// The drawn words in order, borders and line breaks dropped — for a
    /// note that wraps.
    fn flat(text: &str) -> String {
        text.replace(['│', '╭', '╮', '╰', '╯', '─'], " ")
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ")
    }

    /// The row a table shows `needle` on.
    fn row_with<'t>(text: &'t str, needle: &str) -> &'t str {
        text.lines()
            .find(|l| l.contains(needle))
            .unwrap_or_else(|| panic!("no row with {needle:?} in\n{text}"))
    }

    #[test]
    fn a_positive_answer_lists_the_chain_under_its_real_owners() {
        let text = draw(&dig_data(chained()), &Panes::default(), "", None);
        assert!(
            text.contains("dig · A records · www.seer.test"),
            "title names the type and name: {text}"
        );
        assert!(
            text.contains("● NOERROR  flags qr rd ra  server 1.1.1.1  time 12 ms"),
            "{text}"
        );
        // Each hop under its own owner, then the A records under the
        // chain's end, not under the queried name.
        let hop1 = row_with(&text, "shop.seer.test.");
        assert!(
            hop1.contains("www.seer.test") && hop1.contains("CNAME"),
            "{hop1}"
        );
        let hop2 = row_with(&text, "edge.cdn.test.");
        assert!(
            hop2.contains("shop.seer.test") && hop2.contains("CNAME"),
            "{hop2}"
        );
        let a = row_with(&text, "192.0.2.7");
        assert!(a.contains("edge.cdn.test") && a.contains(" A "), "{a}");
        assert!(!a.contains("www.seer.test"), "{a}");
        let order: Vec<usize> = [
            "shop.seer.test.",
            "edge.cdn.test.",
            "192.0.2.7",
            "192.0.2.8",
        ]
        .iter()
        .map(|needle| text.find(needle).unwrap())
        .collect();
        assert!(order.windows(2).all(|w| w[0] < w[1]), "dig's order: {text}");
        assert!(
            !text.contains("NODATA") && !text.contains("NXDOMAIN"),
            "{text}"
        );
    }

    #[test]
    fn answer_data_is_highlighted_but_chain_hops_are_not() {
        let theme = Theme::frappe();
        let data = dig_data(chained());
        let panes = Panes::default();
        let buffer = render_buffer(100, 16, |f| {
            render(f, f.area(), &theme, 0, &data, "", false, 0, &panes);
        });
        let fg_of = |needle: &str| {
            let area = buffer.area();
            (area.top()..area.bottom())
                .find_map(|y| {
                    let row: String = (area.left()..area.right())
                        .map(|x| buffer[(x, y)].symbol())
                        .collect();
                    let col = row.find(needle)?;
                    let x = row[..col].chars().count() as u16;
                    Some(buffer[(x, y)].fg)
                })
                .unwrap_or_else(|| panic!("{needle} not drawn"))
        };
        assert_eq!(fg_of("192.0.2.7"), theme.green);
        assert_eq!(fg_of("edge.cdn.test."), theme.subtext);
        assert_eq!(fg_of("NOERROR"), theme.green);
    }

    #[test]
    fn nxdomain_says_the_name_does_not_exist_and_shows_the_soa() {
        let result = DnsQueryResult {
            authority: vec![soa("seer.test")],
            ..fixtures::dig_status(RecordType::A, DnsStatus::NxDomain)
        };
        let text = draw(&dig_data(result), &Panes::default(), "", None);
        // A negative answer carries its header like any response.
        assert!(
            text.contains("● NXDOMAIN  flags qr rd ra  server default  time 12 ms"),
            "{text}"
        );
        assert!(text.contains("Name does not exist (NXDOMAIN)"), "{text}");
        assert!(!text.contains("NODATA"), "{text}");
        assert!(!text.contains("ANSWER"), "no empty answer table: {text}");
        let soa = row_with(&text, "ns1.seer.test.");
        assert!(soa.contains("seer.test") && soa.contains("SOA"), "{soa}");
        assert!(soa.contains("hostmaster.seer.test."), "{soa}");
        assert!(text.contains("AUTHORITY"), "{text}");
    }

    #[test]
    fn nodata_says_the_name_exists_without_the_type() {
        let result = DnsQueryResult {
            authority: vec![soa("seer.test")],
            ..fixtures::dig(RecordType::AAAA, vec![])
        };
        let text = draw(&dig_data(result), &Panes::default(), "", None);
        assert!(text.contains("● NOERROR"), "{text}");
        assert!(
            text.contains("No AAAA records (NODATA — the name exists)"),
            "{text}"
        );
        assert!(!text.contains("NXDOMAIN"), "{text}");
        assert!(text.contains("AUTHORITY"), "{text}");
    }

    #[test]
    fn a_dangling_cname_shows_its_chain_and_names_the_missing_target() {
        // Regression: the chain was lost and the verdict blamed the queried
        // name, which exists — it owns the CNAME.
        let result = DnsQueryResult {
            answers: vec![fixtures::cname("www.seer.test", "gone.cdn.test.")],
            authority: vec![soa("cdn.test")],
            ..fixtures::dig_status(RecordType::A, DnsStatus::NxDomain)
        };
        let text = draw(&dig_data(result), &Panes::default(), "", None);
        assert!(text.contains("ANSWER"), "{text}");
        assert!(
            row_with(&text, "gone.cdn.test.").contains("CNAME"),
            "{text}"
        );
        assert!(
            flat(&text).contains("The CNAME target gone.cdn.test. does not exist (NXDOMAIN)"),
            "{text}"
        );
        assert!(!text.contains("Name does not exist"), "{text}");
    }

    #[test]
    fn server_failures_are_told_apart_from_negative_answers() {
        let theme = Theme::frappe();
        for (status, verdict) in [
            (
                DnsStatus::ServFail,
                "No answer: the server failed to resolve the name (SERVFAIL)",
            ),
            (
                DnsStatus::Refused,
                "No answer: the server refused the query (REFUSED)",
            ),
        ] {
            let data = dig_data(fixtures::dig_status(RecordType::A, status));
            let panes = Panes::default();
            let text = draw(&data, &panes, "", None);
            assert!(text.contains(verdict), "{text}");
            assert!(!text.contains("AUTHORITY"), "{text}");
            // The status is drawn in the failure color.
            let buffer = render_buffer(100, 16, |f| {
                render(f, f.area(), &theme, 0, &data, "", false, 0, &panes);
            });
            let status_row = (0..16u16)
                .find(|&y| {
                    (0..100u16)
                        .map(|x| buffer[(x, y)].symbol())
                        .collect::<String>()
                        .contains(&status.to_string())
                })
                .expect("status line");
            let fg: Vec<_> = (0..100u16)
                .filter(|&x| buffer[(x, status_row)].symbol() == "●")
                .map(|x| buffer[(x, status_row)].fg)
                .collect();
            assert_eq!(fg, [theme.red], "{status}");
        }
    }

    #[test]
    fn a_referral_names_its_zone_instead_of_claiming_nodata() {
        let ns = |host: &str| DnsRecord {
            name: "child.seer.test".into(),
            record_type: RecordType::NS,
            ttl: 300,
            data: RecordData::NS {
                nameserver: host.into(),
            },
        };
        // A parent-side server's header: no `aa`, no `ra`.
        let result = DnsQueryResult {
            flags: vec!["qr".into(), "rd".into()],
            authority: vec![ns("ns1.child.seer.test."), ns("ns2.child.seer.test.")],
            ..fixtures::dig_status(RecordType::A, DnsStatus::NoError)
        };
        let text = draw(&dig_data(result), &Panes::default(), "", None);
        assert!(text.contains("● NOERROR  flags qr rd  server"), "{text}");
        assert!(
            flat(&text).contains(
                "No answer: referral to child.seer.test — the server is not authoritative for \
                 the name (no aa flag) and does not recurse"
            ),
            "{text}"
        );
        assert!(!text.contains("NODATA"), "{text}");
        assert!(text.contains("AUTHORITY"), "{text}");
        assert!(
            row_with(&text, "ns2.child.seer.test.").contains("NS"),
            "{text}"
        );
    }

    #[test]
    fn a_local_answer_says_no_server_was_asked() {
        let result = DnsQueryResult {
            answered_locally: true,
            flags: vec![],
            ..fixtures::dig(
                RecordType::A,
                vec![fixtures::a("foo.localhost", "127.0.0.1")],
            )
        };
        let text = draw(&dig_data(result), &Panes::default(), "", None);
        assert!(
            text.contains("● NOERROR  server none  time 12 ms"),
            "{text}"
        );
        assert!(flat(&text).contains(wording::LOCAL_NOTE), "{text}");
    }

    #[test]
    fn a_wildcard_note_warns_about_a_likely_synthesized_answer() {
        let mut result = fixtures::dig(
            RecordType::A,
            vec![fixtures::a("anything.seer.test", "192.0.2.9")],
        );
        result.wildcard = Some(WildcardProbe {
            probe_name: "seer-probe-3f9a1c2e7b.seer.test".into(),
            present: true,
            matches_answer: true,
        });
        let text = draw(&dig_data(result.clone()), &Panes::default(), "", None);
        // Wrapped to the pane: the words run across a line break.
        assert!(
            flat(&text).contains(
                "Wildcard: a random sibling (seer-probe-3f9a1c2e7b.seer.test) resolves — this \
                 answer matches it and is likely wildcard-synthesized"
            ),
            "{text}"
        );

        // Right under the answers, not at the foot of the pane.
        let lines: Vec<&str> = text.lines().collect();
        let answer = lines.iter().position(|l| l.contains("192.0.2.9")).unwrap();
        assert!(lines[answer + 2].contains("Wildcard:"), "{text}");

        let probe = result.wildcard.as_mut().unwrap();
        probe.matches_answer = false;
        let text = draw(&dig_data(result.clone()), &Panes::default(), "", None);
        assert!(
            flat(&text).contains("resolves too, but with different data"),
            "{text}"
        );

        // A probe that found no wildcard says nothing.
        result.wildcard.as_mut().unwrap().present = false;
        let text = draw(&dig_data(result), &Panes::default(), "", None);
        assert!(!text.contains("Wildcard"), "{text}");
    }

    #[test]
    fn the_filter_narrows_the_answer_rows_but_not_the_verdict() {
        let data = dig_data(chained());
        let text = draw(&data, &Panes::default(), "192.0.2.8", None);
        assert!(text.contains("192.0.2.8"), "{text}");
        assert!(!text.contains("192.0.2.7"), "{text}");
        assert!(!text.contains("shop.seer.test."), "{text}");
        // Nothing matching is an empty table, not a "no records" verdict.
        let text = draw(&data, &Panes::default(), "zzz", None);
        assert!(text.contains("ANSWER"), "{text}");
        assert!(!text.contains("NODATA"), "{text}");
    }

    #[test]
    fn remote_strings_are_sanitized_onto_one_line() {
        let result = fixtures::dig(
            RecordType::TXT,
            vec![DnsRecord {
                name: "seer.test".into(),
                record_type: RecordType::TXT,
                ttl: 300,
                data: RecordData::TXT {
                    text: "v=spf1\x1b[2J -all\n203.0.113.66".into(),
                },
            }],
        );
        let text = draw(&dig_data(result), &Panes::default(), "", None);
        let row = row_with(&text, "v=spf1");
        assert!(row.contains("\"v=spf1 -all 203.0.113.66\""), "{row}");
        assert!(!text.contains("[2J"), "escape sequence removed: {text}");
    }

    #[test]
    fn selecting_past_the_viewport_scrolls_the_row_into_view() {
        let answers: Vec<DnsRecord> = (1..=30)
            .map(|i| fixtures::a("www.seer.test", &format!("192.0.2.{i}")))
            .collect();
        let data = dig_data(fixtures::dig(RecordType::A, answers));
        let text = draw(&data, &Panes::default(), "", Some(29));
        assert!(text.contains("192.0.2.30"), "{text}");
    }

    #[test]
    fn renders_nameserver_chip_row() {
        let data = dig_data(chained());
        let mut panes = Panes::default();
        let text = draw(&data, &panes, "", None);
        assert!(text.contains(" system   8.8.8.8   1.1.1.1 "), "{text}");
        panes.dns.select_server("9.9.9.9".into());
        let text = draw(&data, &panes, "", None);
        assert!(text.contains(" 1.1.1.1   9.9.9.9 "), "custom slot: {text}");
    }

    #[test]
    fn other_tabs_render_their_own_payloads() {
        let theme = Theme::frappe();
        let panes = Panes::default();
        let trace = LensData::Trace(Box::new(fixtures::trace(
            vec![fixtures::a("www.seer.test", "192.0.2.7")],
            None,
        )));
        let text = render_text(100, 20, |f| {
            render(f, f.area(), &theme, 3, &trace, "", false, 0, &panes);
        });
        assert!(text.contains("trace · www.seer.test A"), "{text}");
        // A Records payload under the Trace tab (a stale cache) draws nothing.
        let text = render_text(100, 20, |f| {
            render(
                f,
                f.area(),
                &theme,
                3,
                &dig_data(chained()),
                "",
                false,
                0,
                &panes,
            );
        });
        assert!(!text.contains("192.0.2.7"), "{text}");
    }
}