use serenity::model::application::CommandDataOption;
use super::{build_commands, translate};
use crate::session::commands::COMMANDS;
fn text_option(name: &str, value: &str) -> CommandDataOption {
serde_json::from_value(serde_json::json!({
"name": name,
"type": 3,
"value": value,
}))
.expect("a string option")
}
fn user_option(name: &str, id: u64) -> CommandDataOption {
serde_json::from_value(serde_json::json!({
"name": name,
"type": 6,
"value": id.to_string(),
}))
.expect("a user option")
}
#[test]
fn an_interaction_becomes_the_text_command_it_stands_for() {
let command = translate(
"cat",
&[text_option("path", "src/main.ts")],
"thread-1",
true,
"u1",
"amelia",
);
assert_eq!(command.content, "!cat src/main.ts");
assert_eq!(command.thread_id.as_deref(), Some("thread-1"));
assert_eq!(command.user_id, "u1");
assert_eq!(command.user_name, "amelia");
}
#[test]
fn a_command_naming_an_account_carries_the_account_id() {
let command = translate(
"allow",
&[user_option("user", 900_000_000_000_000_002)],
"thread-1",
true,
"u1",
"amelia",
);
assert_eq!(command.content, "!allow 900000000000000002");
}
#[test]
fn a_command_with_no_argument_is_just_the_command() {
let command = translate("status", &[], "chan", false, "u1", "amelia");
assert_eq!(command.content, "!status");
assert_eq!(command.thread_id, None);
}
#[test]
fn every_command_is_offered_as_a_slash_command_too() {
assert_eq!(build_commands().len(), COMMANDS.len());
for (name, _) in COMMANDS {
assert_eq!(
super::slash_name(name),
name.strip_prefix('!').unwrap_or(name)
);
}
}
#[test]
fn a_commands_description_says_who_may_run_it() {
let named = |name: &str| {
COMMANDS
.iter()
.find(|(command, _)| *command == name)
.map(|(_, meta)| super::describe_for_test(meta.access, meta.summary))
.unwrap_or_default()
};
assert!(named("!stop").contains("owner only"));
assert!(named("!ls").contains("owner and invited"));
assert!(named("!shutdown").contains("named accounts"));
let status = named("!status");
assert!(!status.contains('('));
}
#[test]
fn a_command_that_takes_an_argument_declares_it() {
let option_of = |name: &str| {
super::TEXT_OPTION
.iter()
.find(|(command, _, _, _)| *command == name)
};
assert_eq!(
option_of("!cat").map(|(_, _, _, required)| *required),
Some(true)
);
assert_eq!(
option_of("!ls").map(|(_, _, _, required)| *required),
Some(false)
);
assert_eq!(option_of("!status"), None);
}