pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
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
//! Disk-persisted LRU cache for parsed PKGBUILD data.

use crate::logic::files::pkgbuild_parse::{
    parse_backup_from_pkgbuild, parse_install_paths_from_pkgbuild,
};
use crate::state::Source;
use lru::LruCache;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::fs;
use std::hash::{Hash, Hasher};
use std::num::NonZeroUsize;
use std::path::PathBuf;
#[cfg(test)]
use std::sync::Arc;
use std::sync::{Mutex, OnceLock};
#[cfg(test)]
use std::thread::ThreadId;

/// Maximum number of PKGBUILD entries to cache.
const CACHE_CAPACITY: usize = 200;
/// Environment variable name for custom PKGBUILD cache path.
const CACHE_PATH_ENV: &str = "PACSEA_PKGBUILD_CACHE_PATH";

/// What: Source kind for PKGBUILD files.
///
/// Inputs: Determined from package source.
///
/// Output: Enum indicating where the PKGBUILD came from.
///
/// Details: Used to categorize PKGBUILD files by their origin (AUR, Official, or Unknown).
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum PkgbuildSourceKind {
    /// PKGBUILD came from AUR.
    Aur,
    /// PKGBUILD came from official repositories.
    Official,
    /// Source could not be determined.
    Unknown,
}

impl From<&Source> for PkgbuildSourceKind {
    fn from(src: &Source) -> Self {
        match src {
            Source::Aur => Self::Aur,
            Source::Official { .. } => Self::Official,
        }
    }
}

/// What: Cached PKGBUILD parse entry.
///
/// Inputs: Parsed from PKGBUILD file.
///
/// Output: Structured PKGBUILD metadata.
///
/// Details: Stores parsed PKGBUILD information for caching purposes.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PkgbuildParseEntry {
    /// Package name.
    pub name: String,
    /// Package version.
    pub version: String,
    /// Source kind (AUR, Official, or Unknown).
    pub source: PkgbuildSourceKind,
    /// PKGBUILD file signature hash.
    pub pkgbuild_signature: u64,
    /// List of backup files specified in the PKGBUILD.
    pub backup_files: Vec<String>,
    /// List of install paths specified in the PKGBUILD.
    pub install_paths: Vec<String>,
}

/// What: On-disk cache structure for PKGBUILD entries.
///
/// Inputs: Loaded from disk cache file.
///
/// Output: Serialized cache data.
///
/// Details: Used for persisting PKGBUILD cache to disk.
#[derive(Debug, Serialize, Deserialize)]
struct PkgbuildCacheDisk {
    /// Cached PKGBUILD parse entries.
    entries: Vec<PkgbuildParseEntry>,
}

/// What: In-memory cache state for PKGBUILD entries.
///
/// Inputs: Initialized with cache path.
///
/// Output: Manages LRU cache and dirty flag.
///
/// Details: Tracks cache state including LRU cache, file path, and whether changes need to be persisted.
#[derive(Debug)]
struct PkgbuildCacheState {
    /// LRU cache of PKGBUILD entries.
    lru: LruCache<String, PkgbuildParseEntry>,
    /// Path to the cache file on disk.
    path: PathBuf,
    /// Whether the cache has been modified and needs to be saved.
    dirty: bool,
}

impl PkgbuildCacheState {
    /// What: Create a new PKGBUILD cache state.
    ///
    /// Inputs:
    /// - `path`: Path to the cache file on disk.
    ///
    /// Output: New cache state with empty LRU cache.
    ///
    /// Details: Initializes a new cache state with the specified path and an empty LRU cache.
    fn new(path: PathBuf) -> Self {
        Self {
            lru: LruCache::new(
                NonZeroUsize::new(CACHE_CAPACITY)
                    .unwrap_or_else(|| NonZeroUsize::new(1).expect("non-zero capacity")),
            ),
            path,
            dirty: false,
        }
    }

