BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / tests / adaptive_card.rs

adaptive_card.rs

adaptive_card.rs 11.4 KiB Download Raw
1use serde_json::json;
2use teams_core::{
3 AdaptiveCard, CardAction, CardActionKind, CardElement, ColumnWidth, ImageSize, Span, TextColor,
4 TextSize, ToggleTarget, VerticalAlignment, card_content_text,
5};
6
7fn digest_card() -> String {
8 json!({
9 "type": "AdaptiveCard",
10 "msTeams": {"width": "Full"},
11 "body": [
12 {"type": "ColumnSet", "columns": [
13 {"type": "Column", "width": "auto", "verticalContentAlignment": "center", "items": [
14 {"type": "Image", "url": "https://img.example.com/person.png", "size": "small", "style": "person", "height": "32px"}
15 ]},
16 {"type": "Column", "width": "stretch", "items": [
17 {"type": "TextBlock", "size": "medium", "weight": "bolder", "wrap": true, "text": "Ada Example edited your page"}
18 ]}
19 ]},
20 {"type": "ColumnSet", "separator": true, "columns": [
21 {"type": "Column", "width": "40px", "items": [
22 {"type": "Image", "url": "https://img.example.com/page.png", "width": "24px", "height": "24px"}
23 ]},
24 {"type": "Column", "width": 2, "items": [
25 {"type": "TextBlock", "weight": "bolder", "wrap": true, "color": "accent", "text": "[**Quarterly Plan**](https://example.com/wiki/plan) in [**Product Space**](https://example.com/wiki/space)"},
26 {"type": "TextBlock", "spacing": "small", "isSubtle": true, "text": "Owned by: Ada Example"}
27 ]}
28 ]},
29 {"type": "ActionSet", "actions": [
30 {"type": "Action.OpenUrl", "title": "View page", "url": "https://example.com/wiki/plan"},
31 {"type": "Action.OpenUrl", "title": "Unsafe", "url": "javascript:alert(1)"},
32 {"type": "Action.Submit", "title": "Watch", "data": {"action": "watch"}},
33 {"type": "Action.Submit", "title": "Hidden", "isVisible": false}
34 ]}
35 ]
36 })
37 .to_string()
38}
39
40#[test]
41fn digest_card_keeps_layout_text_and_actions() {
42 let card = AdaptiveCard::parse(&digest_card()).unwrap();
43 assert!(card.full_width);
44 assert_eq!(card.items.len(), 3);
45
46 let CardElement::Columns(columns) = &card.items[0].element else {
47 panic!("first item is a column set");
48 };
49 assert_eq!(columns[0].width, ColumnWidth::Auto);
50 assert_eq!(columns[0].vertical_alignment, VerticalAlignment::Center);
51 let CardElement::Image(image) = &columns[0].items[0].element else {
52 panic!("avatar column holds an image");
53 };
54 assert!(image.person);
55 assert_eq!(image.size, ImageSize::Small);
56 assert_eq!(image.height, Some(32.));
57 let CardElement::Text(title) = &columns[1].items[0].element else {
58 panic!("title column holds text");
59 };
60 assert!(title.bold && title.wrap);
61 assert_eq!(title.size, TextSize::Medium);
62
63 assert!(card.items[1].separator);
64 let CardElement::Columns(columns) = &card.items[1].element else {
65 panic!("second item is a column set");
66 };
67 assert_eq!(columns[0].width, ColumnWidth::Pixels(40.));
68 assert_eq!(columns[1].width, ColumnWidth::Weighted(2.));
69 let CardElement::Text(line) = &columns[1].items[0].element else {
70 panic!("text expected");
71 };
72 assert_eq!(line.color, TextColor::Accent);
73 assert_eq!(
74 line.spans[0],
75 Span::Link {
76 url: "https://example.com/wiki/plan".to_owned(),
77 children: vec![Span::Bold(vec![Span::Text("Quarterly Plan".to_owned())])],
78 }
79 );
80 let CardElement::Text(owner) = &columns[1].items[1].element else {
81 panic!("text expected");
82 };
83 assert!(owner.subtle);
84
85 let CardElement::Actions(actions) = &card.items[2].element else {
86 panic!("third item is an action set");
87 };
88 assert_eq!(actions.len(), 3);
89 assert_eq!(
90 actions[0].kind,
91 CardActionKind::OpenUrl("https://example.com/wiki/plan".to_owned())
92 );
93 assert_eq!(actions[1].kind, CardActionKind::Unsupported);
94 assert_eq!(actions[2].title, "Watch");
95 let CardActionKind::Submit(submit) = &actions[2].kind else {
96 panic!("watch is a submit action");
97 };
98 assert_eq!(submit.data, json!({"action": "watch"}));
99 assert!(actions[2].is_clickable());
100}
101
102fn card_actions(actions: serde_json::Value) -> Vec<CardAction> {
103 let card = json!({"body": [{"type": "TextBlock", "text": "x"}], "actions": actions});
104 AdaptiveCard::parse(&card.to_string()).unwrap().actions
105}
106
107#[test]
108fn plain_submit_sends_its_data_as_a_message_back() {
109 let actions = card_actions(json!([
110 {"type": "Action.Submit", "title": "Unwatch", "data": {"type": "unwatch", "page": "p1"}}
111 ]));
112 let payload = actions[0].invoke_payload().unwrap();
113 assert_eq!(payload.name, "messageback");
114 assert_eq!(payload.value, json!({"type": "unwatch", "page": "p1"}));
115}
116
117#[test]
118fn task_fetch_submit_wraps_data_without_the_teams_settings() {
119 let actions = card_actions(json!([
120 {"type": "Action.Submit", "title": "Bell", "data": {"cardType": "n", "msteams": {"type": "task/fetch"}}}
121 ]));
122 let payload = actions[0].invoke_payload().unwrap();
123 assert_eq!(payload.name, "task/fetch");
124 assert_eq!(
125 payload.value,
126 json!({"data": {"cardType": "n", "type": "task/fetch"}, "context": {"theme": "dark"}})
127 );
128}
129
130#[test]
131fn message_back_and_im_back_send_the_teams_value() {
132 let actions = card_actions(json!([
133 {"type": "Action.Submit", "title": "A", "data": {"msteams": {"type": "messageBack", "value": {"k": 1}}}},
134 {"type": "Action.Submit", "title": "B", "data": {"msteams": {"type": "imBack"}, "k": 2}}
135 ]));
136 let first = actions[0].invoke_payload().unwrap();
137 assert_eq!((first.name, first.value), ("messageback", json!({"k": 1})));
138 let second = actions[1].invoke_payload().unwrap();
139 assert_eq!(second.value, json!({"msteams": {"type": "imBack"}, "k": 2}));
140}
141
142#[test]
143fn execute_action_carries_verb_id_and_data() {
144 let actions = card_actions(json!([
145 {"type": "Action.Execute", "id": "a1", "title": "Approve", "verb": "approve", "data": {"n": 3}},
146 {"type": "Action.Execute", "title": "No verb"}
147 ]));
148 let payload = actions[0].invoke_payload().unwrap();
149 assert_eq!(payload.name, "adaptiveCard/action");
150 assert_eq!(
151 payload.value,
152 json!({
153 "action": {"type": "Action.Execute", "id": "a1", "verb": "approve", "data": {"n": 3}},
154 "trigger": "manual"
155 })
156 );
157 assert_eq!(actions[1].kind, CardActionKind::Unsupported);
158}
159
160#[test]
161fn show_card_and_toggle_actions_are_parsed_locally() {
162 let actions = card_actions(json!([
163 {"type": "Action.ShowCard", "title": "More", "card": {"body": [{"type": "TextBlock", "text": "nested"}]}},
164 {"type": "Action.ToggleVisibility", "title": "Toggle", "targetElements": ["a", {"elementId": "b", "isVisible": true}]},
165 {"type": "Action.ToggleVisibility", "title": "Empty", "targetElements": []}
166 ]));
167 let CardActionKind::ShowCard(nested) = &actions[0].kind else {
168 panic!("show card expected");
169 };
170 assert_eq!(nested.plain_text().as_deref(), Some("nested"));
171 assert!(actions[0].invoke_payload().is_none());
172 assert_eq!(
173 actions[1].kind,
174 CardActionKind::ToggleVisibility(vec![
175 ToggleTarget {
176 element_id: "a".to_owned(),
177 visible: None
178 },
179 ToggleTarget {
180 element_id: "b".to_owned(),
181 visible: Some(true)
182 },
183 ])
184 );
185 assert_eq!(actions[2].kind, CardActionKind::Unsupported);
186}
187
188#[test]
189fn disabled_actions_stay_disabled_and_hidden_ones_need_an_id() {
190 let actions = card_actions(json!([
191 {"type": "Action.Submit", "title": "Off", "isEnabled": false, "data": {}},
192 {"type": "Action.Submit", "title": "Gone", "isVisible": false},
193 {"type": "Action.Submit", "title": "Later", "id": "later", "isVisible": false}
194 ]));
195 assert_eq!(actions.len(), 2);
196 assert!(!actions[0].is_clickable());
197 assert!(!actions[1].visible);
198 assert_eq!(actions[1].id.as_deref(), Some("later"));
199}
200
201#[test]
202fn toggled_items_keep_their_id_and_stay_out_of_the_text() {
203 let card = json!({"body": [
204 {"type": "TextBlock", "text": "shown"},
205 {"type": "TextBlock", "id": "details", "isVisible": false, "text": "secret"}
206 ]})
207 .to_string();
208 let parsed = AdaptiveCard::parse(&card).unwrap();
209 assert_eq!(parsed.items.len(), 2);
210 assert_eq!(parsed.items[1].id.as_deref(), Some("details"));
211 assert!(!parsed.items[1].visible);
212 assert_eq!(parsed.plain_text().as_deref(), Some("shown"));
213}
214
215#[test]
216fn plain_text_drops_urls_images_and_buttons() {
217 assert_eq!(
218 card_content_text(&digest_card()).unwrap(),
219 "Ada Example edited your page\nQuarterly Plan in Product Space\nOwned by: Ada Example"
220 );
221}
222
223#[test]
224fn hidden_and_unknown_elements_are_skipped_but_fallback_text_stays() {
225 let card = json!({
226 "body": [
227 {"type": "TextBlock", "text": "hidden", "isVisible": false},
228 {"type": "Carousel", "fallbackText": "Open the carousel elsewhere"},
229 {"type": "Carousel"},
230 {"type": "Image", "url": "http://insecure.example.com/a.png"},
231 {"type": "TextBlock", "text": "shown"}
232 ]
233 })
234 .to_string();
235 assert_eq!(
236 card_content_text(&card).unwrap(),
237 "Open the carousel elsewhere\nshown"
238 );
239}
240
241#[test]
242fn containers_fact_sets_and_rich_text_are_parsed() {
243 let card = json!({
244 "body": [
245 {"type": "Container", "style": "emphasis", "items": [
246 {"type": "RichTextBlock", "inlines": [
247 "plain ",
248 {"type": "TextRun", "text": "bold", "weight": "bolder"},
249 {"type": "TextRun", "text": " link", "selectAction": {"type": "Action.OpenUrl", "url": "https://example.com/x"}}
250 ]},
251 {"type": "FactSet", "facts": [{"title": "Env", "value": "prod"}]},
252 {"type": "ImageSet", "imageSize": "medium", "images": [
253 {"type": "Image", "url": "https://img.example.com/a.png"}
254 ]}
255 ]}
256 ]
257 })
258 .to_string();
259 assert_eq!(
260 card_content_text(&card).unwrap(),
261 "plain bold link\nEnv: prod"
262 );
263}
264
265#[test]
266fn cards_without_content_are_none() {
267 assert!(AdaptiveCard::parse("not json").is_none());
268 assert!(AdaptiveCard::parse(r#"{"body": []}"#).is_none());
269 assert!(AdaptiveCard::parse("[1]").is_none());
270}
271
272#[test]
273fn element_visibility_covers_items_actions_and_nested_cards() {
274 let card = json!({
275 "body": [
276 {"type": "TextBlock", "id": "title", "text": "t"},
277 {"type": "Container", "items": [
278 {"type": "TextBlock", "id": "details", "isVisible": false, "text": "d"}
279 ]},
280 {"type": "ActionSet", "actions": [
281 {"type": "Action.Submit", "id": "later", "isVisible": false, "title": "Later"},
282 {"type": "Action.ShowCard", "title": "More", "card": {"body": [
283 {"type": "TextBlock", "id": "nested", "isVisible": false, "text": "n"}
284 ]}}
285 ]}
286 ]
287 })
288 .to_string();
289 let visibility = AdaptiveCard::parse(&card).unwrap().element_visibility();
290 assert_eq!(visibility.len(), 4);
291 assert!(visibility["title"]);
292 assert!(!visibility["details"]);
293 assert!(!visibility["later"]);
294 assert!(!visibility["nested"]);
295}