croft-software 0.1.736

VSCode-style TUI 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
//! Port detection for the PORTS panel and the auto-forward toast.
//!
//! Two independent sources feed the same [`PortHit`] stream:
//!
//! * **Output scrape** — [`PortSniffer::sniff`] runs on every PTY chunk in the
//!   terminal reader thread (right beside `sniff_bracketed_paste_mode`),
//!   matching the `http://localhost:PORT` banners and `listening on :PORT`
//!   lines that dev servers print. Cheap: two precompiled regexes over a small
//!   carry buffer. Emission is position-based — only a match in the freshly
//!   read bytes fires, so the carried tail is never re-emitted, yet a banner
//!   reprinted by a restarted server is. Deduping live ports is the app's job.
//! * **Socket poll** — [`poll_listeners`] shells out to `lsof`/`ss` on a low
//!   cadence to catch servers that bind a port without announcing it. Scoped to
//!   the shell's own process subtree so system daemons don't leak in. On a
//!   remote session this runs on the remote (remote croft polls its own
//!   sockets), which is exactly the host whose ports we want to forward.
//!
//! The detector never decides what to do with a port: it reports, the app
//! deduplicates against its live registry and offers the forward.

use std::collections::HashSet;
use std::process::Command;
use std::sync::LazyLock;

use regex::Regex;

/// A loopback listener we noticed, from either source. The app turns this into
/// a registry entry and (on a remote session) an offer to forward.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PortHit {
    pub port: u16,
    /// The full URL when scraped from one (`http://localhost:3000/`), so the
    /// app can preserve scheme and path when opening. `None` for socket-poll
    /// hits and bare `listening on :PORT` lines, where the app assumes http.
    pub url: Option<String>,
    /// Owning process name when known (socket poll), else `None`.
    pub process: Option<String>,
}

impl PortHit {
    fn bare(port: u16) -> Self {
        Self {
            port,
            url: None,
            process: None,
        }
    }
}

/// `http://localhost:3000/path` — captures the port and keeps the whole match
/// so the app can open the exact URL. Hosts are restricted to the loopback
/// spellings a local dev server uses; a public host isn't ours to forward.
static URL_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)\bhttps?://(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])(?::(\d{1,5}))?(?:/[^\s'\x22)\]]*)?")
        .expect("port URL regex")
});

/// `Listening on port 8080`, `serving at :8080`, `bound to 0.0.0.0:8080` — the
/// announce lines that carry no scheme. The number must be introduced by the
/// word `port` or by a colon: an announce verb followed by a bare number is
/// ordinary output (`running 289 tests`, `Started 15 workers`), and inventing
/// a port from it offers to forward one nothing is listening on. The colon
/// branch still needs [`is_clock_time`] to reject `Started backup at 03:15`.
static BARE_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(
        r"(?i)\b(?:listening|serving|started|running|bound)\b[^\n]{0,40}?(?:\bport\b\s*:?\s*|:\s*)(\d{2,5})\b",
    )
    .expect("bare port regex")
});

/// Whether the digits captured at `start` in `text` are the minutes of a clock
/// time (`03:15`, `running since 10:22`) rather than a port. An address keeps
/// its host in front of the colon (`0.0.0.0:8080`, `[::1]:80`), so only a bare
/// one or two digit number introducing the colon reads as an hour.
fn is_clock_time(text: &str, start: usize, digits: &str) -> bool {
    if digits.len() != 2 {
        return false;
    }
    let before = text[..start].trim_end();
    let Some(head) = before.strip_suffix(':') else {
        return false;
    };
    let hour: String = head
        .chars()
        .rev()
        .take_while(|c| c.is_ascii_digit())
        .collect();
    if hour.is_empty() || hour.len() > 2 {
        return false;
    }
    // Only a STANDALONE number reads as an hour: `0.0.0.0:80` and
    // `127.0.0.1:8080` end in a digit too but the dot marks an address, and
    // in `api1:80` or `web-1:80` the digits are a hostname's tail.
    !head[..head.len() - hour.len()]
        .ends_with(|c: char| c == '.' || c == '-' || c == '_' || c.is_ascii_alphanumeric())
}

