rusty-fez 0.4.0

Agent-native management CLI for Fedora/RHEL (drives cockpit-bridge)
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
//! PackageKit fallback backend (`org.freedesktop.PackageKit`).
//!
//! Used when dnf5daemon (`org.rpm.dnf.v0`) is absent (notably RHEL 10). Drives
//! PackageKit's per-transaction D-Bus API: a transaction object path is created,
//! a method is called on it, and the results arrive as a stream of signals
//! collected by [`crate::protocol::client::BridgeClient::dbus_call_collect`].
//!
//! PackageKit's plan carries no install/download sizes (only `info`,
//! `package_id`, `summary` per package), so size fields are emitted as `null`
//! and every payload carries `"backend":"packagekit"` plus a hint so callers
//! can see the schema is degraded relative to the dnf5daemon path.
use crate::error::{FezError, Result};
use crate::protocol::client::BridgeClient;
use serde_json::{json, Value};

/// Well-known bus name of the PackageKit daemon.
const PK_NAME: &str = "org.freedesktop.PackageKit";
/// Object path of the PackageKit daemon's root controller.
const PK_PATH: &str = "/org/freedesktop/PackageKit";
/// Root controller interface (carries `CreateTransaction`).
const PK_IFACE: &str = "org.freedesktop.PackageKit";
/// Per-transaction interface (carries the operation methods + result signals).
const TX_IFACE: &str = "org.freedesktop.PackageKit.Transaction";

/// PackageKit transaction flag: execute for real (no special handling).
const TF_NONE: u64 = 0;
/// PackageKit transaction flag: simulate only (resolve the plan, change nothing).
const TF_SIMULATE: u64 = 2;

/// PackageKit filter bit: only installed packages (`PK_FILTER_ENUM_INSTALLED`).
const FILTER_INSTALLED: u64 = 1 << 2;
/// PackageKit filter bit: newest version only (`PK_FILTER_ENUM_NEWEST`).
const FILTER_NEWEST: u64 = 1 << 16;
/// PackageKit filter: no filtering (`PK_FILTER_ENUM_NONE`).
const FILTER_NONE: u64 = 0;

/// PackageKit `info` enum: package will be installed.
const INFO_INSTALLING: u64 = 8;
/// PackageKit `info` enum: package will be removed.
const INFO_REMOVING: u64 = 9;
/// PackageKit `info` enum: package will be updated.
const INFO_UPDATING: u64 = 7;
/// PackageKit `info` enum: package will be obsoleted (treated as a removal).
const INFO_OBSOLETING: u64 = 11;
/// PackageKit `info` enum: package will be downgraded.
const INFO_DOWNGRADING: u64 = 13;

/// PackageKit error enum: `PK_ERROR_ENUM_NOT_AUTHORIZED`.
const PK_ERROR_NOT_AUTHORIZED: u64 = 6;

/// A render-ready PackageKit result, assembled into a `packages::View` by the
/// caller (which supplies the host).
pub struct PkView {
    /// Envelope `kind` (e.g. `PackageList`, `PackagePlan`, `PackageMutation`).
    pub kind: &'static str,
    /// The `fez/v1` data payload, carrying `"backend":"packagekit"`.
    pub data: Value,
    /// Human-readable rendering.
    pub human: String,
    /// Optional envelope hints (carries the degraded-schema note).
    pub hints: Option<Value>,
}

/// One package parsed from a PackageKit `Package(info, package_id, summary)`
/// signal. `package_id` is `name;version;arch;data` (data = repo or `installed`).
struct PkPackage {
    info: u64,
    name: String,
    version: String,
    arch: String,
    data: String,
    summary: String,
}

