crawlex 1.0.6

Stealth crawler with Chrome-perfect TLS/H2 fingerprint, render pool, hooks, persistent queue
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
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
// Slice 22 — `crawlex shell` interactive REPL.
//
// The shell is a thin coordinator over three already-existing pieces:
//   * `impersonate::ImpersonateClient` — the HTTP/stealth backend used
//     by `.fetch`.
//   * `parser::TreeHandle` — the v2 selector engine that powers `.css`,
//     `.xpath`, `.findByText`, `.findByRegex`.
//   * `storage::adaptive::AdaptiveStore` — the per-spider fingerprint
//     store written by `.save <identifier>`.
//
// Two layers:
//   * `dispatch` is a pure-ish function over `ShellState`. It is the
//     unit-test surface — feed it a stubbed `Fetcher` and assert on the
//     `ShellOutput` returned for each command.
//   * `run_interactive` is the readline loop that wires `dispatch` to
//     stdin/stdout. History is persisted line-by-line in a plain text
//     file so it survives across sessions even though we do not link
//     rustyline (no network to vendor it, and the acceptance criteria
//     only require persistence, not arrow-key recall).

use std::io::{BufRead, Write};
use std::path::PathBuf;
use std::sync::Arc;

use async_trait::async_trait;
use regex::Regex;
use url::Url;

use crate::error::{Error, Result};
use crate::impersonate::{ImpersonateClient, Profile, Response};
use crate::parser::{fingerprint, parse_tree, ElementHandle, Fingerprint, TextMatch};
use crate::storage::adaptive::AdaptiveStore;

/// Network-facing seam — abstracted so unit tests inject canned HTML.
#[async_trait]
pub trait Fetcher: Send + Sync {
    async fn fetch(&self, url: &Url) -> Result<FetchedPage>;
}

/// Stripped-down response used by the shell. We avoid re-exporting the
/// full `impersonate::Response` so tests don't need to construct one.
#[derive(Debug, Clone)]
pub struct FetchedPage {
    pub final_url: Url,
    pub status: u16,
    pub content_type: Option<String>,
    pub body: Vec<u8>,
}

/// Wraps the project's stealth HTTP client. `stealth = true` is the
/// flag from `crawlex shell --stealth`; this implementation uses the
/// same `ImpersonateClient` for both so the flag is currently a wiring
/// placeholder (slice 22 acceptance only requires the flag exists).
pub struct ImpersonateFetcher {
    client: ImpersonateClient,
}

impl ImpersonateFetcher {
    pub fn new(_stealth: bool) -> Result<Self> {
        let client = ImpersonateClient::new(Profile::Chrome149Stable)?;
        Ok(Self { client })
    }
}

#[async_trait]
impl Fetcher for ImpersonateFetcher {
    async fn fetch(&self, url: &Url) -> Result<FetchedPage> {
        let resp: Response = self.client.get(url).await?;
        let content_type = resp
            .headers
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .map(|s| s.to_string());
        Ok(FetchedPage {
            final_url: resp.final_url,
            status: resp.status.as_u16(),
            content_type,
            body: resp.body.to_vec(),
        })
    }
}

/// In-memory state held by one shell session.
pub struct ShellState {
    pub last_url: Option<Url>,
    pub last_body: Option<Vec<u8>>,
    pub last_content_type: Option<String>,
    pub last_status: Option<u16>,
    /// Fingerprint of the most recently selected element. `.save <id>`
    /// writes this into the adaptive store; commands that return at
    /// least one element refresh it.
    pub last_selection: Option<Fingerprint>,
    /// Set by `.findByText` / `.findByRegex` so an operator can recall
    /// the literal query that produced `last_selection`.
    pub last_selection_query: Option<String>,
}

impl ShellState {
    pub fn new() -> Self {
        Self {
            last_url: None,
            last_body: None,
            last_content_type: None,
            last_status: None,
            last_selection: None,
            last_selection_query: None,
        }
    }
}

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