    /// What: Load cache entries from disk.
    ///
    /// Inputs: None (uses self.path).
    ///
    /// Output: Populates the LRU cache with entries from disk.
    ///
    /// Details: Reads the cache file from disk and populates the in-memory cache. Silently handles missing files.
    fn load_from_disk(&mut self) {
        let raw = match fs::read_to_string(&self.path) {
            Ok(raw) => raw,
            Err(e) => {
                if e.kind() != std::io::ErrorKind::NotFound {
                    tracing::warn!(
                        path = %self.path.display(),
                        error = %e,
                        "[PKGBUILD cache] Failed to read cache file"
                    );
                }
                return;
            }
        };

        let parsed: PkgbuildCacheDisk = match serde_json::from_str(&raw) {
            Ok(cache) => cache,
            Err(e) => {
                tracing::warn!(
                    path = %self.path.display(),
                    error = %e,
                    "[PKGBUILD cache] Failed to parse cache file"
                );
                return;
            }
        };

        // Insert from least-recent to most-recent to preserve order when iterating.
        for entry in parsed.entries.into_iter().rev() {
            let key = cache_key(&entry.name, &entry.version, entry.source);
            let _ = self.lru.put(key, entry);
        }
        tracing::info!(
            path = %self.path.display(),
            count = self.lru.len(),
            "[PKGBUILD cache] Loaded cache entries"
        );
    }

    /// What: Write cache to disk if it has been modified.
    ///
    /// Inputs: None (uses self state).
    ///
    /// Output: Writes cache to disk if dirty flag is set.
    ///
    /// Details: Serializes the cache entries and writes them to disk, then clears the dirty flag.
    fn flush_if_dirty(&mut self) {
        if !self.dirty {
            return;
        }

        let payload = PkgbuildCacheDisk {
            entries: self.lru.iter().map(|(_, v)| v.clone()).collect(),
        };

        let Ok(serialized) = serde_json::to_string(&payload) else {
            tracing::warn!("[PKGBUILD cache] Failed to serialize cache payload");
            return;
        };

        if let Some(parent) = self.path.parent()
            && let Err(e) = fs::create_dir_all(parent)
        {
            tracing::warn!(
                path = %self.path.display(),
                error = %e,
                "[PKGBUILD cache] Failed to create parent directory"
            );
            return;
        }

        match fs::write(&self.path, serialized) {
            Ok(()) => {
                tracing::debug!(
                    path = %self.path.display(),
                    entries = self.lru.len(),
                    "[PKGBUILD cache] Persisted cache to disk"
                );
                self.dirty = false;
            }
            Err(e) => {
                tracing::warn!(
                    path = %self.path.display(),
                    error = %e,
                    "[PKGBUILD cache] Failed to write cache to disk"
                );
            }
        }
    }
}

/// What: Get the path to the PKGBUILD cache file.
///
/// Inputs: None.
///
/// Output: Path to the cache file.
///
/// Details: Checks environment variable first, otherwise uses default path in lists directory.
fn cache_path() -> PathBuf {
    if let Ok(path) = std::env::var(CACHE_PATH_ENV) {
        return PathBuf::from(path);
    }
    crate::theme::lists_dir().join("pkgbuild_parse_cache.json")
}

/// What: Get the global cache state singleton.
///
/// Inputs: None.
///
/// Output: Reference to the global cache state mutex.
///
/// Details: Initializes the cache state on first access, loading from disk if available.
fn cache_state() -> &'static Mutex<PkgbuildCacheState> {
    static STATE: OnceLock<Mutex<PkgbuildCacheState>> = OnceLock::new();
    STATE.get_or_init(|| {
        let path = cache_path();
        let mut state = PkgbuildCacheState::new(path);
        state.load_from_disk();
        Mutex::new(state)
    })
}

/// What: Compute a hash signature for PKGBUILD contents.
///
/// Inputs:
/// - `contents`: PKGBUILD file contents.
///
/// Output: 64-bit hash signature.
///
/// Details: Uses `DefaultHasher` to compute a hash of the PKGBUILD contents for cache invalidation.
fn compute_signature(contents: &str) -> u64 {
    let mut hasher = DefaultHasher::new();
    contents.hash(&mut hasher);
    hasher.finish()
}

/// What: Generate a cache key for a PKGBUILD entry.
///
/// Inputs:
/// - `name`: Package name.
/// - `version`: Package version.
/// - `source`: Source kind.
///
/// Output: Cache key string.
///
/// Details: Creates a unique cache key by combining package name, version, and source kind.
fn cache_key(name: &str, version: &str, source: PkgbuildSourceKind) -> String {
    format!("{name}::{version}::{source:?}")
}

#[cfg(test)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::logic::files) enum CacheTestHookPoint {
    AfterLookup,
}

#[cfg(test)]
pub(in crate::logic::files) type CacheTestHook = dyn Fn(CacheTestHookPoint) + Send + Sync + 'static;

