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); }