jump-core 2.1.2

Search engine, ranking, and plugin host for the jump launcher
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
// Copyright 2026 entro314-labs
// SPDX-License-Identifier: MPL-2.0

//! Full-text search inside files, on SQLite FTS5.
//!
//! ## Why this is a separate index from [`crate::files`]
//!
//! The two have opposite lifecycles. The path index is cheap to throw away and
//! rebuild wholesale — 8.75 s for a home directory — so it is simply rebuilt on
//! a timer. Content is the reverse: extracting text is expensive, so it must be
//! *incremental*, touching only files whose `(mtime, size)` changed since last
//! time. Sharing one store would force the expensive half to follow the cheap
//! half's refresh strategy.
//!
//! ## Why FTS5 rather than a vector index
//!
//! Launcher queries are overwhelmingly known-item: the user knows roughly what
//! the file is called or what phrase is in it. That is lexical retrieval, which
//! BM25 does well and cheaply. Embeddings answer a different question —
//! semantic recall — and would put a model inference on every keystroke, which
//! is irreconcilable with a sub-millisecond result path. If semantic ranking is
//! ever wanted, the shape is a reranker over the top ~50 rows *of this index*,
//! not a replacement for it.
//!
//! ## Bounds
//!
//! Indexing user documents can pull in an unbounded amount of data, so every
//! axis is capped: file size, extensions, and total documents. Extraction is
//! plain-text only — binary formats need external tools, which are a
//! configuration and failure surface this does not take on.

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

use rusqlite::{Connection, OptionalExtension, params};

use crate::model::{Icon, Item, ItemKey, Source};

/// Extensions whose contents are indexed.
///
/// Text-like formats only. Anything requiring an external extractor is out of
/// scope: a launcher that shells out to `pdftotext` inherits its failure modes.
pub const DEFAULT_EXTENSIONS: &[&str] = &[
    "md",
    "txt",
    "rst",
    "org",
    "adoc",
    "tex",
    "log",
    "csv",
    "tsv",
    "json",
    "toml",
    "yaml",
    "yml",
    "ini",
    "conf",
    "cfg",
    "xml",
    "html",
    "css",
    "scss",
    "js",
    "ts",
    "jsx",
    "tsx",
    "rs",
    "py",
    "go",
    "rb",
    "sh",
    "bash",
    "zsh",
    "fish",
    "c",
    "h",
    "cpp",
    "hpp",
    "java",
    "kt",
    "swift",
    "sql",
    "lua",
    "vim",
    "nix",
    "dockerfile",
    "gradle",
    "properties",
    "env",
];

/// Size limits for the content index.
#[derive(Debug, Clone, Copy)]
pub struct Limits {
    /// Stop indexing once the database reaches this size.
    pub max_index_bytes: u64,
    /// Skip files larger than this.
    ///
    /// Beyond a couple of megabytes a text file is almost always generated — a
    /// log, a dump, a minified bundle — and indexing it costs far more than it
    /// returns.
    pub max_file_bytes: u64,
}

impl Default for Limits {
    fn default() -> Self {
        Self {
            max_index_bytes: 512 * 1024 * 1024,
            max_file_bytes: 2 * 1024 * 1024,
        }
    }
}

/// Characters of extracted text stored per document.
const MAX_TEXT_CHARS: usize = 200_000;

/// Ceiling on indexed documents, so a pathological tree cannot grow the
/// database without limit.
const MAX_DOCUMENTS: usize = 200_000;

/// Results returned per query.
const RESULTS: usize = 8;

/// Documents written per transaction. See [`Content::index`].
const BATCH: usize = 400;

/// Why a content-index operation failed.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// The database could not be opened or its schema not created.
    #[error("could not open the content index")]
    Open(#[source] rusqlite::Error),
    /// A query against an open index failed.
    #[error("content index query failed")]
    Query(#[source] rusqlite::Error),
}

/// Full-text index over file contents.
pub struct Content {
    connection: Connection,
    extensions: Vec<String>,
    limits: Limits,
    path: PathBuf,
    /// Reused across documents so a full pass does not allocate a fresh buffer
    /// per file. Sized to the largest file accepted, growing at most once.
    buffer: Vec<u8>,
}

impl std::fmt::Debug for Content {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Content").finish_non_exhaustive()
    }
}