#[cfg(test)]
#[derive(Clone)]
struct CacheTestHookEntry {
    hook: Arc<CacheTestHook>,
    thread_id: ThreadId,
}

#[cfg(test)]
fn cache_test_hook_slot() -> &'static Mutex<Option<CacheTestHookEntry>> {
    static HOOK: OnceLock<Mutex<Option<CacheTestHookEntry>>> = OnceLock::new();
    HOOK.get_or_init(|| Mutex::new(None))
}

#[cfg(test)]
/// What: Temporarily register a cache test hook for synchronization.
///
/// Inputs:
/// - `hook`: Callback executed when a cache hook point is reached.
/// - `thread_id`: Thread id to match before invoking the hook.
///
/// Output:
/// - Guard that clears the hook on drop to restore default behavior.
///
/// Details:
/// - Only compiled in tests; the hook is global and not re-entrant.
pub fn set_cache_test_hook(hook: Arc<CacheTestHook>, thread_id: ThreadId) -> CacheTestHookGuard {
    if let Ok(mut slot) = cache_test_hook_slot().lock() {
        *slot = Some(CacheTestHookEntry { hook, thread_id });
    }
    CacheTestHookGuard
}

#[cfg(test)]
/// What: RAII guard that removes the active cache test hook on drop.
///
/// Inputs: None.
///
/// Output:
/// - Clears any registered test hook when dropped.
///
/// Details:
/// - Scope the guard to the duration the hook should stay active.
pub struct CacheTestHookGuard;

#[cfg(test)]
impl Drop for CacheTestHookGuard {
    fn drop(&mut self) {
        if let Ok(mut slot) = cache_test_hook_slot().lock() {
            slot.take();
        }
    }
}

#[cfg(test)]
fn invoke_cache_test_hook(point: CacheTestHookPoint) {
    // Clone hook entry and release slot mutex before invoking so that other threads
    // can still check the hook slot while this thread is blocked inside the callback.
    let entry = cache_test_hook_slot()
        .lock()
        .ok()
        .and_then(|slot| slot.clone());
    if let Some(hook) = entry
        && std::thread::current().id() == hook.thread_id
    {
        (hook.hook)(point);
    }
}

/// What: Parse PKGBUILD data while leveraging a disk-backed LRU cache.
///
/// Inputs:
/// - `name`: Package name used for keying and install path inference.
/// - `version`: Package version (fall back to `"unknown"` if empty).
/// - `source`: Source kind for keying (Aur/Official/Unknown).
/// - `pkgbuild`: Raw PKGBUILD text to parse.
///
/// Output:
/// - Parsed entry containing backup files and install paths. On cache hit with matching
///   signature, returns the cached entry. On cache miss or signature mismatch, parses
///   fresh data, updates the cache, and returns the new entry.
///
/// Details:
/// - Uses a signature of the PKGBUILD text to detect staleness even when version is unchanged.
/// - Cache is bounded to 200 entries and persists to disk via `flush_pkgbuild_cache()`.
pub fn parse_pkgbuild_cached(
    name: &str,
    version: Option<&str>,
    source: PkgbuildSourceKind,
    pkgbuild: &str,
) -> PkgbuildParseEntry {
    let normalized_version = version
        .filter(|v| !v.is_empty())
        .map_or_else(|| "unknown".to_string(), ToString::to_string);
    let signature = compute_signature(pkgbuild);
    let key = cache_key(name, &normalized_version, source);
    let prior_signature = if let Ok(mut guard) = cache_state().lock()
        && let Some(entry) = guard.lru.get(&key)
    {
        if entry.pkgbuild_signature == signature {
            return entry.clone();
        }
        Some(entry.pkgbuild_signature)
    } else {
        None
    };

    #[cfg(test)]
    invoke_cache_test_hook(CacheTestHookPoint::AfterLookup);

    let parsed = PkgbuildParseEntry {
        name: name.to_string(),
        version: normalized_version,
        source,
        pkgbuild_signature: signature,
        backup_files: parse_backup_from_pkgbuild(pkgbuild),
        install_paths: parse_install_paths_from_pkgbuild(pkgbuild, name),
    };

    let mut guard = match cache_state().lock() {
        Ok(guard) => guard,
        Err(poisoned) => {
            tracing::warn!(
                "[PKGBUILD cache] Cache mutex poisoned; continuing with recovered state"
            );
            poisoned.into_inner()
        }
    };

    if let Some(entry) = guard.lru.get(&key) {
        if entry.pkgbuild_signature == signature {
            return entry.clone();
        }

        if prior_signature.is_some() && prior_signature == Some(entry.pkgbuild_signature) {
            let _ = guard.lru.put(key, parsed.clone());
            guard.dirty = true;
            return parsed;
        }

        return entry.clone();
    }

    let _ = guard.lru.put(key, parsed.clone());
    guard.dirty = true;

    parsed
}

