BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / card_input_tests.rs

card_input_tests.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
card_input_tests.rs 8.6 KiB Download Raw
1use std::sync::Arc;
2
3use gpui_kit::{
4 AppContext as _, Context, Entity, IntoElement, Render, TestAppContext, VisualTestContext,
5 Window,
6};
7use serde_json::json;
8use store::Store;
9use teams_core::AdaptiveCard;
10
11use crate::app_state::{AppHandle, AppState, Mode};
12use crate::card_actions::CardTask;
13use crate::card_inputs::InputField;
14use crate::card_state::{CardScope, Surface};
15use crate::views::adaptive_card::card_view;
16
17struct Harness {
18 app: Entity<AppState>,
19 scope: CardScope,
20 card: AdaptiveCard,
21}
22
23impl 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
32fn 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
46fn 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
60fn 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
72fn 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
98fn 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
107fn 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
119fn 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
128fn 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
132fn type_name(cx: &mut VisualTestContext, app: &Entity<AppState>, scope: &CardScope, text: &str) {
133 focus_name_field(cx, app, scope);
134 cx.simulate_input(text);
135}
136
137fn switch_agree(cx: &mut VisualTestContext, app: &Entity<AppState>, scope: &CardScope) {
138 let key = scope.input_key("agree");
139 cx.update(|_, cx| {
140 app.update(cx, |state, cx| {
141 state.pick_card_input(scope, &key, "true".into(), cx)
142 })
143 });
144}
145
146fn sent_value(task: Option<CardTask>) -> serde_json::Value {
147 match task {
148 Some(CardTask::Action(action)) => action.invoke_payload().unwrap().value,
149 Some(CardTask::Submit(data)) => data,
150 None => serde_json::Value::Null,
151 }
152}
153
154#[gpui_kit::test]
155fn typed_text_and_toggle_are_sent_with_the_action_data(cx: &mut TestAppContext) {
156 let (cx, app, scope, card) = open(cx, Surface::Message);
157 type_name(cx, &app, &scope, "Ada");
158 switch_agree(cx, &app, &scope);
159 let sent = sent_value(task_for(cx, &app, &scope, &card, 0));
160 assert_eq!(
161 sent,
162 object(&[("action", "save"), ("name", "Ada"), ("agree", "true")])
163 );
164}
165
166#[gpui_kit::test]
167fn an_empty_required_input_blocks_the_send_until_it_changes(cx: &mut TestAppContext) {
168 let (cx, app, scope, card) = open(cx, Surface::Message);
169 assert!(task_for(cx, &app, &scope, &card, 0).is_none());
170 let key = scope.input_key("name");
171 assert_eq!(error_of(cx, &app, &key).as_deref(), Some("Name needed"));
172 type_name(cx, &app, &scope, "A");
173 assert_eq!(error_of(cx, &app, &key), None);
174 assert!(task_for(cx, &app, &scope, &card, 0).is_some());
175}
176
177#[gpui_kit::test]
178fn associated_inputs_none_skips_collection_and_validation(cx: &mut TestAppContext) {
179 let (cx, app, scope, card) = open(cx, Surface::Message);
180 let sent = sent_value(task_for(cx, &app, &scope, &card, 1));
181 assert_eq!(sent, object(&[("action", "skip")]));
182}
183
184#[gpui_kit::test]
185fn a_dialog_submit_carries_the_collected_values(cx: &mut TestAppContext) {
186 let (cx, app, scope, card) = open(cx, Surface::Dialog);
187 type_name(cx, &app, &scope, "Ada");
188 let sent = sent_value(task_for(cx, &app, &scope, &card, 0));
189 assert_eq!(
190 sent,
191 object(&[("action", "save"), ("name", "Ada"), ("agree", "false")])
192 );
193}
194
195#[gpui_kit::test]
196fn typing_past_the_max_length_is_ignored(cx: &mut TestAppContext) {
197 let (cx, app, scope, _) = open_card(cx, Surface::Message, typed_card());
198 let key = scope.input_key("code");
199 let field = cx.update(|_, cx| app.read(cx).cards.input_field(&key).cloned());
200 let Some(InputField::Line(line)) = field else {
201 panic!("code is a single line field");
202 };
203 cx.update(|window, cx| line.update(cx, |state, cx| state.focus(window, cx)));
204 cx.simulate_input("abcdef");
205 assert_eq!(
206 cx.update(|_, cx| app.read(cx).cards.input_text(&key, cx)),
207 "abc"
208 );
209}
210
211#[gpui_kit::test]
212fn blur_validation_shows_the_error_message(cx: &mut TestAppContext) {
213 let (cx, app, scope, card) = open_card(cx, Surface::Message, typed_card());
214 let key = scope.input_key("code");
215 let field = cx.update(|_, cx| app.read(cx).cards.input_field(&key).cloned());
216 let Some(InputField::Line(line)) = field else {
217 panic!("code is a single line field");
218 };
219 cx.update(|window, cx| line.update(cx, |state, cx| state.focus(window, cx)));
220 cx.simulate_input("AB");
221 let input = card.own_inputs().remove(0);
222 cx.update(|_, cx| {
223 app.update(cx, |state, cx| {
224 state.validate_card_input(&scope, &key, &input, cx)
225 })
226 });
227 assert_eq!(error_of(cx, &app, &key).as_deref(), Some("Letters only"));
228}
229
230#[gpui_kit::test]
231fn date_time_and_rating_values_are_collected_with_their_types(cx: &mut TestAppContext) {
232 let (cx, app, scope, card) = open_card(cx, Surface::Message, typed_card());
233 let hour_key = scope.input_key("hour");
234 assert!(task_for(cx, &app, &scope, &card, 0).is_none());
235 assert_eq!(error_of(cx, &app, &hour_key).as_deref(), Some("Too early"));
236 cx.update(|_, cx| {
237 app.update(cx, |state, cx| {
238 state.pick_card_input(&scope, &hour_key, "10:15".into(), cx);
239 state.pick_card_input(&scope, &scope.input_key("stars"), "3.5".into(), cx);
240 })
241 });
242 let sent = sent_value(task_for(cx, &app, &scope, &card, 0));
243 assert_eq!(
244 sent,
245 json!({
246 "action": "save", "code": "", "day": "2026-11-02", "hour": "10:15",
247 "count": "4", "stars": 3.5
248 })
249 );
250}