//! Every text here is invented. use serde_json::{json, Map, Value}; use super::withhold; fn args(value: Value) -> Map { value.as_object().cloned().unwrap_or_default() } #[test] fn every_string_of_an_answer_loses_the_code_and_keeps_its_words() { let answer = json!({ "messages": [ { "thread_id": "t1", "channel": "sms", "body": "Your Examplepay code is 482913", "code": "482913", "code_for": "Examplepay" }, { "thread_id": "t1", "channel": "sms", "body": "see you at 6" } ], "preview": "482913 is your Exa…" }); let said = withhold(answer, &args(json!({ "thread_id": "t1" }))).to_string(); assert!(!said.contains("482913"), "{said}"); assert!(said.contains("Your Examplepay code is"), "{said}"); assert!( said.contains("Examplepay\""), "what it is for stays: {said}" ); assert!(said.contains("see you at 6")); assert!(!said.contains("\"code\":"), "{said}"); } #[test] fn a_search_for_a_codes_digits_finds_nothing_of_it() { let answer = json!({ "hits": [ { "kind": "message", "snippet": "Your Examplepay code is 482913" }, { "kind": "message", "snippet": "the gate code changed" } ]}); let said = withhold(answer, &args(json!({ "query": "482 913" }))); let hits = said["hits"].as_array().unwrap(); assert_eq!(hits.len(), 1, "{said}"); assert_eq!(hits[0]["snippet"], "the gate code changed"); } #[test] fn an_answer_with_no_code_is_handed_back_untouched() { let answer = json!({ "rows": [{ "body": "meet at gate 4412", "code": "x" }] }); assert_eq!(withhold(answer.clone(), &Map::new()), answer); }