midenup 1.0.0

The Miden toolchain manager
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
//! Install, activation and update as *one* operation with three intent policies.
//!
//! Everything here is a property of resolution against a manifest, so the components are `file://`
//! stand-ins: one executable, so activation has something to run, and assets for the rest.

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

use clap::Parser;
use midenup::{commands::Midenup, config::Config, state::LocalState};

mod common;

use common::*;

/// One component in a fixture manifest: name, profiles, requirements.
type Spec<'a> = (&'a str, &'a [&'a str], &'a [&'a str]);

/// Local artifacts plus a manifest writer, so a test can describe a channel in one line.
struct Fixture {
    dir: PathBuf,
}

impl Fixture {
    fn new(root: &Path) -> Self {
        let dir = root.join("fixture");
        std::fs::create_dir_all(&dir).unwrap();

        let vm = dir.join("miden-vm");
        std::fs::write(&vm, "#!/bin/sh\nexit 0\n").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&vm, std::fs::Permissions::from_mode(0o755)).unwrap();
        }

        Self { dir }
    }

    fn artifact(&self, name: &str) -> String {
        let path = self.dir.join(format!("{name}.txt"));
        if !path.exists() {
            std::fs::write(&path, format!("{name}\n")).unwrap();
        }
        format!("file://{}", path.display())
    }

    fn component(&self, (name, profiles, requires): Spec<'_>) -> serde_json::Value {
        // `vm` is the one executable, so `miden vm` has something to run.
        if name == "vm" {
            return serde_json::json!({
                "name": "vm",
                "version": {"kind": "registry", "version": "0.1.0"},
                "kind": "executable",
                "installation_method": {"kind": "prebuilt"},
                "installed-executable": "miden-vm",
                "profiles": profiles,
                "requires": requires,
                "artifacts": {
                    "miden-vm": {"uri": format!("file://{}", self.dir.join("miden-vm").display())}
                }
            });
        }

        serde_json::json!({
            "name": name,
            "version": {"kind": "registry", "version": "0.1.0"},
            "kind": "asset",
            "profiles": profiles,
            "requires": requires,
            "artifacts": {format!("{name}.txt"): {"uri": self.artifact(name)}}
        })
    }

    /// As [`Fixture::manifest`], with an extra alias declared on `vm`.
    ///
    /// An alias is the canonical *runtime-metadata-only* change: recorded in local state, resolved
    /// at dispatch, reflected in no installed file.
    fn manifest_with_vm_alias(&self, file: &str, components: &[Spec<'_>], alias: &str) -> String {
        let mut components: Vec<serde_json::Value> =
            components.iter().map(|spec| self.component(*spec)).collect();
        for component in components.iter_mut() {
            if component["name"] == serde_json::json!("vm") {
                component["aliases"] = serde_json::json!({alias: ["%installed-executable", "run"]});
            }
        }
        self.write(file, components)
    }

    /// A `command` component with a `format` prefix and two subcommands, plus the asset its
    /// format refers to.
    fn manifest_with_command(&self, file: &str) -> String {
        let compose = self.dir.join("docker-compose.yml");
        std::fs::write(&compose, "services: {}\n").unwrap();

        let node = serde_json::json!({
            "name": "node",
            "version": {"kind": "registry", "version": "0.1.0"},
            "kind": "command",
            "profiles": ["minimal"],
            "format": ["docker", "compose", "-f", "%etc(node/docker-compose.yml)"],
            "subcommands": {
                "up": ["up", "-d"],
                "down": ["down"]
            },
            "artifacts": {
                "docker-compose.yml": {"uri": format!("file://{}", compose.display())}
            }
        });

        self.write(file, vec![self.component(("vm", &["minimal"], &[])), node])
    }

    /// Two executables that both declare the alias `run`, neither in any profile.
    ///
    /// Nothing installs them together by default, which is the situation section 8.5 is about: a
    /// superset that accreted both from different projects.
    fn manifest_with_conflicting_aliases(&self, file: &str) -> String {
        let executable = |name: &str| {
            let binary = self.dir.join(format!("miden-{name}"));
            std::fs::write(&binary, "#!/bin/sh\nexit 0\n").unwrap();
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755)).unwrap();
            }

            serde_json::json!({
                "name": name,
                "version": {"kind": "registry", "version": "0.1.0"},
                "kind": "executable",
                "installation_method": {"kind": "prebuilt"},
                "installed-executable": format!("miden-{name}"),
                "profiles": [],
                "aliases": {"run": ["%installed-executable", "run"]},
                "artifacts": {
                    format!("miden-{name}"): {"uri": format!("file://{}", binary.display())}
                }
            })
        };

        self.write(
            file,
            vec![
                self.component(("vm", &["minimal"], &[])),
                executable("first"),
                executable("second"),
            ],
        )
    }

    /// Writes a one-channel manifest and returns its URI.
    fn manifest(&self, file: &str, components: &[Spec<'_>]) -> String {
        self.write(file, components.iter().map(|spec| self.component(*spec)).collect())
    }

    fn write(&self, file: &str, components: Vec<serde_json::Value>) -> String {
        let manifest = serde_json::json!({
            "manifest_version": "3.0.0",
            "date": 1735689600,
            "networks": {"mainnet": "0.15.0"},
            "channels": [{
                "name": "0.15.0",
                "components": components
            }]
        });

        let path = self.dir.join(file);
        std::fs::write(&path, serde_json::to_string_pretty(&manifest).unwrap()).unwrap();
        format!("file://{}", path.display())
    }
}

