//! Tests for what a reply leaves in the transcript: the rows that reach //! the scrollback while the reply is still being written, the markdown //! styling they carry, the blank rows between the blocks, and what a //! cancelled turn keeps. The play loop puts the rows there, so these //! tests run scripts through the harness in `screen_tests.rs`. The tests //! for a terminal whose width disagrees with the round's are in //! `transcript_width_tests.rs`, which drives a `Transcript` directly. use crate::play::keys::Key; use crate::play::screen::tests::{ CRAMPED, ROOMY, Step, TALL_BLOCK, TALL_ROWS, done, play_script, play_script_on, press, typing, }; use crate::play::worker::TurnEvent; use super::*; #[test] fn finished_rows_reach_the_transcript_while_the_reply_streams() { let steps = vec![ Step::Turn(TurnEvent::Delta( "You wake in a cell that smells of wet stone ".to_string(), )), Step::Turn(TurnEvent::Delta("and old smoke.".to_string())), ]; let played = play_script(steps); // Nothing ends this turn, so the first row reached the transcript // while the reply was still streaming. assert!( played .transcript() .contains("You wake in a cell that smells of wet") ); assert_eq!(played.tail(), "stone and old smoke."); } #[test] fn a_long_reply_wraps_to_the_width() { let reply = "You wake in a cell that smells of wet stone and old smoke."; let steps = vec![ Step::Turn(TurnEvent::Delta(reply.to_string())), Step::Turn(TurnEvent::Done(reply.to_string())), ]; let played = play_script(steps); assert!( played .transcript() .contains("You wake in a cell that smells of wet") ); assert!(played.transcript().contains("stone and old smoke.")); } #[test] fn a_paragraph_break_streams_as_one_blank_row() { let steps = vec![ Step::Turn(TurnEvent::Delta("one\n\ntwo".to_string())), Step::Turn(TurnEvent::Delta(" more".to_string())), done(), ]; let played = play_script(steps); let transcript = played.transcript(); let lines: Vec<&str> = transcript.lines().collect(); let one = lines.iter().position(|line| *line == "one").unwrap(); let two = lines.iter().position(|line| *line == "two more").unwrap(); assert_eq!(two - one, 2); assert_eq!(lines[one + 1], ""); } #[test] fn narration_ending_in_blank_lines_flushes_without_extra_blank_rows() { let steps = vec![ Step::Turn(TurnEvent::Delta("You wake.\n\n".to_string())), Step::Turn(TurnEvent::Tool(Text::raw("a tool line"))), ]; let played = play_script(steps); let transcript = played.transcript(); let lines: Vec<&str> = transcript.lines().collect(); let narration = lines.iter().position(|line| *line == "You wake.").unwrap(); let tool = lines .iter() .position(|line| *line == "a tool line") .unwrap(); assert_eq!(tool - narration, 2); } #[test] fn a_tool_line_reaches_the_transcript_dim_and_italic() { let text = "the DM checks a rule"; let steps = vec![ Step::Turn(TurnEvent::Tool(Text::raw(text.to_string()))), done(), ]; let played = play_script(steps); assert_eq!( played.transcript_modifiers(text), vec![Modifier::DIM | Modifier::ITALIC; text.chars().count()] ); } #[test] fn a_tool_span_with_its_own_style_is_not_dimmed() { let text = Text::from(Line::from(vec![ Span::raw("the total is "), Span::styled( "20", Style::new().fg(Color::White).add_modifier(Modifier::BOLD), ), ])); let steps = vec![Step::Turn(TurnEvent::Tool(text)), done()]; let played = play_script(steps); // The bold-white total keeps its own style; only the unstyled // "the total is " picks up the dim tool default. assert_eq!(played.transcript_modifiers("20"), vec![Modifier::BOLD; 2]); assert_eq!( played.transcript_modifiers("the total is "), vec![Modifier::DIM | Modifier::ITALIC; 13] ); } #[test] fn a_finished_reply_ending_in_blank_lines_has_no_extra_blank_rows() { let steps = vec![ Step::Turn(TurnEvent::Delta("You wake.\n\n".to_string())), Step::Turn(TurnEvent::Done("You wake.\n\n".to_string())), ]; let played = play_script(steps); let transcript = played.transcript(); let lines: Vec<&str> = transcript.lines().collect(); assert_eq!(lines, vec!["You wake."]); } #[test] fn a_secret_roll_between_narrated_rounds_keeps_every_word_in_the_transcript() { let steps = vec![ Step::Turn(TurnEvent::Delta("You edge along the wall. ".to_string())), Step::Turn(TurnEvent::Delta("Nothing stirs.".to_string())), Step::Turn(TurnEvent::Done("Nothing stirs.".to_string())), ]; let played = play_script(steps); let transcript = played.transcript(); assert_eq!(transcript.matches("You edge along the wall.").count(), 1); assert_eq!(transcript.matches("Nothing stirs.").count(), 1); } #[test] fn a_cancel_mid_stream_keeps_every_streamed_word_in_the_transcript_once() { let mut steps = typing("hi"); steps.push(press(Key::Enter)); steps.push(Step::Turn(TurnEvent::Delta( "You wake in a cell that smells of wet stone ".to_string(), ))); steps.push(Step::Turn(TurnEvent::Delta("and old smoke.".to_string()))); steps.push(press(Key::Cancel)); steps.push(Step::Turn(TurnEvent::Cancelled("partial".to_string()))); let played = play_script(steps); let transcript = played.transcript(); assert_eq!( transcript .matches("You wake in a cell that smells of wet") .count(), 1 ); assert_eq!(transcript.matches("stone and old smoke.").count(), 1); let tail = transcript.find("stone and old smoke.").unwrap(); let interrupted = transcript.find("(interrupted)").unwrap(); assert!(tail < interrupted); } /// A reply whose bold word arrives split across three deltas, so the /// markers land in different ones than the word they mark. fn emphasized_reply() -> Vec { vec![ Step::Turn(TurnEvent::Delta("The door is ".to_string())), Step::Turn(TurnEvent::Delta("**bar".to_string())), Step::Turn(TurnEvent::Delta("red**.".to_string())), done(), ] } #[test] fn a_streamed_emphasis_reaches_the_transcript_as_a_bold_span() { let played = play_script(emphasized_reply()); assert_eq!( played.transcript_modifiers("barred"), vec![Modifier::BOLD; 6] ); } #[test] fn a_streamed_emphasis_leaves_no_markers_in_the_transcript() { let played = play_script(emphasized_reply()); assert!(played.transcript().contains("The door is barred.")); assert!(!played.transcript().contains('*')); } /// A table that arrives one source row per delta. fn table_deltas() -> Vec { [ "| Die | Face |\n", "|-----|------|\n", "| d4 | 3 |\n", "| d20 | 17 |\n", ] .into_iter() .map(|row| Step::Turn(TurnEvent::Delta(row.to_string()))) .collect() } #[test] fn a_table_stays_out_of_the_transcript_while_its_rows_arrive() { let played = play_script_on(ROOMY, table_deltas()); assert!(!played.transcript().contains("Die")); } #[test] fn a_table_reaches_the_transcript_once_the_block_after_it_starts() { let mut steps = table_deltas(); steps.push(Step::Turn(TurnEvent::Delta( "\nThe die is cast.".to_string(), ))); let played = play_script_on(ROOMY, steps); assert!(played.transcript().contains("│ d20 │ 17 │")); } #[test] fn a_table_reaches_the_transcript_bordered_and_aligned_when_the_turn_ends() { let mut steps = table_deltas(); steps.push(done()); let played = play_script_on(ROOMY, steps); let transcript = played.transcript(); let lines: Vec<&str> = transcript.lines().collect(); assert_eq!( lines, vec![ "┌─────┬──────┐", "│ Die │ Face │", "├─────┼──────┤", "│ d4 │ 3 │", "│ d20 │ 17 │", "└─────┴──────┘", ] ); } /// A turn the player cancels while a heading is still streaming. fn cancelled_heading() -> Vec { let mut steps = typing("hi"); steps.push(press(Key::Enter)); steps.push(Step::Turn(TurnEvent::Delta("## The ".to_string()))); steps.push(Step::Turn(TurnEvent::Delta("Cellar".to_string()))); steps.push(press(Key::Cancel)); steps.push(Step::Turn(TurnEvent::Cancelled( "## The Cellar".to_string(), ))); steps } #[test] fn a_cancel_mid_block_commits_the_partial_block_before_the_interrupted_aside() { let played = play_script(cancelled_heading()); let transcript = played.transcript(); let heading = transcript.find("The Cellar").unwrap(); let interrupted = transcript.find("(interrupted)").unwrap(); assert!(heading < interrupted); } #[test] fn a_cancel_mid_block_keeps_the_partial_block_styled() { let played = play_script(cancelled_heading()); assert_eq!( played.transcript_modifiers("The Cellar"), vec![Modifier::BOLD | Modifier::UNDERLINED; 10] ); } #[test] fn a_block_taller_than_the_tail_area_spills_its_top_rows_while_the_turn_runs() { let steps = vec![Step::Turn(TurnEvent::Delta(TALL_BLOCK.to_string()))]; let played = play_script_on(CRAMPED, steps); let transcript = played.transcript(); let lines: Vec<&str> = transcript.lines().collect(); assert_eq!(lines, vec![TALL_ROWS[0], TALL_ROWS[1]]); } #[test] fn every_row_of_a_spilled_block_lands_in_the_transcript_exactly_once() { let steps = vec![Step::Turn(TurnEvent::Delta(TALL_BLOCK.to_string())), done()]; let played = play_script_on(CRAMPED, steps); let transcript = played.transcript(); let lines: Vec<&str> = transcript.lines().collect(); assert_eq!( lines, vec![TALL_ROWS[0], TALL_ROWS[1], TALL_ROWS[2], TALL_ROWS[3]] ); } #[test] fn two_narrated_rounds_around_a_tool_line_keep_one_blank_row_between_the_blocks() { let steps = vec![ Step::Turn(TurnEvent::Delta( "You reach for the lock.\n\nIt resists.".to_string(), )), Step::Turn(TurnEvent::Tool(Text::raw("a tool line"))), Step::Turn(TurnEvent::Delta("The lock gives.".to_string())), done(), ]; let played = play_script(steps); let transcript = played.transcript(); let lines: Vec<&str> = transcript.lines().collect(); assert_eq!( lines, vec![ "You reach for the lock.", "", "It resists.", "", "a tool line", "", "The lock gives.", ] ); }