use clap::Parser;
use sapphire_sync_cli::cli::{AppCommand, Cli, FrameworkCommand, SyncSubcommand};
fn parse(args: &[&str]) -> Cli {
Cli::try_parse_from(std::iter::once("sapphire-sync").chain(args.iter().copied()))
.unwrap_or_else(|err| panic!("{args:?} must parse: {err}"))
}
#[test]
fn a_bare_invocation_prints_the_help() {
let err = Cli::try_parse_from(["sapphire-sync"])
.err()
.expect("a bare invocation does not parse");
assert_eq!(
err.kind(),
clap::error::ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
);
}
#[test]
fn serve_parses_as_the_framework_verb() {
let cli = parse(&["serve"]);
match cli.app {
AppCommand::Framework(FrameworkCommand::Serve) => {}
other => panic!("`serve` must parse as the framework's, got {other:?}"),
}
}
#[test]
fn status_parses() {
let cli = parse(&["status"]);
match cli.app {
AppCommand::Framework(FrameworkCommand::Status) => {}
other => panic!("`status` must parse as the framework's, got {other:?}"),
}
}
#[test]
fn the_service_subcommands_parse() {
for args in [
vec!["service", "install"],
vec!["service", "uninstall"],
vec!["service", "status"],
] {
let cli = parse(&args);
match cli.app {
AppCommand::Framework(FrameworkCommand::Service(_)) => {}
other => panic!("{args:?} must parse as the framework's service, got {other:?}"),
}
}
}
#[test]
fn the_workspace_subcommands_parse() {
for args in [
vec!["workspace", "init"],
vec!["workspace", "init", "notes"],
vec!["workspace", "init", "--sync"],
vec!["workspace", "select", "notes"],
vec!["workspace", "show"],
vec![
"workspace",
"init",
"--sync",
"--remote-workspace",
"notes",
"/srv/notes",
],
] {
let cli = parse(&args);
match cli.app {
AppCommand::Framework(FrameworkCommand::Workspace(_)) => {}
other => panic!("{args:?} must parse as the framework's workspace, got {other:?}"),
}
}
}
#[test]
fn the_api_key_commands_parse() {
for args in [
vec!["api-key", "list"],
vec!["external-device", "list"],
] {
let cli = parse(&args);
match cli.app {
AppCommand::Framework(
FrameworkCommand::ApiKey(_) | FrameworkCommand::ExternalDevice(_),
) => {}
other => panic!("{args:?} must parse as the framework's, got {other:?}"),
}
}
}
#[test]
fn the_bridges_verbs_are_not_the_apps() {
for args in [
["workgroup", "list"],
["device", "list"],
["embedding", "show"],
] {
assert!(
Cli::try_parse_from(std::iter::once("sapphire-sync").chain(args)).is_err(),
"{args:?} must not parse: it is sapphire-bridge's"
);
}
}
#[test]
fn the_apps_own_sync_verb_parses() {
let cli = parse(&["sync", "disable"]);
match cli.app {
AppCommand::Sync(command) => {
assert!(matches!(command.command, SyncSubcommand::Disable));
}
other => panic!("`sync disable` must parse as the app's, got {other:?}"),
}
}
#[test]
fn the_apps_sync_and_the_frameworks_groups_coexist() {
let Err(help) = Cli::try_parse_from(["sapphire-sync", "--help"]) else {
panic!("--help must not parse as success")
};
let text = help.render().to_string();
for verb in ["sync", "workspace", "api-key", "config", "status", "serve"] {
assert!(text.contains(verb), "--help must list {verb}: {text}");
}
}