/// What a single command produced. The interactive loop projects this
/// into stdout; tests assert on the variant directly.
#[derive(Debug, Clone, PartialEq)]
pub enum ShellOutput {
    /// Nothing to print (e.g. `.exit` request — the loop terminates).
    Exit,
    /// One free-form line of feedback ("fetched 200 OK, 1234 bytes").
    Status(String),
    /// Multi-line block. Used by every selector verb so the operator
    /// sees all matches in the order they appear in the tree.
    Lines(Vec<String>),
    /// Help text — kept distinct from `Lines` so tests can target it.
    Help(Vec<String>),
    /// Error that should be shown but not terminate the session.
    Err(String),
}

/// Parse + execute one input line against `state`. Public so the
/// integration test in this module can drive the dispatcher without
/// touching stdin.
pub async fn dispatch(
    line: &str,
    state: &mut ShellState,
    fetcher: &dyn Fetcher,
    store: &AdaptiveStore,
) -> ShellOutput {
    let line = line.trim();
    if line.is_empty() {
        return ShellOutput::Status(String::new());
    }
    // Bare expressions (no leading `.`) are reserved for a future
    // embedded-evaluator extension. For slice 22 we explain the dot
    // grammar instead of silently swallowing the line.
    if !line.starts_with('.') {
        return ShellOutput::Err(format!(
            "expected a `.<command>` directive — got `{}`. Type `.help` for the grammar.",
            line
        ));
    }

    let (cmd, rest) = split_cmd(line);
    match cmd {
        ".help" | ".h" | ".?" => ShellOutput::Help(help_lines()),
        ".exit" | ".quit" | ".q" => ShellOutput::Exit,
        ".fetch" => cmd_fetch(rest, state, fetcher).await,
        ".css" => cmd_css(rest, state),
        ".xpath" => cmd_xpath(rest, state),
        ".findByText" => cmd_find_by_text(rest, state),
        ".findByRegex" => cmd_find_by_regex(rest, state),
        ".save" => cmd_save(rest, state, store),
        ".open" => cmd_open(state),
        other => ShellOutput::Err(format!(
            "unknown command `{}` — type `.help` to list the supported verbs.",
            other
        )),
    }
}

fn split_cmd(line: &str) -> (&str, &str) {
    match line.find(char::is_whitespace) {
        Some(i) => (&line[..i], line[i..].trim_start()),
        None => (line, ""),
    }
}

fn help_lines() -> Vec<String> {
    vec![
        "Commands:".into(),
        "  .fetch <url>          fetch a URL into the session".into(),
        "  .css <selector>       CSS query against the last response".into(),
        "  .xpath <expr>         XPath query against the last response".into(),
        "  .findByText <text>    substring text match (descendant search)".into(),
        "  .findByRegex <regex>  regex match against descendant text".into(),
        "  .save <identifier>    persist the last selection's fingerprint".into(),
        "  .open                 open the last response in a browser".into(),
        "  .help                 print this message".into(),
        "  .exit                 leave the shell".into(),
    ]
}

async fn cmd_fetch(rest: &str, state: &mut ShellState, fetcher: &dyn Fetcher) -> ShellOutput {
    if rest.is_empty() {
        return ShellOutput::Err("usage: .fetch <url>".into());
    }
    let url = match Url::parse(rest) {
        Ok(u) => u,
        Err(e) => return ShellOutput::Err(format!("invalid url: {e}")),
    };
    match fetcher.fetch(&url).await {
        Ok(page) => {
            let msg = format!(
                "{} {} ({} bytes, content-type: {})",
                page.status,
                page.final_url,
                page.body.len(),
                page.content_type.as_deref().unwrap_or("?"),
            );
            state.last_url = Some(page.final_url);
            state.last_body = Some(page.body);
            state.last_content_type = page.content_type;
            state.last_status = Some(page.status);
            state.last_selection = None;
            state.last_selection_query = None;
            ShellOutput::Status(msg)
        }
        Err(e) => ShellOutput::Err(format!("fetch failed: {e}")),
    }
}

fn require_body(state: &ShellState) -> std::result::Result<&[u8], ShellOutput> {
    match state.last_body.as_deref() {
        Some(b) => Ok(b),
        None => Err(ShellOutput::Err(
            "no response in the session — run `.fetch <url>` first.".into(),
        )),
    }
}