impl Content {
    /// Open, creating the schema if needed.
    ///
    /// # Errors
    ///
    /// [`Error::Open`] if the database cannot be opened or the schema cannot
    /// be created.
    pub fn open(path: &Path, extensions: Vec<String>, limits: Limits) -> Result<Self, Error> {
        if let Some(parent) = path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }

        let connection = Connection::open(path).map_err(Error::Open)?;

        // WAL keeps a background reindex from blocking queries, which is the
        // whole point of indexing off the interactive path.
        let _ = connection.pragma_update(None, "journal_mode", "WAL");
        let _ = connection.pragma_update(None, "synchronous", "NORMAL");

        connection
            .execute_batch(
                "CREATE TABLE IF NOT EXISTS documents (
                     path       TEXT PRIMARY KEY,
                     mtime      INTEGER NOT NULL,
                     size       INTEGER NOT NULL
                 );
                 -- `path` is UNINDEXED: it is carried for retrieval, and the
                 -- path index already handles matching on names.
                 CREATE VIRTUAL TABLE IF NOT EXISTS search USING fts5 (
                     path UNINDEXED,
                     body,
                     tokenize = 'unicode61 remove_diacritics 2'
                 );",
            )
            .map_err(Error::Open)?;

        let extensions = if extensions.is_empty() {
            DEFAULT_EXTENSIONS.iter().map(|e| (*e).to_owned()).collect()
        } else {
            extensions
                .iter()
                .map(|e| e.trim_start_matches('.').to_lowercase())
                .collect()
        };

        Ok(Self {
            connection,
            extensions,
            limits,
            path: path.to_path_buf(),
            buffer: Vec::new(),
        })
    }

    /// Whether the index has reached its configured size ceiling.
    ///
    /// The write-ahead log is counted because it is real space the index is
    /// using; ignoring it lets the database sit well over its cap between
    /// checkpoints.
    #[must_use]
    pub fn is_full(&self) -> bool {
        disk_usage(&self.path) >= self.limits.max_index_bytes
    }

    /// Whether this path is a candidate for content indexing.
    #[must_use]
    pub fn accepts(&self, path: &Path) -> bool {
        accepts(&self.extensions, path)
    }

    /// Number of indexed documents.
    #[must_use]
    pub fn len(&self) -> usize {
        self.connection
            .query_row("SELECT count(*) FROM documents", [], |row| {
                row.get::<_, i64>(0)
            })
            .map_or(0, |count| count as usize)
    }

    /// Whether the index holds no documents.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Index `paths`, skipping anything unchanged since last time.
    ///
    /// Returns how many documents were newly extracted. Callers run this off the
    /// interactive path.
    pub fn index(&mut self, paths: &[PathBuf]) -> usize {
        let mut indexed = 0;
        let existing = self.len();

        // Batched rather than one transaction per document. With WAL every
        // commit is an fsync, so committing per file made a first-run index of
        // ~24 000 documents take minutes; a few hundred documents per
        // transaction turns that into one fsync per batch.
        for chunk in paths.chunks(BATCH) {
            if existing + indexed >= MAX_DOCUMENTS {
                tracing::warn!(limit = MAX_DOCUMENTS, "content index full; stopping");
                break;
            }
            // Checked per batch rather than per document: `stat` on the database
            // is cheap but not free, and a batch cannot overshoot the cap by
            // more than one batch of documents.
            if self.is_full() {
                tracing::info!(
                    limit_mb = self.limits.max_index_bytes / (1024 * 1024),
                    "content index reached its size limit; stopping"
                );
                break;
            }

            let Content {
                connection,
                buffer,
                limits,
                extensions,
                ..
            } = self;
            let Ok(transaction) = connection.transaction() else {
                break;
            };

            for path in chunk {
                if !accepts(extensions, path) {
                    continue;
                }

                let Ok(metadata) = std::fs::metadata(path) else {
                    continue;
                };
                if !metadata.is_file() || metadata.len() > limits.max_file_bytes {
                    continue;
                }

                let size = metadata.len() as i64;
                let mtime = metadata
                    .modified()
                    .ok()
                    .and_then(|time| time.duration_since(SystemTime::UNIX_EPOCH).ok())
                    .map_or(0, |age| age.as_secs() as i64);

                if is_current(&transaction, path, mtime, size) {
                    continue;
                }
                if upsert(&transaction, buffer, path, mtime, size) {
                    indexed += 1;
                }
            }

            if transaction.commit().is_err() {
                break;
            }
        }

        if indexed > 0 {
            tracing::info!(indexed, total = self.len(), "content index updated");
        }
        indexed
    }

    /// Drop documents whose files no longer exist.
    pub fn prune_missing(&mut self) {
        let Ok(mut statement) = self.connection.prepare("SELECT path FROM documents") else {
            return;
        };
        let Ok(rows) = statement.query_map([], |row| row.get::<_, String>(0)) else {
            return;
        };

        let gone: Vec<String> = rows
            .flatten()
            .filter(|path| !Path::new(path).exists())
            .collect();
        drop(statement);

        for path in &gone {
            let _ = self
                .connection
                .execute("DELETE FROM search WHERE path = ?1", params![path]);
            let _ = self
                .connection
                .execute("DELETE FROM documents WHERE path = ?1", params![path]);
        }
        if !gone.is_empty() {
            tracing::info!(removed = gone.len(), "pruned deleted documents");
        }
    }

    /// Search document contents, ranked by BM25.
    ///
    /// # Errors
    ///
    /// [`Error::Query`] if the underlying FTS5 query fails.
    pub fn search(&self, query: &str) -> Result<Vec<Item>, Error> {
        let Some(expression) = fts_query(query) else {
            return Ok(Vec::new());
        };

        let mut statement = self
            .connection
            .prepare(
                // bm25() returns a *negative* score where more negative is more
                // relevant, so ascending order puts the best match first.
                "SELECT path,
                        snippet(search, 1, '', '', '…', 12) AS excerpt,
                        bm25(search) AS score
                 FROM search
                 WHERE search MATCH ?1
                 ORDER BY score
                 LIMIT ?2",
            )
            .map_err(Error::Query)?;

        let rows = statement
            .query_map(params![expression, RESULTS as i64], |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, String>(1)?,
                    row.get::<_, f64>(2)?,
                ))
            })
            .map_err(Error::Query)?;

        Ok(rows
            .flatten()
            .filter_map(|(path, excerpt, score)| {
                let path = PathBuf::from(path);
                // The index can lag the filesystem; never offer a file that is
                // no longer there.
                path.is_file().then(|| item(&path, &excerpt, score))
            })
            .collect())
    }
}

