archon-cli 0.1.1

Cross-distro package manager (Arch/Debian/Fedora) with AUR support and a security-monitored (eBPF) build sandbox on Arch
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
//! CLI integration tests: exercise the compiled `archon` binary as a black
//! box. Every test here runs against a fixture sync database (built
//! in-memory, see `common::fixture_sync_dir`) via `$ARCHON_SYNC_DIR` —
//! none of them touch the real system's pacman databases, the real AUR,
//! or need root. The few things that genuinely can't be tested this way
//! (the eBPF monitor, real sandboxed builds) are covered by the unit
//! tests in `archon-sandbox`/`archon-monitor` and by manual live testing —
//! see the README's Status section.

mod common;

use common::{
    FixturePkg, UNREACHABLE_AUR_URL, empty_aur_server, empty_cache_dir, empty_local_dir, fixture_local_dir,
    fixture_os_release, fixture_sync_dir, run_archon,
};

#[test]
fn help_lists_all_subcommands() {
    let out = run_archon(&["--help"], &[]);
    assert!(out.status.success());
    assert!(out.stdout.contains("install"));
    assert!(out.stdout.contains("plan"));
    assert!(out.stdout.contains("remove"));
    assert!(out.stdout.contains("build"));
}

#[test]
fn remove_help_lists_flags() {
    let out = run_archon(&["remove", "--help"], &[]);
    assert!(out.status.success());
    assert!(out.stdout.contains("--dry-run"));
    assert!(out.stdout.contains("--yes"));
}

#[test]
fn remove_rejects_package_that_is_not_installed() {
    let (_local, local_dir) = empty_local_dir();
    let local_dir = local_dir.to_string_lossy().into_owned();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["remove", "not-installed-xyz", "--dry-run"],
        &[("ARCHON_LOCAL_DIR", &local_dir), ("ARCHON_CACHE_DIR", &cache_dir)],
    );

    assert!(!out.status.success());
    assert!(out.stderr.contains("not installed"), "got: {}", out.stderr);
}

#[test]
fn remove_dry_run_shows_plan_and_stops_before_pacman() {
    let (_local, local_dir) =
        fixture_local_dir(&[FixturePkg { name: "leaf", version: "1.0-1", depends: &[] }]);
    let local_dir = local_dir.to_string_lossy().into_owned();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["remove", "leaf", "--dry-run"],
        &[("ARCHON_LOCAL_DIR", &local_dir), ("ARCHON_CACHE_DIR", &cache_dir)],
    );

    assert!(out.status.success(), "stderr: {}", out.stderr);
    assert!(out.stdout.contains("Remove plan"));
    assert!(out.stdout.contains("leaf"));
    assert!(out.stdout.contains("1.0-1"));
    assert!(
        !out.stdout.contains("pacman -Rs leaf"),
        "dry-run must never reach the actual pacman invocation: {}",
        out.stdout
    );
}

#[test]
fn remove_all_or_nothing_when_one_package_is_missing() {
    let (_local, local_dir) = fixture_local_dir(&[FixturePkg { name: "leaf", version: "1.0-1", depends: &[] }]);
    let local_dir = local_dir.to_string_lossy().into_owned();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["remove", "leaf", "also-not-installed", "--dry-run"],
        &[("ARCHON_LOCAL_DIR", &local_dir), ("ARCHON_CACHE_DIR", &cache_dir)],
    );

    assert!(!out.status.success());
    assert!(out.stderr.contains("also-not-installed"));
    assert!(
        !out.stdout.contains("Remove plan"),
        "must not show a partial plan when part of the request is invalid: {}",
        out.stdout
    );
}

#[test]
fn install_help_lists_report_flag() {
    let out = run_archon(&["install", "--help"], &[]);
    assert!(out.status.success());
    assert!(out.stdout.contains("--report"));
    assert!(out.stdout.contains("--dry-run"));
    assert!(out.stdout.contains("--yes"));
}