/// A `Config` rooted in `project`, so `miden-toolchain.toml` discovery finds that project's file.
fn config_in(env: &TestEnvironment, project: &Path, manifest_uri: &str) -> Config {
    Config::init(
        project.to_path_buf(),
        env.midenup_home.clone(),
        env.cargo_home.clone(),
        manifest_uri,
        true,
    )
    .expect("failed to build config")
}

fn project(env: &TestEnvironment, name: &str, components: &[&str]) -> PathBuf {
    let dir = env.tmp_dir.path().join(name);
    std::fs::create_dir_all(&dir).unwrap();

    let components = components
        .iter()
        .map(|name| format!("\"{name}\""))
        .collect::<Vec<_>>()
        .join(", ");
    std::fs::write(
        dir.join("miden-toolchain.toml"),
        format!("[toolchain]\nchannel = \"0.15.0\"\ncomponents = [{components}]\n"),
    )
    .unwrap();

    dir
}

/// Runs `miden help vm` from `project`, which activates that project's toolchain.
fn activate(env: &TestEnvironment, project: &Path, manifest_uri: &str, state: &mut LocalState) {
    let config = config_in(env, project, manifest_uri);
    Midenup::try_parse_from(["miden", "help", "vm"])
        .unwrap()
        .execute_with_state(&config, state)
        .expect("activation failed");
}

fn installed(state: &LocalState) -> Vec<String> {
    let mut names: Vec<String> = state
        .get(&semver::Version::new(0, 15, 0))
        .map(|installation| installation.components.iter().map(|c| c.name.to_string()).collect())
        .unwrap_or_default();
    names.sort();
    names
}

/// Activating one project must never take away what another project asked for.
///
/// Activation resolves against the union of recorded intent rather than against a channel narrowed
/// to the activating project, so switching back and forth adds and never removes.
#[test]
fn integration_switching_between_two_projects_is_additive_in_both_directions() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("two_projects");
    let fixture = Fixture::new(env.tmp_dir.path());
    let manifest = fixture.manifest(
        "manifest.json",
        &[("vm", &["minimal"], &[]), ("debug", &[], &[]), ("client", &[], &[])],
    );

    let first = project(&env, "project-a", &["debug"]);
    let second = project(&env, "project-b", &["client"]);
    let mut state = LocalState::default();

    activate(&env, &first, &manifest, &mut state);
    assert_eq!(installed(&state), vec!["debug", "vm"]);

    activate(&env, &second, &manifest, &mut state);
    assert_eq!(installed(&state), vec!["client", "debug", "vm"], "activation must add");

    activate(&env, &first, &manifest, &mut state);
    assert_eq!(
        installed(&state),
        vec!["client", "debug", "vm"],
        "switching back must not remove the other project's component"
    );
}