/// The files an index at `path` is made of, in the order they are safe to
/// delete: the write-ahead log and its shared-memory index before the
/// database. A database left without its log is a valid, slightly older
/// database; a log left without its database would be replayed into whatever
/// database is created at that path next.
fn files(path: &Path) -> [PathBuf; 3] {
    let beside = |suffix: &str| {
        let mut name = path.as_os_str().to_owned();
        name.push(suffix);
        PathBuf::from(name)
    };
    [beside("-wal"), beside("-shm"), path.to_path_buf()]
}

/// Bytes the content index at `path` occupies on disk, write-ahead log
/// included. Zero when there is no index.
#[must_use]
pub fn disk_usage(path: &Path) -> u64 {
    files(path)
        .iter()
        .filter_map(|file| std::fs::metadata(file).ok())
        .map(|metadata| metadata.len())
        .sum()
}

/// Delete the content index at `path`, returning the bytes that freed.
///
/// Nothing may have the index open. SQLite removes the write-ahead log *by
/// name* when the last connection closes, so a [`Content`] still open across
/// this call would later delete the log of whichever index had been created
/// at `path` in the meantime. Drop every [`Content`] for `path` first.
///
/// # Errors
///
/// The first file that could not be removed. An index that does not exist is
/// not an error: there is nothing to free.
pub fn clear(path: &Path) -> std::io::Result<u64> {
    let freed = disk_usage(path);
    for file in files(path) {
        match std::fs::remove_file(&file) {
            Ok(()) => {}
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => return Err(error),
        }
    }
    Ok(freed)
}

