142 lines
4.2 KiB
Rust
142 lines
4.2 KiB
Rust
use rustbot::bot::module::{Action, Chat, Command, Module};
|
|
use rustbot::bot::modules::karma::{parse_changes, Karma};
|
|
|
|
fn chat<'a>(sender: &'a str, text: &'a str) -> Chat<'a> {
|
|
Chat {
|
|
sender,
|
|
reply_to: "#c",
|
|
text,
|
|
prefix: ">",
|
|
network: "t",
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn parses_increments_and_decrements() {
|
|
assert_eq!(
|
|
parse_changes("foo++ bar-- baz", "someone"),
|
|
vec![("foo".to_string(), 1), ("bar".to_string(), -1)]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn strips_trailing_punctuation_and_parens() {
|
|
let c = parse_changes("nice work rust++, and (team)++!", "someone");
|
|
assert!(c.contains(&("rust".to_string(), 1)), "{c:?}");
|
|
assert!(c.contains(&("team".to_string(), 1)), "{c:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn no_self_karma_case_insensitive() {
|
|
assert!(parse_changes("me++", "me").is_empty());
|
|
assert!(parse_changes("Me++", "me").is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn dedupes_within_a_message() {
|
|
assert_eq!(
|
|
parse_changes("foo++ foo++ foo++", "x"),
|
|
vec![("foo".to_string(), 1)]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn bumps_persist_case_insensitively() {
|
|
let path = std::env::temp_dir().join("rubot-test-karma-a.json");
|
|
let _ = std::fs::remove_file(&path);
|
|
let mut k = Karma::with_path(path.clone());
|
|
k.on_message(&chat("alice", "Rust++ rust++ python--"));
|
|
|
|
let reloaded = Karma::with_path(path.clone());
|
|
assert_eq!(reloaded.get("rust"), 1); // deduped to one bump
|
|
assert_eq!(reloaded.get("RUST"), 1);
|
|
assert_eq!(reloaded.get("python"), -1);
|
|
let _ = std::fs::remove_file(&path);
|
|
}
|
|
|
|
#[test]
|
|
fn command_reports_karma() {
|
|
let path = std::env::temp_dir().join("rubot-test-karma-b.json");
|
|
let _ = std::fs::remove_file(&path);
|
|
let mut k = Karma::with_path(path.clone());
|
|
k.on_message(&chat("a", "foo++ foo++"));
|
|
let cmd = Command {
|
|
sender: "a",
|
|
reply_to: "#c",
|
|
name: "karma",
|
|
args: &["foo"],
|
|
prefix: ">",
|
|
network: "t",
|
|
};
|
|
let out = match k.on_command(&cmd).as_slice() {
|
|
[Action::Reply(s)] => s.clone(),
|
|
_ => panic!("expected one reply"),
|
|
};
|
|
assert!(out.contains("foo has 1 karma"), "{out}");
|
|
let _ = std::fs::remove_file(&path);
|
|
}
|
|
|
|
fn reply_of(k: &mut Karma, name: &str, args: &[&str]) -> String {
|
|
let cmd = Command {
|
|
sender: "z",
|
|
reply_to: "#c",
|
|
name,
|
|
args,
|
|
prefix: ">",
|
|
network: "t",
|
|
};
|
|
match k.on_command(&cmd).as_slice() {
|
|
[Action::Reply(s)] => s.clone(),
|
|
_ => panic!("expected one reply"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn ignores_single_char_and_numeric_targets() {
|
|
assert_eq!(
|
|
parse_changes("i++ 5++ x-- ok++", "s"),
|
|
vec![("ok".to_string(), 1)]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn report_shows_rank() {
|
|
let path = std::env::temp_dir().join("rubot-test-karma-rank.json");
|
|
let _ = std::fs::remove_file(&path);
|
|
let mut k = Karma::with_path(path.clone());
|
|
k.on_message(&chat("a", "alpha++"));
|
|
k.on_message(&chat("b", "alpha++"));
|
|
k.on_message(&chat("c", "alpha++ beta++"));
|
|
assert!(reply_of(&mut k, "karma", &["alpha"]).contains("alpha has 3 karma (#1 of 2)"));
|
|
assert!(reply_of(&mut k, "karma", &["beta"]).contains("beta has 1 karma (#2 of 2)"));
|
|
let _ = std::fs::remove_file(&path);
|
|
}
|
|
|
|
#[test]
|
|
fn unseen_thing_reports_no_karma() {
|
|
let path = std::env::temp_dir().join("rubot-test-karma-unseen.json");
|
|
let _ = std::fs::remove_file(&path);
|
|
let mut k = Karma::with_path(path.clone());
|
|
assert!(reply_of(&mut k, "karma", &["ghost"]).contains("no karma for 'ghost' yet"));
|
|
let _ = std::fs::remove_file(&path);
|
|
}
|
|
|
|
#[test]
|
|
fn leaderboard_top_and_bottom() {
|
|
let path = std::env::temp_dir().join("rubot-test-karma-top.json");
|
|
let _ = std::fs::remove_file(&path);
|
|
let mut k = Karma::with_path(path.clone());
|
|
k.on_message(&chat("a", "high++ mid++ low--"));
|
|
k.on_message(&chat("b", "high++"));
|
|
let top = reply_of(&mut k, "karma", &[]);
|
|
assert!(
|
|
top.starts_with("karma top:") && top.contains("high (2)"),
|
|
"{top}"
|
|
);
|
|
let bottom = reply_of(&mut k, "karmabottom", &[]);
|
|
assert!(
|
|
bottom.starts_with("karma bottom:") && bottom.contains("low (-1)"),
|
|
"{bottom}"
|
|
);
|
|
let _ = std::fs::remove_file(&path);
|
|
}
|