super-voice-app-core/src/pages/contexts.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! The Contexts page: the dictation contexts, which one is in force, and the |
3 | | //! editor open over one. |
4 | | //! |
5 | | //! Nothing in the list moves until the daemon has answered: every write is |
6 | | //! followed by reading the list again. The editor is the exception: what the |
7 | | //! user is typing is theirs, and a list read again under an open editor never |
8 | | //! rewrites the sentence they are halfway through. |
9 | | //! |
10 | | //! Which context one backend uses is set from its Configure sheet, and lives |
11 | | //! with that sheet ([`super::configure`]). |
12 | | |
13 | | use super_voice_shared::models::contexts::DictationContext; |
14 | | |
15 | | use crate::daemon::client::v1::contexts::{ |
16 | | ContextCatalog, delete_context, list_contexts, set_active_context, set_context, |
17 | | }; |
18 | | use crate::state::TextBuffer; |
19 | | use crate::state::contexts::{ContextDraft, ContextsState, RowId}; |
20 | | |
21 | | /// What happened on the page: something the user did, or the answer to work |
22 | | /// an [`Effect`] started. |
23 | | #[derive(Debug, Clone)] |
24 | | pub enum Input { |
25 | | /// Read the list: the page was opened, or another app changed it. |
26 | | Reload, |
27 | | /// The list was read, or why not. |
28 | | Loaded(Result<ContextCatalog, String>), |
29 | | |
30 | | /// Put a context in force, or (`None`) none. |
31 | | Activate(Option<String>), |
32 | | /// It is in force, or why not. |
33 | | Activated(Result<(), String>), |
34 | | |
35 | | /// Delete was pressed once for a context. |
36 | | DeleteRequested(String), |
37 | | /// The delete was called off. |
38 | | DeleteCancelled, |
39 | | /// Delete was pressed again: delete it. |
40 | | DeleteConfirmed(String), |
41 | | /// It was deleted, or why not. |
42 | | Deleted(Result<(), String>), |
43 | | |
44 | | /// Open the editor over a context. |
45 | | Edit(String), |
46 | | /// Open the editor over a new context. |
47 | | Create, |
48 | | /// Close the editor, and drop what was typed. |
49 | | CancelEdit, |
50 | | /// The name field now holds this. |
51 | | NameChanged(String), |
52 | | /// The app changed the prompt in its own editor. |
53 | | PromptEdited, |
54 | | /// A vocabulary row now holds this. Several lines pasted at once become |
55 | | /// several rows. |
56 | | TermChanged { index: usize, value: String }, |
57 | | /// Enter in a vocabulary row: a new row below it. |
58 | | TermSubmitted(usize), |
59 | | /// A vocabulary row's clear button. |
60 | | TermRemoved(usize), |
61 | | /// Save what the editor holds. |
62 | | Save, |
63 | | /// The daemon stored it, as it stored it (blanks dropped, terms trimmed), |
64 | | /// or why not. |
65 | | Saved(Result<DictationContext, String>), |
66 | | } |
67 | | |
68 | | /// Work [`update`] asks for. |
69 | | #[derive(Debug, Clone, PartialEq, Eq)] |
70 | | pub enum Effect { |
71 | | /// Read the list ([`run_list`]); answers [`Input::Loaded`]. |
72 | | List, |
73 | | /// Put this in force ([`run_activate`]); answers [`Input::Activated`]. |
74 | | Activate(Option<String>), |
75 | | /// Delete this ([`run_delete`]); answers [`Input::Deleted`]. |
76 | | Delete(String), |
77 | | /// Store this ([`run_save`]); answers [`Input::Saved`]. |
78 | | Save(DictationContext), |
79 | | /// Put the cursor in this vocabulary row. |
80 | | FocusTerm(RowId), |
81 | | /// Show the editor. |
82 | | OpenEditor, |
83 | | /// Close the editor. |
84 | | CloseEditor, |
85 | | } |
86 | | |
87 | | /// Something to tell the user on the page. Each app says it in its own |
88 | | /// words. The editor's own errors stay in the editor (its draft's `error`). |
89 | | #[derive(Debug, Clone, PartialEq, Eq)] |
90 | | pub enum Notice { |
91 | | /// The list could not be read, or changed. |
92 | | CouldNotLoad(String), |
93 | | /// A save failed after its editor was closed. |
94 | | CouldNotSave(String), |
95 | | } |
96 | | |
97 | | /// What an [`update`] asks of the app. |
98 | | #[derive(Debug, Default)] |
99 | | pub struct Outcome { |
100 | | /// The work to start, in order. |
101 | | pub effects: Vec<Effect>, |
102 | | /// Whether the page's notice is out of date and goes. Applied before |
103 | | /// [`notice`](Self::notice). |
104 | | pub clear_notice: bool, |
105 | | /// What to tell the user. |
106 | | pub notice: Option<Notice>, |
107 | | } |
108 | | |
109 | | /// What `input` does to the page. |
110 | 21 | pub fn update<B: TextBuffer>(state: &mut ContextsState<B>, input: Input) -> Outcome { |
111 | 21 | let mut out = Outcome::default(); |
112 | 21 | match input { |
113 | | Input::Reload | Input::Activated(Ok(())) | Input::Deleted(Ok(())) => { |
114 | 2 | reload(state, &mut out); |
115 | | } |
116 | 3 | Input::Loaded(Ok(catalog)) => state.replace(catalog.contexts, catalog.active), |
117 | | Input::Loaded(Err(e)) | Input::Activated(Err(e)) | Input::Deleted(Err(e)) => { |
118 | 1 | log::warn!("contexts: {e0 }"); |
119 | 1 | state.loading = false; |
120 | | // Read, if only to fail: the page shows the notice rather than a |
121 | | // spinner that never stops. |
122 | 1 | state.loaded = true; |
123 | 1 | out.notice = Some(Notice::CouldNotLoad(e)); |
124 | | } |
125 | | Input::Activate(id) => { |
126 | 1 | out.clear_notice = true; |
127 | 1 | out.effects.push(Effect::Activate(id)); |
128 | | } |
129 | 1 | Input::DeleteRequested(id) => state.confirming_delete = Some(id), |
130 | 0 | Input::DeleteCancelled => state.confirming_delete = None, |
131 | | Input::DeleteConfirmed(id) => { |
132 | 1 | state.confirming_delete = None; |
133 | 1 | out.clear_notice = true; |
134 | 1 | out.effects.push(Effect::Delete(id)); |
135 | | } |
136 | 12 | editing => editor(state, editing, &mut out), |
137 | | } |
138 | 21 | out |
139 | 21 | } |
140 | | |
141 | | /// Read the list again. Saying so, so the page says "loading" rather than |
142 | | /// "no contexts". |
143 | 3 | fn reload<B>(state: &mut ContextsState<B>, out: &mut Outcome) { |
144 | 3 | state.loading = true; |
145 | 3 | out.effects.push(Effect::List); |
146 | 3 | } |
147 | | |
148 | | /// The editor over one context. |
149 | 12 | fn editor<B: TextBuffer>(state: &mut ContextsState<B>, input: Input, out: &mut Outcome) { |
150 | 12 | match input { |
151 | | Input::Edit(id) => { |
152 | 0 | state.confirming_delete = None; |
153 | 0 | if let Some(context) = state.get(&id) { |
154 | 0 | state.draft = Some(ContextDraft::editing(context)); |
155 | 0 | out.effects.push(Effect::OpenEditor); |
156 | 0 | } |
157 | | } |
158 | | Input::Create => { |
159 | 3 | state.draft = Some(ContextDraft::creating(state.next_id())); |
160 | 3 | out.effects.push(Effect::OpenEditor); |
161 | | } |
162 | | Input::CancelEdit => { |
163 | 1 | state.draft = None; |
164 | 1 | out.effects.push(Effect::CloseEditor); |
165 | | } |
166 | 2 | Input::Save => save(state, out), |
167 | | // The context saved stays open, and selected, over what the daemon |
168 | | // stored: a new one is now one to edit. Its row changes at once, |
169 | | // rather than after the list is read again; reading it again picks |
170 | | // up anything else the save moved. |
171 | | Input::Saved(Ok(stored)) => { |
172 | 1 | state.draft = Some(ContextDraft::editing(&stored)); |
173 | 1 | state.upsert(stored); |
174 | 1 | reload(state, out); |
175 | | } |
176 | 2 | Input::Saved(Err(e)) => match state.draft.as_mut() { |
177 | | Some(draft) => { |
178 | 1 | draft.saving = false; |
179 | 1 | draft.error = Some(e); |
180 | | } |
181 | | // The editor was closed under the save: the page is the only |
182 | | // place left to say so. |
183 | 1 | None => out.notice = Some(Notice::CouldNotSave(e)), |
184 | | }, |
185 | | typing => { |
186 | 3 | let Some(draft) = state.draft.as_mut() else { |
187 | 0 | return; |
188 | | }; |
189 | 3 | match typing { |
190 | | Input::NameChanged(name) => { |
191 | 1 | draft.name = name; |
192 | 1 | draft.error = None; |
193 | | } |
194 | 0 | Input::PromptEdited => draft.error = None, |
195 | | Input::TermChanged { index, value } => { |
196 | 1 | draft.set_row(index, &value); |
197 | 1 | draft.error = None; |
198 | | } |
199 | | // Enter opens the next row with the cursor in it, so a list |
200 | | // is typed rather than clicked together. |
201 | | Input::TermSubmitted(index) => { |
202 | 1 | out.effects |
203 | 1 | .push(Effect::FocusTerm(draft.insert_row_after(index))); |
204 | | } |
205 | | Input::TermRemoved(index) => { |
206 | 0 | if let Some(focus) = draft.remove_row(index) { |
207 | 0 | out.effects.push(Effect::FocusTerm(focus)); |
208 | 0 | } |
209 | | } |
210 | 0 | _ => {} |
211 | | } |
212 | | } |
213 | | } |
214 | 12 | } |
215 | | |
216 | | /// Store what the editor holds, once it has a name. |
217 | 2 | fn save<B: TextBuffer>(state: &mut ContextsState<B>, out: &mut Outcome) { |
218 | 2 | let Some(draft) = state.draft.as_mut() else { |
219 | 0 | return; |
220 | | }; |
221 | 2 | let context = draft.to_context(); |
222 | | // Checked here as well as by the daemon, so an empty name is answered by |
223 | | // the form rather than a round trip. |
224 | 2 | if let Err(e) = context.check() { |
225 | 1 | draft.error = Some(e); |
226 | 1 | return; |
227 | 1 | } |
228 | 1 | draft.saving = true; |
229 | 1 | draft.error = None; |
230 | 1 | out.effects.push(Effect::Save(context)); |
231 | 2 | } |
232 | | |
233 | | /// How far a context reaches among the installed backends, when the page |
234 | | /// says so: a context that reaches nothing is the one failure the user cannot |
235 | | /// see, so the page says it rather than let them wonder why nothing changed. |
236 | | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
237 | | pub enum Reach { |
238 | | /// None of the installed backends can use a context. |
239 | | Nothing, |
240 | | /// `accepting` of the `total` installed backends can. |
241 | | Partly { accepting: usize, total: usize }, |
242 | | } |
243 | | |
244 | | /// How far a context reaches among `backends`, the installed ones; `None` |
245 | | /// when there is nothing to say (no backend installed, or all of them can use |
246 | | /// one). |
247 | | #[must_use] |
248 | 0 | pub fn reach(backends: &[crate::daemon::backends::BackendInfo]) -> Option<Reach> { |
249 | 0 | let accepting = backends |
250 | 0 | .iter() |
251 | 0 | .filter(|b| { |
252 | 0 | super_voice_registry_types::index::supports::has( |
253 | 0 | &b.supports, |
254 | 0 | super_voice_registry_types::index::supports::DICTATION_CONTEXT, |
255 | 0 | ) |
256 | 0 | }) |
257 | | .count(); |
258 | 0 | reach_of(accepting, backends.len()) |
259 | 0 | } |
260 | | |
261 | | /// [`reach`], from the count of backends that can use a context and the count |
262 | | /// installed. |
263 | | #[must_use] |
264 | 4 | pub const fn reach_of(accepting: usize, total: usize) -> Option<Reach> { |
265 | 4 | if total == 0 || accepting == total3 { |
266 | 2 | None |
267 | 2 | } else if accepting == 0 { |
268 | 1 | Some(Reach::Nothing) |
269 | | } else { |
270 | 1 | Some(Reach::Partly { accepting, total }) |
271 | | } |
272 | 4 | } |
273 | | |
274 | | /// What a [`Reach`] says, in the app's language. |
275 | | #[must_use] |
276 | 1 | pub fn reach_text(reach: Reach) -> String { |
277 | 1 | match reach { |
278 | 0 | Reach::Nothing => crate::i18n::text("none-of-your-installed-backends-can-use", &[]), |
279 | 1 | Reach::Partly { accepting, total } => crate::i18n::text( |
280 | 1 | "context-supported-count", |
281 | 1 | &[ |
282 | 1 | ("accepting", &accepting.to_string()), |
283 | 1 | ("value0", &total.to_string()), |
284 | 1 | ], |
285 | 1 | ) |
286 | 1 | .replace(['\u{2068}', '\u{2069}'], ""), |
287 | | } |
288 | 1 | } |
289 | | |
290 | | /// How many of a context's words its row in the list shows. |
291 | | const ROW_TERMS: usize = 3; |
292 | | |
293 | | /// A context's row in the list, under its name: its first words, or that it |
294 | | /// has only a prompt, or nothing at all. |
295 | | #[must_use] |
296 | 4 | pub fn row_summary(context: &DictationContext) -> String { |
297 | 4 | if !context.vocabulary.is_empty() { |
298 | 2 | let head = context |
299 | 2 | .vocabulary |
300 | 2 | .iter() |
301 | 2 | .take(ROW_TERMS) |
302 | 2 | .map(String::as_str) |
303 | 2 | .collect::<Vec<_>>() |
304 | 2 | .join(", "); |
305 | 2 | return match context.vocabulary.len().saturating_sub(ROW_TERMS) { |
306 | 1 | 0 => head, |
307 | 1 | rest => crate::i18n::text( |
308 | 1 | "context-more-terms", |
309 | 1 | &[("head", &head), ("rest", &rest.to_string())], |
310 | 1 | ) |
311 | 1 | .replace(['\u{2068}', '\u{2069}'], ""), |
312 | | }; |
313 | 2 | } |
314 | 2 | if context.prompt.trim().is_empty() { |
315 | 1 | crate::i18n::text("context-sends-nothing", &[]) |
316 | | } else { |
317 | 1 | crate::i18n::text("context-prompt-only", &[]) |
318 | | } |
319 | 4 | } |
320 | | |
321 | | /// Whether `event` may change how far a context reaches: a backend install |
322 | | /// that finished. Not its progress, which comes many times a second while it |
323 | | /// downloads; and an uninstall sends no event, so the page reads the reach |
324 | | /// again when it is shown. |
325 | | #[must_use] |
326 | 3 | pub fn changes_reach(event: &crate::events::Event) -> bool { |
327 | 3 | matches!( |
328 | 3 | event, |
329 | | crate::events::Event::Install( |
330 | | super_voice_shared::registry::events::RegistryEvent::Completed { .. } |
331 | | ) |
332 | | ) |
333 | 3 | } |
334 | | |
335 | | /// How far a context reaches among the installed backends, read from the |
336 | | /// daemon: `None` when there is nothing to say. |
337 | | /// |
338 | | /// # Errors |
339 | | /// |
340 | | /// When the daemon can't list its backends. |
341 | 0 | pub async fn run_read_reach() -> Result<Option<Reach>, String> { |
342 | 0 | crate::daemon::client::v1::backends::list_backends() |
343 | 0 | .await |
344 | 0 | .map(|backends| reach(&backends)) |
345 | 0 | .map_err(|e| e.to_string()) |
346 | 0 | } |
347 | | |
348 | | /// Read the list. |
349 | 0 | pub async fn run_list() -> Input { |
350 | 0 | Input::Loaded(list_contexts().await.map_err(|e| e.to_string())) |
351 | 0 | } |
352 | | |
353 | | /// Put `id` in force, or none. |
354 | 0 | pub async fn run_activate(id: Option<String>) -> Input { |
355 | | Input::Activated( |
356 | 0 | set_active_context(id) |
357 | 0 | .await |
358 | | .map(|_| ()) |
359 | 0 | .map_err(|e| e.to_string()), |
360 | | ) |
361 | 0 | } |
362 | | |
363 | | /// Delete `id`. |
364 | 0 | pub async fn run_delete(id: String) -> Input { |
365 | 0 | Input::Deleted(delete_context(id).await.map_err(|e| e.to_string())) |
366 | 0 | } |
367 | | |
368 | | /// Store `context`. |
369 | 0 | pub async fn run_save(context: DictationContext) -> Input { |
370 | 0 | Input::Saved(set_context(context).await.map_err(|e| e.to_string())) |
371 | 0 | } |
372 | | |
373 | | #[cfg(test)] |
374 | | mod tests { |
375 | | use super::*; |
376 | | |
377 | | #[test] |
378 | 1 | fn only_a_finished_install_changes_the_reach() { |
379 | | use crate::events::Event; |
380 | | use super_voice_shared::registry::events::{InstallPhase, RegistryEvent}; |
381 | 1 | let progress = Event::Install(RegistryEvent::Progress { |
382 | 1 | install_id: "1".into(), |
383 | 1 | source: "kokoro".into(), |
384 | 1 | phase: InstallPhase::Downloading, |
385 | 1 | bytes_done: Some(9), |
386 | 1 | bytes_total: Some(90), |
387 | 1 | }); |
388 | 1 | let completed = Event::Install(RegistryEvent::Completed { |
389 | 1 | install_id: "1".into(), |
390 | 1 | source: "kokoro".into(), |
391 | 1 | version: "0.2.1".into(), |
392 | 1 | }); |
393 | 1 | assert!(!changes_reach(&progress)); |
394 | 1 | assert!(changes_reach(&completed)); |
395 | 1 | assert!(!changes_reach(&Event::MicrophoneLevel { |
396 | 1 | level: 0.5, |
397 | 1 | is_speech: true, |
398 | 1 | bands: Vec::new(), |
399 | 1 | total_energy: 0.0, |
400 | 1 | })); |
401 | 1 | } |
402 | | |
403 | | #[test] |
404 | 1 | fn the_reach_is_said_only_when_some_backend_misses_out() { |
405 | 1 | assert_eq!(reach_of(0, 0), None); |
406 | 1 | assert_eq!(reach_of(2, 2), None); |
407 | 1 | assert_eq!(reach_of(0, 3), Some(Reach::Nothing)); |
408 | 1 | assert_eq!( |
409 | 1 | reach_of(1, 3), |
410 | 1 | Some(Reach::Partly { |
411 | 1 | accepting: 1, |
412 | 1 | total: 3 |
413 | 1 | }) |
414 | | ); |
415 | 1 | } |
416 | | |
417 | | #[test] |
418 | 1 | fn a_row_names_its_first_words_or_what_it_holds() { |
419 | | let context = |prompt: &str, words: &[&str]| DictationContext { |
420 | 4 | id: "coding".into(), |
421 | 4 | name: "Coding".into(), |
422 | 4 | prompt: prompt.into(), |
423 | 8 | vocabulary: words.iter()4 .map(|w| (*w).to_string()).collect(), |
424 | 4 | }; |
425 | 1 | assert_eq!( |
426 | 1 | row_summary(&context("", &["Rust", "GPUI", "cargo"])), |
427 | 1 | "Rust, GPUI, cargo" |
428 | | ); |
429 | 1 | let more = row_summary(&context("", &["Rust", "GPUI", "cargo", "tokio", "serde"])); |
430 | 1 | assert!( |
431 | 1 | more.starts_with("Rust, GPUI, cargo") && more.contains('2'), |
432 | | "{more}" |
433 | | ); |
434 | 1 | assert!(!more.contains('\u{2068}'), "no isolation marks"); |
435 | 1 | assert_ne!( |
436 | 1 | row_summary(&context("Be terse.", &[])), |
437 | 1 | row_summary(&context(" ", &[])), |
438 | | "a prompt alone is not nothing" |
439 | | ); |
440 | 1 | assert!( |
441 | 1 | !reach_text(Reach::Partly { |
442 | 1 | accepting: 1, |
443 | 1 | total: 3 |
444 | 1 | }) |
445 | 1 | .contains('\u{2068}') |
446 | | ); |
447 | 1 | } |
448 | | |
449 | 3 | fn catalog(ids: &[&str], active: Option<&str>) -> ContextCatalog { |
450 | | ContextCatalog { |
451 | 3 | active: active.map(str::to_string), |
452 | 3 | contexts: ids |
453 | 3 | .iter() |
454 | | .map(|id| DictationContext { |
455 | 3 | id: (*id).to_string(), |
456 | 3 | name: id.to_uppercase(), |
457 | 3 | vocabulary: vec!["kubectl".into()], |
458 | 3 | ..DictationContext::default() |
459 | 3 | }) |
460 | | .collect(), |
461 | | } |
462 | 3 | } |
463 | | |
464 | | #[test] |
465 | 1 | fn a_write_reads_the_list_again() { |
466 | 1 | let mut state = ContextsState::<String>::default(); |
467 | 1 | let out = update(&mut state, Input::Activate(Some("a".into()))); |
468 | 1 | assert_eq!(out.effects, [Effect::Activate(Some("a".into()))]); |
469 | 1 | let out = update(&mut state, Input::Activated(Ok(()))); |
470 | 1 | assert_eq!(out.effects, [Effect::List]); |
471 | 1 | assert!(state.loading); |
472 | 1 | update(&mut state, Input::Loaded(Ok(catalog(&["a"], Some("a"))))); |
473 | 1 | assert!(state.loaded && !state.loading); |
474 | 1 | assert_eq!(state.active.as_deref(), Some("a")); |
475 | 1 | } |
476 | | |
477 | | #[test] |
478 | 1 | fn a_failed_read_is_said_and_stops_loading() { |
479 | 1 | let mut state = ContextsState::<String>::default(); |
480 | 1 | update(&mut state, Input::Reload); |
481 | 1 | let out = update(&mut state, Input::Loaded(Err("down".into()))); |
482 | 1 | assert_eq!(out.notice, Some(Notice::CouldNotLoad("down".into()))); |
483 | 1 | assert!(state.loaded && !state.loading); |
484 | 1 | } |
485 | | |
486 | | #[test] |
487 | 1 | fn delete_takes_two_presses() { |
488 | 1 | let mut state = ContextsState::<String>::default(); |
489 | 1 | update(&mut state, Input::Loaded(Ok(catalog(&["a"], None)))); |
490 | 1 | assert!( |
491 | 1 | update(&mut state, Input::DeleteRequested("a".into())) |
492 | 1 | .effects |
493 | 1 | .is_empty() |
494 | | ); |
495 | 1 | assert_eq!(state.confirming_delete.as_deref(), Some("a")); |
496 | 1 | let out = update(&mut state, Input::DeleteConfirmed("a".into())); |
497 | 1 | assert_eq!(out.effects, [Effect::Delete("a".into())]); |
498 | 1 | assert!(state.confirming_delete.is_none()); |
499 | 1 | } |
500 | | |
501 | | #[test] |
502 | 1 | fn the_editor_opens_saves_and_closes() { |
503 | 1 | let mut state = ContextsState::<String>::default(); |
504 | 1 | update(&mut state, Input::Loaded(Ok(catalog(&["a"], None)))); |
505 | 1 | let out = update(&mut state, Input::Create); |
506 | 1 | assert_eq!(out.effects, [Effect::OpenEditor]); |
507 | | |
508 | | // No name: answered by the form. |
509 | 1 | assert!(update(&mut state, Input::Save).effects.is_empty()); |
510 | 1 | assert!(state.draft.as_ref().is_some_and(|d| d.error.is_some())); |
511 | | |
512 | 1 | update(&mut state, Input::NameChanged("Coding".into())); |
513 | 1 | update( |
514 | 1 | &mut state, |
515 | 1 | Input::TermChanged { |
516 | 1 | index: 0, |
517 | 1 | value: "kubectl\nrebase".into(), |
518 | 1 | }, |
519 | 1 | ); |
520 | 1 | let out = update(&mut state, Input::Save); |
521 | 1 | let [Effect::Save(context)] = out.effects.as_slice() else { |
522 | 0 | panic!("a save, got {:?}", out.effects); |
523 | | }; |
524 | 1 | assert_eq!(context.vocabulary, ["kubectl", "rebase"]); |
525 | | |
526 | 1 | let out = update(&mut state, Input::Saved(Ok(context.clone()))); |
527 | 1 | assert_eq!(out.effects, [Effect::List]); |
528 | 1 | assert!(state.get(&context.id).is_some()); |
529 | 1 | let draft = state.draft.as_ref().expect("still open"); |
530 | 1 | assert_eq!(draft.id, context.id, "the one saved"); |
531 | 1 | assert!(!draft.is_new, "now one to edit"); |
532 | 1 | assert!(!draft.saving); |
533 | 1 | assert_eq!(draft.name, "Coding"); |
534 | 1 | } |
535 | | |
536 | | #[test] |
537 | 1 | fn enter_in_a_row_makes_the_next_and_focuses_it() { |
538 | 1 | let mut state = ContextsState::<String>::default(); |
539 | 1 | update(&mut state, Input::Create); |
540 | 1 | let out = update(&mut state, Input::TermSubmitted(0)); |
541 | 1 | let draft = state.draft.as_ref().expect("a draft"); |
542 | 1 | assert_eq!(draft.terms.len(), 2); |
543 | 1 | assert_eq!(out.effects, [Effect::FocusTerm(draft.terms[1].0)]); |
544 | 1 | } |
545 | | |
546 | | #[test] |
547 | 1 | fn a_failed_save_stays_in_the_editor_unless_it_was_closed() { |
548 | 1 | let mut state = ContextsState::<String>::default(); |
549 | 1 | update(&mut state, Input::Create); |
550 | 1 | let out = update(&mut state, Input::Saved(Err("taken".into()))); |
551 | 1 | assert!(out.notice.is_none()); |
552 | 1 | assert_eq!( |
553 | 1 | state.draft.as_ref().and_then(|d| d.error.as_deref()), |
554 | 1 | Some("taken") |
555 | | ); |
556 | 1 | update(&mut state, Input::CancelEdit); |
557 | 1 | let out = update(&mut state, Input::Saved(Err("taken".into()))); |
558 | 1 | assert_eq!(out.notice, Some(Notice::CouldNotSave("taken".into()))); |
559 | 1 | } |
560 | | } |