/// A general http(s) URL token, for Cmd/Ctrl+click in the terminal (any host,
/// not just loopback). Stops at whitespace and the brackets/quotes that
/// commonly surround a URL in log output.
static CLICK_URL_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"(?i)\bhttps?://[^\s'"<>()\[\]]+"#).expect("click url regex"));

/// The http(s) URL covering character index `col` in `text`, if any — used to
/// resolve which printed link a Cmd/Ctrl+click landed on. Trailing sentence
/// punctuation is trimmed so a link at the end of a line doesn't grab the dot.
pub fn url_at(text: &str, col: usize) -> Option<String> {
    for m in CLICK_URL_RE.find_iter(text) {
        let start = text[..m.start()].chars().count();
        let end = start + m.as_str().chars().count();
        if col >= start && col < end {
            let url = m.as_str().trim_end_matches(['.', ',', ';', ':', '!', '?']);
            return Some(url.to_string());
        }
    }
    None
}

/// The loopback port of `url` when the whole string is a loopback dev-server
/// URL (`http://localhost:3000/...`), else `None`. Lets a click decide whether
/// a URL is a remote port to forward or an ordinary link to open. Defaults to
/// the scheme's port when none is spelled out.
pub fn loopback_url_port(url: &str) -> Option<u16> {
    let cap = URL_RE.captures(url)?;
    if cap.get(0)?.start() != 0 {
        return None;
    }
    cap.get(1).and_then(|m| parse_port(m.as_str())).or(Some(
        if url.to_ascii_lowercase().starts_with("https") {
            443
        } else {
            80
        },
    ))
}

fn parse_port(s: &str) -> Option<u16> {
    s.parse::<u32>()
        .ok()
        .filter(|p| *p >= 1 && *p <= 65535)
        .map(|p| p as u16)
}

/// Stateful scraper owned by a terminal's reader thread. Carries a short tail of
/// already-scanned text so a banner split across two `read()`s still matches.
/// Emission is position-based: only a match reaching into the *new* chunk fires,
/// so the carried tail is never re-emitted, yet a banner printed afresh (a
/// restarted server) is. Dedup against the live set is the app's job, not the
/// sniffer's — which is what lets a restart re-toast.
pub struct PortSniffer {
    carry: String,
}

/// How many trailing chars of the combined text to keep for the next scan,
/// covering a URL straddling a read boundary without rescanning whole chunks.
const CARRY_BYTES: usize = 256;

impl PortSniffer {
    pub fn new() -> Self {
        Self {
            carry: String::new(),
        }
    }

    /// Scan one PTY chunk, pushing a [`PortHit`] for each port whose match
    /// extends past the carried prefix (i.e. lives in the new bytes). The send
    /// is best-effort: a dropped hit just means the app learns of the port from
    /// the next line or the socket poll.
    pub fn sniff(&mut self, chunk: &[u8], tx: &std::sync::mpsc::Sender<PortHit>) {
        // ANSI colour codes around a URL are outside the match, and a colour
        // reset mid-URL is vanishingly rare, so a string scan is enough without
        // a full terminal parse.
        let carry_len = self.carry.len();
        let text = format!("{}{}", self.carry, String::from_utf8_lossy(chunk));
        for (end, hit) in scan_with_pos(&text) {
            // A match ending within the carried prefix was already scanned last
            // call; only emit one that reaches into the freshly read bytes.
            if end > carry_len {
                let _ = tx.send(hit);
            }
        }
        // Keep the tail (on a char boundary) for the next call's split coverage.
        let mut keep_from = text.len().saturating_sub(CARRY_BYTES);
        while keep_from < text.len() && !text.is_char_boundary(keep_from) {
            keep_from += 1;
        }
        self.carry = text[keep_from..].to_string();
    }
}

impl Default for PortSniffer {
    fn default() -> Self {
        Self::new()
    }
}

/// Position-free view of [`scan_with_pos`] for the tests. URL hits win over bare
/// hits for the same port (the URL carries scheme and path).
#[cfg(test)]
fn scan(text: &str) -> Vec<PortHit> {
    scan_with_pos(text).into_iter().map(|(_, h)| h).collect()
}