impl PkPackage {
    /// Parse a `Package` signal's `[info, package_id, summary]` args.
    ///
    /// Returns `None` if the args are malformed (missing `info` or `package_id`).
    fn from_signal(args: &[Value]) -> Option<Self> {
        let info = args.first()?.as_u64()?;
        let pid = args.get(1)?.as_str()?;
        let summary = args
            .get(2)
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string();
        let mut parts = pid.splitn(4, ';');
        let name = parts.next()?.to_string();
        let version = parts.next().unwrap_or("").to_string();
        let arch = parts.next().unwrap_or("").to_string();
        let data = parts.next().unwrap_or("").to_string();
        Some(PkPackage {
            info,
            name,
            version,
            arch,
            data,
            summary,
        })
    }

    /// The NEVRA-ish label `name-version.arch` for plan rendering.
    fn label(&self) -> String {
        format!("{}-{}.{}", self.name, self.version, self.arch)
    }

    /// The full `name;version;arch;data` package_id (mutations need this form).
    fn package_id(&self) -> String {
        format!("{};{};{};{}", self.name, self.version, self.arch, self.data)
    }

    /// The repo/data field; PackageKit puts the repo id (or `installed`) here.
    fn repo(&self) -> &str {
        &self.data
    }
}

/// Pull the `Package` rows out of a collected signal stream, in arrival order.
fn packages_from(signals: &[(String, Vec<Value>)]) -> Vec<PkPackage> {
    signals
        .iter()
        .filter(|(member, _)| member == "Package")
        .filter_map(|(_, args)| PkPackage::from_signal(args))
        .collect()
}

/// Map a PackageKit `ErrorCode(code, details)` in the stream, if any, to a
/// [`FezError`].
///
/// # Errors
///
/// Returns [`FezError::AccessDenied`] (exit 11) when the stream carries an
/// `ErrorCode` with `PK_ERROR_ENUM_NOT_AUTHORIZED`, or [`FezError::Dbus`] for
/// any other PackageKit error.
fn check_stream(signals: &[(String, Vec<Value>)]) -> Result<()> {
    if let Some((_, args)) = signals.iter().find(|(m, _)| m == "ErrorCode") {
        let code = args.first().and_then(Value::as_u64).unwrap_or(0);
        let details = args
            .get(1)
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string();
        if code == PK_ERROR_NOT_AUTHORIZED {
            return Err(FezError::AccessDenied {
                remediation: format!(
                    "PackageKit denied the operation ({details}). Ensure privilege escalation is available (passwordless sudo or a polkit rule) and retry."
                ),
            });
        }
        return Err(FezError::Dbus {
            name: "org.freedesktop.PackageKit.Error".into(),
            message: details,
        });
    }
    Ok(())
}

/// Column order for PackageKit list/search payloads, identical to the
/// dnf5daemon backend's `PKG_COLUMNS` so the two schemas line up, except
/// `install_size` is always JSON `null` here (PackageKit reports no size).
const PK_COLUMNS: &[&str] = &["name", "evr", "arch", "repo_id", "install_size", "summary"];

/// Column order for the PackageKit `RepoList` payload, matching the dnf
/// backend's `REPO_COLUMNS`.
const PK_REPO_COLUMNS: &[&str] = &["id", "name", "enabled"];

/// Open an (optionally privileged) channel to the PackageKit bus name.
///
/// Reads use an unprivileged channel; mutations pass `privileged = true` so the
/// bridge escalates to root (which bypasses PackageKit's `auth_admin` polkit
/// actions).
///
/// # Errors
///
/// Propagates transport / channel-open errors.
fn open_pk(client: &mut BridgeClient, privileged: bool) -> Result<String> {
    if privileged {
        client.dbus_open_privileged(PK_NAME)
    } else {
        client.dbus_open(PK_NAME)
    }
}

/// Create a fresh PackageKit transaction object on `channel`.
///
/// Each PackageKit transaction is single-use (it is gone once `Finished`
/// fires), so callers create one per operation. A ServiceUnknown D-Bus error
/// here means PackageKit itself is absent; the caller maps that to
/// dependency-missing.
///
/// # Errors
///
/// Propagates the `CreateTransaction` call error (including ServiceUnknown when
/// PackageKit is not installed).
fn new_tx(client: &mut BridgeClient, channel: &str) -> Result<String> {
    let out = client.dbus_call(channel, PK_PATH, PK_IFACE, "CreateTransaction", json!([]))?;
    Ok(out
        .as_array()
        .and_then(|a| a.first())
        .or(Some(&out))
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_string())
}

