use super::chat::chat; use super::figures::{of_scene, pages_of}; use super::fountain::{read, write, Doc, SceneDoc}; use super::heading::{compose, parse, Parts}; use super::layout::{script, wrap, SceneIn, CHARACTER_INDENT, DIALOGUE_INDENT}; use super::lines::{lines, lines_chosen, Kind}; use super::names::{offered, speakers}; use super::press::{kind, press, Key}; use super::utf16_len; fn kinds(body: &str, names: &[&str]) -> Vec { let names: Vec = names.iter().map(|name| name.to_string()).collect(); lines(body, &names).into_iter().map(|line| line.kind).collect() } fn names(list: &[&str]) -> Vec { list.iter().map(|name| name.to_string()).collect() } use Kind::*; const SCENE: &str = "Rows of buses hum under orange lights.\n\nMEERA\nYou waiting for someone?\n\nARJUN\n(holding up a ticket)\nThe bus on this ticket.\n\nMeera takes the ticket.\n\nCUT TO:"; // RULE (contract, 17 Nov 2026): each line of a scene has its kind. #[test] fn each_line_of_a_scene_has_its_kind() { assert_eq!( kinds(SCENE, &[]), [Action, Blank, Character, Dialogue, Blank, Character, Parenthetical, Dialogue, Blank, Action, Blank, Transition] ); let read = lines(SCENE, &[]); assert_eq!(read[5].name.as_deref(), Some("ARJUN")); assert_eq!((read[2].start, read[2].end), (40, 45), "offsets in UTF-16 units"); // Forced marks, centred, notes, headings in a body, extensions. let marks = "!SILENCE.\n\n@McCLANE\nYippee.\n\n>THE END<\n\n[[fix this]]\n\nEXT. ROOF - DAY\n\nMEERA (V.O.)\nHello.\n\n> SMASH CUT\n\n= a synopsis\n\n# Act one"; assert_eq!( kinds(marks, &[]), [Action, Blank, Character, Dialogue, Blank, Centered, Blank, Note, Blank, Heading, Blank, Character, Dialogue, Blank, Transition, Blank, Note, Blank, Note] ); assert_eq!(lines(marks, &[])[11].name.as_deref(), Some("MEERA"), "a name without its extension"); // A capital line with no speech under it is action; a sentence is action. assert_eq!(kinds("He stops.\n\nBANG\n\nShe runs.", &[]), [Action, Blank, Action, Blank, Action]); // A name just typed, nothing under it yet, is a name. assert_eq!(kinds("She waits.\n\nMEERA", &[]), [Action, Blank, Character]); assert_eq!(kinds("She waits.\n\nMEERA\n", &[]), [Action, Blank, Character, Blank]); // A known name in any case is a name; an unknown lower-case one is not. assert_eq!(kinds("Meera\nHi.", &["MEERA"]), [Character, Dialogue]); assert_eq!(kinds("Meera\nHi.", &[]), [Action, Action]); // CRLF reads as LF. assert_eq!(kinds("MEERA\r\nHi.", &[]), [Character, Dialogue]); } // RULE (contract): a character line in a script without capitals is known by // its place and the story's names, never only by capitals. #[test] fn a_telugu_scene_reads_its_kinds_without_capitals() { let body = "వర్షం పడుతోంది. లత ఊగుతోంది.\n\nఅమ్మమ్మ\n(మెల్లగా)\nవర్షం పెద్దదవుతోంది, లోపలికి రా.\n\nలత\nఇంకో ఐదు నిమిషాలు!"; assert_eq!( kinds(body, &["అమ్మమ్మ", "లత"]), [Action, Blank, Character, Parenthetical, Dialogue, Blank, Character, Dialogue] ); // Without the names they are action: Telugu has no capitals to go by. assert_eq!(kinds(body, &[])[2], Action); // A forced name reads as one with no names known. assert_eq!(kinds("@అమ్మమ్మ\nరా.", &[]), [Character, Dialogue]); assert_eq!(lines("@అమ్మమ్మ\nరా.", &[])[0].name.as_deref(), Some("అమ్మమ్మ")); // Offsets count UTF-16 units: each Telugu letter is one. let telugu_lines = lines(body, &names(&["అమ్మమ్మ", "లత"])); assert_eq!(telugu_lines[2].start, utf16_len("వర్షం పడుతోంది. లత ఊగుతోంది.\n\n")); // The Fountain round trip keeps every byte. let doc = Doc { title: vec![("Title".into(), "వర్షం".into())], scenes: vec![SceneDoc { heading: "EXT. వాకిలి - సాయంత్రం".into(), summary: None, body: body.into() }] }; let text = write(&doc); assert_eq!(read_back(&text), doc); assert_eq!(write(&read(&text)), text); } fn read_back(text: &str) -> Doc { read(text) } // RULE (contract): Return after a name opens speech; after speech, a blank // line and a line for the next name; an empty Return after speech is action. #[test] fn return_follows_each_kind() { let known = names(&["MEERA", "ARJUN"]); // After a name: speech, no blank line. let after_name = press("MEERA", 5, Key::Return, &known, None); assert_eq!((after_name.body.as_str(), after_name.cursor, after_name.kind), ("MEERA\n", 6, Dialogue)); // After speech: a blank line and a line for a name. let body = "MEERA\nYou waiting?"; let after_speech = press(body, utf16_len(body), Key::Return, &known, None); assert_eq!((after_speech.body.as_str(), after_speech.kind), ("MEERA\nYou waiting?\n\n", Character)); assert_eq!(after_speech.cursor, utf16_len(&after_speech.body)); // The empty Return there: action, the body untouched. let empty = press(&after_speech.body, after_speech.cursor, Key::Return, &known, Some(Character)); assert_eq!((empty.body.as_str(), empty.kind), (after_speech.body.as_str(), Action)); // After an aside: speech. let aside = press("MEERA\n(quietly)", 15, Key::Return, &known, None); assert_eq!((aside.body.as_str(), aside.kind), ("MEERA\n(quietly)\n", Dialogue)); // After action: a blank line and action. let action = press("She waits.", 10, Key::Return, &known, None); assert_eq!((action.body.as_str(), action.kind, action.cursor), ("She waits.\n\n", Action, 12)); // Inside a line the split keeps the kind. let inside = press("She waits. He runs.", 10, Key::Return, &known, None); assert_eq!((inside.body.as_str(), inside.kind), ("She waits.\n He runs.", Action)); // A name typed in lower case on a line chosen as a name is written in capitals. let chosen = press("She waits.\n\nmeera", 18, Key::Return, &[], Some(Character)); assert_eq!((chosen.body.as_str(), chosen.kind), ("She waits.\n\nMEERA\n", Dialogue)); // A Telugu name chosen as a name is forced with @, and reads as one. let telugu = press("అమ్మమ్మ", 7, Key::Return, &[], Some(Character)); assert_eq!((telugu.body.as_str(), telugu.kind), ("@అమ్మమ్మ\n", Dialogue)); assert_eq!(kinds(&telugu.body, &[])[0], Character); } // RULE (contract): Tab on an empty line makes a name; with words it turns // the line to the next kind. #[test] fn tab_turns_a_line() { let known = names(&["MEERA"]); let empty = press("She waits.\n\n", 12, Key::Tab, &known, None); assert_eq!((empty.kind, empty.body.as_str()), (Character, "She waits.\n\n")); let back = press("She waits.\n\n", 12, Key::Tab, &known, Some(Character)); assert_eq!(back.kind, Action); // Speech → an aside with its brackets, the cursor inside. let aside = press("MEERA\n", 6, Key::Tab, &known, Some(Dialogue)); assert_eq!((aside.body.as_str(), aside.cursor, aside.kind), ("MEERA\n()", 7, Parenthetical)); let speech = press(&aside.body, aside.cursor, Key::Tab, &known, None); assert_eq!((speech.body.as_str(), speech.kind), ("MEERA\n", Dialogue)); // With words: action → name (capitals) and back (forced action). let name = press("She waits.\n\nraghu", 15, Key::Tab, &known, None); assert_eq!((name.body.as_str(), name.kind), ("She waits.\n\nRAGHU", Character)); let action = press(&name.body, 15, Key::Tab, &known, None); assert_eq!((action.body.as_str(), action.kind), ("She waits.\n\n!RAGHU", Action)); } // RULE (contract): the element bar makes a line that kind, writing the marks // Fountain needs. #[test] fn the_bar_makes_a_line_each_kind() { let known = names(&["MEERA"]); let body = "She waits.\nCut to the chase"; let transition = kind(body, 1, Transition, &known); assert_eq!((transition.body.as_str(), transition.kind), ("She waits.\n\n>CUT TO THE CHASE", Transition)); let cut = kind("She waits.\n\ncut to:", 2, Transition, &known); assert_eq!((cut.body.as_str(), cut.kind), ("She waits.\n\nCUT TO:", Transition)); let aside = kind("MEERA\nquietly", 1, Parenthetical, &known); assert_eq!((aside.body.as_str(), aside.kind), ("MEERA\n(quietly)", Parenthetical)); let speech = kind("MEERA\n(quietly)", 1, Dialogue, &known); assert_eq!((speech.body.as_str(), speech.kind), ("MEERA\nquietly", Dialogue)); let action = kind("MEERA\nHello there.", 1, Action, &known); assert_eq!((action.body.as_str(), action.kind), ("MEERA\n!Hello there.", Action)); let name = kind("She waits.\narjun", 1, Character, &known); assert_eq!((name.body.as_str(), name.kind), ("She waits.\n\nARJUN", Character)); let heading = kind("kitchen - night", 0, Heading, &known); assert_eq!((heading.body.as_str(), heading.kind), (".KITCHEN - NIGHT", Heading)); let note = kind("fix this", 0, Note, &known); assert_eq!((note.body.as_str(), note.kind), ("[[fix this]]", Note)); let centered = kind("the end", 0, Centered, &known); assert_eq!((centered.body.as_str(), centered.kind), (">the end<", Centered)); // On an empty line the kind stands as chosen. let empty = kind("She waits.\n\n", 2, Character, &known); assert_eq!((empty.body.as_str(), empty.kind), ("She waits.\n\n", Character)); } // RULE (contract): names to offer, the scene's other speaker first. #[test] fn names_offer_the_other_speaker_first() { let story = names(&["MEERA", "ARJUN", "RAGHU", "DEVI"]); let scene = lines("MEERA\nHi.\n\nARJUN\nHello.\n\n", &story); assert_eq!(offered(&scene, scene.len(), &story, ""), ["MEERA", "ARJUN", "RAGHU", "DEVI"], "Arjun spoke last: Meera answers"); assert_eq!(offered(&scene, scene.len(), &story, "r"), ["RAGHU"]); let one = lines("MEERA\nHi.\n\n", &story); assert_eq!(offered(&one, one.len(), &story, "")[..2], ["ARJUN".to_string(), "MEERA".to_string()], "one speaker so far: someone else first"); assert_eq!(speakers(&scene), ["MEERA", "ARJUN"]); let telugu = names(&["అమ్మమ్మ", "లత"]); assert_eq!(offered(&[], 0, &telugu, "అ"), ["అమ్మమ్మ"]); } // RULE (contract): a heading and its parts stay in step. #[test] fn a_heading_reads_into_its_parts_and_back() { let parts = parse("INT. KITCHEN - NIGHT"); assert_eq!(parts, Parts { setting: Some("INT.".into()), place: "KITCHEN".into(), time: Some("NIGHT".into()) }); assert_eq!(compose(&parts), "INT. KITCHEN - NIGHT"); assert_eq!(parse("int/ext car - moving - day").setting.as_deref(), Some("INT./EXT.")); assert_eq!(parse("int/ext car - moving - day").place, "car - moving"); assert_eq!(parse("EXT. BUS DEPOT - OFFICE - NIGHT").place, "BUS DEPOT - OFFICE"); assert_eq!(parse(".THE VOID").setting, None); assert_eq!(compose(&Parts { setting: Some("EXT.".into()), place: "porch".into(), time: Some("evening".into()) }), "EXT. PORCH - EVENING"); assert_eq!(compose(&Parts { setting: Some("EXT.".into()), place: "వాకిలి".into(), time: None }), "EXT. వాకిలి"); } // RULE (contract): words, pages and who speaks follow the body as it changes. #[test] fn figures_follow_the_body() { let empty = of_scene("INT. ROOM - DAY", "", &[]); assert_eq!((empty.words, empty.speakers.len()), (0, 0)); assert_eq!(empty.pages, 0.125, "a heading alone is an eighth"); let one = of_scene("INT. ROOM - DAY", SCENE, &[]); assert_eq!(one.words, 28, "every word on the page, names too"); assert_eq!(one.speakers, ["MEERA", "ARJUN"]); assert_eq!(one.speeches, [("MEERA".to_string(), 1), ("ARJUN".to_string(), 1)]); let longer = format!("{SCENE}\n\nMEERA\nThis bus left an hour ago."); let two = of_scene("INT. ROOM - DAY", &longer, &[]); assert_eq!(two.words, 35); assert_eq!(two.speeches[0], ("MEERA".to_string(), 2)); assert!(two.pages >= one.pages && two.pages <= 1.0); assert_eq!(two.minutes, two.pages); assert_eq!((pages_of(55), pages_of(56), pages_of(6), pages_of(7), pages_of(0)), (1.0, 1.125, 0.125, 0.25, 0.0)); } fn long_speech(lines_of_speech: usize) -> String { let mut body = String::from("MEERA\n"); for at in 0..lines_of_speech { body.push_str(&format!("Line {at} of a speech that goes on.\n")); } body } // RULE (contract): pages break by screenplay rules: about 55 lines, a speech // broken with (MORE) and (CONT'D), never a name alone at a page's foot. #[test] fn pages_break_by_screenplay_rules() { let filler = (0..24).map(|at| format!("Action line {at}.")).collect::>().join("\n\n"); let scenes = vec![SceneIn { id: "s1".into(), heading: "INT. DEPOT - NIGHT".into(), body: format!("{filler}\n\n{}", long_speech(20)) }]; let laid = script(&scenes, &[]); assert!(laid.pages.len() >= 2); for page in &laid.pages { assert!(page.lines.len() <= 55, "page {} holds {}", page.number, page.lines.len()); let last = page.lines.iter().rev().find(|row| row.kind != "blank").unwrap(); assert!(!matches!(last.kind.as_str(), "character" | "heading" | "parenthetical"), "page {} ends on a {}", page.number, last.kind); } let first = &laid.pages[0]; let last = first.lines.last().unwrap(); assert_eq!((last.kind.as_str(), last.text.as_str(), last.indent), ("more", "(MORE)", CHARACTER_INDENT)); let cont = &laid.pages[1].lines[0]; assert_eq!((cont.kind.as_str(), cont.text.as_str()), ("character", "MEERA (CONT'D)")); assert_eq!(laid.pages[1].lines[1].indent, DIALOGUE_INDENT); assert_eq!(laid.scenes[0].page, 1); assert_eq!(first.lines[0].scene.as_ref().map(|scene| scene.number), Some(1)); // A heading with no room for two lines under it goes to the next page. let filler = (0..26).map(|at| format!("Action line {at}.")).collect::>().join("\n\n"); let scenes = vec![ SceneIn { id: "a".into(), heading: "INT. A - DAY".into(), body: filler.clone() }, SceneIn { id: "b".into(), heading: "INT. B - DAY".into(), body: "Two lines\nof action.".into() }, ]; let laid = script(&scenes, &[]); assert_eq!(laid.scenes[1].page, 2, "scene two's heading is not left at page one's foot"); assert_eq!(laid.pages[1].lines[0].kind, "heading"); // Wrapping keeps words whole. assert_eq!(wrap("one two three", 7), ["one two", "three"]); assert_eq!(wrap("", 7), [""]); } // RULE (contract): Fountain out and in, byte for byte on a normal script. #[test] fn fountain_round_trips_byte_for_byte() { let doc = Doc { title: vec![("Title".into(), "The Last Bus".into()), ("Credit".into(), "Written by".into()), ("Author".into(), "A Cotton sample".into()), ("Notes".into(), "line one\nline two".into())], scenes: vec![ SceneDoc { heading: "EXT. BUS DEPOT - NIGHT".into(), summary: Some("Meera starts her last shift.".into()), body: SCENE.into() }, SceneDoc { heading: "THE VOID".into(), summary: None, body: "INT. THIS IS ACTION\n\n= not a synopsis\n\n!INT. ALREADY FORCED".into() }, SceneDoc { heading: "INT. EMPTY - DAY".into(), summary: None, body: String::new() }, ], }; let text = write(&doc); assert!(text.starts_with("Title: The Last Bus\nCredit: Written by\nAuthor: A Cotton sample\nNotes:\n line one\n line two\n\nEXT. BUS DEPOT - NIGHT\n\n= Meera starts her last shift.\n\nRows of buses")); assert!(text.contains("\n.THE VOID\n\n!INT. THIS IS ACTION\n\n!= not a synopsis\n\n!!INT. ALREADY FORCED\n")); assert_eq!(read(&text), doc); assert_eq!(write(&read(&text)), text); // A file from elsewhere: no title page, words before the first heading. let foreign = read("FADE IN:\n\nINT. HOUSE - DAY\n\nShe enters.\n\nEXT. GARDEN - DAY\nThis is not a heading line under one.\n"); assert_eq!(foreign.title, []); assert_eq!(foreign.scenes.iter().map(|scene| scene.heading.as_str()).collect::>(), ["", "INT. HOUSE - DAY", "EXT. GARDEN - DAY"]); assert_eq!(foreign.scenes[0].body, "FADE IN:"); } // RULE (owner's idea): a scene reads as a chat. #[test] fn a_scene_reads_as_a_chat() { let read = chat(SCENE, &[]); assert!(read.two_hander); assert_eq!(read.items.len(), 5); assert_eq!((read.items[1].who.as_deref(), read.items[1].side.as_deref(), read.items[1].text.as_str()), (Some("MEERA"), Some("left"), "You waiting for someone?")); assert_eq!((read.items[2].who.as_deref(), read.items[2].side.as_deref(), read.items[2].aside.as_deref()), (Some("ARJUN"), Some("right"), Some("holding up a ticket"))); assert_eq!(read.items[4].kind, "transition"); } // RULE (contract): the line being typed keeps the kind the person chose. #[test] fn a_chosen_kind_stands_while_the_text_reads_action() { let chosen = lines_chosen("She waits.\n\nmee", &[], Some((2, Character))); assert_eq!(chosen[2].kind, Character); let ignored = lines_chosen("MEERA\nHi.", &[], Some((1, Action))); assert_eq!(ignored[1].kind, Dialogue, "a chosen kind never overrides what the text says"); } // RULE: the Worker reaches the pure story rules through the boundary. #[test] fn the_boundary_answers_the_story_rules() { let call = |name: &str, input: serde_json::Value| crate::boundary::call(name, &input.to_string()).unwrap_or_else(|why| panic!("{name}: {why}")); assert_eq!(call("story.lines", serde_json::json!({ "body": "MEERA\nHi." }))["lines"][0]["kind"], "character"); assert_eq!(call("story.figures", serde_json::json!({ "heading": "INT. A - DAY", "body": "MEERA\nHi." }))["speakers"], serde_json::json!(["MEERA"])); assert_eq!(call("story.heading.parse", serde_json::json!({ "heading": "EXT. ROOF - DAY" }))["place"], "ROOF"); assert_eq!(call("story.heading.compose", serde_json::json!({ "setting": "INT.", "place": "kitchen", "time": "night" })), "INT. KITCHEN - NIGHT"); let doc = serde_json::json!({ "title": [["Title", "One"]], "scenes": [{ "heading": "INT. A - DAY", "body": "Hi." }] }); let text = call("story.fountain.write", doc.clone()); assert_eq!(call("story.fountain.read", serde_json::json!({ "text": text })), doc); }