croft-software 0.1.758

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
//! Locate a test's source location by name. libtest's `--list` gives no file or
//! line (the JSON format that does is nightly-only), so we find the `fn` in the
//! source tree instead: walk the workspace (honouring `.gitignore` even outside
//! a git checkout, with `target`/`node_modules` always skipped), grep for
//! `fn <leaf>`, and rank candidates by how well the file path matches the
//! test's module path. Good enough to jump the editor there. pytest and JS
//! node IDs carry their file, so those skip the walk and grep just that file.

use std::path::{Path, PathBuf};

use grep_regex::RegexMatcher;
use grep_searcher::{BinaryDetection, MmapChoice, Searcher, SearcherBuilder, Sink, SinkMatch};
use ignore::WalkBuilder;

const RUST_EXT: &str = "rs";

/// Records the first matching line in a file and stops scanning it.
struct FirstLine(Option<u64>);

impl Sink for FirstLine {
    type Error = std::io::Error;
    fn matched(&mut self, _s: &Searcher, mat: &SinkMatch<'_>) -> Result<bool, std::io::Error> {
        self.0 = mat.line_number();
        Ok(false) // one hit is enough
    }
}

/// The source location `(path, line)` of the test named `full_name` (e.g.
/// `widgets::testing::tests::foo`), or `None` if no `fn` is found. `line` is
/// 0-based, ready for [`crate::app`]'s go-to-definition. pytest node IDs
/// (`tests/test_x.py::test_y`) carry their file, so those skip the walk and
/// grep just that file for the `def`.
pub fn find_test_source(root: &Path, full_name: &str) -> Option<(PathBuf, u32)> {
    if full_name.contains(".py::") {
        return pytest_source(root, full_name);
    }
    if let Some((file, _)) = full_name.split_once("::")
        && crate::testing::parse::is_js_test_file(file)
    {
        return js_source(root, full_name);
    }
    let mut segments: Vec<&str> = full_name.split("::").collect();
    let leaf = segments.pop()?;
    // Module segments minus the conventional `tests` wrapper: these are what a
    // file path is scored against (e.g. `widgets::testing` -> src/widgets/testing.rs).
    let module: Vec<&str> = segments.into_iter().filter(|s| *s != "tests").collect();

    // `fn <leaf>` followed by `(`, `<` (generics) or whitespace. leaf is
    // escaped: it is normally a Rust identifier, but never trust a runner.
    let matcher =
        RegexMatcher::new(&format!(r"\bfn\s+{}\s*[(<\s]", super::regex_escape(leaf))).ok()?;
    let mut searcher = SearcherBuilder::new()
        .line_number(true)
        .binary_detection(BinaryDetection::quit(b'\x00'))
        .memory_map(MmapChoice::never())
        .build();

    let mut best: Option<(PathBuf, u32, usize)> = None; // (path, line, score)
    // `require_git(false)`: an extracted tarball or vendored drop has no
    // .git, but its .gitignore still marks the generated trees. target/ and
    // node_modules/ are skipped unconditionally — build output can define a
    // same-named fn and win the path score.
    for entry in WalkBuilder::new(root)
        .require_git(false)
        .filter_entry(|e| {
            !(e.file_type().is_some_and(|t| t.is_dir())
                && matches!(e.file_name().to_str(), Some("target" | "node_modules")))
        })
        .build()
        .flatten()
    {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some(RUST_EXT) {
            continue;
        }
        let mut sink = FirstLine(None);
        if searcher.search_path(&matcher, path, &mut sink).is_err() {
            continue;
        }
        let Some(line1) = sink.0 else { continue };
        let score = path_score(path, &module);
        let line0 = line1.saturating_sub(1) as u32;
        if best.as_ref().is_none_or(|(_, _, s)| score > *s) {
            best = Some((path.to_path_buf(), line0, score));
        }
    }
    best.map(|(p, l, _)| (p, l))
}

