use clap::Parser;
use crate::cli::{Cli, Command, RepairAction};
#[test]
fn cli_parses_repair_deploy() {
let cli = Cli::try_parse_from(["trusty-mpm", "repair", "deploy"]).unwrap();
match cli.command.unwrap() {
Command::Repair {
action: RepairAction::Deploy { force },
} => assert!(!force),
other => panic!("expected Command::Repair(Deploy), got {other:?}"),
}
}
#[test]
fn cli_parses_repair_push_guard() {
let cli = Cli::try_parse_from(["trusty-mpm", "repair", "push-guard"]).unwrap();
match cli.command.unwrap() {
Command::Repair {
action: RepairAction::PushGuard { path, dry_run },
} => {
assert_eq!(path, None, "the default target is the current directory");
assert!(!dry_run, "a bare invocation must actually install");
}
other => panic!("expected Command::Repair(PushGuard), got {other:?}"),
}
}
#[test]
fn cli_parses_repair_push_guard_flags() {
let cli = Cli::try_parse_from([
"trusty-mpm",
"repair",
"push-guard",
"--path",
"/tmp/some-clone",
"--dry-run",
])
.unwrap();
match cli.command.unwrap() {
Command::Repair {
action: RepairAction::PushGuard { path, dry_run },
} => {
assert_eq!(path, Some("/tmp/some-clone".to_string()));
assert!(dry_run);
}
other => panic!("expected Command::Repair(PushGuard), got {other:?}"),
}
}
#[test]
fn cli_parses_repair_session_store() {
let cli = Cli::try_parse_from(["trusty-mpm", "repair", "session-store"]).unwrap();
match cli.command.unwrap() {
Command::Repair {
action:
RepairAction::SessionStore {
path,
dry_run,
force,
},
} => {
assert_eq!(path, None, "the default target is the real store");
assert!(!dry_run, "a bare invocation actually repairs");
assert!(!force, "a bare invocation refuses an orphaned tail");
}
other => panic!("expected Command::Repair(SessionStore), got {other:?}"),
}
}
#[test]
fn cli_parses_repair_delegation() {
let cli =
Cli::try_parse_from(["trusty-mpm", "repair", "delegation", "af20cc838b2b30a55"]).unwrap();
match cli.command.unwrap() {
Command::Repair {
action: RepairAction::Delegation {
agent_id, force, ..
},
} => {
assert_eq!(agent_id.as_deref(), Some("af20cc838b2b30a55"));
assert!(
!force,
"a bare invocation must not assert the owner is gone"
);
}
other => panic!("expected Command::Repair(Delegation), got {other:?}"),
}
}
#[test]
fn cli_parses_repair_delegation_by_id_and_list_8257() {
let by_id = Cli::try_parse_from([
"trusty-mpm",
"repair",
"delegation",
"--delegation-id",
"64237d1c-0aa4-4090-bd14-d3c273da7e95",
])
.unwrap();
match by_id.command.unwrap() {
Command::Repair {
action:
RepairAction::Delegation {
agent_id,
delegation_id,
list,
..
},
} => {
assert_eq!(agent_id, None);
assert_eq!(
delegation_id.as_deref(),
Some("64237d1c-0aa4-4090-bd14-d3c273da7e95")
);
assert_eq!(list, None);
}
other => panic!("expected Command::Repair(Delegation), got {other:?}"),
}
let list = Cli::try_parse_from(["trusty-mpm", "repair", "delegation", "--list"]).unwrap();
match list.command.unwrap() {
Command::Repair {
action: RepairAction::Delegation { list, .. },
} => assert_eq!(list, Some(std::path::PathBuf::from("."))),
other => panic!("expected Command::Repair(Delegation), got {other:?}"),
}
assert!(Cli::try_parse_from(["trusty-mpm", "repair", "delegation"]).is_err());
assert!(
Cli::try_parse_from(["trusty-mpm", "repair", "delegation", "--list", "--force"]).is_err()
);
}
#[test]
fn cli_rejects_a_caller_session_argument_8257() {
let owner = "64237d1c-0aa4-4090-bd14-d3c273da7e95";
for extra in [
vec!["--session", owner],
vec!["--session-id", owner],
vec!["--caller-session", owner],
vec!["--owner", owner],
vec![owner],
] {
let mut argv = vec!["trusty-mpm", "repair", "delegation", "a0wner"];
argv.extend(extra.iter().copied());
assert!(
Cli::try_parse_from(&argv).is_err(),
"`{}` must not parse: no argument names the caller session",
argv.join(" ")
);
}
}
#[test]
fn cli_parses_repair_delegation_force() {
let cli = Cli::try_parse_from([
"trusty-mpm",
"repair",
"delegation",
"af20cc838b2b30a55",
"--force",
])
.unwrap();
match cli.command.unwrap() {
Command::Repair {
action: RepairAction::Delegation { force, .. },
} => assert!(force),
other => panic!("expected Command::Repair(Delegation), got {other:?}"),
}
}
#[test]
fn cli_parses_repair_savings_ledger() {
let cli = Cli::try_parse_from(["trusty-mpm", "repair", "savings-ledger"]).unwrap();
match cli.command.unwrap() {
Command::Repair {
action:
RepairAction::SavingsLedger {
root,
apply,
dry_run,
markers,
},
} => {
assert_eq!(root, None, "the default target is the real framework root");
assert!(!apply, "a bare invocation writes nothing");
assert!(!dry_run, "dry-run is the default, not a flag it must carry");
assert!(!markers, "a bare invocation leaves the markers alone");
}
other => panic!("expected Command::Repair(SavingsLedger), got {other:?}"),
}
}
#[test]
fn cli_parses_repair_savings_ledger_flags() {
let cli = Cli::try_parse_from([
"trusty-mpm",
"repair",
"savings-ledger",
"--root",
"/tmp/rehearsal",
"--apply",
"--markers",
])
.unwrap();
match cli.command.unwrap() {
Command::Repair {
action:
RepairAction::SavingsLedger {
root,
apply,
dry_run,
markers,
},
} => {
assert_eq!(root, Some("/tmp/rehearsal".to_string()));
assert!(apply);
assert!(!dry_run);
assert!(markers);
}
other => panic!("expected Command::Repair(SavingsLedger), got {other:?}"),
}
}
#[test]
fn cli_rejects_repair_savings_ledger_apply_with_dry_run() {
assert!(
Cli::try_parse_from([
"trusty-mpm",
"repair",
"savings-ledger",
"--apply",
"--dry-run",
])
.is_err(),
"--apply and --dry-run must not be combinable"
);
}
#[test]
fn cli_parses_repair_session_store_flags() {
let cli = Cli::try_parse_from([
"trusty-mpm",
"repair",
"session-store",
"--path",
"/tmp/sessions.json",
"--dry-run",
"--force",
])
.unwrap();
match cli.command.unwrap() {
Command::Repair {
action:
RepairAction::SessionStore {
path,
dry_run,
force,
},
} => {
assert_eq!(path, Some("/tmp/sessions.json".to_string()));
assert!(dry_run);
assert!(force);
}
other => panic!("expected Command::Repair(SessionStore), got {other:?}"),
}
}