fn cmd_css(rest: &str, state: &mut ShellState) -> ShellOutput {
    if rest.is_empty() {
        return ShellOutput::Err("usage: .css <selector>".into());
    }
    let body = match require_body(state) {
        Ok(b) => b,
        Err(o) => return o,
    };
    let tree = parse_tree(body, charset_from(state.last_content_type.as_deref()));
    let matches = tree.css(rest);
    record_first(&matches, state, rest);
    pretty_handles(&matches)
}

fn cmd_xpath(rest: &str, state: &mut ShellState) -> ShellOutput {
    if rest.is_empty() {
        return ShellOutput::Err("usage: .xpath <expr>".into());
    }
    let body = match require_body(state) {
        Ok(b) => b,
        Err(o) => return o,
    };
    let tree = parse_tree(body, charset_from(state.last_content_type.as_deref()));
    let matches = tree.xpath(rest);
    record_first(&matches, state, rest);
    pretty_handles(&matches)
}

fn cmd_find_by_text(rest: &str, state: &mut ShellState) -> ShellOutput {
    if rest.is_empty() {
        return ShellOutput::Err("usage: .findByText <text>".into());
    }
    let body = match require_body(state) {
        Ok(b) => b,
        Err(o) => return o,
    };
    let tree = parse_tree(body, charset_from(state.last_content_type.as_deref()));
    let matches = leaf_matches(tree.find_by_text(rest, TextMatch::contains().with_trim(true)));
    record_first(&matches, state, rest);
    pretty_handles(&matches)
}

fn cmd_find_by_regex(rest: &str, state: &mut ShellState) -> ShellOutput {
    if rest.is_empty() {
        return ShellOutput::Err("usage: .findByRegex <pattern>".into());
    }
    let body = match require_body(state) {
        Ok(b) => b,
        Err(o) => return o,
    };
    let re = match Regex::new(rest) {
        Ok(r) => r,
        Err(e) => return ShellOutput::Err(format!("invalid regex: {e}")),
    };
    let tree = parse_tree(body, charset_from(state.last_content_type.as_deref()));
    let matches = leaf_matches(tree.find_by_regex(&re));
    record_first(&matches, state, rest);
    pretty_handles(&matches)
}

fn cmd_save(rest: &str, state: &mut ShellState, store: &AdaptiveStore) -> ShellOutput {
    if rest.is_empty() {
        return ShellOutput::Err("usage: .save <identifier>".into());
    }
    let Some(fp) = state.last_selection.clone() else {
        return ShellOutput::Err(
            "no selection — run `.css`, `.xpath`, `.findByText`, or `.findByRegex` first.".into(),
        );
    };
    let Some(url) = state.last_url.as_ref() else {
        return ShellOutput::Err("no fetched url — `.save` needs a domain.".into());
    };
    let domain = url.host_str().unwrap_or("unknown").to_string();
    match store.save(&domain, rest, fp) {
        Ok(()) => ShellOutput::Status(format!("saved `{rest}` for `{domain}`.")),
        Err(e) => ShellOutput::Err(format!("save failed: {e}")),
    }
}

fn cmd_open(state: &ShellState) -> ShellOutput {
    let Some(body) = state.last_body.as_ref() else {
        return ShellOutput::Err("no response in the session — run `.fetch <url>` first.".into());
    };
    let path = match write_tmp_html(body) {
        Ok(p) => p,
        Err(e) => return ShellOutput::Err(format!("failed to write tmp file: {e}")),
    };
    match try_open_browser(&path) {
        Ok(cmd) => ShellOutput::Status(format!("opened {} via `{}`.", path.display(), cmd)),
        Err(e) => ShellOutput::Err(format!(
            "wrote {} but failed to launch a browser: {e}",
            path.display()
        )),
    }
}

fn charset_from(content_type: Option<&str>) -> Option<&str> {
    let ct = content_type?;
    let idx = ct.to_ascii_lowercase().find("charset=")?;
    let tail = &ct[idx + "charset=".len()..];
    let end = tail
        .find(|c: char| c == ';' || c == ' ' || c == '"')
        .unwrap_or(tail.len());
    Some(&tail[..end])
}