/// The envelope hint every PackageKit payload carries, flagging the degraded
/// schema (no sizes) and the active backend.
fn pk_hints() -> Option<Value> {
    Some(json!({
        "backend": "packagekit",
        "note": "Running via the PackageKit fallback backend; install/download sizes are unavailable on this backend.",
    }))
}

/// One positional row aligned to [`PK_COLUMNS`]; `install_size` is `null`.
fn row(p: &PkPackage) -> Value {
    json!([p.name, p.version, p.arch, p.repo(), Value::Null, p.summary,])
}

/// Human-readable NAME/VERSION/ARCH/REPO table for list output.
fn human_table(pkgs: &[&PkPackage]) -> String {
    let mut s = format!(
        "{:<24} {:<20} {:<10} {}\n",
        "NAME", "VERSION", "ARCH", "REPO"
    );
    for p in pkgs {
        s.push_str(&format!(
            "{:<24} {:<20} {:<10} {}\n",
            p.name,
            p.version,
            p.arch,
            p.repo()
        ));
    }
    s
}

/// `packages list`: `GetPackages` with the INSTALLED or NEWEST filter.
///
/// # Errors
///
/// Propagates transport, transaction, or PackageKit stream errors.
pub fn list(
    client: &mut BridgeClient,
    available: bool,
    repos: &[String],
    name: Option<&str>,
    limit: Option<usize>,
    offset: usize,
) -> Result<PkView> {
    let channel = open_pk(client, false)?;
    let tx = new_tx(client, &channel)?;
    let filter = if available {
        FILTER_NEWEST
    } else {
        FILTER_INSTALLED
    };
    let signals =
        client.dbus_call_collect(&channel, &tx, TX_IFACE, "GetPackages", json!([filter]))?;
    check_stream(&signals)?;
    let pkgs = packages_from(&signals);
    let filtered: Vec<&PkPackage> = pkgs
        .iter()
        .filter(|p| repos.is_empty() || repos.iter().any(|r| r == p.repo()))
        .filter(|p| name.is_none_or(|pattern| p.name.contains(pattern)))
        .collect();
    let total = filtered.len();
    let start = offset.min(total);
    let end = match limit {
        Some(limit) => (start + limit).min(total),
        None => total,
    };
    let page = &filtered[start..end];
    let rows: Vec<Value> = page.iter().map(|p| row(p)).collect();
    let mut data = crate::envelope::table_data(PK_COLUMNS, rows);
    data["scope"] = json!(if available { "available" } else { "installed" });
    data["repos"] = json!(repos);
    data["name"] = json!(name);
    data["total"] = json!(total);
    data["returned"] = json!(end - start);
    data["limit"] = json!(limit);
    data["offset"] = json!(offset);
    data["next_offset"] = json!((end < total).then_some(end));
    data["backend"] = json!("packagekit");
    let human = human_table(page);
    let hints = if limit.is_none() && total > 1000 {
        Some(json!([
            pk_hints().expect("PackageKit hint is always present"),
            format!(
                "This response has {total} rows. Prefer packages search <pattern>, use --name, or use --limit."
            )
        ]))
    } else {
        pk_hints()
    };
    Ok(PkView {
        kind: "PackageList",
        data,
        human,
        hints,
    })
}

