use kakao_rs::prelude::*; use std::matches; #[test] fn basic_card_test() { let mut result = Template::new(); let basic_card = BasicCard::new() .set_title("제목입니다.") .set_thumbnail("http://k.kakaocdn.net/dn/APR96/btqqH7zLanY/kD5mIPX7TdD2NAxgP29cC0/1x1.jpg"); result.add_output(basic_card.build()); let serialized = r#"{"template":{"outputs":[{"basicCard":{"title":"제목입니다.","thumbnail":{"imageUrl":"http://k.kakaocdn.net/dn/APR96/btqqH7zLanY/kD5mIPX7TdD2NAxgP29cC0/1x1.jpg"}}}]},"version":"2.0"}"#; assert_eq!(serialized, serde_json::to_string(&result).expect("Failed")); let deserialized: Template = serde_json::from_str(result.to_string().as_str()).unwrap(); assert!(matches!( deserialized.template.outputs[0], Types::Basic { .. } )); } #[test] fn item_card_test() { let mut result = Template::new(); let item_card = ItemCard::new() .set_title("title") .set_desc("desc") .set_thumbnail( "http://dev-mk.kakao.com/dn/bot/scripts/with_barcode_blue_1x1.png".to_string(), ) .set_thumbnail_width(800) .set_thumbnail_height(800) .set_image_title("DOFQTK") .set_image_desc("Boarding Number") .set_item_list_alignment("right") .set_item_list_summary("total", "$4,032.54") .add_button( Button::new(ButtonType::Link) .set_label("View Boarding Pass") .set_link("https://namu.wiki/w/%EB%82%98%EC%97%B0(TWICE)"), ) .set_button_layout("vertical"); result.add_output(item_card.build()); let serialized = r#"{"template":{"outputs":[{"itemCard":{"thumbnail":{"imageUrl":"http://dev-mk.kakao.com/dn/bot/scripts/with_barcode_blue_1x1.png","width":800,"height":800},"imageTitle":{"title":"DOFQTK","description":"Boarding Number"},"itemList":[],"itemListAlignment":"right","itemListSummary":{"title":"total","description":"$4,032.54"},"title":"title","description":"desc","buttons":[{"label":"View Boarding Pass","action":"webLink","webLinkUrl":"https://namu.wiki/w/%EB%82%98%EC%97%B0(TWICE)"}],"buttonLayout":"vertical"}}]},"version":"2.0"}"#; assert_eq!(serialized, serde_json::to_string(&result).expect("Failed")); let deserialized: Template = serde_json::from_str(result.to_string().as_str()).unwrap(); assert!(matches!( deserialized.template.outputs[0], Types::Item { .. } )); } #[test] fn text_card_test() { let mut result = Template::new(); let text_card = TextCard::new() .set_title("챗봇 관리자센터에 오신 것을 환영합니다.") .set_description("챗봇 관리자센터로 챗봇을 제작해 보세요.") .add_button( Button::new(ButtonType::Link) .set_label("View Boarding Pass") .set_link("https://namu.wiki/w/%EB%82%98%EC%97%B0(TWICE)"), ); result.add_output(text_card.build()); let serialized = r#"{"template":{"outputs":[{"textCard":{"title":"챗봇 관리자센터에 오신 것을 환영합니다.","description":"챗봇 관리자센터로 챗봇을 제작해 보세요.","buttons":[{"label":"View Boarding Pass","action":"webLink","webLinkUrl":"https://namu.wiki/w/%EB%82%98%EC%97%B0(TWICE)"}]}}]},"version":"2.0"}"#; assert_eq!(serialized, serde_json::to_string(&result).expect("Failed")); let deserialized: Template = serde_json::from_str(result.to_string().as_str()).unwrap(); assert!(matches!( deserialized.template.outputs[0], Types::Text { .. } )); } #[test] fn carousel_all_cards_test() { let mut result = Template::new(); result.add_qr(QuickReply::new("빠른 응답", "빠른 응답 ㅋㅋ")); let mut carousel = Carousel::new(); carousel.set_header("오늘 공지 n개", "n개를 더 불러왔습니다!", "https://"); let basic_card = BasicCard::new() .set_title("1번") .set_thumbnail("http://k.kakaocdn.net/dn/APR96/btqqH7zLanY/kD5mIPX7TdD2NAxgP29cC0/1x1.jpg"); let text_card = TextCard::new() .set_title("챗봇 관리자센터에 오신 것을 환영합니다.") .set_description("챗봇 관리자센터로 챗봇을 제작해 보세요.") .add_button( Button::new(ButtonType::Link) .set_label("View Boarding Pass") .set_link("https://namu.wiki/w/%EB%82%98%EC%97%B0(TWICE)"), ); let item_card = ItemCard::new() .set_title("title") .set_desc("desc") .set_thumbnail("http://dev-mk.kakao.com/dn/bot/scripts/with_barcode_blue_1x1.png") .set_thumbnail_width(800) .set_thumbnail_height(800) .set_image_title("DOFQTK") .set_image_desc("Boarding Number") .set_item_list_alignment("right") .set_item_list_summary("total", "$4,032.54") .add_button( Button::new(ButtonType::Link) .set_label("View Boarding Pass") .set_link("https://namu.wiki/w/%EB%82%98%EC%97%B0(TWICE)"), ) .set_button_layout("vertical"); let commerce_card = CommerceCard::new() .set_desc("커머스 카드") .set_price(15000) .set_thumbnail("http://dev-mk.kakao.com/dn/bot/scripts/with_barcode_blue_1x1.png"); let list_card = ListCard::new("챗봇 관리자센터를 소개합니다.").add_item( ListItem::new("title") .set_desc("description") .set_link("https://naver.com"), ); carousel.add_card(text_card.build_card()); carousel.add_card(basic_card.build_card()); carousel.add_card(item_card.build_card()); carousel.add_card(commerce_card.build_card()); carousel.add_card(list_card.build_card()); result.add_output(carousel.build()); let serialized = r#"{"template":{"outputs":[{"carousel":{"type":"listCard","items":[{"title":"챗봇 관리자센터에 오신 것을 환영합니다.","description":"챗봇 관리자센터로 챗봇을 제작해 보세요.","buttons":[{"label":"View Boarding Pass","action":"webLink","webLinkUrl":"https://namu.wiki/w/%EB%82%98%EC%97%B0(TWICE)"}]},{"title":"1번","thumbnail":{"imageUrl":"http://k.kakaocdn.net/dn/APR96/btqqH7zLanY/kD5mIPX7TdD2NAxgP29cC0/1x1.jpg"}},{"thumbnail":{"imageUrl":"http://dev-mk.kakao.com/dn/bot/scripts/with_barcode_blue_1x1.png","width":800,"height":800},"imageTitle":{"title":"DOFQTK","description":"Boarding Number"},"itemList":[],"itemListAlignment":"right","itemListSummary":{"title":"total","description":"$4,032.54"},"title":"title","description":"desc","buttons":[{"label":"View Boarding Pass","action":"webLink","webLinkUrl":"https://namu.wiki/w/%EB%82%98%EC%97%B0(TWICE)"}],"buttonLayout":"vertical"},{"description":"커머스 카드","price":15000,"currency":"","thumbnails":[{"imageUrl":"http://dev-mk.kakao.com/dn/bot/scripts/with_barcode_blue_1x1.png"}]},{"header":{"title":"챗봇 관리자센터를 소개합니다."},"items":[{"title":"title","description":"description","link":{"web":"https://naver.com"}}]}],"header":{"title":"오늘 공지 n개","description":"n개를 더 불러왔습니다!","thumbnail":{"imageUrl":"https://"}}}}],"quickReplies":[{"action":"message","label":"빠른 응답","messageText":"빠른 응답 ㅋㅋ"}]},"version":"2.0"}"#; assert_eq!(serialized, serde_json::to_string(&result).expect("Failed")); }