/// Resolve a pytest node ID: the segment before `.py::` is the file (relative
/// to the workspace root), the last segment the test name — its parametrize
/// suffix (`[1]`) stripped, since the source `def` carries no bracket.
fn pytest_source(root: &Path, full_name: &str) -> Option<(PathBuf, u32)> {
    let (file, rest) = full_name.split_once(".py::")?;
    let path = root.join(format!("{file}.py"));
    let leaf = rest.rsplit("::").next()?.split('[').next()?;
    let matcher =
        RegexMatcher::new(&format!(r"\bdef\s+{}\s*\(", super::regex_escape(leaf))).ok()?;
    let mut searcher = SearcherBuilder::new()
        .line_number(true)
        .binary_detection(BinaryDetection::quit(b'\x00'))
        .memory_map(MmapChoice::never())
        .build();
    let mut sink = FirstLine(None);
    searcher.search_path(&matcher, &path, &mut sink).ok()?;
    let line1 = sink.0?;
    Some((path, line1.saturating_sub(1) as u32))
}

/// Resolve a JS node ID (`file::describe...::title`): the first segment is the
/// test file, the last the title — an arbitrary string, so it is escaped and
/// matched literally. The title alone can also live in a comment or fixture
/// above the test, so a `test(`/`it(` declaration carrying it wins; the bare
/// title is only a fallback (`xit`, a title split across lines).
fn js_source(root: &Path, full_name: &str) -> Option<(PathBuf, u32)> {
    let (file, rest) = full_name.split_once("::")?;
    let path = root.join(file);
    let leaf = rest.rsplit("::").next()?;
    let escaped = super::regex_escape(leaf);
    let mut searcher = SearcherBuilder::new()
        .line_number(true)
        .binary_detection(BinaryDetection::quit(b'\x00'))
        .memory_map(MmapChoice::never())
        .build();
    let decl = RegexMatcher::new(&format!(r#"\b(?:test|it)\b[^'"`]*['"`]{escaped}"#)).ok()?;
    let mut sink = FirstLine(None);
    searcher.search_path(&decl, &path, &mut sink).ok()?;
    let line1 = match sink.0 {
        Some(l) => l,
        None => {
            let bare = RegexMatcher::new(&escaped).ok()?;
            let mut sink = FirstLine(None);
            searcher.search_path(&bare, &path, &mut sink).ok()?;
            sink.0?
        }
    };
    Some((path, line1.saturating_sub(1) as u32))
}

/// The name of the nearest `fn` at or above `cursor_row` — the test the caret
/// sits in, for run-at-cursor. Scans upward and returns the first `fn <ident>`
/// it finds. `None` if the caret is above every function, or the buffer empty.
pub fn enclosing_fn_name(lines: &[String], cursor_row: usize) -> Option<String> {
    let start = cursor_row.min(lines.len().checked_sub(1)?);
    for line in lines[..=start].iter().rev() {
        if let Some(name) = fn_name_in(line) {
            return Some(name);
        }
    }
    None
}

/// The name of the TEST function whose definition starts on `lines[idx]`, or
/// `None` when the line defines no function or the function is not a test.
/// Backs the editor's gutter play glyph. Python follows pytest's collection
/// convention (`def test_*` in a `test_*.py` / `*_test.py` file); Rust
/// requires a `test` attribute (`#[test]`, `#[tokio::test]`, `#[rstest]`,
/// ...) in the contiguous attribute block directly above the `fn`. `path` is
/// the buffer's file: an unsaved buffer gets no beads (nothing could run
/// them), and the extension picks the language rules.
pub fn test_fn_on_line(path: Option<&Path>, lines: &[String], idx: usize) -> Option<String> {
    let file = path?.file_name()?.to_str()?;
    let line = lines.get(idx)?;
    if file.ends_with(".py") {
        let name = name_after(line, "def ")?;
        // pytest's default collection reads test_*.py / *_test.py only: a
        // `def test_connection()` in production code gets no bead.
        let collectable = file.starts_with("test_") || file.ends_with("_test.py");
        return (collectable && name.starts_with("test")).then_some(name);
    }
    if !file.ends_with(".rs") {
        return None;
    }
    let name = name_after(line, "fn ")?;
    for above in lines[..idx].iter().rev() {
        let t = above.trim_start();
        if !t.starts_with("#[") {
            break;
        }
        if attr_marks_test(t) {
            return Some(name);
        }
    }
    None
}

/// Whether an attribute line marks a test fn: the attribute path's LAST
/// segment IS `test` (`#[test]`, `#[tokio::test]`, `#[googletest::test]`)
/// or `rstest` — never a mere suffix. `#[contest]`/`#[mytest]` are ordinary
/// attributes cargo does not collect, and a bead on them runs zero tests.
/// `#[cfg(test)]` and `#[cfg_attr(test, ...)]` are configuration (path
/// segment `cfg`/`cfg_attr`), and treating them as test markers put a play
/// bead on every `#[cfg(test)] fn helper()` in a codebase.
fn attr_marks_test(attr_line: &str) -> bool {
    let inner = attr_line.trim_start_matches("#[");
    let name_end = inner
        .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == ':'))
        .unwrap_or(inner.len());
    inner[..name_end]
        .rsplit("::")
        .next()
        .is_some_and(|seg| seg == "test" || seg == "rstest")
}