/// `packages info <spec>`: `Resolve` the spec and render the first match.
///
/// # Errors
///
/// Returns [`FezError::NotFound`] when the spec resolves to nothing, or
/// propagates transport / stream errors.
pub fn info(client: &mut BridgeClient, spec: &str) -> Result<PkView> {
    let channel = open_pk(client, false)?;
    let tx = new_tx(client, &channel)?;
    let signals = client.dbus_call_collect(
        &channel,
        &tx,
        TX_IFACE,
        "Resolve",
        json!([FILTER_NEWEST, [spec]]),
    )?;
    check_stream(&signals)?;
    let pkgs = packages_from(&signals);
    let p = pkgs
        .first()
        .ok_or_else(|| FezError::NotFound(spec.to_string()))?;
    let data = json!({
        "name": p.name,
        "evr": p.version,
        "arch": p.arch,
        "repo_id": p.repo(),
        "install_size": Value::Null,
        "summary": p.summary,
        "backend": "packagekit",
    });
    let human = format!(
        "Name        : {}\nVersion     : {}\nArch        : {}\nRepo        : {}\nInstall size: (unavailable)\nSummary     : {}\n",
        p.name,
        p.version,
        p.arch,
        p.repo(),
        p.summary,
    );
    Ok(PkView {
        kind: "PackageInfo",
        data,
        human,
        hints: pk_hints(),
    })
}

/// `packages search <pattern>`: `SearchNames` over available packages.
///
/// # Errors
///
/// Propagates transport, transaction, or PackageKit stream errors.
pub fn search(client: &mut BridgeClient, pattern: &str) -> Result<PkView> {
    let channel = open_pk(client, false)?;
    let tx = new_tx(client, &channel)?;
    let signals = client.dbus_call_collect(
        &channel,
        &tx,
        TX_IFACE,
        "SearchNames",
        json!([FILTER_NEWEST, [pattern]]),
    )?;
    check_stream(&signals)?;
    let pkgs = packages_from(&signals);
    let refs: Vec<&PkPackage> = pkgs.iter().collect();
    let rows: Vec<Value> = refs.iter().map(|p| row(p)).collect();
    let mut data = crate::envelope::table_data(PK_COLUMNS, rows);
    data["pattern"] = json!(pattern);
    data["backend"] = json!("packagekit");
    let mut human = String::new();
    for p in &refs {
        human.push_str(&format!("{} - {}\n", p.name, p.summary));
    }
    Ok(PkView {
        kind: "PackageSearch",
        data,
        human,
        hints: pk_hints(),
    })
}

/// `packages check-update`: `GetUpdates` reports the available upgrades.
///
/// # Errors
///
/// Propagates transport, transaction, or PackageKit stream errors.
pub fn check_update(client: &mut BridgeClient) -> Result<PkView> {
    let channel = open_pk(client, false)?;
    let tx = new_tx(client, &channel)?;
    let signals =
        client.dbus_call_collect(&channel, &tx, TX_IFACE, "GetUpdates", json!([FILTER_NONE]))?;
    check_stream(&signals)?;
    let pkgs = packages_from(&signals);
    let refs: Vec<&PkPackage> = pkgs.iter().collect();
    let rows: Vec<Value> = refs.iter().map(|p| row(p)).collect();
    let mut data = crate::envelope::table_data(PK_COLUMNS, rows);
    data["backend"] = json!("packagekit");
    let mut human = format!("{:<24} {:<20} {}\n", "NAME", "VERSION", "REPO");
    for p in &refs {
        human.push_str(&format!("{:<24} {:<20} {}\n", p.name, p.version, p.repo()));
    }
    Ok(PkView {
        kind: "PackageUpdates",
        data,
        human,
        hints: pk_hints(),
    })
}