#[test]
fn dry_run_resolves_and_stops_before_any_mutation() {
    let (_dir, sync_dir) = fixture_sync_dir(&[FixturePkg { name: "leaf", version: "1.0-1", depends: &[] }]);
    let sync_dir = sync_dir.to_string_lossy().into_owned();
    let (_local, local_dir) = empty_local_dir();
    let local_dir = local_dir.to_string_lossy().into_owned();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["install", "leaf", "--dry-run"],
        &[
            ("ARCHON_SYNC_DIR", &sync_dir),
            ("ARCHON_AUR_URL", UNREACHABLE_AUR_URL),
            ("ARCHON_LOCAL_DIR", &local_dir),
            ("ARCHON_CACHE_DIR", &cache_dir),
        ],
    );

    assert!(out.status.success(), "stderr: {}", out.stderr);
    assert!(out.stdout.contains("Install plan"));
    assert!(out.stdout.contains("leaf"));
    assert!(out.stdout.contains("1 official"));
    assert!(
        !out.stdout.contains("pacman -S"),
        "dry-run must never reach the pacman invocation: {}",
        out.stdout
    );
}

#[test]
fn dry_run_shows_transitive_dependencies() {
    let (_dir, sync_dir) = fixture_sync_dir(&[
        FixturePkg { name: "leaf", version: "1.0-1", depends: &[] },
        FixturePkg { name: "top", version: "2.0-1", depends: &["leaf"] },
    ]);
    let sync_dir = sync_dir.to_string_lossy().into_owned();
    let (_local, local_dir) = empty_local_dir();
    let local_dir = local_dir.to_string_lossy().into_owned();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["install", "top", "--dry-run"],
        &[
            ("ARCHON_SYNC_DIR", &sync_dir),
            ("ARCHON_AUR_URL", UNREACHABLE_AUR_URL),
            ("ARCHON_LOCAL_DIR", &local_dir),
            ("ARCHON_CACHE_DIR", &cache_dir),
        ],
    );

    assert!(out.status.success(), "stderr: {}", out.stderr);
    assert!(out.stdout.contains("top"));
    assert!(out.stdout.contains("leaf"));
    assert!(out.stdout.contains("required by: top"));
}

#[test]
fn plan_is_equivalent_to_install_dry_run() {
    let (_dir, sync_dir) = fixture_sync_dir(&[FixturePkg { name: "leaf", version: "1.0-1", depends: &[] }]);
    let sync_dir = sync_dir.to_string_lossy().into_owned();
    let (_local, local_dir) = empty_local_dir();
    let local_dir = local_dir.to_string_lossy().into_owned();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["plan", "leaf"],
        &[
            ("ARCHON_SYNC_DIR", &sync_dir),
            ("ARCHON_AUR_URL", UNREACHABLE_AUR_URL),
            ("ARCHON_LOCAL_DIR", &local_dir),
            ("ARCHON_CACHE_DIR", &cache_dir),
        ],
    );

    assert!(out.status.success(), "stderr: {}", out.stderr);
    assert!(out.stdout.contains("Install plan"));
    assert!(out.stdout.contains("leaf"));
}

#[test]
fn package_not_found_anywhere_fails_with_clear_error() {
    let (_dir, sync_dir) = fixture_sync_dir(&[FixturePkg { name: "leaf", version: "1.0-1", depends: &[] }]);
    let sync_dir = sync_dir.to_string_lossy().into_owned();
    let aur_url = empty_aur_server();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["install", "does-not-exist-anywhere", "--dry-run"],
        &[
            ("ARCHON_SYNC_DIR", &sync_dir),
            ("ARCHON_AUR_URL", &aur_url),
            ("ARCHON_CACHE_DIR", &cache_dir),
        ],
    );

    assert!(!out.status.success(), "expected failure, stdout: {}", out.stdout);
    assert!(
        out.stderr.contains("not found"),
        "expected a clear not-found error, got: {}",
        out.stderr
    );
}