/// Whether `path`'s extension is one we index.
fn accepts(extensions: &[String], path: &Path) -> bool {
    path.extension()
        .and_then(|extension| extension.to_str())
        .is_some_and(|extension| {
            let extension = extension.to_lowercase();
            extensions.contains(&extension)
        })
}

/// Whether the stored copy already matches what is on disk.
fn is_current(connection: &Connection, path: &Path, mtime: i64, size: i64) -> bool {
    let stored: Option<(i64, i64)> = connection
        .query_row(
            "SELECT mtime, size FROM documents WHERE path = ?1",
            params![path.to_string_lossy()],
            |row| Ok((row.get(0)?, row.get(1)?)),
        )
        .optional()
        .ok()
        .flatten();

    stored == Some((mtime, size))
}

/// Extract and store one document, replacing any previous copy.
fn upsert(
    connection: &Connection,
    buffer: &mut Vec<u8>,
    path: &Path,
    mtime: i64,
    size: i64,
) -> bool {
    let Some(text) = extract(buffer, path, size as u64) else {
        return false;
    };
    let path_text = path.to_string_lossy().into_owned();

    // FTS5 has no upsert; the old row has to go first or the document is
    // matched twice, once with stale content.
    let removed = connection.execute("DELETE FROM search WHERE path = ?1", params![path_text]);
    let inserted = connection.execute(
        "INSERT INTO search (path, body) VALUES (?1, ?2)",
        params![path_text, text],
    );
    let recorded = connection.execute(
        "INSERT INTO documents (path, mtime, size) VALUES (?1, ?2, ?3)
         ON CONFLICT(path) DO UPDATE SET mtime = excluded.mtime, size = excluded.size",
        params![path_text, mtime, size],
    );

    removed.is_ok() && inserted.is_ok() && recorded.is_ok()
}

/// Build an FTS5 MATCH expression from user input.
///
/// Every token is quoted and the quotes inside are doubled, because FTS5 MATCH
/// is a query language: a bare `AND`, `*`, `"` or `:` from the user would either
/// change the meaning of the query or raise a syntax error mid-keystroke.
fn fts_query(query: &str) -> Option<String> {
    let tokens: Vec<String> = query
        .split_whitespace()
        .filter(|token| token.chars().any(char::is_alphanumeric))
        .map(|token| format!("\"{}\"", token.replace('"', "\"\"")))
        .collect();

    if tokens.is_empty() {
        return None;
    }

    // All terms required, and the last one is a prefix so results appear while
    // the user is still typing it.
    let last = tokens.len() - 1;
    let mut expression = tokens;
    expression[last].push('*');
    Some(expression.join(" AND "))
}

/// Read a file as text, if it looks like text.
///
/// Reads into a caller-owned buffer that is reused across the whole pass, so a
/// 24 000-document index performs one allocation growth rather than 24 000
/// allocations. `std::fs::read` was doing the latter, plus a second allocation
/// for the lossy conversion and a third for the truncating `collect`.
fn extract(buffer: &mut Vec<u8>, path: &Path, size: u64) -> Option<String> {
    use std::io::Read;

    let file = std::fs::File::open(path).ok()?;
    buffer.clear();
    buffer.reserve(size as usize);
    // Bounded at the reader so a file that grows between `stat` and `read`
    // cannot pull in more than expected.
    file.take(size).read_to_end(buffer).ok()?;

    // A NUL byte in the first block is the standard heuristic for "not text",
    // and catches files whose extension lies.
    if buffer[..buffer.len().min(8192)].contains(&0) {
        return None;
    }

    // Borrow rather than allocate when the bytes are already valid UTF-8, which
    // is the overwhelmingly common case for the extensions indexed here. The
    // binding keeps the lossy fallback alive without relying on temporary
    // lifetime extension inside the match.
    let lossy;
    let text = match std::str::from_utf8(buffer) {
        Ok(text) => text,
        Err(_) => {
            lossy = String::from_utf8_lossy(buffer);
            &lossy
        }
    };

    Some(match text.char_indices().nth(MAX_TEXT_CHARS) {
        Some((cutoff, _)) => text[..cutoff].to_owned(),
        None => text.to_owned(),
    })
}