/// `packages repolist`: `GetRepoList` plus the enabled/disabled/all filter.
///
/// `accepts` mirrors the dnf backend's `RepoFilter::accepts`: `enabled` keeps
/// only enabled repos, `disabled` only disabled, `all` keeps everything.
///
/// # Errors
///
/// Propagates transport, transaction, or PackageKit stream errors.
pub fn repolist(client: &mut BridgeClient, accepts: impl Fn(bool) -> bool) -> Result<PkView> {
    let channel = open_pk(client, false)?;
    let tx = new_tx(client, &channel)?;
    let signals =
        client.dbus_call_collect(&channel, &tx, TX_IFACE, "GetRepoList", json!([FILTER_NONE]))?;
    check_stream(&signals)?;
    let mut rows = Vec::new();
    let mut human = format!("{:<24} {:<10} {}\n", "REPO ID", "ENABLED", "NAME");
    for (member, args) in &signals {
        if member != "RepoDetail" {
            continue;
        }
        let id = args.first().and_then(Value::as_str).unwrap_or("");
        let name = args.get(1).and_then(Value::as_str).unwrap_or("");
        let enabled = args.get(2).and_then(Value::as_bool).unwrap_or(false);
        if !accepts(enabled) {
            continue;
        }
        human.push_str(&format!("{id:<24} {enabled:<10} {name}\n"));
        rows.push(json!([id, name, enabled]));
    }
    let mut data = crate::envelope::table_data(PK_REPO_COLUMNS, rows);
    data["backend"] = json!("packagekit");
    Ok(PkView {
        kind: "RepoList",
        data,
        human,
        hints: pk_hints(),
    })
}

/// A mutation plan parsed from a SIMULATE transaction's `Package` signals,
/// bucketed by the `info` enum (mirrors the dnf backend's `ResolvedPlan`).
struct PkPlan {
    install: Vec<String>,
    remove: Vec<String>,
    upgrade: Vec<String>,
    downgrade: Vec<String>,
    remove_names: Vec<String>,
}

/// Bucket a SIMULATE stream's packages by their `info` enum into a [`PkPlan`].
fn plan_from(signals: &[(String, Vec<Value>)]) -> PkPlan {
    let mut plan = PkPlan {
        install: vec![],
        remove: vec![],
        upgrade: vec![],
        downgrade: vec![],
        remove_names: vec![],
    };
    for p in packages_from(signals) {
        match p.info {
            INFO_INSTALLING => plan.install.push(p.label()),
            INFO_UPDATING => plan.upgrade.push(p.label()),
            INFO_DOWNGRADING => plan.downgrade.push(p.label()),
            INFO_REMOVING | INFO_OBSOLETING => {
                plan.remove_names.push(p.name.clone());
                plan.remove.push(p.label());
            }
            _ => {}
        }
    }
    plan
}

/// The PackageKit method name and argument array for each mutation verb.
fn method_for(verb: &str, flags: u64, ids: &[String]) -> (&'static str, Value) {
    match verb {
        "install" => ("InstallPackages", json!([flags, ids])),
        "upgrade" => ("UpdatePackages", json!([flags, ids])),
        // RemovePackages(flags, ids, allow_deps=true, autoremove=false)
        "remove" => ("RemovePackages", json!([flags, ids, true, false])),
        other => unreachable!("unknown mutation verb {other}"),
    }
}

/// Resolve specs to full package_ids (mutations need them, not bare names).
///
/// # Errors
///
/// Propagates transport / stream errors. An empty resolve yields an empty id
/// list, which the caller surfaces as not-found.
fn resolve_ids(client: &mut BridgeClient, channel: &str, specs: &[String]) -> Result<Vec<String>> {
    let tx = new_tx(client, channel)?;
    let signals = client.dbus_call_collect(
        channel,
        &tx,
        TX_IFACE,
        "Resolve",
        json!([FILTER_NEWEST, specs]),
    )?;
    check_stream(&signals)?;
    Ok(packages_from(&signals)
        .iter()
        .map(PkPackage::package_id)
        .collect())
}