/// Like [`scan`] but pairs each hit with the byte offset where its match ends,
/// so [`PortSniffer::sniff`] can tell a hit in the freshly read bytes from one
/// that only lives in the carried-over prefix.
fn scan_with_pos(text: &str) -> Vec<(usize, PortHit)> {
    let mut out: Vec<(usize, PortHit)> = Vec::new();
    for cap in URL_RE.captures_iter(text) {
        let m = cap.get(0).expect("group 0 always present");
        let whole = m.as_str().to_string();
        // Explicit port, or the scheme's default (http=80, https=443).
        let port = cap.get(1).and_then(|g| parse_port(g.as_str())).unwrap_or(
            if whole.to_ascii_lowercase().starts_with("https") {
                443
            } else {
                80
            },
        );
        out.push((
            m.end(),
            PortHit {
                port,
                url: Some(whole),
                process: None,
            },
        ));
    }
    for cap in BARE_RE.captures_iter(text) {
        if let Some(g) = cap.get(1)
            && !is_clock_time(text, g.start(), g.as_str())
            && let Some(port) = parse_port(g.as_str())
            && !out.iter().any(|(_, h)| h.port == port)
        {
            out.push((g.end(), PortHit::bare(port)));
        }
    }
    out
}

/// PIDs in `root`'s process subtree (inclusive), from one `ps` snapshot. Used
/// to scope the socket poll to the shell's own children so we don't surface
/// every system daemon listening on loopback. Empty on `ps` failure (the poll
/// then reports nothing, falling back to the output scrape).
fn descendant_pids(root: i32) -> HashSet<i32> {
    let mut subtree: HashSet<i32> = HashSet::new();
    subtree.insert(root);
    let Ok(out) = Command::new("ps").args(["-axo", "pid=,ppid="]).output() else {
        return subtree;
    };
    let text = String::from_utf8_lossy(&out.stdout);
    let pairs: Vec<(i32, i32)> = text
        .lines()
        .filter_map(|l| {
            let mut it = l.split_whitespace();
            let pid = it.next()?.parse().ok()?;
            let ppid = it.next()?.parse().ok()?;
            Some((pid, ppid))
        })
        .collect();
    // Iterate to a fixed point: a child whose parent joined the set joins too.
    loop {
        let before = subtree.len();
        for (pid, ppid) in &pairs {
            if subtree.contains(ppid) {
                subtree.insert(*pid);
            }
        }
        if subtree.len() == before {
            break;
        }
    }
    subtree
}

/// Listening loopback TCP ports owned by `shell_pid`'s subtree, via `lsof`
/// (macOS + Linux) with an `ss` fallback (Linux without lsof). Best-effort:
/// any failure yields an empty list and the output scrape carries the load.
pub fn poll_listeners(shell_pid: i32) -> Vec<PortHit> {
    let pids = descendant_pids(shell_pid);
    if let Some(hits) = poll_via_lsof(&pids) {
        return hits;
    }
    poll_via_ss(&pids).unwrap_or_default()
}

/// Every loopback TCP port listening on this machine, whoever owns it. The
/// scoped [`poll_listeners`] decides what to SURFACE (a system daemon is not
/// the user's dev server); this decides what is still UP, which is a different
/// question and one the subtree scope cannot answer — a container's published
/// port, or a server started before croft, is nobody's descendant. `None` when
/// neither tool ran, so the caller can tell "nothing is listening" from "no
/// evidence" and leave the registry alone rather than retiring live rows.
pub fn poll_all_listening() -> Option<HashSet<u16>> {
    let out = Command::new("lsof")
        .args(["-nP", "-iTCP", "-sTCP:LISTEN"])
        .output()
        .ok();
    if let Some(out) = out
        && (out.status.success() || !out.stdout.is_empty())
    {
        return Some(
            parse_lsof(&String::from_utf8_lossy(&out.stdout))
                .into_iter()
                .map(|h| h.port)
                .collect(),
        );
    }
    let out = Command::new("ss").args(["-tlnH"]).output().ok()?;
    if !out.status.success() {
        return None;
    }
    Some(
        String::from_utf8_lossy(&out.stdout)
            .lines()
            .filter_map(|l| l.split_whitespace().nth(3).and_then(loopback_port))
            .collect(),
    )
}