/// What: Persist the PKGBUILD parse cache to disk when dirty.
///
/// Inputs: None.
///
/// Output:
/// - Best-effort disk write of the cache file; clears the dirty flag on success.
///
/// Details:
/// - Safe to call frequently; returns immediately when nothing has changed.
pub fn flush_pkgbuild_cache() {
    if let Ok(mut guard) = cache_state().lock() {
        guard.flush_if_dirty();
    }
}

#[cfg(test)]
pub fn reset_cache_for_tests(path: PathBuf) {
    if let Ok(mut guard) = cache_state().lock() {
        let mut state = PkgbuildCacheState::new(path);
        state.load_from_disk();
        *guard = state;
    }
}

#[cfg(test)]
pub fn peek_cache_entry_for_tests(
    name: &str,
    version: &str,
    source: PkgbuildSourceKind,
) -> Option<PkgbuildParseEntry> {
    let key = cache_key(name, version, source);
    cache_state()
        .lock()
        .ok()
        .and_then(|mut guard| guard.lru.get(&key).cloned())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Barrier, mpsc};
    use std::time::Duration;

    fn sample_pkgbuild() -> String {
        r#"
pkgname=sample
pkgver=1.2.3
pkgrel=1
backup=('etc/sample.conf' '/etc/sample.d/more.conf')
package() {
  install -Dm755 "$srcdir/sample" "$pkgdir/usr/bin/sample"
  install -Dm644 "$srcdir/sample.conf" "$pkgdir/etc/sample.conf"
}
"#
        .to_string()
    }

    fn temp_cache_path(label: &str) -> PathBuf {
        let mut path = std::env::temp_dir();
        path.push(format!(
            "pacsea_pkgb_cache_{label}_{}_{}.json",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("system time ok")
                .as_nanos()
        ));
        path
    }

    #[test]
    fn cache_hit_returns_same_signature_entry() {
        let path = temp_cache_path("hit");
        reset_cache_for_tests(path);
        let text = sample_pkgbuild();
        let entry = parse_pkgbuild_cached("sample", Some("1.2.3"), PkgbuildSourceKind::Aur, &text);
        assert!(entry.backup_files.contains(&"etc/sample.conf".to_string()));
        assert!(entry.install_paths.contains(&"/usr/bin/sample".to_string()));
        let hit = parse_pkgbuild_cached("sample", Some("1.2.3"), PkgbuildSourceKind::Aur, &text);
        assert_eq!(hit.pkgbuild_signature, entry.pkgbuild_signature);
        assert_eq!(hit.install_paths, entry.install_paths);
    }

    #[test]
    fn cache_miss_on_signature_change_reparses() {
        let path = temp_cache_path("miss");
        reset_cache_for_tests(path);
        let text = sample_pkgbuild();
        let _ = parse_pkgbuild_cached("sample", Some("1.2.3"), PkgbuildSourceKind::Official, &text);
        let modified = format!("{text}\n# change");
        let updated = parse_pkgbuild_cached(
            "sample",
            Some("1.2.3"),
            PkgbuildSourceKind::Official,
            &modified,
        );
        assert!(updated.pkgbuild_signature != compute_signature(&text));
    }

    #[test]
    fn flush_and_reload_persists_entries() {
        let path = temp_cache_path("persist");
        reset_cache_for_tests(path.clone());
        let text = sample_pkgbuild();
        let entry = parse_pkgbuild_cached("sample", Some("1.2.3"), PkgbuildSourceKind::Aur, &text);
        flush_pkgbuild_cache();
        reset_cache_for_tests(path);
        let cached = peek_cache_entry_for_tests("sample", "1.2.3", PkgbuildSourceKind::Aur)
            .expect("entry should reload");
        assert_eq!(cached.pkgbuild_signature, entry.pkgbuild_signature);
        assert_eq!(cached.backup_files, entry.backup_files);
    }

    #[test]
    fn cache_evicts_oldest_when_capacity_exceeded() {
        let path = temp_cache_path("evict");
        reset_cache_for_tests(path);
        let text = sample_pkgbuild();
        for i in 0..(CACHE_CAPACITY + 5) {
            let name = format!("pkg{i}");
            parse_pkgbuild_cached(&name, Some("1"), PkgbuildSourceKind::Unknown, &text);
        }
        assert!(
            peek_cache_entry_for_tests("pkg0", "1", PkgbuildSourceKind::Unknown).is_none(),
            "oldest entry should be evicted past capacity"
        );
    }

    #[test]
    fn concurrent_parse_does_not_overwrite_newer_entry() {
        let path = temp_cache_path("concurrent");
        reset_cache_for_tests(path);
        let name = "racepkg";
        let stale_pkgbuild = sample_pkgbuild();
        let newer_pkgbuild = r#"
pkgname=sample
pkgver=9.9.9
pkgrel=1
backup=('etc/sample.conf')
package() {
  install -Dm755 "$srcdir/sample" "$pkgdir/usr/bin/sample"
  install -Dm644 "$srcdir/sample.conf" "$pkgdir/etc/sample.conf"
}
"#
        .to_string();

        let (reached_tx, reached_rx) = mpsc::channel();
        let (resume_tx, resume_rx) = mpsc::channel();
        let resume_rx = Arc::new(Mutex::new(resume_rx));
        let hook_consumed = Arc::new(AtomicBool::new(false));
        let hook_flag = Arc::clone(&hook_consumed);
        let hook_resume = Arc::clone(&resume_rx);
        let hook = Arc::new(move |point: CacheTestHookPoint| {
            if point == CacheTestHookPoint::AfterLookup
                && hook_flag
                    .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
                    .is_ok()
            {
                let _ = reached_tx.send(());
                hook_resume
                    .lock()
                    .expect("resume_rx lock poisoned")
                    .recv()
                    .expect("resume signal should arrive");
            }
        });
        let start_barrier = Arc::new(Barrier::new(2));

        let stale_pkgbuild_for_thread = stale_pkgbuild.clone();
        let stale_start = Arc::clone(&start_barrier);
        let stale_handle = std::thread::spawn(move || {
            stale_start.wait();
            parse_pkgbuild_cached(
                name,
                Some("1.2.3"),
                PkgbuildSourceKind::Aur,
                &stale_pkgbuild_for_thread,
            )
        });

        let stale_thread_id = stale_handle.thread().id();
        let _guard = set_cache_test_hook(hook, stale_thread_id);
        start_barrier.wait();

        reached_rx
            .recv_timeout(Duration::from_secs(2))
            .expect("stale thread should reach hook before proceeding");

        let newer_pkgbuild_for_thread = newer_pkgbuild.clone();
        let new_handle = std::thread::spawn(move || {
            parse_pkgbuild_cached(
                name,
                Some("1.2.3"),
                PkgbuildSourceKind::Aur,
                &newer_pkgbuild_for_thread,
            )
        });

        let new_entry = new_handle
            .join()
            .expect("new parsing thread should finish without panic");
        resume_tx
            .send(())
            .expect("should release stale thread after new parse completes");
        let stale_entry = stale_handle
            .join()
            .expect("stale parsing thread should finish without panic");

        let cached = peek_cache_entry_for_tests(name, "1.2.3", PkgbuildSourceKind::Aur)
            .expect("cache entry should exist after concurrent parses");
        let stale_signature = compute_signature(&stale_pkgbuild);
        let new_signature = compute_signature(&newer_pkgbuild);

        assert_eq!(
            cached.pkgbuild_signature, new_signature,
            "newer entry must remain in cache"
        );
        assert_eq!(
            cached.pkgbuild_signature, new_entry.pkgbuild_signature,
            "cache entry should match result of newer parse"
        );
        assert_ne!(
            cached.pkgbuild_signature, stale_signature,
            "stale parse must not overwrite newer cache entry"
        );
        // When the stale thread loses the race, it should return the cached (newer)
        // entry rather than its own stale parse result.
        assert_eq!(
            stale_entry.pkgbuild_signature, new_entry.pkgbuild_signature,
            "stale thread should return cached newer entry after losing race"
        );
        assert_ne!(
            stale_signature, new_signature,
            "test setup should use distinct PKGBUILD contents"
        );
    }
}