fn record_first(matches: &[ElementHandle<'_>], state: &mut ShellState, query: &str) {
    if let Some(first) = matches.first() {
        state.last_selection = Some(fingerprint(first));
        state.last_selection_query = Some(query.to_string());
    }
}

fn leaf_matches<'a>(matches: Vec<ElementHandle<'a>>) -> Vec<ElementHandle<'a>> {
    matches
        .iter()
        .copied()
        .filter(|candidate| {
            !matches.iter().any(|other| {
                other.inner().id() != candidate.inner().id() && is_descendant_of(*other, *candidate)
            })
        })
        .collect()
}

fn is_descendant_of(mut child: ElementHandle<'_>, ancestor: ElementHandle<'_>) -> bool {
    while let Some(parent) = child.parent() {
        if parent.inner().id() == ancestor.inner().id() {
            return true;
        }
        child = parent;
    }
    false
}

fn pretty_handles(matches: &[ElementHandle<'_>]) -> ShellOutput {
    if matches.is_empty() {
        return ShellOutput::Lines(vec!["(no matches)".into()]);
    }
    let mut out = Vec::with_capacity(matches.len() + 1);
    out.push(format!("{} match(es):", matches.len()));
    for (i, h) in matches.iter().enumerate() {
        // Trim outerHTML so a huge `<body>` match doesn't drown the prompt.
        let html = h.html();
        let snippet: String = html.chars().take(200).collect();
        let suffix = if html.len() > snippet.len() {
            ""
        } else {
            ""
        };
        out.push(format!("  [{i}] <{}> {}{}", h.name(), snippet, suffix));
    }
    ShellOutput::Lines(out)
}

fn write_tmp_html(body: &[u8]) -> std::io::Result<PathBuf> {
    let mut path = std::env::temp_dir();
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    path.push(format!("crawlex-shell-{nanos}.html"));
    std::fs::write(&path, body)?;
    Ok(path)
}

/// Best-effort browser launch. We try each platform-typical opener in
/// turn and return the one that exited 0. The user only needs *some*
/// browser to fire; the loop continues either way.
fn try_open_browser(path: &std::path::Path) -> std::result::Result<&'static str, String> {
    let candidates: &[&'static str] = if cfg!(target_os = "macos") {
        &["open"]
    } else if cfg!(target_os = "windows") {
        &["cmd"]
    } else {
        &["xdg-open", "gio", "wslview"]
    };
    let mut last_err = String::new();
    for bin in candidates {
        let mut cmd = std::process::Command::new(bin);
        if *bin == "gio" {
            cmd.arg("open");
        } else if *bin == "cmd" {
            cmd.args(["/C", "start", ""]);
        }
        cmd.arg(path);
        match cmd.spawn() {
            Ok(_) => return Ok(bin),
            Err(e) => last_err = format!("{bin}: {e}"),
        }
    }
    Err(last_err)
}

// ─────────────────────────────────────────────────────────────────────
// Interactive loop
// ─────────────────────────────────────────────────────────────────────

pub struct ShellOptions {
    pub stealth: bool,
    pub history_file: Option<PathBuf>,
    pub adaptive_dir: PathBuf,
    pub spider_id: String,
}

impl ShellOptions {
    pub fn default_history_path() -> PathBuf {
        if let Some(dir) = std::env::var_os("XDG_DATA_HOME") {
            return PathBuf::from(dir).join("crawlex").join("shell_history");
        }
        if let Some(home) = std::env::var_os("HOME") {
            return PathBuf::from(home).join(".local/share/crawlex/shell_history");
        }
        PathBuf::from("./.crawlex/shell_history")
    }
}