fn poll_via_lsof(pids: &HashSet<i32>) -> Option<Vec<PortHit>> {
    if pids.is_empty() {
        return Some(Vec::new());
    }
    let pid_arg = pids
        .iter()
        .map(|p| p.to_string())
        .collect::<Vec<_>>()
        .join(",");
    let out = Command::new("lsof")
        .args(["-nP", "-iTCP", "-sTCP:LISTEN", "-a", "-p", &pid_arg])
        .output()
        .ok()?;
    if !out.status.success() && out.stdout.is_empty() {
        return None;
    }
    Some(parse_lsof(&String::from_utf8_lossy(&out.stdout)))
}

/// Parse `lsof -nP -iTCP -sTCP:LISTEN` rows. Field 0 is the command; the NAME
/// column (`*:3000`, `127.0.0.1:3000`) is followed by a `(LISTEN)` token, so we
/// scan the row for the first field that parses as a loopback/wildcard address.
fn parse_lsof(text: &str) -> Vec<PortHit> {
    let mut out: Vec<PortHit> = Vec::new();
    for line in text.lines().skip(1) {
        let fields: Vec<&str> = line.split_whitespace().collect();
        let Some(cmd) = fields.first() else { continue };
        if let Some(port) = fields.iter().find_map(|f| loopback_port(f))
            && !out.iter().any(|h| h.port == port)
        {
            out.push(PortHit {
                port,
                url: None,
                process: Some((*cmd).to_string()),
            });
        }
    }
    out
}

fn poll_via_ss(pids: &HashSet<i32>) -> Option<Vec<PortHit>> {
    let out = Command::new("ss").args(["-tlnpH"]).output().ok()?;
    if !out.status.success() {
        return None;
    }
    Some(parse_ss(&String::from_utf8_lossy(&out.stdout), pids))
}

/// Parse `ss -tlnpH` rows. The local-address column is field 3 and the process
/// column embeds `pid=NNN`; we keep only rows whose pid is in the subtree.
fn parse_ss(text: &str, pids: &HashSet<i32>) -> Vec<PortHit> {
    let mut out: Vec<PortHit> = Vec::new();
    for line in text.lines() {
        let fields: Vec<&str> = line.split_whitespace().collect();
        let Some(local) = fields.get(3) else { continue };
        let Some(port) = loopback_port(local) else {
            continue;
        };
        let proc = fields
            .last()
            .and_then(|f| ss_pid(f).map(|pid| (pid, ss_proc_name(f))));
        match proc {
            Some((pid, name))
                if (pids.is_empty() || pids.contains(&pid))
                    && !out.iter().any(|h| h.port == port) =>
            {
                out.push(PortHit {
                    port,
                    url: None,
                    process: name,
                });
            }
            _ => {}
        }
    }
    out
}

fn ss_pid(field: &str) -> Option<i32> {
    let start = field.find("pid=")? + 4;
    let rest = &field[start..];
    let end = rest
        .find(|c: char| !c.is_ascii_digit())
        .unwrap_or(rest.len());
    rest[..end].parse().ok()
}

fn ss_proc_name(field: &str) -> Option<String> {
    let start = field.find("((\"")? + 3;
    let rest = &field[start..];
    let end = rest.find('"')?;
    Some(rest[..end].to_string())
}

