Fix compilation, add tests

This commit is contained in:
SkyfallWasTaken 2024-07-23 09:35:27 +01:00
parent d377973c32
commit 4b0e3605f4
2 changed files with 61 additions and 2 deletions

View file

@ -84,7 +84,7 @@ pub fn format_deleted_item(item: &ShopItem) -> String {
} }
} }
pub fn get_slack_blocks(diffs: &Vec<String>) -> serde_json::Value { pub fn get_slack_body(diffs: &Vec<String>) -> serde_json::Value {
let mut blocks_vec = vec![]; let mut blocks_vec = vec![];
for diff in diffs { for diff in diffs {
blocks_vec.push(json!({ blocks_vec.push(json!({
@ -113,6 +113,64 @@ pub fn get_slack_blocks(diffs: &Vec<String>) -> serde_json::Value {
}) })
} }
#[cfg(test)]
mod slack_tests {
use super::*;
use pretty_assertions::assert_eq;
use serde_json::json;
#[test]
fn slack_body_is_correct() {
let body = get_slack_body(&vec!["Test 1".into(), "Test 2".into(), "Test 3".into()]);
assert_eq!(
body,
json!({
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Test 1"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Test 2"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Test 3"
}
},
{
"type": "divider"
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": format!("Arcade Monitor v{}", env!("CARGO_PKG_VERSION"))
}
]
}
]
})
)
}
}
#[cfg(test)] #[cfg(test)]
mod format_new_tests { mod format_new_tests {
use super::*; use super::*;
@ -146,6 +204,7 @@ mod format_new_tests {
mod diff_tests { mod diff_tests {
use super::*; use super::*;
use indoc::indoc; use indoc::indoc;
use pretty_assertions::assert_eq;
#[test] #[test]
fn price_diff() { fn price_diff() {

View file

@ -60,7 +60,7 @@ async fn run_scrape(env: Env) -> Result<String> {
let changes = result.join("\n\n"); let changes = result.join("\n\n");
// slack webhook // slack webhook
let slack_body = format::get_slack_blocks(&result); let slack_body = format::get_slack_body(&result);
client client
.post(&slack_webhook_url) .post(&slack_webhook_url)
.body(slack_body.to_string()) .body(slack_body.to_string())