#[test]
fn not_found_suggests_similarly_named_official_packages() {
    // Regression case: `archon install cuttlefish` used to just fail with
    // "not found", even though `cuttlefish-base-git` exists — no exact
    // package is named `cuttlefish` at all, so exact lookup alone can never
    // find it, and the user has no way to discover the real name.
    let (_dir, sync_dir) =
        fixture_sync_dir(&[FixturePkg { name: "cuttlefish-base-git", version: "1:r1-1", depends: &[] }]);
    let sync_dir = sync_dir.to_string_lossy().into_owned();
    let aur_url = empty_aur_server();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["install", "cuttlefish", "--dry-run"],
        &[
            ("ARCHON_SYNC_DIR", &sync_dir),
            ("ARCHON_AUR_URL", &aur_url),
            ("ARCHON_CACHE_DIR", &cache_dir),
        ],
    );

    assert!(!out.status.success(), "expected failure, stdout: {}", out.stdout);
    assert!(out.stderr.contains("not found"), "got: {}", out.stderr);
    assert!(
        out.stderr.contains("cuttlefish-base-git"),
        "expected a 'did you mean' suggestion naming the real package, got: {}",
        out.stderr
    );
}

#[test]
fn search_finds_official_matches_by_substring() {
    let (_dir, sync_dir) = fixture_sync_dir(&[
        FixturePkg { name: "firefox", version: "128.0-1", depends: &[] },
        FixturePkg { name: "firefox-developer-edition", version: "129.0-1", depends: &[] },
        FixturePkg { name: "unrelated", version: "1.0-1", depends: &[] },
    ]);
    let sync_dir = sync_dir.to_string_lossy().into_owned();
    let aur_url = empty_aur_server();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["search", "firefox"],
        &[
            ("ARCHON_SYNC_DIR", &sync_dir),
            ("ARCHON_AUR_URL", &aur_url),
            ("ARCHON_CACHE_DIR", &cache_dir),
        ],
    );

    assert!(out.status.success(), "stdout: {}\nstderr: {}", out.stdout, out.stderr);
    assert!(out.stdout.contains("firefox"));
    assert!(out.stdout.contains("firefox-developer-edition"));
    assert!(!out.stdout.contains("unrelated"));
}

#[test]
fn search_with_no_matches_still_succeeds() {
    let (_dir, sync_dir) = fixture_sync_dir(&[FixturePkg { name: "leaf", version: "1.0-1", depends: &[] }]);
    let sync_dir = sync_dir.to_string_lossy().into_owned();
    let aur_url = empty_aur_server();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["search", "does-not-exist-anywhere"],
        &[
            ("ARCHON_SYNC_DIR", &sync_dir),
            ("ARCHON_AUR_URL", &aur_url),
            ("ARCHON_CACHE_DIR", &cache_dir),
        ],
    );

    assert!(out.status.success(), "a search with no hits is not an error: {}", out.stderr);
}

#[test]
fn install_dry_run_on_debian_delegates_to_apt() {
    let (_os, os_release) = fixture_os_release("ID=ubuntu\nID_LIKE=debian\n");
    let os_release = os_release.to_string_lossy().into_owned();

    let out = run_archon(&["install", "firefox", "--dry-run"], &[("ARCHON_OS_RELEASE", &os_release)]);

    assert!(out.status.success(), "stdout: {}\nstderr: {}", out.stdout, out.stderr);
    assert!(out.stdout.contains("Debian/Ubuntu"), "got: {}", out.stdout);
    assert!(out.stdout.contains("apt install firefox"), "got: {}", out.stdout);
}

#[test]
fn install_yes_on_fedora_passes_through_assume_yes_flag() {
    let (_os, os_release) = fixture_os_release("ID=fedora\n");
    let os_release = os_release.to_string_lossy().into_owned();

    let out = run_archon(
        &["install", "firefox", "--dry-run", "--yes"],
        &[("ARCHON_OS_RELEASE", &os_release)],
    );

    assert!(out.status.success(), "stdout: {}\nstderr: {}", out.stdout, out.stderr);
    assert!(out.stdout.contains("dnf install -y firefox"), "got: {}", out.stdout);
}

#[test]
fn install_on_unsupported_distro_fails_naming_what_was_detected() {
    let (_os, os_release) = fixture_os_release("ID=slackware\n");
    let os_release = os_release.to_string_lossy().into_owned();

    let out = run_archon(&["install", "firefox", "--dry-run"], &[("ARCHON_OS_RELEASE", &os_release)]);

    assert!(!out.status.success(), "expected failure, stdout: {}", out.stdout);
    assert!(out.stderr.contains("slackware"), "got: {}", out.stderr);
    assert!(out.stderr.contains("no supported package manager"), "got: {}", out.stderr);
}