/// The port from a `host:port` address, but only for loopback / wildcard hosts
/// (`*`, `0.0.0.0`, `127.0.0.1`, `[::]`, `[::1]`, `localhost`). A bound public
/// interface isn't a dev server we should be offering to tunnel home.
fn loopback_port(addr: &str) -> Option<u16> {
    let (host, port) = addr.rsplit_once(':')?;
    let host = host.trim_matches(|c| c == '[' || c == ']');
    let loopback = matches!(
        host,
        "" | "*" | "0.0.0.0" | "127.0.0.1" | "::" | "::1" | "localhost"
    );
    if !loopback {
        return None;
    }
    parse_port(port)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn scans_a_vite_banner_url_with_port_and_path() {
        let hits = scan("  VITE v5.2  ready  ->  Local: http://localhost:3000/app");
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].port, 3000);
        assert_eq!(hits[0].url.as_deref(), Some("http://localhost:3000/app"));
    }

    #[test]
    fn scans_a_bare_listening_line() {
        let hits = scan("INFO server listening on port 8080");
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].port, 8080);
        assert!(hits[0].url.is_none());
    }

    /// The reconciliation snapshot must cover every loopback listener, not just
    /// a croft pane's process subtree. A container's published port is nobody's
    /// descendant; judging it by the scoped scan retired the row and cancelled
    /// its tunnel seconds after the output scrape found it. This test's own
    /// listener is owned by the test binary, which is exactly such an outsider.
    #[test]
    fn the_reconciliation_snapshot_covers_listeners_outside_any_subtree() {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind a loopback port");
        let port = listener.local_addr().unwrap().port();
        let Some(all) = poll_all_listening() else {
            // No lsof and no ss: the app skips reconciliation entirely, which
            // is the safe direction, and there is nothing to assert here.
            return;
        };
        assert!(
            all.contains(&port),
            "port {port} is listening but missing from the snapshot"
        );
    }

    /// A test harness counting its cases is not a dev server. Matching a bare
    /// number after an announce verb fabricates a port, which on a remote
    /// session offers to `ssh -O forward` a port nothing is listening on.
    #[test]
    fn a_plain_count_after_an_announce_verb_is_not_a_port() {
        for line in [
            "running 289 tests",
            "Started 15 workers",
            "started 42 jobs, running 1200 checks",
        ] {
            assert!(scan(line).is_empty(), "{line:?} must not report a port");
        }
    }

    /// A clock time is not a port either. The colon branch has to let
    /// `0.0.0.0:80` through, so the digits before it are what separate an
    /// address from an hour.
    #[test]
    fn a_clock_time_after_an_announce_verb_is_not_a_port() {
        for line in [
            "Started backup at 03:15",
            "server started, running since 10:22",
            "running 289 tests across 12:30 minutes",
        ] {
            assert!(scan(line).is_empty(), "{line:?} must not report a port");
        }
    }

    /// The announce forms the scrape exists for still match: a spelled-out
    /// `port N`, `port: N` (Express's own boilerplate), and a `host:N` / `:N`
    /// address, including the two-digit well-known ones a clock could shadow.
    #[test]
    fn announce_lines_with_a_port_or_a_colon_still_match() {
        for (line, port) in [
            ("Listening on port 8080", 8080u16),
            ("Example app listening on port: 3000", 3000),
            ("Server listening on port : 8080", 8080),
            ("serving at :4000", 4000),
            ("bound to 0.0.0.0:9229", 9229),
            ("bound to 0.0.0.0:80", 80),
            ("listening on 127.0.0.1:80", 80),
            // A numeric-suffixed hostname is not a clock: only a STANDALONE
            // one or two digit number before the colon reads as an hour.
            ("bound to api1:80", 80),
            ("listening on web-1:80", 80),
        ] {
            let hits = scan(line);
            assert_eq!(hits.len(), 1, "{line:?}");
            assert_eq!(hits[0].port, port, "{line:?}");
        }
    }

    #[test]
    fn url_wins_over_bare_for_the_same_port() {
        let hits = scan("listening on http://127.0.0.1:5000 — now serving 5000");
        assert_eq!(hits.iter().filter(|h| h.port == 5000).count(), 1);
        assert!(hits.iter().any(|h| h.port == 5000 && h.url.is_some()));
    }

    #[test]
    fn ignores_public_hosts() {
        assert!(scan("see https://example.com:8443/docs").is_empty());
    }

    #[test]
    fn https_url_without_explicit_port_defaults_to_443() {
        let hits = scan("ready at https://localhost/");
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].port, 443);
    }

    #[test]
    fn sniffer_does_not_double_emit_a_banner_via_the_carry() {
        let (tx, rx) = std::sync::mpsc::channel();
        let mut s = PortSniffer::new();
        s.sniff(b"up at http://localhost:3000/\n", &tx);
        // The banner now lives only in the carried tail; a follow-up chunk that
        // doesn't reprint it must not re-emit the port.
        s.sniff(b"$ \n", &tx);
        let got: Vec<_> = rx.try_iter().collect();
        assert_eq!(got.len(), 1, "carried banner is not re-emitted: {got:?}");
        assert_eq!(got[0].port, 3000);
    }

    #[test]
    fn sniffer_re_emits_a_port_after_the_server_stops_and_restarts() {
        let (tx, rx) = std::sync::mpsc::channel();
        let mut s = PortSniffer::new();
        s.sniff(b"Serving on http://localhost:8000/\n", &tx);
        // Server killed: a chunk with no port clears the suppression.
        s.sniff(b"^C\nKeyboard interrupt received, exiting.\n", &tx);
        // Restarted: the same port must be reported again so the toast fires.
        s.sniff(b"Serving on http://localhost:8000/\n", &tx);
        let got: Vec<_> = rx.try_iter().collect();
        assert_eq!(got.len(), 2, "restart re-emits the port: {got:?}");
        assert!(got.iter().all(|h| h.port == 8000));
    }

    #[test]
    fn sniffer_catches_a_port_split_across_chunks() {
        let (tx, rx) = std::sync::mpsc::channel();
        let mut s = PortSniffer::new();
        s.sniff(b"Local:  http://localho", &tx);
        s.sniff(b"st:4173/ ready", &tx);
        let got: Vec<_> = rx.try_iter().collect();
        assert_eq!(got.len(), 1);
        assert_eq!(got[0].port, 4173);
    }

    #[test]
    fn parses_lsof_loopback_listeners_only() {
        let sample = "COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME\n\
                      node 123 me 21u IPv4 0xabc 0t0 TCP 127.0.0.1:3000 (LISTEN)\n\
                      ssh 99 me 5u IPv4 0xdef 0t0 TCP 10.0.0.2:22 (LISTEN)\n";
        let hits = parse_lsof(sample);
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].port, 3000);
        assert_eq!(hits[0].process.as_deref(), Some("node"));
    }

    #[test]
    fn parses_ss_rows_scoped_to_pids() {
        let sample = "LISTEN 0 511 127.0.0.1:5432 0.0.0.0:* users:((\"postgres\",pid=4242,fd=7))\n\
                      LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:((\"sshd\",pid=1,fd=3))\n";
        let mut pids = HashSet::new();
        pids.insert(4242);
        let hits = parse_ss(sample, &pids);
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].port, 5432);
        assert_eq!(hits[0].process.as_deref(), Some("postgres"));
    }

    #[test]
    fn url_at_finds_the_link_under_the_cursor() {
        let line = "  visit http://localhost:3000/app for the UI";
        // Column inside the URL.
        assert_eq!(
            url_at(line, 20).as_deref(),
            Some("http://localhost:3000/app")
        );
        // Column in the surrounding prose: no URL.
        assert!(url_at(line, 2).is_none());
    }

    #[test]
    fn url_at_trims_trailing_punctuation() {
        let line = "see https://example.com/docs.";
        assert_eq!(
            url_at(line, 10).as_deref(),
            Some("https://example.com/docs")
        );
    }

    #[test]
    fn loopback_url_port_classifies_dev_servers() {
        assert_eq!(loopback_url_port("http://localhost:3000/app"), Some(3000));
        assert_eq!(loopback_url_port("https://127.0.0.1/"), Some(443));
        assert_eq!(loopback_url_port("http://localhost/"), Some(80));
        assert!(loopback_url_port("https://example.com:8443/").is_none());
    }

    #[test]
    fn loopback_port_rejects_public_bind() {
        assert_eq!(loopback_port("127.0.0.1:3000"), Some(3000));
        assert_eq!(loopback_port("*:8080"), Some(8080));
        assert_eq!(loopback_port("[::1]:9000"), Some(9000));
        assert_eq!(loopback_port("192.168.1.5:3000"), None);
    }
}