| 1 | use serde_json::{Value, json}; |
| 2 | use teams_core::InputChoice; |
| 3 | |
| 4 | const DATASET_SERVICES: &str = "demo.services"; |
| 5 | |
| 6 | const SERVICES: [(&str, &str); 10] = [ |
| 7 | ("Billing API", "billing-api"), |
| 8 | ("Build Farm", "build-farm"), |
| 9 | ("Chat Gateway", "chat-gateway"), |
| 10 | ("Identity Provider", "identity"), |
| 11 | ("Notification Hub", "notification-hub"), |
| 12 | ("Search Index", "search-index"), |
| 13 | ("Storage Broker", "storage-broker"), |
| 14 | ("Task Scheduler", "task-scheduler"), |
| 15 | ("Telemetry Collector", "telemetry"), |
| 16 | ("Wiki Renderer", "wiki-renderer"), |
| 17 | ]; |
| 18 | |
| 19 | pub fn search(dataset: &str, query_text: &str) -> Vec<InputChoice> { |
| 20 | if dataset != DATASET_SERVICES { |
| 21 | return Vec::new(); |
| 22 | } |
| 23 | let needle = query_text.to_lowercase(); |
| 24 | SERVICES |
| 25 | .iter() |
| 26 | .filter(|(title, _)| title.to_lowercase().contains(&needle)) |
| 27 | .map(|(title, value)| InputChoice { |
| 28 | title: (*title).to_owned(), |
| 29 | value: (*value).to_owned(), |
| 30 | }) |
| 31 | .collect() |
| 32 | } |
| 33 | |
| 34 | pub fn all_inputs_card() -> Value { |
| 35 | json!({ |
| 36 | "type": "AdaptiveCard", |
| 37 | "body": [ |
| 38 | {"type": "TextBlock", "size": "medium", "weight": "bolder", "text": "Maintenance window"}, |
| 39 | {"type": "Input.Date", "id": "window_day", "label": "Day", "isRequired": true, "errorMessage": "Pick a day in the planning range", "min": "2026-10-01", "max": "2026-12-31", "value": "2026-11-02"}, |
| 40 | {"type": "Input.Time", "id": "window_start", "label": "Start (24h)", "isRequired": true, "errorMessage": "Start between 06:00 and 20:00", "min": "06:00", "max": "20:00", "value": "09:30"}, |
| 41 | {"type": "Input.Text", "id": "ticket", "label": "Ticket", "placeholder": "ABC-1234", "maxLength": 8, "regex": "^[A-Z]{3}-\\d{1,4}$", "isRequired": true, "errorMessage": "Use the form ABC-1234", |
| 42 | "inlineAction": {"type": "Action.Submit", "title": "Look up", "data": {"action": "lookup"}}}, |
| 43 | {"type": "Input.Number", "id": "servers", "label": "Servers", "placeholder": "1-20", "min": 1, "max": 20, "isRequired": true, "errorMessage": "Between 1 and 20 servers", "value": 4}, |
| 44 | {"type": "Input.ChoiceSet", "id": "service", "label": "Service", "placeholder": "Type to search services", "isRequired": true, "errorMessage": "Pick a service", |
| 45 | "choices.data": {"type": "Data.Query", "dataset": DATASET_SERVICES, "count": 5}}, |
| 46 | {"type": "Input.Rating", "id": "risk", "label": "Risk", "max": 5, "value": 2.5, "allowHalf": true, "color": "marigold", "size": "large", "isRequired": true, "errorMessage": "Rate the risk"}, |
| 47 | {"type": "Input.Toggle", "id": "notify", "title": "Notify everyone who uses the service and wait for the confirmation before the window starts", "wrap": true, "value": "true"}, |
| 48 | {"type": "Rating", "value": 4.5, "count": 128, "max": 5}, |
| 49 | {"type": "Rating", "value": 3.7, "count": 1204, "style": "compact"} |
| 50 | ], |
| 51 | "actions": [ |
| 52 | {"type": "Action.Submit", "title": "Schedule", "data": {"action": "schedule"}} |
| 53 | ] |
| 54 | }) |
| 55 | } |
| 56 | |
| 57 | #[cfg(test)] |
| 58 | mod tests { |
| 59 | use super::*; |
| 60 | |
| 61 | #[test] |
| 62 | fn search_filters_the_local_list_case_insensitively() { |
| 63 | let values: Vec<String> = search(DATASET_SERVICES, "CH") |
| 64 | .into_iter() |
| 65 | .map(|choice| choice.value) |
| 66 | .collect(); |
| 67 | assert_eq!(values, ["chat-gateway", "search-index", "task-scheduler"]); |
| 68 | assert!(search("other", "ch").is_empty()); |
| 69 | } |
| 70 | } |