#[test]
fn remove_dry_run_on_fedora_delegates_to_dnf() {
    let (_os, os_release) = fixture_os_release("ID=fedora\n");
    let os_release = os_release.to_string_lossy().into_owned();

    let out = run_archon(&["remove", "firefox", "--dry-run"], &[("ARCHON_OS_RELEASE", &os_release)]);

    assert!(out.status.success(), "stdout: {}\nstderr: {}", out.stdout, out.stderr);
    assert!(out.stdout.contains("Fedora/RHEL"), "got: {}", out.stdout);
    assert!(out.stdout.contains("dnf remove firefox"), "got: {}", out.stdout);
}

#[test]
fn install_dry_run_on_nixos_uses_user_profile_without_sudo() {
    let (_os, os_release) = fixture_os_release("ID=nixos\n");
    let os_release = os_release.to_string_lossy().into_owned();

    let out = run_archon(&["install", "firefox", "--dry-run"], &[("ARCHON_OS_RELEASE", &os_release)]);

    assert!(out.status.success(), "stdout: {}\nstderr: {}", out.stdout, out.stderr);
    assert!(out.stdout.contains("NixOS"), "got: {}", out.stdout);
    // Channel-attribute install, and critically NO sudo — nix-env installs
    // into the invoking user's profile; sudo would target root's instead.
    assert!(out.stdout.contains("nix-env -iA nixos.firefox"), "got: {}", out.stdout);
    assert!(!out.stdout.contains("sudo nix-env"), "nix-env must not run under sudo: {}", out.stdout);
}

#[test]
fn install_dry_run_on_opensuse_delegates_to_zypper() {
    let (_os, os_release) = fixture_os_release("ID=opensuse-tumbleweed\nID_LIKE=\"opensuse suse\"\n");
    let os_release = os_release.to_string_lossy().into_owned();

    let out = run_archon(&["install", "vim", "--dry-run"], &[("ARCHON_OS_RELEASE", &os_release)]);

    assert!(out.status.success(), "stdout: {}\nstderr: {}", out.stdout, out.stderr);
    assert!(out.stdout.contains("sudo zypper install vim"), "got: {}", out.stdout);
}

#[test]
fn install_dry_run_on_alpine_delegates_to_apk() {
    let (_os, os_release) = fixture_os_release("ID=alpine\n");
    let os_release = os_release.to_string_lossy().into_owned();

    let out = run_archon(&["install", "vim", "--dry-run"], &[("ARCHON_OS_RELEASE", &os_release)]);

    assert!(out.status.success(), "stdout: {}\nstderr: {}", out.stdout, out.stderr);
    assert!(out.stdout.contains("sudo apk add vim"), "got: {}", out.stdout);
}

#[test]
fn remove_dry_run_on_void_and_gentoo_delegate_natively() {
    let (_os_void, void_release) = fixture_os_release("ID=void\n");
    let void_release = void_release.to_string_lossy().into_owned();
    let out = run_archon(&["remove", "vim", "--dry-run"], &[("ARCHON_OS_RELEASE", &void_release)]);
    assert!(out.status.success(), "stderr: {}", out.stderr);
    assert!(out.stdout.contains("sudo xbps-remove -R vim"), "got: {}", out.stdout);

    let (_os_gentoo, gentoo_release) = fixture_os_release("ID=gentoo\n");
    let gentoo_release = gentoo_release.to_string_lossy().into_owned();
    let out = run_archon(&["remove", "vim", "--dry-run"], &[("ARCHON_OS_RELEASE", &gentoo_release)]);
    assert!(out.status.success(), "stderr: {}", out.stderr);
    assert!(out.stdout.contains("sudo emerge --ask --depclean vim"), "got: {}", out.stdout);
}

#[test]
fn install_report_on_non_arch_warns_and_falls_back_to_plain_install() {
    let (_os, os_release) = fixture_os_release("ID=debian\n");
    let os_release = os_release.to_string_lossy().into_owned();

    let out = run_archon(
        &["install", "firefox", "--dry-run", "--report"],
        &[("ARCHON_OS_RELEASE", &os_release)],
    );

    assert!(out.status.success(), "stdout: {}\nstderr: {}", out.stdout, out.stderr);
    assert!(
        out.stdout.contains("only Arch/AUR has today"),
        "expected a note that --report isn't available here, got: {}",
        out.stdout
    );
    assert!(out.stdout.contains("apt install firefox"), "got: {}", out.stdout);
}