/// Extract the identifier after a function keyword (`fn ` for Rust, `def ` for
/// Python) in a single line, or `None`. Deliberately simple: it does not skip
/// keywords inside comments or strings, which is a rare enough case for a
/// run-at-cursor convenience that it isn't worth a parser.
fn fn_name_in(line: &str) -> Option<String> {
    ["fn ", "def "].iter().find_map(|kw| name_after(line, kw))
}

fn name_after(line: &str, keyword: &str) -> Option<String> {
    let idx = line.find(keyword)?;
    let name: String = line[idx + keyword.len()..]
        .chars()
        .take_while(|c| c.is_alphanumeric() || *c == '_')
        .collect();
    // The keyword must sit on a word boundary (start of line or after
    // whitespace), and the name non-empty — guards against `define_fn foo`.
    let before = &line[..idx];
    let boundary = before.is_empty() || before.ends_with(|c: char| c.is_whitespace());
    (boundary && !name.is_empty()).then_some(name)
}

/// How many module segments appear as components of `path` — the more, the more
/// likely this file defines the test (disambiguates same-named fns).
fn path_score(path: &Path, module: &[&str]) -> usize {
    let components: Vec<&str> = path
        .components()
        .filter_map(|c| c.as_os_str().to_str())
        .collect();
    module
        .iter()
        .filter(|seg| {
            // A module segment matches a path component, or the file stem (the
            // last module segment is usually the file name, e.g. `testing.rs`).
            components
                .iter()
                .any(|c| c == *seg || c.strip_suffix(".rs") == Some(seg))
        })
        .count()
}

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

    #[test]
    fn finds_the_fn_and_prefers_the_module_matching_path() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        // A decoy in an unrelated file, and the real one under the module path.
        std::fs::create_dir_all(root.join("src/other")).unwrap();
        std::fs::create_dir_all(root.join("src/widgets")).unwrap();
        std::fs::write(
            root.join("src/other/misc.rs"),
            "fn helper() {}\nfn target_test() { /* decoy */ }\n",
        )
        .unwrap();
        std::fs::write(
            root.join("src/widgets/testing.rs"),
            "mod tests {\n    fn setup() {}\n    fn target_test() { assert!(true); }\n}\n",
        )
        .unwrap();

        let (path, line) = find_test_source(root, "widgets::testing::tests::target_test").unwrap();
        assert!(
            path.ends_with("src/widgets/testing.rs"),
            "the module-matching file wins over the decoy, got {path:?}"
        );
        assert_eq!(line, 2, "0-based line of `fn target_test` in testing.rs");
    }

    #[test]
    fn enclosing_fn_scans_upward_to_the_nearest_fn() {
        let lines: Vec<String> = [
            "mod tests {",
            "    fn first() {",
            "        assert!(true);", // cursor here -> first
            "    }",
            "    fn second() {",
            "        assert!(false);", // cursor here -> second
            "    }",
            "}",
        ]
        .iter()
        .map(|s| s.to_string())
        .collect();

        assert_eq!(enclosing_fn_name(&lines, 2).as_deref(), Some("first"));
        assert_eq!(enclosing_fn_name(&lines, 5).as_deref(), Some("second"));
        // Above every fn: nothing to run.
        assert_eq!(enclosing_fn_name(&lines, 0), None);
        // A word merely ending in "fn" is not a keyword boundary.
        let noise = vec![String::from("let define_fn_thing = 3;")];
        assert_eq!(enclosing_fn_name(&noise, 0), None);
    }

    #[test]
    fn pytest_node_ids_resolve_to_the_def_in_their_file() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        std::fs::create_dir_all(root.join("tests")).unwrap();
        std::fs::write(
            root.join("tests/test_sample.py"),
            "import pytest\n\ndef test_passes():\n    assert True\n\nclass TestGroup:\n    def test_method(self):\n        assert True\n",
        )
        .unwrap();

        let (path, line) = find_test_source(root, "tests/test_sample.py::test_passes").unwrap();
        assert!(path.ends_with("tests/test_sample.py"), "got {path:?}");
        assert_eq!(line, 2, "0-based line of `def test_passes`");
        let (_, line) =
            find_test_source(root, "tests/test_sample.py::TestGroup::test_method").unwrap();
        assert_eq!(line, 6, "class methods resolve through the last segment");
        // Parametrized IDs strip their bracket suffix before the lookup.
        let (_, line) = find_test_source(root, "tests/test_sample.py::test_passes[1]").unwrap();
        assert_eq!(line, 2);
        // A node ID pointing at a missing file finds nothing.
        assert!(find_test_source(root, "tests/gone.py::test_x").is_none());
    }

    #[test]
    fn enclosing_fn_understands_python_defs() {
        let lines: Vec<String> = [
            "import pytest",
            "def test_first():",
            "    assert True", // cursor here -> test_first
            "class TestGroup:",
            "    def test_method(self):",
            "        assert True", // cursor here -> test_method
        ]
        .iter()
        .map(|s| s.to_string())
        .collect();
        assert_eq!(enclosing_fn_name(&lines, 2).as_deref(), Some("test_first"));
        assert_eq!(enclosing_fn_name(&lines, 5).as_deref(), Some("test_method"));
        assert_eq!(enclosing_fn_name(&lines, 0), None);
    }

    #[test]
    fn an_empty_buffer_has_no_enclosing_fn() {
        // Cmd+K Enter with no file open: Editor::new() holds zero lines, and
        // this must be a clean None, not a slice-range panic.
        assert_eq!(enclosing_fn_name(&[], 0), None);
        assert_eq!(enclosing_fn_name(&[], 5), None);
    }

    #[test]
    fn gitignore_is_honoured_even_outside_a_git_repo() {
        // An extracted tarball or vendored drop has no .git; its .gitignore
        // must still keep generated trees out of the jump-target search.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        std::fs::write(root.join(".gitignore"), "gen/\n").unwrap();
        std::fs::create_dir_all(root.join("gen/widgets")).unwrap();
        std::fs::create_dir_all(root.join("src")).unwrap();
        // The ignored decoy outscores the real file on the module path.
        std::fs::write(root.join("gen/widgets/parse.rs"), "fn target_case() {}\n").unwrap();
        std::fs::write(root.join("src/lib.rs"), "fn target_case() {}\n").unwrap();

        let (path, _) = find_test_source(root, "widgets::parse::tests::target_case").unwrap();
        assert!(
            path.ends_with("src/lib.rs"),
            "an ignored dir must never win the jump target, got {path:?}"
        );
    }

    #[test]
    fn build_output_dirs_are_skipped_even_without_a_gitignore() {
        // No .git AND no .gitignore: target/ and node_modules/ still hold
        // only generated or vendored code and must never be jump targets.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        std::fs::create_dir_all(root.join("target/debug")).unwrap();
        std::fs::create_dir_all(root.join("node_modules/pkg")).unwrap();
        std::fs::create_dir_all(root.join("src")).unwrap();
        std::fs::write(root.join("target/debug/parse.rs"), "fn target_case() {}\n").unwrap();
        std::fs::write(
            root.join("node_modules/pkg/parse.rs"),
            "fn target_case() {}\n",
        )
        .unwrap();
        std::fs::write(root.join("src/lib.rs"), "fn target_case() {}\n").unwrap();

        let (path, _) = find_test_source(root, "widgets::parse::tests::target_case").unwrap();
        assert!(
            path.ends_with("src/lib.rs"),
            "build output must never win the jump target, got {path:?}"
        );
    }

    #[test]
    fn js_test_titles_resolve_to_their_file_and_line() {
        // vitest/jest node IDs carry their file like pytest's, and the title
        // is an arbitrary string: regex metacharacters are literals.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        std::fs::create_dir_all(root.join("src")).unwrap();
        std::fs::write(
            root.join("src/math.test.ts"),
            "import { test } from 'vitest'\n\ntest('adds (1 + 1)', () => {})\n\ntest('parses [ tokens', () => {})\n",
        )
        .unwrap();

        let (path, line) = find_test_source(root, "src/math.test.ts::adds (1 + 1)").unwrap();
        assert!(path.ends_with("src/math.test.ts"), "got {path:?}");
        assert_eq!(line, 2, "0-based line of the first test title");
        // A describe chain still resolves through the last segment, and an
        // unbalanced bracket must not abort the regex build.
        let (_, line) = find_test_source(root, "src/math.test.ts::suite::parses [ tokens").unwrap();
        assert_eq!(line, 4);
    }

    #[test]
    fn a_title_in_a_comment_above_the_test_does_not_hijack_the_jump() {
        // The title text often appears before the declaration — a comment, a
        // fixture string — and the first bare match would land the jump
        // there. The `test(`/`it(` line carrying the title must win.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        std::fs::write(
            root.join("math.test.js"),
            "// adds (1 + 1) covers the carry path\nconst fixture = 'adds (1 + 1)'\n\ntest('adds (1 + 1)', () => {})\n",
        )
        .unwrap();
        let (_, line) = find_test_source(root, "math.test.js::adds (1 + 1)").unwrap();
        assert_eq!(line, 3, "the declaration, not the comment or the fixture");
    }

    #[test]
    fn a_title_only_in_an_exotic_declaration_still_resolves() {
        // `xit(` has no `test`/`it` word boundary, so the declaration-shaped
        // match finds nothing; the bare-title fallback must still resolve
        // instead of reporting no source.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        std::fs::write(
            root.join("a.test.js"),
            "xit('odd little title', () => {})\n",
        )
        .unwrap();
        let (_, line) = find_test_source(root, "a.test.js::odd little title").unwrap();
        assert_eq!(line, 0);
    }

    #[test]
    fn returns_none_when_the_fn_is_absent() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("a.rs"), "fn something_else() {}\n").unwrap();
        assert!(find_test_source(tmp.path(), "m::nope").is_none());
    }

    #[test]
    fn test_fn_on_line_detects_rust_test_attributes() {
        let lines: Vec<String> = [
            "fn helper() {}",
            "#[test]",
            "fn plain_case() {}",
            "#[rstest]",
            "#[tokio::test(flavor = \"multi_thread\")]",
            "async fn async_case() {}",
            "#[derive(Debug)]",
            "fn derived_not_a_test() {}",
        ]
        .iter()
        .map(|s| s.to_string())
        .collect();
        let rs = Some(Path::new("a.rs"));
        assert_eq!(
            test_fn_on_line(rs, &lines, 0),
            None,
            "no attribute, no test"
        );
        assert_eq!(
            test_fn_on_line(rs, &lines, 2).as_deref(),
            Some("plain_case")
        );
        assert_eq!(
            test_fn_on_line(rs, &lines, 5).as_deref(),
            Some("async_case"),
            "tokio::test in a stacked attribute block must count"
        );
        assert_eq!(
            test_fn_on_line(rs, &lines, 7),
            None,
            "a non-test attribute above a fn must not count"
        );
        assert_eq!(
            test_fn_on_line(rs, &lines, 1),
            None,
            "attribute lines define no fn"
        );
    }

    #[test]
    fn cfg_test_is_configuration_not_a_test_attribute() {
        // croft's own source has dozens of `#[cfg(test)] fn helper()` sites
        // (clipboard.rs, port_detect.rs, overlay.rs...). A bead on those
        // runs `cargo test <helper_name>`, matches nothing, and wipes the
        // Testing tree with an empty run.
        let lines: Vec<String> = [
            "#[cfg(test)]",
            "pub fn clear_native() {}",
            "#[cfg(not(test))]",
            "fn scan() {}",
            "#[cfg_attr(test, derive(Clone))]",
            "fn configured() {}",
        ]
        .iter()
        .map(|s| s.to_string())
        .collect();
        let rs = Some(Path::new("a.rs"));
        assert_eq!(
            test_fn_on_line(rs, &lines, 1),
            None,
            "#[cfg(test)] gates compilation; it does not mark a test"
        );
        assert_eq!(test_fn_on_line(rs, &lines, 3), None);
        assert_eq!(
            test_fn_on_line(rs, &lines, 5),
            None,
            "cfg_attr is configuration"
        );
    }

    #[test]
    fn suffix_named_attributes_are_not_test_markers() {
        // `#[contest]` / `#[mytest]` are ordinary proc-macro attributes
        // cargo never collects; a play bead on them runs zero tests. Only
        // the exact `test` segment (bare or path-qualified) and `rstest`
        // mark a test.
        let lines: Vec<String> = [
            "#[contest]",
            "fn fake_case() {}",
            "#[mytest]",
            "fn other_fake_case() {}",
            "#[googletest::test]",
            "fn qualified_case() {}",
        ]
        .iter()
        .map(|s| s.to_string())
        .collect();
        let rs = Some(Path::new("a.rs"));
        assert_eq!(
            test_fn_on_line(rs, &lines, 1),
            None,
            "#[contest] merely ends in `test`"
        );
        assert_eq!(
            test_fn_on_line(rs, &lines, 3),
            None,
            "#[mytest] merely ends in `test`"
        );
        assert_eq!(
            test_fn_on_line(rs, &lines, 5).as_deref(),
            Some("qualified_case"),
            "a path-qualified exact `test` segment still counts"
        );
    }

    #[test]
    fn test_fn_on_line_detects_pytest_defs_by_name() {
        let lines: Vec<String> = [
            "def test_first():",
            "def helper():",
            "    async def test_inner(self):",
        ]
        .iter()
        .map(|s| s.to_string())
        .collect();
        let tf = Some(Path::new("test_app.py"));
        assert_eq!(
            test_fn_on_line(tf, &lines, 0).as_deref(),
            Some("test_first")
        );
        assert_eq!(
            test_fn_on_line(tf, &lines, 1),
            None,
            "pytest only collects test_*"
        );
        assert_eq!(
            test_fn_on_line(tf, &lines, 2).as_deref(),
            Some("test_inner")
        );
        assert_eq!(
            test_fn_on_line(Some(Path::new("app_test.py")), &lines, 0).as_deref(),
            Some("test_first"),
            "the *_test.py collection form counts too"
        );
        assert_eq!(
            test_fn_on_line(Some(Path::new("app.py")), &lines, 0),
            None,
            "a def test_* in production code (non-collectable file) gets no bead"
        );
        assert_eq!(
            test_fn_on_line(None, &lines, 0),
            None,
            "an unsaved buffer has nothing a runner could target"
        );
    }
}