| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use gpui_kit::{ |
| 4 | AppContext as _, Context, Entity, IntoElement, Render, TestAppContext, VisualTestContext, |
| 5 | Window, |
| 6 | }; |
| 7 | use serde_json::json; |
| 8 | use store::Store; |
| 9 | use teams_core::AdaptiveCard; |
| 10 | |
| 11 | use crate::card_inputs::InputField; |
| 12 | use crate::app_state::{AppHandle, AppState, Mode}; |
| 13 | use crate::card_actions::CardTask; |
| 14 | use crate::card_state::{CardScope, Surface}; |
| 15 | use crate::views::adaptive_card::card_view; |
| 16 | |
| 17 | struct Harness { |
| 18 | app: Entity<AppState>, |
| 19 | scope: CardScope, |
| 20 | card: AdaptiveCard, |
| 21 | } |
| 22 | |
| 23 | impl Render for Harness { |
| 24 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 25 | self.app.update(cx, |state, cx| { |
| 26 | state.ensure_card_inputs(&self.scope, &self.card, window, cx) |
| 27 | }); |
| 28 | card_view(&self.card, &self.scope, cx) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn form_card() -> AdaptiveCard { |
| 33 | let card = json!({ |
| 34 | "body": [ |
| 35 | {"type": "Input.Text", "id": "name", "label": "Name", "isRequired": true, "errorMessage": "Name needed"}, |
| 36 | {"type": "Input.Toggle", "id": "agree", "title": "Agree"} |
| 37 | ], |
| 38 | "actions": [ |
| 39 | {"type": "Action.Submit", "title": "Save", "data": {"action": "save"}}, |
| 40 | {"type": "Action.Submit", "title": "Skip", "associatedInputs": "none", "data": {"action": "skip"}} |
| 41 | ] |
| 42 | }); |
| 43 | AdaptiveCard::parse(&card.to_string()).unwrap() |
| 44 | } |
| 45 | |
| 46 | fn typed_card() -> AdaptiveCard { |
| 47 | let card = json!({ |
| 48 | "body": [ |
| 49 | {"type": "Input.Text", "id": "code", "maxLength": 3, "regex": "^[a-z]+$", "errorMessage": "Letters only"}, |
| 50 | {"type": "Input.Date", "id": "day", "value": "2026-11-02", "min": "2026-10-01", "max": "2026-12-31"}, |
| 51 | {"type": "Input.Time", "id": "hour", "value": "09:30", "min": "10:00", "errorMessage": "Too early"}, |
| 52 | {"type": "Input.Number", "id": "count", "value": 4}, |
| 53 | {"type": "Input.Rating", "id": "stars", "value": 2, "allowHalf": true} |
| 54 | ], |
| 55 | "actions": [{"type": "Action.Submit", "title": "Save", "data": {"action": "save"}}] |
| 56 | }); |
| 57 | AdaptiveCard::parse(&card.to_string()).unwrap() |
| 58 | } |
| 59 | |
| 60 | fn open( |
| 61 | cx: &mut TestAppContext, |
| 62 | surface: Surface, |
| 63 | ) -> ( |
| 64 | &mut VisualTestContext, |
| 65 | Entity<AppState>, |
| 66 | CardScope, |
| 67 | AdaptiveCard, |
| 68 | ) { |
| 69 | open_card(cx, surface, form_card()) |
| 70 | } |
| 71 | |
| 72 | fn open_card( |
| 73 | cx: &mut TestAppContext, |
| 74 | surface: Surface, |
| 75 | card: AdaptiveCard, |
| 76 | ) -> ( |
| 77 | &mut VisualTestContext, |
| 78 | Entity<AppState>, |
| 79 | CardScope, |
| 80 | AdaptiveCard, |
| 81 | ) { |
| 82 | cx.update(gpui_kit::init); |
| 83 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 84 | let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default()))); |
| 85 | cx.update(|cx| cx.set_global(AppHandle(app.clone()))); |
| 86 | let scope = CardScope { |
| 87 | surface, |
| 88 | ..CardScope::message("19:chat@thread.v2", "m1", 0) |
| 89 | }; |
| 90 | let (_, window_cx) = cx.add_window_view({ |
| 91 | let (app, scope, card) = (app.clone(), scope.clone(), card.clone()); |
| 92 | move |_, _| Harness { app, scope, card } |
| 93 | }); |
| 94 | window_cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 95 | (window_cx, app, scope, card) |
| 96 | } |
| 97 | |
| 98 | fn focus_name_field(cx: &mut VisualTestContext, app: &Entity<AppState>, scope: &CardScope) { |
| 99 | let key = scope.input_key("name"); |
| 100 | let field = cx.update(|_, cx| app.read(cx).cards.input_field(&key).cloned()); |
| 101 | let Some(InputField::Line(line)) = field else { |
| 102 | panic!("name is a single line field"); |
| 103 | }; |
| 104 | cx.update(|window, cx| line.update(cx, |state, cx| state.focus(window, cx))); |
| 105 | } |
| 106 | |
| 107 | fn task_for( |
| 108 | cx: &mut VisualTestContext, |
| 109 | app: &Entity<AppState>, |
| 110 | scope: &CardScope, |
| 111 | card: &AdaptiveCard, |
| 112 | action_index: usize, |
| 113 | ) -> Option<CardTask> { |
| 114 | let action = card.actions[action_index].clone(); |
| 115 | let inputs = card.own_inputs(); |
| 116 | cx.update(|_, cx| app.update(cx, |state, cx| state.card_task(scope, action, &inputs, cx))) |
| 117 | } |
| 118 | |
| 119 | fn object(pairs: &[(&str, &str)]) -> serde_json::Value { |
| 120 | serde_json::Value::Object( |
| 121 | pairs |
| 122 | .iter() |
| 123 | .map(|(key, value)| ((*key).to_owned(), serde_json::Value::from(*value))) |
| 124 | .collect(), |
| 125 | ) |
| 126 | } |
| 127 | |
| 128 | fn error_of(cx: &mut VisualTestContext, app: &Entity<AppState>, key: &str) -> Option<String> { |
| 129 | cx.update(|_, cx| app.read(cx).cards.input_error(key).map(str::to_owned)) |
| 130 | } |
| 131 | |
| 132 | fn type_name( |
| 133 | cx: &mut VisualTestContext, |
| 134 | app: &Entity<AppState>, |
| 135 | scope: &CardScope, |
| 136 | text: &str, |
| 137 | ) { |
| 138 | focus_name_field(cx, app, scope); |
| 139 | cx.simulate_input(text); |
| 140 | } |
| 141 | |
| 142 | fn switch_agree(cx: &mut VisualTestContext, app: &Entity<AppState>, scope: &CardScope) { |
| 143 | let key = scope.input_key("agree"); |
| 144 | cx.update(|_, cx| { |
| 145 | app.update(cx, |state, cx| { |
| 146 | state.pick_card_input(scope, &key, "true".into(), cx) |
| 147 | }) |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | fn sent_value(task: Option<CardTask>) -> serde_json::Value { |
| 152 | match task { |
| 153 | Some(CardTask::Action(action)) => action.invoke_payload().unwrap().value, |
| 154 | Some(CardTask::Submit(data)) => data, |
| 155 | None => serde_json::Value::Null, |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[gpui_kit::test] |
| 160 | fn typed_text_and_toggle_are_sent_with_the_action_data(cx: &mut TestAppContext) { |
| 161 | let (cx, app, scope, card) = open(cx, Surface::Message); |
| 162 | type_name(cx, &app, &scope, "Ada"); |
| 163 | switch_agree(cx, &app, &scope); |
| 164 | let sent = sent_value(task_for(cx, &app, &scope, &card, 0)); |
| 165 | assert_eq!( |
| 166 | sent, |
| 167 | object(&[("action", "save"), ("name", "Ada"), ("agree", "true")]) |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | #[gpui_kit::test] |
| 172 | fn an_empty_required_input_blocks_the_send_until_it_changes(cx: &mut TestAppContext) { |
| 173 | let (cx, app, scope, card) = open(cx, Surface::Message); |
| 174 | assert!(task_for(cx, &app, &scope, &card, 0).is_none()); |
| 175 | let key = scope.input_key("name"); |
| 176 | assert_eq!(error_of(cx, &app, &key).as_deref(), Some("Name needed")); |
| 177 | type_name(cx, &app, &scope, "A"); |
| 178 | assert_eq!(error_of(cx, &app, &key), None); |
| 179 | assert!(task_for(cx, &app, &scope, &card, 0).is_some()); |
| 180 | } |
| 181 | |
| 182 | #[gpui_kit::test] |
| 183 | fn associated_inputs_none_skips_collection_and_validation(cx: &mut TestAppContext) { |
| 184 | let (cx, app, scope, card) = open(cx, Surface::Message); |
| 185 | let sent = sent_value(task_for(cx, &app, &scope, &card, 1)); |
| 186 | assert_eq!(sent, object(&[("action", "skip")])); |
| 187 | } |
| 188 | |
| 189 | #[gpui_kit::test] |
| 190 | fn a_dialog_submit_carries_the_collected_values(cx: &mut TestAppContext) { |
| 191 | let (cx, app, scope, card) = open(cx, Surface::Dialog); |
| 192 | type_name(cx, &app, &scope, "Ada"); |
| 193 | let sent = sent_value(task_for(cx, &app, &scope, &card, 0)); |
| 194 | assert_eq!( |
| 195 | sent, |
| 196 | object(&[("action", "save"), ("name", "Ada"), ("agree", "false")]) |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | #[gpui_kit::test] |
| 201 | fn typing_past_the_max_length_is_ignored(cx: &mut TestAppContext) { |
| 202 | let (cx, app, scope, _) = open_card(cx, Surface::Message, typed_card()); |
| 203 | let key = scope.input_key("code"); |
| 204 | let field = cx.update(|_, cx| app.read(cx).cards.input_field(&key).cloned()); |
| 205 | let Some(InputField::Line(line)) = field else { |
| 206 | panic!("code is a single line field"); |
| 207 | }; |
| 208 | cx.update(|window, cx| line.update(cx, |state, cx| state.focus(window, cx))); |
| 209 | cx.simulate_input("abcdef"); |
| 210 | assert_eq!( |
| 211 | cx.update(|_, cx| app.read(cx).cards.input_text(&key, cx)), |
| 212 | "abc" |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | #[gpui_kit::test] |
| 217 | fn blur_validation_shows_the_error_message(cx: &mut TestAppContext) { |
| 218 | let (cx, app, scope, card) = open_card(cx, Surface::Message, typed_card()); |
| 219 | let key = scope.input_key("code"); |
| 220 | let field = cx.update(|_, cx| app.read(cx).cards.input_field(&key).cloned()); |
| 221 | let Some(InputField::Line(line)) = field else { |
| 222 | panic!("code is a single line field"); |
| 223 | }; |
| 224 | cx.update(|window, cx| line.update(cx, |state, cx| state.focus(window, cx))); |
| 225 | cx.simulate_input("AB"); |
| 226 | let input = card.own_inputs().remove(0); |
| 227 | cx.update(|_, cx| { |
| 228 | app.update(cx, |state, cx| { |
| 229 | state.validate_card_input(&scope, &key, &input, cx) |
| 230 | }) |
| 231 | }); |
| 232 | assert_eq!(error_of(cx, &app, &key).as_deref(), Some("Letters only")); |
| 233 | } |
| 234 | |
| 235 | #[gpui_kit::test] |
| 236 | fn date_time_and_rating_values_are_collected_with_their_types(cx: &mut TestAppContext) { |
| 237 | let (cx, app, scope, card) = open_card(cx, Surface::Message, typed_card()); |
| 238 | let hour_key = scope.input_key("hour"); |
| 239 | assert!(task_for(cx, &app, &scope, &card, 0).is_none()); |
| 240 | assert_eq!(error_of(cx, &app, &hour_key).as_deref(), Some("Too early")); |
| 241 | cx.update(|_, cx| { |
| 242 | app.update(cx, |state, cx| { |
| 243 | state.pick_card_input(&scope, &hour_key, "10:15".into(), cx); |
| 244 | state.pick_card_input(&scope, &scope.input_key("stars"), "3.5".into(), cx); |
| 245 | }) |
| 246 | }); |
| 247 | let sent = sent_value(task_for(cx, &app, &scope, &card, 0)); |
| 248 | assert_eq!( |
| 249 | sent, |
| 250 | json!({ |
| 251 | "action": "save", "code": "", "day": "2026-11-02", "hour": "10:15", |
| 252 | "count": "4", "stars": 3.5 |
| 253 | }) |
| 254 | ); |
| 255 | } |