/// A direct install is the documented way to shrink a channel back to a known set -- and a project
/// that still wants more gets it back on its next activation.
#[test]
fn integration_direct_install_can_shrink_and_activation_re_adds() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("shrink_then_readd");
    let fixture = Fixture::new(env.tmp_dir.path());
    let manifest =
        fixture.manifest("manifest.json", &[("vm", &["minimal"], &[]), ("debug", &[], &[])]);

    let dir = project(&env, "project-a", &["debug"]);
    let mut state = LocalState::default();

    activate(&env, &dir, &manifest, &mut state);
    assert_eq!(installed(&state), vec!["debug", "vm"]);

    let config = config_in(&env, env.tmp_dir.path(), &manifest);
    Midenup::try_parse_from(["midenup", "install", "0.15.0", "--profile", "minimal"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to install");
    assert_eq!(
        installed(&state),
        vec!["vm"],
        "a direct install replaces intent, and may shrink"
    );

    activate(&env, &dir, &manifest, &mut state);
    assert_eq!(installed(&state), vec!["debug", "vm"], "the project's request is re-added");
}

/// `profiles` are re-resolved on update, so a `minimal` installation gains a component newly
/// tagged `minimal` upstream.
///
/// `update stable` resolves the channel as upstream defines it; it is not narrowed to the set of
/// component names already installed locally, so a component with no local counterpart can appear.
#[test]
fn integration_a_minimal_installation_receives_newly_profiled_components() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("new_profile_members");
    let fixture = Fixture::new(env.tmp_dir.path());

    let before = fixture.manifest("before.json", &[("vm", &["minimal"], &[])]);
    let (mut state, config) = test_setup(&env, &before);
    Midenup::try_parse_from(["midenup", "install", "0.15.0", "--profile", "minimal"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to install");
    assert_eq!(installed(&state), vec!["vm"]);

    let after = fixture
        .manifest("after.json", &[("vm", &["minimal"], &[]), ("newthing", &["minimal"], &[])]);
    let (_, config) = test_setup(&env, &after);
    Midenup::try_parse_from(["midenup", "update", "0.15.0"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to update");

    assert_eq!(installed(&state), vec!["newthing", "vm"]);
}

/// A roots-only installation gains new *dependencies* of its roots, but not unrelated components
/// that merely joined a profile it never asked for.
#[test]
fn integration_a_roots_only_installation_gains_dependencies_but_not_unrelated_members() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("roots_only_update");
    let fixture = Fixture::new(env.tmp_dir.path());

    let before =
        fixture.manifest("before.json", &[("vm", &["minimal"], &[]), ("client", &[], &[])]);
    let (mut state, config) = test_setup(&env, &before);
    Midenup::try_parse_from([
        "midenup",
        "install",
        "0.15.0",
        "--profile",
        "empty",
        "--component",
        "client",
    ])
    .unwrap()
    .execute_with_state(&config, &mut state)
    .expect("failed to install");
    assert_eq!(installed(&state), vec!["client"]);

    // `client` gains a dependency; `unrelated` appears in the `minimal` profile, which this
    // installation never asked for.
    let after = fixture.manifest(
        "after.json",
        &[
            ("vm", &["minimal"], &[]),
            ("client", &[], &["newdep"]),
            ("newdep", &[], &[]),
            ("unrelated", &["minimal"], &[]),
        ],
    );
    let (_, config) = test_setup(&env, &after);
    Midenup::try_parse_from(["midenup", "update", "0.15.0"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to update");

    assert_eq!(installed(&state), vec!["client", "newdep"]);
}

/// An explicit root that no longer exists upstream blocks the update, and the installation is left
/// exactly as it was. The schema has no rename declaration, so there is nothing to guess with.
#[test]
fn integration_a_removed_root_blocks_the_update_and_preserves_the_installation() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("removed_root");
    let fixture = Fixture::new(env.tmp_dir.path());

    let before =
        fixture.manifest("before.json", &[("vm", &["minimal"], &[]), ("goingaway", &[], &[])]);
    let (mut state, config) = test_setup(&env, &before);
    Midenup::try_parse_from([
        "midenup",
        "install",
        "0.15.0",
        "--profile",
        "empty",
        "--component",
        "goingaway",
    ])
    .unwrap()
    .execute_with_state(&config, &mut state)
    .expect("failed to install");
    assert_eq!(installed(&state), vec!["goingaway"]);

    let after = fixture.manifest("after.json", &[("vm", &["minimal"], &[])]);
    let (_, config) = test_setup(&env, &after);
    let err = Midenup::try_parse_from(["midenup", "update", "0.15.0"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect_err("an update that cannot be resolved must not proceed");

    assert!(
        format!("{err:#}").contains("goingaway"),
        "the error must name the component that disappeared: {err:#}"
    );
    assert_eq!(
        installed(&LocalState::load(&midenup::paths::state_path(&env.midenup_home)).unwrap()),
        vec!["goingaway"],
        "a blocked update must leave the installation exactly as it was"
    );
}

/// Spec section 9.8: a change that no installed file reflects is committed as a single atomic
/// `state.json` write -- no journal, no staging, no new publication.
///
/// Adding an alias to a component does not force a reinstall: `is_up_to_date` compares the fields
/// that decide what gets installed, not the component's kind wholesale.
#[test]
fn integration_a_metadata_only_change_updates_state_without_republishing() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("logical_only_update");
    let fixture = Fixture::new(env.tmp_dir.path());
    let channel = semver::Version::new(0, 15, 0);

    let before = fixture.manifest("before.json", &[("vm", &["minimal"], &[])]);
    let (mut state, config) = test_setup(&env, &before);
    Midenup::try_parse_from(["midenup", "install", "0.15.0"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to install");

    let publication_of = |state: &LocalState| match &state.get(&channel).unwrap().publication {
        midenup::state::PublicationRef::Managed { id, .. } => id.clone(),
        other => panic!("expected a managed publication, got {other:?}"),
    };
    let before_publication = publication_of(&state);

    // Upstream adds an alias, and changes nothing else.
    let after = fixture.manifest_with_vm_alias("after.json", &[("vm", &["minimal"], &[])], "run");
    let (_, config) = test_setup(&env, &after);
    Midenup::try_parse_from(["midenup", "update", "0.15.0"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to update");

    let reloaded = LocalState::load(&midenup::paths::state_path(&env.midenup_home)).unwrap();
    assert!(
        reloaded.get(&channel).unwrap().as_channel().get_alias_names().contains("run"),
        "the new alias must be recorded"
    );
    assert_eq!(
        publication_of(&reloaded),
        before_publication,
        "a metadata-only change must not produce a new publication"
    );

    let publications: Vec<_> =
        std::fs::read_dir(midenup::paths::publications_dir(&env.midenup_home))
            .unwrap()
            .flatten()
            .map(|entry| entry.file_name().to_string_lossy().into_owned())
            .collect();
    assert_eq!(publications.len(), 1, "nothing was staged, so nothing was published");
}

/// Naming a subcommand a component does not declare must say which ones it does.
///
/// Spec section 13.3: `argv[1]` must name a declared subcommand, and the error lists the valid
/// ones -- a bare "invalid subcommand" leaves the user to go read the manifest.
#[test]
fn integration_an_invalid_subcommand_lists_the_valid_ones() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("invalid_subcommand");
    let fixture = Fixture::new(env.tmp_dir.path());
    let manifest = fixture.manifest_with_command("manifest.json");

    let (mut state, config) = test_setup(&env, &manifest);
    Midenup::try_parse_from(["midenup", "install", "0.15.0"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to install");

    let err = Midenup::try_parse_from(["miden", "node", "bogus"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect_err("an undeclared subcommand must not be passed through");

    let message = format!("{err:#}");
    assert!(message.contains("bogus"), "the error must name what was asked for: {message}");
    assert!(
        message.contains("up") && message.contains("down"),
        "and list what is available: {message}"
    );
}

/// A declared subcommand expands to its own words, with the component's `format` in front.
///
/// The map is consulted against the raw argument rather than through clap's `matches.subcommand()`,
/// which is always `None` for an external subcommand: going through clap leaves `miden node up`
/// passing `up` on as a literal argument, and a component whose `format` is empty -- the shipped
/// `node` among them -- then tries to execute it as a program.
#[test]
fn integration_a_declared_subcommand_expands_to_its_own_words() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("subcommand_expansion");
    let fixture = Fixture::new(env.tmp_dir.path());
    let manifest = fixture.manifest_with_command("manifest.json");

    let (mut state, config) = test_setup(&env, &manifest);
    Midenup::try_parse_from(["midenup", "install", "0.15.0"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to install");

    // `docker` is not installed here, and that is the point: the failure names what midenup tried
    // to run, which is what proves the expansion happened.
    let err = Midenup::try_parse_from(["miden", "node", "up"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect_err("docker is not available in the test environment");

    let message = format!("{err:#}");
    assert!(
        message.contains("miden node up"),
        "the failure must name the user's command: {message}"
    );

    // ...and asking for help on it lists the verbs rather than trying to run one.
    Midenup::try_parse_from(["miden", "help", "node"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("`miden help <command>` must list its subcommands, not fail");
}

/// A conflict that exists only in the superset must not break every command.
///
/// The installed set accretes components from every project that ever activated the channel, so two
/// components no project uses together could otherwise make `miden <anything>` fail. Section 8.5:
/// that is a warning, and the component in the active view wins.
#[test]
fn integration_a_superset_only_alias_conflict_does_not_break_every_command() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("alias_conflict");
    let fixture = Fixture::new(env.tmp_dir.path());
    let manifest = fixture.manifest_with_conflicting_aliases("manifest.json");

    // Install both, as two projects asking for one each would have.
    let (mut state, config) = test_setup(&env, &manifest);
    Midenup::try_parse_from(["midenup", "install", "0.15.0", "--profile", "complete"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to install");
    assert_eq!(installed(&state), vec!["first", "second", "vm"]);

    // This project wants only `first`, so only one definition of `run` is in view.
    let dir = project(&env, "project-a", &["first"]);
    let config = config_in(&env, &dir, &manifest);

    Midenup::try_parse_from(["miden", "help", "vm"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("a superset-only conflict must not be fatal");

    Midenup::try_parse_from(["miden", "run"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("and the alias must resolve to the component in the active view");
}

/// A conflict *within* the active view is a real ambiguity: this project asked for both, and
/// `miden run` has no defensible answer.
#[test]
fn integration_an_alias_conflict_inside_the_active_view_is_an_error() {
    let _guard = common::harness::mutating_test_guard();
    let env = environment_setup("alias_conflict_in_view");
    let fixture = Fixture::new(env.tmp_dir.path());
    let manifest = fixture.manifest_with_conflicting_aliases("manifest.json");

    let (mut state, config) = test_setup(&env, &manifest);
    Midenup::try_parse_from(["midenup", "install", "0.15.0", "--profile", "complete"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect("failed to install");

    let dir = project(&env, "project-both", &["first", "second"]);
    let config = config_in(&env, &dir, &manifest);

    let err = Midenup::try_parse_from(["miden", "run"])
        .unwrap()
        .execute_with_state(&config, &mut state)
        .expect_err("an ambiguous alias in the active view must be reported");

    let message = format!("{err:#}");
    assert!(message.contains("run"), "the error must name the alias: {message}");
    assert!(
        message.contains("first") && message.contains("second"),
        "and both components that define it: {message}"
    );
}

/// Spec section 8.6: local state carries no partial flag. The display derives it by comparing what
/// is installed against the complete upstream channel -- and when upstream is unavailable, simply
/// does not show it rather than guessing.
#[test]
fn integration_partial_status_is_derived_from_upstream_not_stored() {
    let _guard = common::harness::mutating_test_guard();
    // Not named "partial": the temp directory path ends up inside state.json, in artifact URIs.
    let env = environment_setup("derived_status");
    let fixture = Fixture::new(env.tmp_dir.path());
    let manifest =
        fixture.manifest("manifest.json", &[("vm", &["minimal"], &[]), ("extra", &[], &[])]);

    let midenup = |manifest_uri: &str, args: &[&str]| {
        midenup_command(env!("CARGO_BIN_EXE_midenup"), &env, manifest_uri)
            .args(args)
            .output()
            .expect("failed to run midenup")
    };

    let installed = midenup(&manifest, &["install", "0.15.0", "--profile", "minimal"]);
    assert!(installed.status.success(), "{}", String::from_utf8_lossy(&installed.stderr));

    let raw = std::fs::read_to_string(midenup::paths::state_path(&env.midenup_home)).unwrap();
    assert!(!raw.contains("partial"), "state must not persist a partial flag: {raw}");

    let shown = midenup(&manifest, &["show", "list"]);
    assert!(
        String::from_utf8_lossy(&shown.stdout).contains("partially installed"),
        "with upstream available it must be derived and shown: {}",
        String::from_utf8_lossy(&shown.stdout)
    );
    assert!(
        String::from_utf8_lossy(&shown.stdout).contains("(mainnet"),
        "and so must the networks naming the channel: {}",
        String::from_utf8_lossy(&shown.stdout)
    );

    // With upstream unavailable it is not shown -- and never guessed at. The cached manifest would
    // answer, so it goes too.
    std::fs::remove_file(midenup::paths::manifest_cache(&env.midenup_home)).unwrap();
    let offline = midenup("https://127.0.0.1:1/nope.json", &["show", "list"]);
    assert!(offline.status.success(), "listing what is installed must work offline");

    let stdout = String::from_utf8_lossy(&offline.stdout);
    assert!(
        stdout.contains("0.15.0"),
        "the installed channel must still be listed: {stdout}"
    );
    assert!(!stdout.contains("partial"), "but its partial status must not be: {stdout}");
    assert!(!stdout.contains("mainnet"), "nor the networks naming it: {stdout}");
}