use super::*;
use crate::cli::clap::render_help;
use crate::cli::common::NetworkOutputOptions;
use crate::nns::neuron::{NeuronCacheCommand, NeuronCommand};
use ic_query::nns::neuron::DEFAULT_NNS_NEURON_SOURCE_ENDPOINT;
use ic_query::nns::neuron::NNS_NEURON_MAX_PAGE_SIZE;
#[test]
fn nns_neuron_options_parse_defaults_and_explicit_values() {
let (list_format, list) = parse_test_options(neuron_list_command(), &[], |matches, network| {
crate::cli::clap::parsed_options::<NeuronListOptions>(matches)
.into_request(network, TEST_NOW_UNIX_SECS)
})
.expect("list defaults");
assert_eq!(list.governance.network, MAINNET_NETWORK);
assert_eq!(list_format, OutputFormat::Text);
assert_eq!(
replica_endpoint(&list.governance),
DEFAULT_NNS_NEURON_SOURCE_ENDPOINT
);
assert_eq!(list.limit, 25);
assert_eq!(list.exclusive_start_neuron_id, None);
let max_page_size = NNS_NEURON_MAX_PAGE_SIZE.to_string();
let (list_format, list) = parse_test_options(
neuron_list_command(),
&[
"--limit",
&max_page_size,
"--start-neuron-id",
"123",
"--verbose",
"--json",
],
|matches, network| {
crate::cli::clap::parsed_options::<NeuronListOptions>(matches)
.into_request(network, TEST_NOW_UNIX_SECS)
},
)
.expect("explicit list options");
assert_eq!(list.limit, NNS_NEURON_MAX_PAGE_SIZE);
assert_eq!(list.exclusive_start_neuron_id, Some(123));
assert_eq!(list_format, OutputFormat::Json);
let (_, info) = parse_test_options(
neuron_info_command(),
&["456", "--verbose"],
|matches, network| {
crate::cli::clap::parsed_options::<NeuronInfoOptions>(matches)
.into_request(network, TEST_NOW_UNIX_SECS)
},
)
.expect("info options");
assert_eq!(info.neuron_id, 456);
let refresh = parse_test_options(
neuron_refresh_command(),
&["--page-size", "100", "--max-pages", "2"],
paged_refresh_options,
)
.expect("refresh options");
assert_eq!(refresh.page_size, 100);
assert_eq!(refresh.max_pages, Some(2));
let cache = parse_test_options(
neuron_cache_status_command(),
&["--json"],
NetworkOutputOptions::from_matches,
)
.expect("cache options");
assert_eq!(cache.network, MAINNET_NETWORK);
assert_eq!(cache.format, OutputFormat::Json);
}
#[test]
fn nns_neuron_rejects_invalid_numeric_values() {
assert!(
parse_test_options(
neuron_list_command(),
&["--limit", "0"],
|matches, network| crate::cli::clap::parsed_options::<NeuronListOptions>(matches)
.into_request(network, TEST_NOW_UNIX_SECS),
)
.is_err()
);
assert!(
parse_test_options(
neuron_refresh_command(),
&["--page-size", "301"],
paged_refresh_options,
)
.is_err()
);
assert!(
parse_test_options(neuron_info_command(), &["0"], |matches, network| {
crate::cli::clap::parsed_options::<NeuronInfoOptions>(matches)
.into_request(network, TEST_NOW_UNIX_SECS)
},)
.is_err()
);
}
#[test]
fn nns_neuron_help_advertises_collection_modes_and_commands() {
assert!(render_help(command()).contains("neuron"));
let family = render_help(neuron_command());
assert!(family.contains("list"));
assert!(family.contains("info"));
assert!(family.contains("refresh"));
assert!(family.contains("cache"));
assert!(family.contains("distribution"));
let matches = parse_test_matches(neuron_distribution_command(), &["--json"]).unwrap();
assert_eq!(
NetworkOutputOptions::from_matches(&matches, MAINNET_NETWORK).format,
OutputFormat::Json
);
assert!(render_help(neuron_distribution_command()).contains("does not make a network request"));
assert!(render_help(neuron_list_command()).contains("Cache-preferred read"));
assert!(render_help(neuron_info_command()).contains("Cache-preferred read"));
assert!(render_help(neuron_refresh_command()).contains("Forced live refresh"));
assert!(render_help(neuron_cache_command()).contains("Local cache inspection"));
assert!(render_help(neuron_cache_status_command()).contains("does not make a network request"));
}
#[test]
fn typed_neuron_operations_keep_their_admitted_arguments() {
type Case<'a> = (&'a [&'a str], fn(NeuronCommand) -> bool);
let cases: [Case<'_>; 5] = [
(
&["list", "--limit", "3", "--verbose"],
|command| matches!(command, NeuronCommand::List(options) if options.limit == 3 && options.verbose),
),
(
&["info", "7"],
|command| matches!(command, NeuronCommand::Info(options) if options.neuron_id == 7),
),
(
&["distribution", "--json"],
|command| matches!(command, NeuronCommand::Distribution(options) if options.json),
),
(
&["refresh", "--max-pages", "2"],
|command| matches!(command, NeuronCommand::Refresh(options) if options.page_size == 300 && options.max_pages == Some(2)),
),
(
&["cache", "status", "--json"],
|command| matches!(command, NeuronCommand::Cache(NeuronCacheCommand::Status(options)) if options.json),
),
];
for (args, check) in cases {
let matches = parse_test_matches(neuron_command(), args).unwrap();
assert!(
check(crate::cli::clap::parsed_options(&matches)),
"{args:?}"
);
}
}