diff --git a/apps/desktop/src/opening_session.rs b/apps/desktop/src/opening_session.rs index e4c153e..9fe7337 100644 --- a/apps/desktop/src/opening_session.rs +++ b/apps/desktop/src/opening_session.rs @@ -110,11 +110,8 @@ enum CommitKind { outcome: PlayOutcome, shown_prefix: ShownPrefix, }, - /// `new`: the first showing of a new move; otherwise a hint (or the move revealed - /// after another repertoire move). - Preview { - new: bool, - }, + /// A hint, or the move revealed after another repertoire move. + Preview, } #[derive(Clone)] @@ -215,6 +212,9 @@ pub struct OpeningSessionUi { presentation: Option, completed_attempt_ids: BTreeSet, pending_commit: Option, + /// The showing of the new move on the board, recorded together with the move once + /// it is played: a new move left unplayed stays new. + unplayed_preview: Option, commit_in_flight: Option>>, #[cfg(test)] synchronous_commits: bool, @@ -384,6 +384,7 @@ impl OpeningSessionUi { presentation: None, completed_attempt_ids, pending_commit: None, + unplayed_preview: None, commit_in_flight: None, #[cfg(test)] synchronous_commits: false, @@ -635,10 +636,11 @@ impl OpeningSessionUi { LearningSessionPhase::AwaitingIndependent if self.session.current_is_new(&self.learning) => { - // A new move is shown before it is asked (decision 2026-11-26). + // A new move is shown before it is asked; the showing is recorded + // only with the played move. self.ready_since = None; let id = unique_id(self.session.id()); - self.preview_with_id_at(id, true, now, layout::now()); + self.show_new_with_id_at(id, layout::now()); } LearningSessionPhase::AwaitingIndependent | LearningSessionPhase::AwaitingContextIndependent => { @@ -1039,7 +1041,7 @@ impl OpeningSessionUi { if alternative && self.session.phase == LearningSessionPhase::AwaitingIndependent { self.alternative_revealed = true; let id = unique_id(self.session.id()); - self.preview_with_id_at(id, false, now, happened_at); + self.preview_with_id_at(id, now, happened_at); } else if alternative { self.alternative_revealed = true; } @@ -1068,13 +1070,37 @@ impl OpeningSessionUi { if !self.can_hint() || self.completed_attempt_ids.contains(&client_attempt_id) { return false; } - self.preview_with_id_at(client_attempt_id, false, now, happened_at) + self.preview_with_id_at(client_attempt_id, now, happened_at) + } + + /// S2: the new move goes on the board with its arrow. Nothing is written yet. + fn show_new_with_id_at(&mut self, client_attempt_id: String, happened_at: i64) { + if self.has_unsaved() || self.completed_attempt_ids.contains(&client_attempt_id) { + return; + } + match self.session.preview_new(client_attempt_id, happened_at) { + Ok(attempt) => { + self.unplayed_preview = attempt; + self.board.clear(); + } + Err(error) => { + self.error = Some(error.to_string()); + return; + } + } + // The arrow comes from the presentation of the shown move. + match self + .session + .presentation_with_learning(&self.library, &self.learning) + { + Ok(presentation) => self.presentation = Some(presentation), + Err(error) => self.error = Some(error.to_string()), + } } fn preview_with_id_at( &mut self, client_attempt_id: String, - new: bool, now: Instant, happened_at: i64, ) -> bool { @@ -1082,12 +1108,7 @@ impl OpeningSessionUi { return false; } let target = self.session.current_target().cloned(); - let result = if new { - self.session.preview_new(client_attempt_id, happened_at) - } else { - self.session.preview(client_attempt_id, happened_at) - }; - let attempt = match result { + let attempt = match self.session.preview(client_attempt_id, happened_at) { Ok(Some(attempt)) => attempt, Ok(None) => return false, Err(error) => { @@ -1096,7 +1117,7 @@ impl OpeningSessionUi { } }; self.board.clear(); - self.begin_commit_for(attempt, CommitKind::Preview { new }, target, now); + self.begin_commit_for(attempt, CommitKind::Preview, target, now); true } @@ -1116,8 +1137,10 @@ impl OpeningSessionUi { Some("another learning event is still waiting to be saved".to_owned()); return; } + let mut attempts: Vec<_> = self.unplayed_preview.take().into_iter().collect(); + attempts.push(attempt); self.pending_commit = Some(PendingCommit { - attempts: vec![attempt], + attempts, kind, target, }); @@ -1214,7 +1237,7 @@ impl OpeningSessionUi { .then_some(shown_prefix); self.start_playback(*visual, outcome, now); } - CommitKind::Preview { .. } => { + CommitKind::Preview => { self.needs_prepare = true; self.prepare_presentation(now); } @@ -1250,12 +1273,11 @@ impl OpeningSessionUi { | PlayOutcome::ReplayAfterError | PlayOutcome::PreviewedReplay => {} }, - CommitKind::Preview { new: false } => { + CommitKind::Preview => { self.tally.hints += 1; self.tally.miss(target); } - CommitKind::Preview { new: true } | CommitKind::Context | CommitKind::ReusedContext => { - } + CommitKind::Context | CommitKind::ReusedContext => {} } } @@ -1298,6 +1320,7 @@ impl OpeningSessionUi { fn stop(&mut self) { self.stopped = true; + self.unplayed_preview = None; self.pending_automatic = None; if let Some(Playback::Automatic { .. }) = self.playback { self.playback = None; @@ -1486,12 +1509,9 @@ impl OpeningSessionUi { if self.pending_automatic.is_some() { return Some(self.watch()); } - if let Some(pending) = &self.pending_commit { - return match pending.kind { - CommitKind::Preview { new: true } => Some(State::New), - // Between moves: the card keeps what it said. - _ => None, - }; + if self.pending_commit.is_some() { + // Between moves: the card keeps what it said. + return None; } if self.needs_prepare { return None; diff --git a/apps/desktop/src/opening_session_tests.rs b/apps/desktop/src/opening_session_tests.rs index 6a2b614..7515ab2 100644 --- a/apps/desktop/src/opening_session_tests.rs +++ b/apps/desktop/src/opening_session_tests.rs @@ -1275,10 +1275,9 @@ fn a_new_move_is_shown_before_it_is_asked_and_counts_once_played() { ); assert_eq!(f.ui.session.remaining(), 1); tick_with_prime(&mut f.ui, Instant::now(), Duration::ZERO, NOW); - // Shown at once: PREVIEWED recorded, the arrow and the comment visible, no hint. - let attempts = f.attempts(); - assert_eq!(attempts.len(), 1); - assert_eq!(attempts[0].event_type, TrainingEventType::Previewed); + // Shown at once: the arrow and the comment visible, no hint; nothing recorded + // until it is played. + assert!(f.attempts().is_empty()); assert!(f.ui.session.showing_new); assert_eq!(f.ui.state(), Some(State::New)); assert_eq!(f.ui.revealed_uci().as_deref(), Some("e2e4")); @@ -1304,9 +1303,50 @@ fn a_new_move_is_shown_before_it_is_asked_and_counts_once_played() { ); let attempts = f.attempts(); assert_eq!(attempts.len(), 2); + assert_eq!(attempts[0].event_type, TrainingEventType::Previewed); assert_eq!(attempts[1].event_type, TrainingEventType::AutoContext); } +#[test] +fn a_new_move_left_unplayed_stays_new() { + let mut f = Fixture::with( + "1. e4 e5 2. Nf3 *", + LearningSide::White, + &[], + &["e2e4", "g1f3"], + "new-move-unplayed", + SessionKind::Daily, + ); + let at = Instant::now(); + tick_with_prime(&mut f.ui, at, Duration::ZERO, NOW); + assert!(f.ui.play_uci_with_id_at("e2e4", "play-e4".into(), at, NOW)); + let later = at + Duration::from_secs(5); + for _ in 0..4 { + tick_with_prime(&mut f.ui, later, Duration::ZERO, NOW); + } + assert_eq!(f.ui.state(), Some(State::New)); + assert_eq!(f.ui.session.current_expected_uci(), Some("g1f3")); + // A wrong move on a shown new move records nothing either. + assert!(f.ui.play_uci_with_id_at("d2d4", "wrong-nf3".into(), later, NOW)); + tick_with_prime(&mut f.ui, later + WRONG_SHOWN, Duration::ZERO, NOW); + assert_eq!(f.ui.state(), Some(State::New)); + let mut actions = Vec::new(); + f.ui.request_exit(&mut actions); + assert!(f.ui.summary_visible()); + let attempts = f.attempts(); + let nf3 = attempts + .iter() + .filter(|attempt| attempt.expected_uci == "g1f3") + .count(); + assert_eq!( + nf3, + 0, + "{:?}", + attempts.iter().map(|a| a.event_type).collect::>() + ); + assert_eq!(f.ui.tally.new_moves, 1); +} + #[test] fn a_daily_session_brings_mistakes_and_hints_back_and_its_numbers_agree() { // White's moves: e4, Nf3, Bb5, then O-O after a6 Ba4 Nf6 is new. diff --git a/apps/desktop/src/repertoire_ui_tests.rs b/apps/desktop/src/repertoire_ui_tests.rs index 0ebae52..a2611ab 100644 --- a/apps/desktop/src/repertoire_ui_tests.rs +++ b/apps/desktop/src/repertoire_ui_tests.rs @@ -520,8 +520,8 @@ fn a_session_needs_no_question_to_go_back_and_counts_refresh_after_it() { frame(&mut ui, &ctx, &mut settings, size, vec![key(Key::Enter)]); settle(&mut ui, &ctx, &mut settings, size); assert!(matches!(ui.screen, Screen::Repertoires)); - // e4 was shown and played, Nf3 shown when the session stopped: both are due - // again today (a shown move comes back a minute later), d4 is still new. + // e4 was shown and played: it is due again today. Nf3 was on the board, unplayed, + // when the session stopped: like d4 it is still new. let stats = ui.stats.get(&id).unwrap().as_ref().unwrap(); - assert_eq!((stats.unseen, stats.due), (1, 2)); + assert_eq!((stats.unseen, stats.due), (2, 1)); }