/// Build a result row from a content hit.
fn item(path: &Path, excerpt: &str, score: f64) -> Item {
    let title = path.file_name().map_or_else(
        || path.display().to_string(),
        |name| name.to_string_lossy().into_owned(),
    );

    let excerpt = excerpt.split_whitespace().collect::<Vec<_>>().join(" ");

    // The same filename often exists in several projects — four copies of
    // CLAUDE.md produced four identical-looking rows. The directory is what
    // tells them apart, so it leads the subtitle and the excerpt follows.
    let location = path
        .parent()
        .map(|parent| {
            let text = parent.display().to_string();
            dirs::home_dir()
                .and_then(|home| {
                    text.strip_prefix(home.to_str()?)
                        .map(|rest| format!("~{rest}"))
                })
                .unwrap_or(text)
        })
        .unwrap_or_default();

    Item {
        key: ItemKey(format!("content:{}", path.display())),
        id: 0,
        title,
        // Location plus the matching text: the excerpt explains why the result
        // is here, the directory says which file it is.
        subtitle: if location.is_empty() {
            excerpt
        } else {
            format!("{location}  ·  {excerpt}")
        },
        icon: Some(Icon::Name("text-x-generic".to_owned())),
        category_icon: Some(Icon::Name("edit-find-symbolic".to_owned())),
        window: None,
        source: Source::File {
            path: path.to_path_buf(),
        },
        // bm25 is negative-better and unbounded; map it onto the positive scale
        // the rest of the ranking uses. -10 or better is treated as excellent.
        autocomplete: None,
        score: (((-score) / 10.0).clamp(0.0, 1.0) as f32) * 3.5,
    }
}

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

    fn index() -> Content {
        // A file-backed database in a temporary directory, because FTS5 and WAL
        // behave differently in memory and the tests should exercise the real
        // configuration.
        let dir = std::env::temp_dir().join(format!("jump-content-test-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        Content::open(&dir.join("content.db"), Vec::new(), Limits::default()).expect("open index")
    }

    #[test]
    fn quotes_tokens_so_fts_syntax_cannot_leak() {
        // `AND` and `*` are FTS5 operators; unquoted they would change meaning.
        let expression = fts_query("AND value").expect("expression");
        assert!(expression.starts_with("\"AND\""));
        assert!(expression.contains("AND \"value\"*"));
    }

    #[test]
    fn embedded_quotes_are_escaped() {
        let expression = fts_query("say \"hello\"").expect("expression");
        assert!(expression.contains("\"\"hello\"\""));
    }

    #[test]
    fn punctuation_only_queries_are_rejected() {
        assert!(fts_query("!!! ???").is_none());
        assert!(fts_query("   ").is_none());
    }

    #[test]
    fn extension_filter_matches_case_insensitively() {
        let content = index();
        assert!(content.accepts(Path::new("/tmp/a.MD")));
        assert!(content.accepts(Path::new("/tmp/a.rs")));
        assert!(!content.accepts(Path::new("/tmp/a.png")));
    }

    #[test]
    fn indexes_and_finds_by_content() {
        let dir = std::env::temp_dir().join("jump-content-find");
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).expect("create dir");

        let file = dir.join("notes.md");
        std::fs::write(&file, "the quick brown fox jumps over the lazy dog").expect("write");

        let mut content = Content::open(&dir.join("content.db"), Vec::new(), Limits::default())
            .expect("open index");
        assert_eq!(content.index(std::slice::from_ref(&file)), 1);

        let results = content.search("brown fox").expect("search");
        assert_eq!(results.len(), 1);
        assert!(results[0].subtitle.contains("brown"));

        // Re-indexing an unchanged file must do nothing.
        assert_eq!(content.index(std::slice::from_ref(&file)), 0);

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn changed_files_are_reindexed_without_duplicating() {
        let dir = std::env::temp_dir().join("jump-content-change");
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).expect("create dir");

        let file = dir.join("doc.txt");
        std::fs::write(&file, "original contents here").expect("write");

        let mut content = Content::open(&dir.join("content.db"), Vec::new(), Limits::default())
            .expect("open index");
        content.index(std::slice::from_ref(&file));

        // A different size guarantees the change is detected regardless of
        // filesystem timestamp granularity.
        std::fs::write(&file, "replacement contents entirely different now").expect("rewrite");
        assert_eq!(content.index(std::slice::from_ref(&file)), 1);
        assert_eq!(content.len(), 1, "the document is replaced, not duplicated");

        assert!(content.search("original").expect("search").is_empty());
        assert_eq!(content.search("replacement").expect("search").len(), 1);

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn binary_files_are_skipped() {
        let dir = std::env::temp_dir().join("jump-content-binary");
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).expect("create dir");

        // A .txt containing NUL bytes: the extension lies, the probe catches it.
        let file = dir.join("fake.txt");
        std::fs::write(&file, [0x00, 0x01, 0x02, b'h', b'i']).expect("write");

        let mut content = Content::open(&dir.join("content.db"), Vec::new(), Limits::default())
            .expect("open index");
        assert_eq!(content.index(&[file]), 0);

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn clearing_deletes_the_database_and_its_log() {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("content.db");

        // No index yet: nothing to free, and not an error.
        assert_eq!(clear(&path).expect("clear nothing"), 0);

        let file = dir.path().join("diary.md");
        std::fs::write(&file, "words nobody else should find").expect("write");
        let mut content = Content::open(&path, Vec::new(), Limits::default()).expect("open index");
        assert_eq!(content.index(std::slice::from_ref(&file)), 1);

        // While the index is open its write-ahead log is on disk beside it,
        // and counts towards what the index occupies.
        let [log, shared, database] = files(&path);
        assert!(log.exists(), "WAL mode keeps a log beside the database");
        let database_bytes = std::fs::metadata(&database).expect("database").len();
        assert!(disk_usage(&path) > database_bytes);

        // Closing checkpoints the log away; put both sidecars back, as a
        // launcher that was killed mid-index would have left them.
        drop(content);
        std::fs::write(&log, b"left behind").expect("log");
        std::fs::write(&shared, b"left behind").expect("shared memory");

        let occupied = disk_usage(&path);
        assert_eq!(clear(&path).expect("clear"), occupied);
        for file in files(&path) {
            assert!(!file.exists(), "{} survived", file.display());
        }
        assert_eq!(disk_usage(&path), 0);
        // The document itself is not the index's to delete.
        assert!(file.exists());

        // Switching content search back on starts from nothing.
        let content = Content::open(&path, Vec::new(), Limits::default()).expect("reopen");
        assert!(content.is_empty());
        assert!(content.search("nobody").expect("search").is_empty());
    }

    #[test]
    fn deleted_files_are_pruned() {
        let dir = std::env::temp_dir().join("jump-content-prune");
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).expect("create dir");

        let file = dir.join("temp.md");
        std::fs::write(&file, "some indexed words").expect("write");

        let mut content = Content::open(&dir.join("content.db"), Vec::new(), Limits::default())
            .expect("open index");
        content.index(std::slice::from_ref(&file));
        assert_eq!(content.len(), 1);

        std::fs::remove_file(&file).expect("remove");
        content.prune_missing();
        assert_eq!(content.len(), 0);

        let _ = std::fs::remove_dir_all(&dir);
    }
}