/// `packages install|remove|upgrade` over PackageKit.
///
/// Opens a privileged channel (cockpit escalates the bridge to root, which
/// bypasses PackageKit's `auth_admin` polkit actions), SIMULATEs to build a
/// plan, applies removal guardrails, audits, and executes. Honors `dry_run` and
/// `force` exactly like the dnf5daemon backend.
///
/// # Errors
///
/// Returns [`FezError::NotFound`] when the specs resolve to nothing,
/// [`FezError::DangerousTransaction`] (exit 8) when a removal guardrail fires
/// without `force`, [`FezError::AccessDenied`] (exit 11) when PackageKit denies
/// the operation, or propagates transport / stream errors.
pub fn mutate(
    client: &mut BridgeClient,
    verb: &str,
    specs: &[String],
    host: &str,
    dry_run: bool,
    force: bool,
) -> Result<PkView> {
    let channel = open_pk(client, true)?;
    let ids = resolve_ids(client, &channel, specs)?;
    if ids.is_empty() {
        return Err(FezError::NotFound(specs.join(", ")));
    }

    // SIMULATE to build the plan (separate single-use transaction).
    let sim_tx = new_tx(client, &channel)?;
    let (m, args) = method_for(verb, TF_SIMULATE, &ids);
    let sim = client.dbus_call_collect(&channel, &sim_tx, TX_IFACE, m, args)?;
    check_stream(&sim)?;
    let plan = plan_from(&sim);

    if dry_run {
        return Ok(plan_view(verb, host, specs, &plan, true));
    }
    crate::safety::check_removal_plan(&plan.remove_names, force)?;

    let sink = crate::audit::sink_from_env();
    let audit = crate::audit::AuditContext::new(
        &crate::audit::actor(),
        host,
        verb,
        &specs.join(","),
        &crate::audit::correlation_id(),
    );
    sink.write(&audit.record(crate::audit::Outcome::Attempt));
    let (m, args) = method_for(verb, TF_NONE, &ids);
    let exec_tx = new_tx(client, &channel)?;
    let exec = client
        .dbus_call_collect(&channel, &exec_tx, TX_IFACE, m, args)
        .and_then(|s| check_stream(&s));
    match &exec {
        Ok(()) => sink.write(&audit.record(crate::audit::Outcome::Ok)),
        Err(e) => sink.write(&audit.record(crate::audit::Outcome::Error(e.to_string()))),
    }
    exec?;
    Ok(plan_view(verb, host, specs, &plan, false))
}