#[test]
fn missing_sync_dir_fails_with_actionable_error() {
    let out = run_archon(
        &["install", "leaf", "--dry-run"],
        &[
            ("ARCHON_SYNC_DIR", "/nonexistent/path/archon-test-fixture"),
            ("ARCHON_AUR_URL", UNREACHABLE_AUR_URL),
        ],
    );
    assert!(!out.status.success());
    assert!(out.stderr.contains("failed to load official repo databases"));
}

#[test]
fn report_flag_without_root_fails_before_any_prompt() {
    let (_dir, sync_dir) = fixture_sync_dir(&[FixturePkg { name: "leaf", version: "1.0-1", depends: &[] }]);
    let sync_dir = sync_dir.to_string_lossy().into_owned();
    let (_cache, cache_dir) = empty_cache_dir();
    let cache_dir = cache_dir.to_string_lossy().into_owned();

    let out = run_archon(
        &["install", "leaf", "--report", "--yes"],
        &[
            ("ARCHON_SYNC_DIR", &sync_dir),
            ("ARCHON_AUR_URL", UNREACHABLE_AUR_URL),
            ("ARCHON_CACHE_DIR", &cache_dir),
        ],
    );

    // This test suite never runs as root, so --report must fail cleanly —
    // and critically, it must fail *before* trying to prompt for anything
    // or touch pacman, matching the "no privileged operation without a
    // clear confirmation step" contract from the README/design notes.
    assert!(!out.status.success());
    assert!(out.stderr.contains("root") || out.stderr.contains("sudo"), "got: {}", out.stderr);
    assert!(!out.stdout.contains("sudo pacman"));
}

#[test]
fn build_command_rejects_directory_without_pkgbuild() {
    let dir = tempfile::tempdir().unwrap();
    let out = run_archon(&["build", &dir.path().to_string_lossy()], &[]);
    assert!(!out.status.success());
    assert!(out.stderr.contains("PKGBUILD"));
}

#[test]
fn build_command_rejects_nonexistent_directory() {
    let out = run_archon(&["build", "/nonexistent/archon-test-dir-xyz"], &[]);
    assert!(!out.status.success());
}

#[test]
fn build_help_lists_report_flag() {
    let out = run_archon(&["build", "--help"], &[]);
    assert!(out.status.success());
    assert!(out.stdout.contains("--report"));
}

/// The one test that actually runs a sandboxed build — skipped
/// automatically if `bwrap` isn't on this machine, since that's a real
/// system dependency unit tests elsewhere already document as required.
#[test]
fn build_command_succeeds_on_benign_demo_package() {
    if std::process::Command::new("bwrap").arg("--version").output().is_err() {
        eprintln!("skipping: bwrap not available");
        return;
    }

    let demo_src = concat!(env!("CARGO_MANIFEST_DIR"), "/../../demo/archon-demo-benign");
    let dir = tempfile::tempdir().unwrap();
    let dest = dir.path().join("archon-demo-benign");
    copy_dir(std::path::Path::new(demo_src), &dest);

    let out = run_archon(&["build", &dest.to_string_lossy()], &[]);
    assert!(out.status.success(), "stdout: {}\nstderr: {}", out.stdout, out.stderr);
    assert!(out.stdout.contains(".pkg.tar"));

    let has_pkg_file = std::fs::read_dir(&dest)
        .unwrap()
        .filter_map(|e| e.ok())
        .any(|e| e.file_name().to_string_lossy().contains(".pkg.tar"));
    assert!(has_pkg_file, "expected a .pkg.tar* file in {}", dest.display());
}

fn copy_dir(src: &std::path::Path, dst: &std::path::Path) {
    std::fs::create_dir_all(dst).unwrap();
    for entry in std::fs::read_dir(src).unwrap() {
        let entry = entry.unwrap();
        let dest_path = dst.join(entry.file_name());
        if entry.file_type().unwrap().is_dir() {
            copy_dir(&entry.path(), &dest_path);
        } else {
            std::fs::copy(entry.path(), &dest_path).unwrap();
        }
    }
}