Skip to main content

pacsea/sources/
details.rs

1//! Package details fetching from official repositories and AUR.
2
3use serde_json::Value;
4
5use crate::integrations::arch_toolkit::ToolkitContext;
6use crate::state::{PackageDetails, PackageItem, Source};
7use crate::util::{arrs, ss, u64_of};
8
9/// Result type alias for package details fetching operations.
10type Result<T> = super::Result<T>;
11
12/// Split a whitespace-separated field to Vec<String>, treating "None"/missing as empty.
13///
14/// Inputs:
15/// - `s`: Optional string field from pacman output
16///
17/// Output:
18/// - Vector of tokens, or empty when field is missing or "None".
19fn split_ws_or_none(s: Option<&String>) -> Vec<String> {
20    match s {
21        Some(v) if v != "None" => v.split_whitespace().map(ToString::to_string).collect(),
22        _ => Vec::new(),
23    }
24}
25
26/// Process a continuation line (indented line) for a given key in the map.
27///
28/// Inputs:
29/// - `map`: Map to update
30/// - `key`: Current key being continued
31/// - `line`: Continuation line content
32///
33/// Output:
34/// - Updates the map entry for the key with the continuation content.
35///
36/// Details:
37/// - Handles special formatting for "Optional Deps" (newline-separated) vs other fields (space-separated).
38fn process_continuation_line(
39    map: &mut std::collections::BTreeMap<String, String>,
40    key: &str,
41    line: &str,
42) {
43    let entry = map.entry(key.to_string()).or_default();
44    if key == "Optional Deps" {
45        entry.push('\n');
46    } else if !entry.ends_with(' ') {
47        entry.push(' ');
48    }
49    entry.push_str(line.trim());
50}
51
52/// Run `pacman -Si` command and return the output text.
53///
54/// Inputs:
55/// - `repo`: Preferred repository prefix (may be empty to let pacman resolve)
56/// - `name`: Package name
57///
58/// Output:
59/// - `Ok(String)` with command output on success; `Err` if command fails.
60///
61/// Details:
62/// - Sets locale to C for consistent output parsing.
63fn run_pacman_si(repo: &str, name: &str) -> Result<String> {
64    let spec = if repo.is_empty() {
65        name.to_string()
66    } else {
67        format!("{repo}/{name}")
68    };
69    let out = std::process::Command::new("pacman")
70        .env("LC_ALL", "C")
71        .env("LANG", "C")
72        .args(["-Si", &spec])
73        .output()?;
74    if !out.status.success() {
75        return Err(format!("pacman -Si failed: {:?}", out.status).into());
76    }
77    String::from_utf8(out.stdout).map_err(std::convert::Into::into)
78}
79
80/// Parse pacman output text into a key-value map.
81///
82/// Inputs:
83/// - `text`: Raw output from `pacman -Si`
84///
85/// Output:
86/// - `BTreeMap<String, String>` with parsed key-value pairs.
87///
88/// Details:
89/// - Handles continuation lines (indented lines) that extend previous keys.
90/// - Skips empty lines and processes key:value pairs.
91fn parse_pacman_output(text: &str) -> std::collections::BTreeMap<String, String> {
92    let mut map: std::collections::BTreeMap<String, String> = std::collections::BTreeMap::new();
93    let mut last_key: Option<String> = None;
94    for line in text.lines() {
95        if line.trim().is_empty() {
96            continue;
97        }
98        if let Some((k, v)) = line.split_once(':') {
99            let key = k.trim().to_string();
100            let val = v.trim().to_string();
101            map.insert(key.clone(), val);
102            last_key = Some(key);
103        } else if line.starts_with(' ')
104            && let Some(k) = &last_key
105        {
106            process_continuation_line(&mut map, k, line);
107        }
108    }
109    map
110}
111
112/// Extracted fields from pacman output parsing.
113///
114/// Groups related fields together to reduce data flow complexity.
115struct ParsedFields {
116    /// Package licenses.
117    licenses: Vec<String>,
118    /// Package groups.
119    groups: Vec<String>,
120    /// Packages provided by this package.
121    provides: Vec<String>,
122    /// Required dependencies.
123    depends: Vec<String>,
124    /// Optional dependencies.
125    opt_depends: Vec<String>,
126    /// Packages that require this package.
127    required_by: Vec<String>,
128    /// Packages that optionally depend on this package.
129    optional_for: Vec<String>,
130    /// Packages that conflict with this package.
131    conflicts: Vec<String>,
132    /// Packages that this package replaces.
133    replaces: Vec<String>,
134    /// Package description.
135    description: String,
136    /// Target architecture.
137    architecture: String,
138    /// Download size in bytes.
139    download_size: Option<u64>,
140    /// Installed size in bytes.
141    install_size: Option<u64>,
142}
143
144/// Extract all dependency and metadata fields from the parsed map.
145///
146/// Inputs:
147/// - `map`: Parsed key-value map from pacman output
148///
149/// Output:
150/// - `ParsedFields` struct containing all extracted fields.
151///
152/// Details:
153/// - Handles multiple field name variants (e.g., "Licenses" vs "License").
154/// - Parses optional dependencies with special formatting.
155fn extract_fields(map: &std::collections::BTreeMap<String, String>) -> ParsedFields {
156    let licenses = split_ws_or_none(map.get("Licenses").or_else(|| map.get("License")));
157    let groups = split_ws_or_none(map.get("Groups"));
158    let provides = split_ws_or_none(map.get("Provides"));
159    let depends = split_ws_or_none(map.get("Depends On"));
160    let opt_depends = map
161        .get("Optional Deps")
162        .map(|s| {
163            s.lines()
164                .filter_map(|l| l.split_once(':').map(|(pkg, _)| pkg.trim().to_string()))
165                .filter(|x| !x.is_empty() && x != "None")
166                .collect()
167        })
168        .unwrap_or_default();
169    let required_by = split_ws_or_none(map.get("Required By"));
170    let optional_for = split_ws_or_none(map.get("Optional For"));
171    let conflicts = split_ws_or_none(map.get("Conflicts With"));
172    let replaces = split_ws_or_none(map.get("Replaces"));
173
174    ParsedFields {
175        licenses,
176        groups,
177        provides,
178        depends,
179        opt_depends,
180        required_by,
181        optional_for,
182        conflicts,
183        replaces,
184        description: map.get("Description").cloned().unwrap_or_default(),
185        architecture: map.get("Architecture").cloned().unwrap_or_default(),
186        download_size: map.get("Download Size").and_then(|s| parse_size_bytes(s)),
187        install_size: map.get("Installed Size").and_then(|s| parse_size_bytes(s)),
188    }
189}
190
191/// Fill missing description and architecture from the official index if needed.
192///
193/// Inputs:
194/// - `name`: Package name to search for
195/// - `description`: Description string to fill if empty (mutable)
196/// - `architecture`: Architecture string to fill if empty (mutable)
197///
198/// Output:
199/// - Updates description and architecture in place if found in index.
200///
201/// Details:
202/// - Searches official repositories for matching package name.
203/// - Only updates fields that are currently empty.
204fn fill_missing_fields(name: &str, description: &mut String, architecture: &mut String) {
205    if description.is_empty() || architecture.is_empty() {
206        let mut from_idx = None;
207        // Use normal substring search for this helper (not fuzzy)
208        let official_results = crate::index::search_official(name, false);
209        for (it, _) in official_results {
210            if it.name.eq_ignore_ascii_case(name) {
211                from_idx = Some(it);
212                break;
213            }
214        }
215        if let Some(it) = from_idx {
216            if description.is_empty() {
217                *description = it.description;
218            }
219            if architecture.is_empty()
220                && let Source::Official { arch, .. } = it.source
221            {
222                *architecture = arch;
223            }
224        }
225    }
226}
227
228/// Build `PackageDetails` from parsed map and extracted fields.
229///
230/// Inputs:
231/// - `repo`: Repository name (fallback if not in map)
232/// - `name`: Package name (fallback if not in map)
233/// - `map`: Parsed key-value map
234/// - `fields`: Extracted fields struct
235///
236/// Output:
237/// - `PackageDetails` struct with all fields populated.
238fn build_package_details(
239    repo: &str,
240    name: &str,
241    map: &std::collections::BTreeMap<String, String>,
242    fields: ParsedFields,
243) -> PackageDetails {
244    PackageDetails {
245        repository: map
246            .get("Repository")
247            .cloned()
248            .unwrap_or_else(|| repo.to_string()),
249        name: map.get("Name").cloned().unwrap_or_else(|| name.to_string()),
250        version: map.get("Version").cloned().unwrap_or_default(),
251        description: fields.description,
252        architecture: fields.architecture,
253        url: map.get("URL").cloned().unwrap_or_default(),
254        licenses: fields.licenses,
255        groups: fields.groups,
256        provides: fields.provides,
257        depends: fields.depends,
258        opt_depends: fields.opt_depends,
259        required_by: fields.required_by,
260        optional_for: fields.optional_for,
261        conflicts: fields.conflicts,
262        replaces: fields.replaces,
263        download_size: fields.download_size,
264        install_size: fields.install_size,
265        owner: map.get("Packager").cloned().unwrap_or_default(),
266        build_date: map.get("Build Date").cloned().unwrap_or_default(),
267        popularity: None,
268        out_of_date: None,
269        orphaned: false,
270    }
271}
272
273/// Run `pacman -Si` for a package, parsing its key-value output into `PackageDetails`.
274///
275/// Inputs:
276/// - `repo`: Preferred repository prefix (may be empty to let pacman resolve)
277/// - `name`: Package name
278///
279/// Output:
280/// - `Ok(PackageDetails)` on success; `Err` if command fails or parse errors occur.
281fn pacman_si(repo: &str, name: &str) -> Result<PackageDetails> {
282    let text = run_pacman_si(repo, name)?;
283    let map = parse_pacman_output(&text);
284    let mut fields = extract_fields(&map);
285    fill_missing_fields(name, &mut fields.description, &mut fields.architecture);
286    Ok(build_package_details(repo, name, &map, fields))
287}
288
289/// Parse a pacman human-readable size like "1.5 MiB" into bytes.
290///
291/// Inputs:
292/// - `s`: Size string containing a number and unit
293///
294/// Output:
295/// - `Some(bytes)` when parsed; `None` for invalid strings. Accepts B, KiB, MiB, GiB, TiB, PiB.
296fn parse_size_bytes(s: &str) -> Option<u64> {
297    // Maximum f64 value that fits in u64 (2^64 - 1, but f64 can represent up to 2^53 exactly)
298    // For values beyond 2^53, we check if they exceed u64::MAX by comparing with a threshold
299    const MAX_U64_AS_F64: f64 = 18_446_744_073_709_551_615.0; // u64::MAX as approximate f64
300    let mut it = s.split_whitespace();
301    let num = it.next()?.parse::<f64>().ok()?;
302    let unit = it.next().unwrap_or("");
303    let mult = match unit {
304        "KiB" => 1024.0,
305        "MiB" => 1024.0 * 1024.0,
306        "GiB" => 1024.0 * 1024.0 * 1024.0,
307        "TiB" => 1024.0 * 1024.0 * 1024.0 * 1024.0,
308        _ => 1.0,
309    };
310    let result = num * mult;
311    // Check bounds: negative values are invalid
312    if result < 0.0 {
313        return None;
314    }
315    if result > MAX_U64_AS_F64 {
316        return None;
317    }
318    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
319    let bytes = result as u64;
320    Some(bytes)
321}
322
323#[cfg(test)]
324mod size_tests {
325    #[test]
326    /// What: Ensure `parse_size_bytes` converts human-readable sizes into raw bytes.
327    ///
328    /// Inputs:
329    /// - Representative strings covering `B`, `KiB`, `MiB`, `GiB`, `TiB`, and an invalid token.
330    ///
331    /// Output:
332    /// - Returns the correct byte counts for valid inputs and `None` for malformed strings.
333    ///
334    /// Details:
335    /// - Covers the unit matching branch and protects against accidental unit regression.
336    fn details_parse_size_bytes_units() {
337        assert_eq!(super::parse_size_bytes("10 B"), Some(10));
338        assert_eq!(super::parse_size_bytes("1 KiB"), Some(1024));
339        assert_eq!(super::parse_size_bytes("2 MiB"), Some(2 * 1024 * 1024));
340        assert_eq!(
341            super::parse_size_bytes("3 GiB"),
342            Some(3 * 1024 * 1024 * 1024)
343        );
344        assert_eq!(
345            super::parse_size_bytes("4 TiB"),
346            Some(4 * 1024 * 1024 * 1024 * 1024)
347        );
348        assert!(super::parse_size_bytes("bad").is_none());
349    }
350}
351
352/// What: Fetch package details for either official repositories or AUR, based on the item's source.
353///
354/// Inputs:
355/// - `item`: Package to fetch details for.
356///
357/// Output:
358/// - `Ok(PackageDetails)` on success; `Err` if retrieval or parsing fails.
359///
360/// # Errors
361/// - Returns `Err` when network request fails (curl execution error)
362/// - Returns `Err` when package details cannot be fetched from official repositories or AUR
363/// - Returns `Err` when response parsing fails (invalid JSON or missing fields)
364///
365pub async fn fetch_details(item: PackageItem) -> Result<PackageDetails> {
366    let context = ToolkitContext::new()?;
367    fetch_details_with_context(&context, item).await
368}
369
370/// What: Fetch package details with a caller-owned shared toolkit context.
371///
372/// Inputs:
373/// - `context`: Runtime integration context.
374/// - `item`: Package to fetch.
375///
376/// Output:
377/// - Pacsea package details or an actionable error.
378///
379/// Details:
380/// - AUR details use arch-toolkit; the pacman-first official fallback remains Pacsea-owned.
381pub async fn fetch_details_with_context(
382    context: &ToolkitContext,
383    item: PackageItem,
384) -> Result<PackageDetails> {
385    match item.source.clone() {
386        Source::Official { repo, arch } => {
387            fetch_official_details_with_context(context, repo, arch, item).await
388        }
389        Source::Aur => crate::integrations::arch_toolkit::aur::details(context, item)
390            .await
391            .map_err(Into::into),
392    }
393}
394
395/// Fetch official repository package details via pacman JSON endpoints.
396///
397/// Inputs:
398/// - `repo`: Repository name to prefer when multiple are available.
399/// - `arch`: Architecture string to prefer.
400/// - `item`: Package to fetch.
401///
402/// Output: `Ok(PackageDetails)` with repository fields filled; `Err` on network/parse failure.
403/// What: Fetch official details with a caller-owned shared toolkit context.
404///
405/// Inputs:
406/// - `context`: Runtime integration context.
407/// - `repo`: Preferred repository.
408/// - `arch`: Preferred architecture.
409/// - `item`: Existing Pacsea row used for fallback fields.
410///
411/// Output:
412/// - Pacsea package details or an actionable error.
413///
414/// Details:
415/// - Pacman remains first, Pacsea's richer JSON mapping remains second, and toolkit's bounded
416///   metadata primitive supplies the final exact-match fallback.
417pub async fn fetch_official_details_with_context(
418    context: &ToolkitContext,
419    repo: String,
420    arch: String,
421    item: PackageItem,
422) -> Result<PackageDetails> {
423    if let Ok(Ok(pd)) = tokio::task::spawn_blocking({
424        let repo = repo.clone();
425        let name = item.name.clone();
426        move || pacman_si(&repo, &name)
427    })
428    .await
429    {
430        let has_core =
431            !(pd.description.is_empty() && pd.architecture.is_empty() && pd.licenses.is_empty());
432        if has_core {
433            return Ok(pd);
434        }
435    }
436
437    let arch_candidates: Vec<String> = if arch.trim().is_empty() {
438        vec!["x86_64".to_string(), "any".to_string()]
439    } else if arch.to_lowercase() == "any" {
440        vec!["any".to_string()]
441    } else {
442        vec![arch.clone(), "any".to_string()]
443    };
444    let repo_candidates: Vec<String> = if repo.trim().is_empty() {
445        vec!["core".to_string(), "extra".to_string()]
446    } else {
447        vec![repo.clone()]
448    };
449    let mut v: Option<Value> = None;
450    let mut repo_selected = repo.clone();
451    let mut arch_selected = arch.clone();
452    'outer: for r in &repo_candidates {
453        for a in &arch_candidates {
454            let url = format!(
455                "https://archlinux.org/packages/{}/{}/{}/json/",
456                r.to_lowercase(),
457                a,
458                item.name
459            );
460            if let Ok(Ok(val)) = tokio::task::spawn_blocking({
461                let url = url.clone();
462                move || crate::util::curl::curl_json(&url)
463            })
464            .await
465            {
466                v = Some(val);
467                repo_selected.clone_from(r);
468                arch_selected.clone_from(a);
469                break 'outer;
470            }
471        }
472    }
473
474    if let Some(v) = v {
475        let obj = v.get("pkg").unwrap_or(&v);
476        let d = PackageDetails {
477            repository: repo_selected,
478            name: item.name.clone(),
479            version: ss(obj, &["pkgver", "Version"]).unwrap_or(item.version),
480            description: ss(obj, &["pkgdesc", "Description"]).unwrap_or(item.description),
481            architecture: ss(obj, &["arch", "Architecture"]).unwrap_or(arch_selected),
482            url: ss(obj, &["url", "URL"]).unwrap_or_default(),
483            licenses: arrs(obj, &["licenses", "Licenses"]),
484            groups: arrs(obj, &["groups", "Groups"]),
485            provides: arrs(obj, &["provides", "Provides"]),
486            depends: arrs(obj, &["depends", "Depends"]),
487            opt_depends: arrs(obj, &["optdepends", "OptDepends"]),
488            required_by: arrs(obj, &["requiredby", "RequiredBy"]),
489            optional_for: vec![],
490            conflicts: arrs(obj, &["conflicts", "Conflicts"]),
491            replaces: arrs(obj, &["replaces", "Replaces"]),
492            download_size: u64_of(obj, &["compressed_size", "CompressedSize"]),
493            install_size: u64_of(obj, &["installed_size", "InstalledSize"]),
494            owner: ss(obj, &["packager", "Packager"]).unwrap_or_default(),
495            build_date: ss(obj, &["build_date", "BuildDate"]).unwrap_or_default(),
496            popularity: None,
497            out_of_date: None,
498            orphaned: false,
499        };
500        return Ok(d);
501    }
502
503    let selector = arch_toolkit::OfficialPackage {
504        name: item.name.clone(),
505        repo,
506        arch,
507        version: item.version.clone(),
508        description: item.description.clone(),
509    };
510    if let Some(package) = arch_toolkit::fetch_arch_package_detail(
511        context.http_client(),
512        &selector,
513        arch_toolkit::types::package::MetadataFetchLimits::default(),
514    )
515    .await
516    .map_err(|error| format!("official package metadata unavailable: {error}"))?
517    {
518        return Ok(PackageDetails {
519            repository: package.repo,
520            name: package.name,
521            version: package.version,
522            description: package.description,
523            architecture: package.arch,
524            ..PackageDetails::default()
525        });
526    }
527
528    Err("official details unavailable; verify pacman repositories and network access".into())
529}
530
531#[cfg(test)]
532mod tests {
533    // use super::*;
534
535    #[test]
536    /// What: Parse official repository JSON into `PackageDetails`, ensuring defaults mirror the packages API.
537    ///
538    /// Inputs:
539    /// - Minimal JSON payload containing version metadata, sizes, and packager fields.
540    /// - Sample `PackageItem` representing the queried package.
541    ///
542    /// Output:
543    /// - Populated `PackageDetails` carries expected strings and parsed size values.
544    ///
545    /// Details:
546    /// - Exercises helper extraction functions (`ss`, `arrs`, `u64_of`) and fallback behaviour when fields are missing.
547    fn sources_details_parse_official_json_defaults_and_fields() {
548        fn parse_official_from_json(
549            obj: &serde_json::Value,
550            repo_selected: String,
551            arch_selected: String,
552            item: &crate::state::PackageItem,
553        ) -> crate::state::PackageDetails {
554            use crate::util::{arrs, ss, u64_of};
555            crate::state::PackageDetails {
556                repository: repo_selected,
557                name: item.name.clone(),
558                version: ss(obj, &["pkgver", "Version"]).unwrap_or_else(|| item.version.clone()),
559                description: ss(obj, &["pkgdesc", "Description"])
560                    .unwrap_or_else(|| item.description.clone()),
561                architecture: ss(obj, &["arch", "Architecture"]).unwrap_or(arch_selected),
562                url: ss(obj, &["url", "URL"]).unwrap_or_default(),
563                licenses: arrs(obj, &["licenses", "Licenses"]),
564                groups: arrs(obj, &["groups", "Groups"]),
565                provides: arrs(obj, &["provides", "Provides"]),
566                depends: arrs(obj, &["depends", "Depends"]),
567                opt_depends: arrs(obj, &["optdepends", "OptDepends"]),
568                required_by: arrs(obj, &["requiredby", "RequiredBy"]),
569                optional_for: vec![],
570                conflicts: arrs(obj, &["conflicts", "Conflicts"]),
571                replaces: arrs(obj, &["replaces", "Replaces"]),
572                download_size: u64_of(obj, &["compressed_size", "CompressedSize"]),
573                install_size: u64_of(obj, &["installed_size", "InstalledSize"]),
574                owner: ss(obj, &["packager", "Packager"]).unwrap_or_default(),
575                build_date: ss(obj, &["build_date", "BuildDate"]).unwrap_or_default(),
576                popularity: None,
577                out_of_date: None,
578                orphaned: false,
579            }
580        }
581        let v: serde_json::Value = serde_json::json!({
582            "pkg": {
583                "pkgver": "14",
584                "pkgdesc": "ripgrep fast search",
585                "arch": "x86_64",
586                "url": "https://example.com",
587                "licenses": ["MIT"],
588                "groups": [],
589                "provides": ["rg"],
590                "depends": ["pcre2"],
591                "optdepends": ["bash: completions"],
592                "requiredby": [],
593                "conflicts": [],
594                "replaces": [],
595                "compressed_size": 1024u64,
596                "installed_size": 2048u64,
597                "packager": "Arch Dev",
598                "build_date": "2024-01-01"
599            }
600        });
601        let item = crate::state::PackageItem {
602            name: "ripgrep".into(),
603            version: String::new(),
604            description: String::new(),
605            source: crate::state::Source::Official {
606                repo: "extra".into(),
607                arch: "x86_64".into(),
608            },
609            popularity: None,
610            out_of_date: None,
611            orphaned: false,
612        };
613        let d = parse_official_from_json(&v["pkg"], "extra".into(), "x86_64".into(), &item);
614        assert_eq!(d.repository, "extra");
615        assert_eq!(d.name, "ripgrep");
616        assert_eq!(d.version, "14");
617        assert_eq!(d.description, "ripgrep fast search");
618        assert_eq!(d.architecture, "x86_64");
619        assert_eq!(d.url, "https://example.com");
620        assert_eq!(d.download_size, Some(1024));
621        assert_eq!(d.install_size, Some(2048));
622        assert_eq!(d.owner, "Arch Dev");
623        assert_eq!(d.build_date, "2024-01-01");
624    }
625
626    #[test]
627    /// What: Parse AUR RPC JSON into `PackageDetails`, handling optional fields and popularity.
628    ///
629    /// Inputs:
630    /// - Minimal AUR JSON document providing version, description, popularity, and URL.
631    /// - Seed `PackageItem` used to supply fallback values.
632    ///
633    /// Output:
634    /// - Resulting `PackageDetails` retains `AUR` repository label, uses JSON data when present, and sets popularity.
635    ///
636    /// Details:
637    /// - Validates interplay between helper functions and fallback assignments for missing fields.
638    fn sources_details_parse_aur_json_defaults_and_popularity() {
639        fn parse_aur_from_json(
640            obj: &serde_json::Value,
641            item: &crate::state::PackageItem,
642        ) -> crate::state::PackageDetails {
643            use crate::util::{arrs, s};
644            let version0 = s(obj, "Version");
645            let description0 = s(obj, "Description");
646            let popularity0 = obj.get("Popularity").and_then(serde_json::Value::as_f64);
647            crate::state::PackageDetails {
648                repository: "AUR".into(),
649                name: item.name.clone(),
650                version: if version0.is_empty() {
651                    item.version.clone()
652                } else {
653                    version0
654                },
655                description: if description0.is_empty() {
656                    item.description.clone()
657                } else {
658                    description0
659                },
660                architecture: "any".into(),
661                url: s(obj, "URL"),
662                licenses: arrs(obj, &["License", "Licenses"]),
663                groups: arrs(obj, &["Groups"]),
664                provides: arrs(obj, &["Provides"]),
665                depends: arrs(obj, &["Depends"]),
666                opt_depends: arrs(obj, &["OptDepends"]),
667                required_by: vec![],
668                optional_for: vec![],
669                conflicts: arrs(obj, &["Conflicts"]),
670                replaces: arrs(obj, &["Replaces"]),
671                download_size: None,
672                install_size: None,
673                owner: s(obj, "Maintainer"),
674                build_date: crate::util::ts_to_date(
675                    obj.get("LastModified").and_then(serde_json::Value::as_i64),
676                ),
677                popularity: popularity0,
678                out_of_date: None,
679                orphaned: false,
680            }
681        }
682        let obj: serde_json::Value = serde_json::json!({
683            "Version": "1.2.3",
684            "Description": "cool",
685            "Popularity": std::f64::consts::PI,
686            "URL": "https://aur.example/ripgrep"
687        });
688        let item = crate::state::PackageItem {
689            name: "ripgrep-git".into(),
690            version: String::new(),
691            description: String::new(),
692            source: crate::state::Source::Aur,
693            popularity: None,
694            out_of_date: None,
695            orphaned: false,
696        };
697        let d = parse_aur_from_json(&obj, &item);
698        assert_eq!(d.repository, "AUR");
699        assert_eq!(d.name, "ripgrep-git");
700        assert_eq!(d.version, "1.2.3");
701        assert_eq!(d.description, "cool");
702        assert_eq!(d.architecture, "any");
703        assert_eq!(d.url, "https://aur.example/ripgrep");
704        assert_eq!(d.popularity, Some(std::f64::consts::PI));
705    }
706
707    #[test]
708    /// What: Parse AUR RPC JSON with `OutOfDate` and orphaned status fields.
709    ///
710    /// Inputs:
711    /// - AUR JSON document with `OutOfDate` timestamp and empty Maintainer (orphaned).
712    ///
713    /// Output:
714    /// - Resulting `PackageDetails` correctly sets `out_of_date` and orphaned flags.
715    ///
716    /// Details:
717    /// - Validates that `OutOfDate` timestamp is extracted and orphaned status is determined from empty Maintainer.
718    fn sources_details_parse_aur_status_fields() {
719        use crate::util::s;
720        let obj: serde_json::Value = serde_json::json!({
721            "Version": "1.0.0",
722            "Description": "test package",
723            "OutOfDate": 1_704_067_200_i64, // 2024-01-01 timestamp
724            "Maintainer": "" // Empty means orphaned
725        });
726        let _item = crate::state::PackageItem {
727            name: "test-pkg".into(),
728            version: String::new(),
729            description: String::new(),
730            source: crate::state::Source::Aur,
731            popularity: None,
732            out_of_date: None,
733            orphaned: false,
734        };
735        // Extract OutOfDate timestamp (i64 or null)
736        let out_of_date = obj
737            .get("OutOfDate")
738            .and_then(serde_json::Value::as_i64)
739            .and_then(|ts| u64::try_from(ts).ok())
740            .filter(|&ts| ts > 0);
741        // Extract Maintainer and determine if orphaned (empty or null means orphaned)
742        let maintainer = s(&obj, "Maintainer");
743        let orphaned = maintainer.is_empty();
744
745        assert_eq!(out_of_date, Some(1_704_067_200));
746        assert!(orphaned);
747    }
748
749    #[test]
750    /// What: Parse AUR RPC JSON with non-orphaned package (has maintainer).
751    ///
752    /// Inputs:
753    /// - AUR JSON document with Maintainer field set to a username.
754    ///
755    /// Output:
756    /// - Resulting package is not marked as orphaned.
757    ///
758    /// Details:
759    /// - Validates that packages with a maintainer are not marked as orphaned.
760    fn sources_details_parse_aur_with_maintainer() {
761        use crate::util::s;
762        let obj: serde_json::Value = serde_json::json!({
763            "Version": "1.0.0",
764            "Description": "test package",
765            "Maintainer": "someuser"
766        });
767        let maintainer = s(&obj, "Maintainer");
768        let orphaned = maintainer.is_empty();
769
770        assert!(!orphaned);
771        assert_eq!(maintainer, "someuser");
772    }
773}