pub async fn run_interactive(opts: ShellOptions) -> Result<()> {
    let fetcher: Arc<dyn Fetcher> = Arc::new(ImpersonateFetcher::new(opts.stealth)?);
    let store = AdaptiveStore::open(&opts.adaptive_dir, &opts.spider_id).map_err(Error::Io)?;

    let history_path = opts
        .history_file
        .clone()
        .unwrap_or_else(ShellOptions::default_history_path);
    if let Some(parent) = history_path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }

    let mut state = ShellState::new();
    let stdin = std::io::stdin();
    let mut stdout = std::io::stdout();
    let _ = writeln!(
        stdout,
        "crawlex shell — type `.help` for commands, `.exit` to leave."
    );

    let mut line = String::new();
    loop {
        write!(stdout, "crawlex> ").ok();
        stdout.flush().ok();
        line.clear();
        let n = stdin.lock().read_line(&mut line).map_err(Error::Io)?;
        if n == 0 {
            // EOF — same as `.exit`.
            let _ = writeln!(stdout);
            break;
        }
        let raw = line.trim_end_matches(['\n', '\r']).to_string();
        if !raw.trim().is_empty() {
            append_history(&history_path, &raw);
        }
        let out = dispatch(&raw, &mut state, fetcher.as_ref(), &store).await;
        match out {
            ShellOutput::Exit => break,
            ShellOutput::Status(s) => {
                if !s.is_empty() {
                    let _ = writeln!(stdout, "{s}");
                }
            }
            ShellOutput::Lines(lines) | ShellOutput::Help(lines) => {
                for l in lines {
                    let _ = writeln!(stdout, "{l}");
                }
            }
            ShellOutput::Err(e) => {
                let _ = writeln!(stdout, "error: {e}");
            }
        }
    }
    Ok(())
}

fn append_history(path: &std::path::Path, line: &str) {
    if let Ok(mut f) = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
    {
        let _ = writeln!(f, "{line}");
    }
}

/// Public so `crawlex shell --history-file path` callers can read the
/// stored history. The format is one verbatim line per entry.
pub fn read_history(path: &std::path::Path) -> Vec<String> {
    let Ok(f) = std::fs::File::open(path) else {
        return Vec::new();
    };
    std::io::BufReader::new(f)
        .lines()
        .map_while(|r| r.ok())
        .collect()
}