/// Build a [`PkView`] for a plan (dry-run preview or executed mutation),
/// schema-compatible with the dnf5daemon backend's plan payload but with a
/// `null` `install_size_total` and a `"backend":"packagekit"` marker.
fn plan_view(verb: &str, host: &str, specs: &[String], plan: &PkPlan, dry_run: bool) -> PkView {
    let kind = if dry_run {
        "PackagePlan"
    } else {
        "PackageMutation"
    };
    let data = json!({
        "operation": verb,
        "specs": specs,
        "dry_run": dry_run,
        "backend": "packagekit",
        "install": plan.install,
        "remove": plan.remove,
        "upgrade": plan.upgrade,
        "downgrade": plan.downgrade,
        "install_size_total": Value::Null,
        "counts": {
            "install": plan.install.len(),
            "remove": plan.remove.len(),
            "upgrade": plan.upgrade.len(),
            "downgrade": plan.downgrade.len(),
        },
    });
    let human = if dry_run {
        format!(
            "DRY-RUN: {} {} on {} would install {}, remove {}, upgrade {}, downgrade {} package(s)\n",
            verb,
            specs.join(" "),
            host,
            plan.install.len(),
            plan.remove.len(),
            plan.upgrade.len(),
            plan.downgrade.len(),
        )
    } else {
        format!(
            "{} {} on {}: installed {}, removed {}, upgraded {}, downgraded {} package(s)\n",
            verb,
            specs.join(" "),
            host,
            plan.install.len(),
            plan.remove.len(),
            plan.upgrade.len(),
            plan.downgrade.len(),
        )
    };
    PkView {
        kind,
        data,
        human,
        hints: pk_hints(),
    }
}

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

    #[test]
    fn parses_package_signal() {
        let args = vec![
            json!(8),
            json!("htop;3.4.1-3.fc44;x86_64;fedora"),
            json!("Interactive process viewer"),
        ];
        let p = PkPackage::from_signal(&args).unwrap();
        assert_eq!(p.info, 8);
        assert_eq!(p.name, "htop");
        assert_eq!(p.version, "3.4.1-3.fc44");
        assert_eq!(p.arch, "x86_64");
        assert_eq!(p.repo(), "fedora");
        assert_eq!(p.label(), "htop-3.4.1-3.fc44.x86_64");
        assert_eq!(p.package_id(), "htop;3.4.1-3.fc44;x86_64;fedora");
    }

    #[test]
    fn malformed_package_signal_is_none() {
        assert!(PkPackage::from_signal(&[]).is_none());
        assert!(PkPackage::from_signal(&[json!(8)]).is_none());
    }

    #[test]
    fn error_code_six_is_access_denied() {
        let signals = vec![
            (
                "ErrorCode".to_string(),
                vec![json!(6), json!("not authorized")],
            ),
            ("Finished".to_string(), vec![json!(4), json!(10)]),
        ];
        let err = check_stream(&signals).unwrap_err();
        assert!(matches!(err, FezError::AccessDenied { .. }));
    }

    #[test]
    fn other_error_code_is_dbus() {
        let signals = vec![(
            "ErrorCode".to_string(),
            vec![json!(4), json!("package not found")],
        )];
        let err = check_stream(&signals).unwrap_err();
        assert!(matches!(err, FezError::Dbus { .. }));
    }

    #[test]
    fn clean_stream_is_ok() {
        let signals = vec![
            (
                "Package".to_string(),
                vec![json!(8), json!("htop;1;x86_64;fedora"), json!("")],
            ),
            ("Finished".to_string(), vec![json!(1), json!(20)]),
        ];
        assert!(check_stream(&signals).is_ok());
    }

    #[test]
    fn plan_buckets_by_info_enum() {
        let signals = vec![
            (
                "Package".to_string(),
                vec![json!(8), json!("nginx;1;x86_64;fedora"), json!("")],
            ),
            (
                "Package".to_string(),
                vec![json!(9), json!("htop;1;x86_64;installed"), json!("")],
            ),
            (
                "Package".to_string(),
                vec![json!(7), json!("bash;2;x86_64;updates"), json!("")],
            ),
            ("Finished".to_string(), vec![json!(1), json!(20)]),
        ];
        let plan = plan_from(&signals);
        assert_eq!(plan.install, vec!["nginx-1.x86_64"]);
        assert_eq!(plan.remove, vec!["htop-1.x86_64"]);
        assert_eq!(plan.remove_names, vec!["htop"]);
        assert_eq!(plan.upgrade, vec!["bash-2.x86_64"]);
        assert!(plan.downgrade.is_empty());
    }

    #[test]
    fn method_for_maps_verbs() {
        let ids = vec!["nginx;1;x86_64;fedora".to_string()];
        assert_eq!(method_for("install", TF_NONE, &ids).0, "InstallPackages");
        assert_eq!(method_for("upgrade", TF_NONE, &ids).0, "UpdatePackages");
        let (m, args) = method_for("remove", TF_SIMULATE, &ids);
        assert_eq!(m, "RemovePackages");
        // RemovePackages carries (flags, ids, allow_deps, autoremove).
        assert_eq!(args.as_array().unwrap().len(), 4);
    }

    #[test]
    fn dry_run_plan_view_has_null_size_and_backend() {
        let plan = PkPlan {
            install: vec!["nginx-1.x86_64".into()],
            remove: vec![],
            upgrade: vec![],
            downgrade: vec![],
            remove_names: vec![],
        };
        let view = plan_view("install", "host", &["nginx".to_string()], &plan, true);
        assert_eq!(view.kind, "PackagePlan");
        assert_eq!(view.data["backend"], json!("packagekit"));
        assert_eq!(view.data["install_size_total"], Value::Null);
        assert_eq!(view.data["dry_run"], json!(true));
        assert_eq!(view.data["counts"]["install"], json!(1));
    }
}