// ─────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use std::collections::HashMap;
    use std::sync::Mutex;
    use tempfile::tempdir;

    struct StubFetcher {
        pages: Mutex<HashMap<String, FetchedPage>>,
    }
    impl StubFetcher {
        fn new() -> Self {
            Self {
                pages: Mutex::new(HashMap::new()),
            }
        }
        fn insert(&self, url: &str, html: &str) {
            let parsed = Url::parse(url).unwrap();
            self.pages.lock().unwrap().insert(
                url.to_string(),
                FetchedPage {
                    final_url: parsed,
                    status: 200,
                    content_type: Some("text/html; charset=utf-8".into()),
                    body: html.as_bytes().to_vec(),
                },
            );
        }
    }
    #[async_trait]
    impl Fetcher for StubFetcher {
        async fn fetch(&self, url: &Url) -> Result<FetchedPage> {
            self.pages
                .lock()
                .unwrap()
                .get(url.as_str())
                .cloned()
                .ok_or_else(|| Error::Http(format!("stub: no page for {url}")))
        }
    }

    fn store() -> (tempfile::TempDir, AdaptiveStore) {
        let dir = tempdir().unwrap();
        let s = AdaptiveStore::open(dir.path(), "test").unwrap();
        (dir, s)
    }

    const HTML: &str = "<!doctype html><html><body>\
        <p class='greet'>hello world</p>\
        <a href='/x' id='cta'>Buy now</a>\
        <ul><li>alpha</li><li>beta</li></ul></body></html>";

    #[tokio::test]
    async fn fetch_populates_state() {
        let fetcher = StubFetcher::new();
        fetcher.insert("https://example.com/", HTML);
        let (_t, s) = store();
        let mut state = ShellState::new();
        let out = dispatch(".fetch https://example.com/", &mut state, &fetcher, &s).await;
        assert!(matches!(&out, ShellOutput::Status(_)), "got {out:?}");
        assert_eq!(state.last_status, Some(200));
        assert!(state.last_body.is_some());
    }

    #[tokio::test]
    async fn css_returns_matches_and_records_selection() {
        let fetcher = StubFetcher::new();
        fetcher.insert("https://example.com/", HTML);
        let (_t, s) = store();
        let mut state = ShellState::new();
        dispatch(".fetch https://example.com/", &mut state, &fetcher, &s).await;
        let out = dispatch(".css p.greet", &mut state, &fetcher, &s).await;
        match out {
            ShellOutput::Lines(lines) => {
                assert!(lines[0].starts_with("1 match"), "lines={lines:?}");
                assert!(lines[1].contains("hello world"));
            }
            other => panic!("expected Lines, got {other:?}"),
        }
        assert!(state.last_selection.is_some());
    }

    #[tokio::test]
    async fn xpath_returns_matches() {
        let fetcher = StubFetcher::new();
        fetcher.insert("https://example.com/", HTML);
        let (_t, s) = store();
        let mut state = ShellState::new();
        dispatch(".fetch https://example.com/", &mut state, &fetcher, &s).await;
        let out = dispatch(".xpath //li", &mut state, &fetcher, &s).await;
        let ShellOutput::Lines(lines) = out else {
            panic!("expected Lines");
        };
        assert!(lines[0].starts_with("2 match"), "lines={lines:?}");
    }

    #[tokio::test]
    async fn find_by_text_and_regex_record_selection() {
        let fetcher = StubFetcher::new();
        fetcher.insert("https://example.com/", HTML);
        let (_t, s) = store();
        let mut state = ShellState::new();
        dispatch(".fetch https://example.com/", &mut state, &fetcher, &s).await;
        let out = dispatch(".findByText Buy now", &mut state, &fetcher, &s).await;
        let ShellOutput::Lines(lines) = out else {
            panic!("findByText output");
        };
        assert!(lines[0].starts_with("1 match"), "lines={lines:?}");
        assert!(state.last_selection.is_some());

        let out = dispatch(".findByRegex ^alpha$", &mut state, &fetcher, &s).await;
        let ShellOutput::Lines(lines) = out else {
            panic!("findByRegex output");
        };
        assert!(lines[0].starts_with("1 match"), "lines={lines:?}");
    }

    #[tokio::test]
    async fn save_persists_fingerprint() {
        let fetcher = StubFetcher::new();
        fetcher.insert("https://example.com/", HTML);
        let dir = tempdir().unwrap();
        let s = AdaptiveStore::open(dir.path(), "test").unwrap();
        let mut state = ShellState::new();
        dispatch(".fetch https://example.com/", &mut state, &fetcher, &s).await;
        dispatch(".css a#cta", &mut state, &fetcher, &s).await;
        let out = dispatch(".save buy_button", &mut state, &fetcher, &s).await;
        assert!(matches!(&out, ShellOutput::Status(_)), "got {out:?}");
        assert!(s.retrieve("example.com", "buy_button").is_some());
    }

    #[tokio::test]
    async fn save_without_selection_errors() {
        let fetcher = StubFetcher::new();
        let (_t, s) = store();
        let mut state = ShellState::new();
        let out = dispatch(".save x", &mut state, &fetcher, &s).await;
        assert!(matches!(out, ShellOutput::Err(_)));
    }

    #[tokio::test]
    async fn unknown_and_help() {
        let fetcher = StubFetcher::new();
        let (_t, s) = store();
        let mut state = ShellState::new();
        let out = dispatch(".help", &mut state, &fetcher, &s).await;
        assert!(matches!(out, ShellOutput::Help(_)));
        let out = dispatch(".wat", &mut state, &fetcher, &s).await;
        assert!(matches!(out, ShellOutput::Err(_)));
        let out = dispatch("foo bar", &mut state, &fetcher, &s).await;
        assert!(matches!(out, ShellOutput::Err(_)));
    }

    #[test]
    fn history_round_trip() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("h");
        append_history(&path, ".fetch https://example.com/");
        append_history(&path, ".css p");
        let got = read_history(&path);
        assert_eq!(got, vec![".fetch https://example.com/", ".css p"]);
    }
}