//! Story's four screens over invented stories, signed out. use serde_json::{json, Value}; use tempfile::TempDir; use crate::handle::{CottonHandle, OpenConfig, OpenMode}; use crate::methods::dispatch; fn phone(dir: &TempDir) -> CottonHandle { let manifest = json!({ "genesis": "personal", "flavour": "story", "name": "Cotton Story", "databases": "single", "database": "personal", "business": { "server": { "real": "https://cottondb.com", "dev": "https://dev.example.invalid" } }, "fan": { "home": false, "tools": false, "opens": "story" }, "kit": "story", "ground": "blank" }); let handle = CottonHandle::open(OpenConfig { mode: OpenMode::Phone { container_path: dir.path().to_string_lossy().to_string(), previous_container_path: None }, app_version: None, time_zone: Some("Asia/Kolkata".to_string()), device_secret: Some(crate::fixture::paired_secret()), vault_secret: None, flavor: cotton_core::flavor::Flavor::Real, manifest: Some(manifest), }) .expect("an open core"); dispatch(&handle, "workspace.startLocal", &json!({ "deviceName": "An invented phone" })).expect("started"); handle } fn ask(handle: &CottonHandle, method: &str, request: Value) -> Value { dispatch(handle, method, &request).unwrap_or_else(|error| panic!("{method} {request}: {error:?}")) } fn id(value: &Value) -> String { value.as_str().expect("an id").to_string() } fn start(handle: &CottonHandle) -> String { id(&ask(handle, "story.start", json!({ "title": "The Last Letter" }))["story"]) } fn kinds(view: &Value) -> Vec { view["lines"].as_array().unwrap().iter().map(|line| line["kind"].as_str().unwrap().to_string()).collect() } fn texts(view: &Value) -> Vec { view["lines"].as_array().unwrap().iter().map(|line| line["text"].as_str().unwrap().to_string()).collect() } fn row_kinds(view: &Value) -> Vec { view["rows"].as_array().unwrap().iter().map(|row| row["kind"].as_str().unwrap().to_string()).collect() } /// A story shaped as the old app left it: a heading and a Fountain body, no /// line rows, no tints, no answers. fn old_story(handle: &CottonHandle) -> (String, String) { let story = start(handle); let scene = id(&ask(handle, "story.scene.new", json!({ "story": story }))["scene"]); let body = "Maya scrubs a clean plate.\n\nRAVI\n(softly)\nYou're going to wear a hole in that.\n\nMAYA\nI'm almost done.\n\n[[She should look at the drawer first.]]\n\nShe slides a letter under the dish rack."; ask(handle, "story.scene.write", json!({ "id": scene, "heading": "INT. KITCHEN - NIGHT", "body": body })); (story, scene) } /// RULE (Story's new screens, 18 Nov 2026): a story written before the new /// screens reads whole in them with no step run over it: its place and time /// set as words, its dot from its time of day, its last speech as the preview /// in its speaker's tint, minutes worked out, the lines of the scene in order. #[test] fn an_old_story_reads_in_the_new_screens() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let (story, scene) = old_story(&handle); let list = ask(&handle, "story.list", json!({})); assert_eq!(list["stories"][0]["summary"], "1 scene · 1 min"); let scenes = ask(&handle, "story.scenes", json!({ "story": story })); assert_eq!(scenes["story"]["summary"], "1 scene · 1 min"); let row = &scenes["rows"][0]; assert_eq!((row["title"].as_str(), row["band"].as_str(), row["minutes_word"].as_str()), (Some("Kitchen · night"), Some("night"), Some("1 min"))); assert_eq!(row["preview"]["text"], "MAYA: I'm almost done."); assert!(row["preview"]["tint"]["paper"].is_string(), "a speech wears its speaker's tint"); assert_eq!(scenes["slip"]["question"], "Who is your story about?"); let view = ask(&handle, "story.scene", json!({ "id": scene })); assert_eq!(kinds(&view), ["action", "dialogue", "dialogue", "note", "action"]); let (second, third) = (id(&view["lines"][1]["id"]), id(&view["lines"][2]["id"])); assert!(!second.starts_with("body:"), "every line is a row with its own id from the first write"); assert_eq!(view["lines"][1]["aside"], "softly"); assert_eq!(view["slugline"], "INT. KITCHEN - NIGHT"); assert_eq!(view["chips"].as_array().unwrap().len(), 2); assert_eq!(view["lines"][2]["label"], "Maya says: I'm almost done."); assert_eq!(view["picker"]["band"], "night"); assert_eq!(view["picker"]["times"].as_array().unwrap().len(), 4); let changing = ask(&handle, "story.scene", json!({ "id": scene, "editing": third, "after": second })); assert_eq!(changing["composer"]["banner"], "Changing Maya's line"); assert_eq!(changing["composer"]["text"], "I'm almost done."); assert_eq!((changing["editing"].as_str(), changing["insert"].clone()), (Some(third.as_str()), Value::Null), "changing a line is not adding one"); } /// RULE: tapping between two lines says where the next line goes, the line /// goes there, the writer stays after it, and the scene's Fountain body is /// kept from the lines, so the script and every figure read the same text. #[test] fn a_line_goes_between_two_others() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let (story, scene) = old_story(&handle); let second = id(&ask(&handle, "story.scene", json!({ "id": scene }))["lines"][1]["id"]); let view = ask(&handle, "story.scene", json!({ "id": scene, "after": second })); assert_eq!(view["insert"]["words"], "Adding after RAVI's line".replace("RAVI", "Ravi")); let maya = view["chips"].as_array().unwrap().iter().find(|chip| chip["name"] == "Maya").unwrap()["id"].clone(); let added = ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "dialogue", "character": maya, "text": "Since when do you care?", "after": second })); let lines = &added["scene"]; assert_eq!(texts(lines)[2], "Since when do you care?"); assert_eq!(lines["insert"]["after"], added["line"], "the writer goes on from the line just written"); let fountain = ask(&handle, "story.fountain.write", json!({ "story": story })); let text = fountain["text"].as_str().unwrap(); let (earlier, later) = (text.find("wear a hole").unwrap(), text.find("Since when").unwrap()); assert!(earlier < later && later < text.find("almost done").unwrap(), "{text}"); assert!(text.contains("[[She should look at the drawer first.]]"), "a note stays in the body: {text}"); } /// RULE: a line can be added between the same two lines again and again /// without the others being renumbered out of order. #[test] fn many_lines_fit_in_one_gap() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let (_, scene) = old_story(&handle); let first = ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "action", "text": "line 0", "after": "start" })); let anchor = id(&first["line"]); for n in 1..=40 { ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "action", "text": format!("line {n}"), "after": anchor })); } let view = ask(&handle, "story.scene", json!({ "id": scene })); let said = texts(&view); assert_eq!(said[0], "line 0"); let expected: Vec = (1..=40).rev().map(|n| format!("line {n}")).collect(); assert_eq!(said[1..41], expected[..]); } /// RULE: a body written elsewhere (an assistant, an older phone) is the /// truth over rows kept for an earlier body, and nothing written is lost. #[test] fn a_body_written_elsewhere_wins() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let (_, scene) = old_story(&handle); ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "action", "text": "She turns." })); ask(&handle, "story.scene.write", json!({ "id": scene, "body": "The kitchen is empty." })); let view = ask(&handle, "story.scene", json!({ "id": scene })); assert_eq!(texts(&view), ["The kitchen is empty."]); assert!(!view["lines"][0]["id"].as_str().unwrap().starts_with("body:"), "the body is laid over the rows at once"); let again = ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "note", "text": "Too quiet?" })); assert_eq!(texts(&again["scene"]), ["The kitchen is empty.", "Too quiet?"]); } /// RULE: every kind of line is written, edited and removed through the /// core, an entrance worded from the kit and a speech needing its character. #[test] fn every_kind_of_line_is_written() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let story = start(&handle); let scene = id(&ask(&handle, "story.scene.add", json!({ "story": story, "place": "Kitchen", "time": "night" }))["scene"]); let ravi = ask(&handle, "story.character.add", json!({ "story": story, "name": "Ravi" }))["character"].clone(); assert_eq!((ravi["initial"].as_str(), ravi["tint"]["key"].as_str()), (Some("R"), Some("rose"))); let again = ask(&handle, "story.character.add", json!({ "story": story, "name": "RAVI" })); assert_eq!(again["character"]["id"], ravi["id"], "one person, however the name is typed"); let refused = dispatch(&handle, "story.line.add", &json!({ "scene": scene, "kind": "dialogue", "text": "Hello" })).unwrap_err(); assert_eq!(refused.message, "A spoken line needs its character."); ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "enters", "character": ravi["id"] })); let spoke = ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "dialogue", "character": ravi["id"], "text": "Hello" })); let view = &spoke["scene"]; assert_eq!(texts(view), ["Ravi enters", "Hello"]); assert_eq!(view["slugline"], "INT. KITCHEN - NIGHT", "the heading is worked out from the place and the time"); let edited = ask(&handle, "story.line.edit", json!({ "scene": scene, "id": spoke["line"], "text": "Hello, Maya" })); assert_eq!(texts(&edited)[1], "Hello, Maya"); let removed = ask(&handle, "story.line.remove", json!({ "scene": scene, "id": spoke["line"] })); assert_eq!(texts(&removed), ["Ravi enters"]); let fountain = ask(&handle, "story.fountain.write", json!({ "story": story })); assert!(fountain["text"].as_str().unwrap().contains("Ravi enters."), "{}", fountain["text"]); } /// RULE: scenes reorder and cross the interval, sequences fold, and an /// unwritten scene shows its open question instead of a preview. #[test] fn the_list_folds_and_reorders() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let story = start(&handle); let add = |place: &str| id(&ask(&handle, "story.scene.add", json!({ "story": story, "place": place, "time": "day" }))["scene"]); let (one, two, three) = (add("Kitchen"), add("Station"), add("Wedding")); let open = ask(&handle, "story.scene.add", json!({ "story": story, "place": "Rooftop", "time": "dawn", "question": "How does Ravi find out?" })); let four = id(&open["scene"]); let row = open["scenes"]["rows"].as_array().unwrap().iter().find(|row| row["id"] == four.as_str()).unwrap().clone(); assert_eq!((row["unwritten"].as_bool(), row["question"].as_str()), (Some(true), Some("How does Ravi find out?"))); let view = ask(&handle, "story.interval", json!({ "story": story, "after": two })); assert_eq!(row_kinds(&view), ["scene", "scene", "interval", "scene", "scene"]); let across = ask(&handle, "story.scene.place", json!({ "id": one, "after": two, "across": true })); let order: Vec<&str> = across["rows"].as_array().unwrap().iter().map(|row| row["id"].as_str().unwrap_or("|")).collect(); assert_eq!(order, [two.as_str(), "|", one.as_str(), three.as_str(), four.as_str()], "the scene crossed the interval"); let back = ask(&handle, "story.scene.place", json!({ "id": one, "after": two })); let order: Vec<&str> = back["rows"].as_array().unwrap().iter().map(|row| row["id"].as_str().unwrap_or("|")).collect(); assert_eq!(order, [two.as_str(), one.as_str(), "|", three.as_str(), four.as_str()], "and came back"); let acted = ask(&handle, "story.sequence.add", json!({ "story": story, "title": "The wedding", "after": one })); let sequence = acted["rows"].as_array().unwrap().iter().find(|row| row["kind"] == "sequence").unwrap().clone(); assert_eq!(sequence["summary"], "2 scenes · 0 min"); let folded = ask(&handle, "story.sequence.edit", json!({ "id": sequence["id"], "folded": true })); assert_eq!(row_kinds(&folded), ["scene", "scene", "interval", "sequence"], "a folded sequence hides its scenes"); // The old move renumbers the scenes; the interval and the sequence hold their places. ask(&handle, "story.scene.move", json!({ "id": two, "by": 1 })); let held = ask(&handle, "story.scenes", json!({ "story": story })); assert_eq!(row_kinds(&held), ["scene", "scene", "interval", "sequence"]); let gone = ask(&handle, "story.scene.remove", json!({ "id": two })); assert_eq!(gone["story"]["summary"], "3 scenes · 0 min"); } /// RULE: Your story asks the kit's questions in order; an answer may point at /// scenes, a skipped question comes back after the rest, and the summary reads /// the answers back as one page. #[test] fn your_story_asks_and_reads_back() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let story = start(&handle); let scene = id(&ask(&handle, "story.scene.add", json!({ "story": story, "place": "Rooftop", "time": "dawn" }))["scene"]); let first = ask(&handle, "story.interview", json!({ "story": story })); assert_eq!(first["current"], "who"); assert_eq!(first["summary"]["empty"], "Your answers will read back here as one page."); let answered = ask(&handle, "story.answer", json!({ "story": story, "question": "who", "text": "Maya, 34. A nurse who never opens her mail.", "scenes": [scene] })); assert_eq!(answered["current"], "want"); assert_eq!(answered["questions"][0]["scenes"][0]["title"], "Rooftop · dawn"); let skipped = ask(&handle, "story.skip", json!({ "story": story, "question": "want" })); assert_eq!(skipped["current"], "obstacle"); assert_eq!(skipped["questions"][1]["state"], "skipped"); let mut last = skipped; for key in ["obstacle", "stakes", "opening", "turn", "ending", "heart"] { last = ask(&handle, "story.answer", json!({ "story": story, "question": key, "text": format!("About {key}.") })); } assert_eq!(last["current"], "want", "the skipped question comes back last"); let all = ask(&handle, "story.answer", json!({ "story": story, "question": "want", "text": "To keep Ravi from reading the letter." })); assert_eq!(all["current"], Value::Null); assert!(all["summary"]["text"].as_str().unwrap().starts_with("Maya, 34.")); assert_eq!(ask(&handle, "story.scenes", json!({ "story": story }))["slip"]["question"], "Read your story back"); let refused = dispatch(&handle, "story.answer", &json!({ "story": story, "question": "nonsense", "text": "x" })).unwrap_err(); assert_eq!(refused.message, "Story does not ask that question."); } /// RULE: the draft is the standard page, its heading worked out from the /// place and time, a note left off the page. #[test] fn the_draft_is_the_standard_page() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let story = start(&handle); let scene = id(&ask(&handle, "story.scene.add", json!({ "story": story, "place": "Kitchen", "time": "night" }))["scene"]); let ravi = ask(&handle, "story.character.add", json!({ "story": story, "name": "Ravi" }))["character"]["id"].clone(); ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "dialogue", "character": ravi, "text": "You came." })); ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "note", "text": "Too soon?" })); let draft = ask(&handle, "story.draft", json!({ "scene": scene, "paper": "a4" })); assert_eq!(draft["label"], "DRAFT"); assert_eq!(draft["fountain_name"], "The Last Letter.fountain"); assert!(draft["fountain_text"].as_str().unwrap().contains("You came.")); let page: Vec<&str> = draft["pages"][0]["lines"].as_array().unwrap().iter().filter_map(|row| row["text"].as_str()).collect(); assert!(page.iter().any(|text| text.contains("INT. KITCHEN - NIGHT")), "{page:?}"); assert!(page.contains(&"RAVI") && page.contains(&"You came."), "{page:?}"); assert!(!page.iter().any(|text| text.contains("Too soon")), "{page:?}"); } fn fan_ids(fan: &Value) -> Vec { fan["tiles"][0]["views"].as_array().unwrap().iter().map(|view| view["id"].as_str().unwrap().to_string()).collect() } /// RULE (owner on Cotton Story build 4, 18 Nov 2026): the fan holds Account, /// one tile per screenplay by its title, and one tile that starts a new /// screenplay, and none of Scenes, Script, Characters, Places, Notes or /// Stories. It stands on the screenplay being written, and choosing another /// moves no tile. #[test] fn the_fan_is_account_the_screenplays_and_a_new_one() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); assert_eq!(cotton_core::kit::AppKit::named("story").unwrap().fan_of.as_deref(), Some("story_story")); let first = ask(&handle, "shell.fan", json!({ "timezone": "Asia/Kolkata" })); assert_eq!(first["tiles"].as_array().unwrap().len(), 1, "one tool's tile"); assert_eq!(fan_ids(&first), ["account", "new_story"], "no screenplay yet"); assert_eq!(first["opens"]["tier"], "views"); assert_eq!(first["tiles"][0]["current"], "new_story"); let letter = start(&handle); let bus = id(&ask(&handle, "story.start", json!({ "title": "The Last Bus" }))["story"]); let fan = ask(&handle, "shell.fan", json!({ "timezone": "Asia/Kolkata" })); let (letter_card, bus_card) = (format!("story:{letter}"), format!("story:{bus}")); assert_eq!(fan_ids(&fan), ["account", letter_card.as_str(), bus_card.as_str(), "new_story"]); let views = fan["tiles"][0]["views"].as_array().unwrap(); assert_eq!((views[1]["label"].as_str(), views[1]["story"].as_str()), (Some("The Last Letter"), Some(letter.as_str()))); assert_eq!(views[3]["label"], "New screenplay"); assert_eq!(views[3]["create"]["method"], "story.start"); assert_eq!(fan["tiles"][0]["current"], bus_card.as_str(), "it stands on the newest screenplay"); assert_eq!(fan["root"]["raised"], bus_card.as_str()); ask(&handle, "story.choose", json!({ "story": letter })); let chosen = ask(&handle, "shell.fan", json!({ "timezone": "Asia/Kolkata" })); assert_eq!(fan_ids(&chosen), fan_ids(&fan), "no tile moves"); assert_eq!(chosen["tiles"][0]["current"], letter_card.as_str(), "the fan follows the screenplay chosen"); // What the phone routes each tile by: its face, and the story its read names. let views = chosen["tiles"][0]["views"].as_array().unwrap(); assert_eq!((views[2]["face"].as_str(), views[2]["read"]["where"]["story"].as_str()), (Some("screenplay"), Some(bus.as_str()))); assert_eq!(views[3]["face"], "screenplay_new"); assert_eq!(views[3]["read"]["table"], "story_scene", "no tile reads a table the app cannot draw"); // A tile just picked stands, though the current screenplay is another. let tool = chosen["tiles"][0]["key"].as_str().unwrap().to_string(); let picked = ask(&handle, "shell.fan", json!({ "timezone": "Asia/Kolkata", "chosen": { tool.clone(): bus_card } })); assert_eq!(picked["tiles"][0]["current"], bus_card.as_str(), "the picked tile is the one shown"); let new_tile = ask(&handle, "shell.fan", json!({ "timezone": "Asia/Kolkata", "chosen": { tool.clone(): "new_story" } })); assert_eq!(new_tile["tiles"][0]["current"], "new_story"); let gone = ask(&handle, "shell.fan", json!({ "timezone": "Asia/Kolkata", "chosen": { tool: "story:gone" } })); assert_eq!(gone["tiles"][0]["current"], letter_card.as_str(), "a tile no longer there falls back to the current screenplay"); } /// RULE (owner on build 5, 19 Nov 2026): Plus and back adds no scene, and /// reading a story never makes one; a scene holding anything at all is kept. #[test] fn plus_and_back_adds_no_scene() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let story = start(&handle); let count = |handle: &CottonHandle| ask(handle, "story.scenes", json!({ "story": story }))["rows"].as_array().unwrap().len(); for _ in 0..3 { ask(&handle, "shell.fan", json!({ "timezone": "Asia/Kolkata" })); ask(&handle, "story.list", json!({})); } assert_eq!(count(&handle), 0, "opening makes no scene"); let blank = id(&ask(&handle, "story.scene.add", json!({ "story": story }))["scene"]); assert_eq!(count(&handle), 1); let left = ask(&handle, "story.scene.leave", json!({ "id": blank })); assert_eq!(left["rows"].as_array().unwrap().len(), 0, "Plus and back leaves nothing"); let placed = id(&ask(&handle, "story.scene.add", json!({ "story": story, "place": "Kitchen" }))["scene"]); ask(&handle, "story.scene.leave", json!({ "id": placed })); let written = id(&ask(&handle, "story.scene.add", json!({ "story": story }))["scene"]); ask(&handle, "story.line.add", json!({ "scene": written, "kind": "action", "text": "She waits." })); ask(&handle, "story.scene.leave", json!({ "id": written })); let asked = id(&ask(&handle, "story.scene.add", json!({ "story": story, "question": "Who knows?" }))["scene"]); ask(&handle, "story.scene.leave", json!({ "id": asked })); assert_eq!(count(&handle), 3, "a place, a line or a question keeps the scene"); } fn pick_names(view: &Value) -> Vec { view["pick"]["sections"][0]["rows"].as_array().unwrap().iter().map(|row| row["name"].as_str().unwrap().to_string()).collect() } fn next_of(view: &Value) -> (String, Option) { let next = &view["pick"]["next"]; (next["kind"].as_str().unwrap().to_string(), next["character"].as_str().map(str::to_string)) } /// RULE (owner, 19 Nov 2026, the shared composer): the pick chip's menu is /// Action and Note, then the characters by full name: the one suggested next /// first, then those speaking in this scene most recent first, then the rest /// by their lines in the screenplay; New character… last. In an exchange /// between two people the other one is suggested; someone who just entered /// speaks; a fresh scene starts on Action; a line changed keeps its own. #[test] fn the_pick_suggests_who_speaks_next() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let story = start(&handle); let add = |name: &str| id(&ask(&handle, "story.character.add", json!({ "story": story, "name": name }))["character"]["id"]); let (ravi, maya, anu) = (add("Ravi"), add("Maya"), add("Anu Rao")); // Anu speaks most in the screenplay, in another scene. let elsewhere = id(&ask(&handle, "story.scene.add", json!({ "story": story, "place": "Wedding", "time": "day" }))["scene"]); for words in ["You came.", "I did.", "Stay."] { ask(&handle, "story.line.add", json!({ "scene": elsewhere, "kind": "dialogue", "character": anu, "text": words })); } let scene = id(&ask(&handle, "story.scene.add", json!({ "story": story, "place": "Kitchen", "time": "night" }))["scene"]); let fresh = ask(&handle, "story.scene", json!({ "id": scene })); assert_eq!(next_of(&fresh), ("action".to_string(), None), "a fresh scene starts on Action"); let kinds: Vec<&str> = fresh["pick"]["switches"].as_array().unwrap().iter().map(|row| row["kind"].as_str().unwrap()).collect(); assert_eq!(kinds, ["action", "note"]); assert_eq!(fresh["pick"]["switches"][0]["suggested"], true, "a fresh scene's Action switch is on"); assert!(fresh["pick"]["sections"].as_array().unwrap().iter().all(|section| section["rows"].as_array().unwrap().iter().all(|row| row["kind"] == "character")), "the chooser lists characters only"); assert_eq!(pick_names(&fresh)[0], "Anu Rao", "with no one speaking here, the screenplay's most lines first"); assert_eq!(fresh["pick"]["new"]["label"], "New character…"); let entered = &ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "enters", "character": maya }))["scene"]; assert_eq!(next_of(entered), ("character".to_string(), Some(maya.clone())), "who just entered speaks"); let said = &ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "dialogue", "character": ravi, "text": "You're going to wear a hole in that." }))["scene"]; assert_eq!(next_of(said), ("character".to_string(), Some(maya.clone())), "after Ravi, the other one here"); assert_eq!(pick_names(said), ["Maya", "Ravi", "Anu Rao"]); let answered = ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "dialogue", "character": maya, "text": "I'm almost done." })); assert_eq!(next_of(&answered["scene"]), ("character".to_string(), Some(ravi.clone())), "in an exchange, the other one"); assert_eq!(pick_names(&answered["scene"]), ["Ravi", "Maya", "Anu Rao"]); assert_eq!(answered["scene"]["pick"]["sections"][0]["rows"][0]["suggested"], true); let changing = ask(&handle, "story.scene", json!({ "id": scene, "editing": answered["line"] })); assert_eq!(next_of(&changing), ("character".to_string(), Some(maya.clone())), "a line changed keeps its speaker"); } /// RULE (owner, 19 Nov 2026): Story opens in the plain pair, light and dark; /// the paper look is one more a person may pick for light, kept on this /// phone; a dark pick must be a dark theme. #[test] fn the_themes_are_kit_data_and_a_pick_is_kept() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let themes = ask(&handle, "story.themes", json!({})); assert_eq!((themes["light"].as_str(), themes["dark"].as_str()), (Some("plain-light"), Some("plain-dark"))); let paper = themes["themes"].as_array().unwrap().iter().find(|theme| theme["id"] == "paper").expect("the paper look").clone(); assert_eq!((paper["look"].as_str(), paper["name"].as_str()), (Some("paper"), Some("Paper and typewriter"))); assert_eq!(paper["faces"]["voice"], "Caveat-Regular.ttf"); let picked = ask(&handle, "story.theme.set", json!({ "appearance": "light", "id": "paper" })); assert_eq!(picked["light"], "paper"); assert_eq!(ask(&handle, "story.themes", json!({}))["light"], "paper", "kept"); assert!(dispatch(&handle, "story.theme.set", &json!({ "appearance": "dark", "id": "paper" })).is_err()); } #[test] fn the_ink_home_has_the_cast_and_a_telling_line() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); let story = start(&handle); let jetty = id(&ask(&handle, "story.scene.new", json!({ "story": story }))["scene"]); ask(&handle, "story.scene.write", json!({ "id": jetty, "heading": "EXT. JETTY - DAWN", "body": "Ravi finds a stranger asleep in his boat." })); let stall = id(&ask(&handle, "story.scene.new", json!({ "story": story }))["scene"]); ask(&handle, "story.scene.write", json!({ "id": stall, "heading": "INT. TEA STALL - MORNING", "body": "MEERA\nYou took money from him?\n\nRAVI\nHe paid.\n\nMEERA\nNobody pays that much." })); let third = id(&ask(&handle, "story.scene.new", json!({ "story": story }))["scene"]); ask(&handle, "story.scene.write", json!({ "id": third, "heading": "EXT. JETTY - NIGHT", "summary": "They cross.", "body": "RAVI\nGet in." })); let view = ask(&handle, "story.scenes", json!({ "story": story })); let cast: Vec<&str> = view["cast"].as_array().unwrap().iter().map(|one| one["name"].as_str().unwrap()).collect(); assert_eq!(cast[0..2], ["Meera", "Ravi"], "ordered by lines spoken, then name: {}", view["cast"]); assert_eq!(view["cast"][0]["initial"], "M"); assert!(view["cast"][0]["picture"].is_null()); let lines: Vec = view["rows"].as_array().unwrap().iter().map(|row| row["line"].clone()).collect(); assert_eq!(lines, [json!("Ravi finds a stranger asleep in his boat."), json!("MEERA: You took money from him?"), json!("They cross.")]); let scene = ask(&handle, "story.scene", json!({ "id": third })); assert_eq!(scene["picker"]["used"], json!(["Jetty", "Tea Stall"])); assert_eq!(scene["picker"]["setting"], "EXT."); assert_eq!(scene["picker"]["summary"], "They cross."); assert_eq!(scene["picker"]["settings"][0]["word"], "Inside"); } #[test] fn a_line_moves_one_place_and_keeps_its_id() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); let story = start(&handle); let scene = id(&ask(&handle, "story.scene.new", json!({ "story": story }))["scene"]); for n in 0..3 { ask(&handle, "story.line.add", json!({ "scene": scene, "kind": "action", "text": format!("line {n}") })); } let view = ask(&handle, "story.scene", json!({ "id": scene })); let last = view["lines"][2]["id"].as_str().unwrap().to_string(); let moved = ask(&handle, "story.line.move", json!({ "scene": scene, "id": last, "by": -1 })); assert_eq!(texts(&moved), ["line 0", "line 2", "line 1"]); assert_eq!(moved["lines"][1]["id"], json!(last)); let top = ask(&handle, "story.line.move", json!({ "scene": scene, "id": last, "by": -1 })); assert_eq!(texts(&top), ["line 2", "line 0", "line 1"]); let still = ask(&handle, "story.line.move", json!({ "scene": scene, "id": last, "by": -1 })); assert_eq!(texts(&still), ["line 2", "line 0", "line 1"]); } /// RULE (the Ink script home, DECISIONS gate 2, 20 Nov 2026): the home's /// pills are Read, Places once a place exists, then All views; the story /// carries About this script's facts (In one line, Written by) when written. #[test] fn the_home_has_its_doors_and_about_its_facts() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let story = start(&handle); let keys = |view: &Value| view["doors"].as_array().unwrap().iter().map(|door| door["key"].as_str().unwrap().to_string()).collect::>(); let bare = ask(&handle, "story.scenes", json!({ "story": story })); assert_eq!(keys(&bare), ["read", "views"], "no Places before a place"); assert_eq!(bare["doors"][0]["words"], "Read"); assert!(bare["story"]["logline"].is_null() && bare["story"]["written_by"].is_null()); ask(&handle, "story.scene.add", json!({ "story": story, "place": "Jetty", "time": "dawn" })); dispatch(&handle, "records.update", &json!({ "table": "story_story", "id": story, "patch": { "logline": "A bus that never arrives.", "written_by": "An invented writer" } })).expect("written"); let home = ask(&handle, "story.scenes", json!({ "story": story })); assert_eq!(keys(&home), ["read", "places", "views"]); assert_eq!((home["story"]["logline"].as_str(), home["story"]["written_by"].as_str()), (Some("A bus that never arrives."), Some("An invented writer"))); } /// RULE (DECISIONS.md, Account; the Android walk, 25 Nov 2026): the name asked at first sign-in is a script's /// "Written by" until the person writes one there, and the title page follows. #[test] fn written_by_defaults_to_the_name_given_at_sign_in() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let story = start(&handle); let by = |handle: &CottonHandle| ask(handle, "story.scenes", json!({ "story": story }))["story"]["written_by"].clone(); assert_eq!(by(&handle), Value::Null, "nobody before sign-in"); handle .client() .with_writer(|replica| { let ws = rusqlite::types::Value::Text(replica.workspace_id.to_string()); replica.driver.run("INSERT INTO cotton_people (id, workspace_id, full_name, created_at, updated_at) VALUES ('p-me', ?, 'Nadia Ferreira', '2026-11-25T09:00:00.000Z', '2026-11-25T09:00:00.000Z')", &[ws.clone()]).map_err(|e| e.to_string())?; replica.driver.run("UPDATE cotton_workspaces SET owner_person_id = 'p-me' WHERE id = ?", &[ws]).map(|_| ()).map_err(|e| e.to_string()) }) .expect("signed in as an invented person"); assert_eq!(by(&handle), json!("Nadia Ferreira")); assert_eq!(ask(&handle, "story.script", json!({ "story": story }))["title_page"]["author"], "Nadia Ferreira"); dispatch(&handle, "records.update", &json!({ "table": "story_story", "id": story, "patch": { "written_by": "An invented writer" } })).expect("written"); assert_eq!(by(&handle), json!("An invented writer"), "what the person writes stands"); } /// RULE (the Ink writing bar, ink.html "Ravi ▾"): the next-speaker menu names /// a character as a name, not in the capitals a brought-in script wrote. #[test] fn the_speaker_menu_says_names_as_names() { let dir = tempfile::tempdir().unwrap(); let handle = phone(&dir); let (_, scene) = old_story(&handle); let view = ask(&handle, "story.scene", json!({ "id": scene })); let names: Vec<&str> = view["pick"]["sections"].as_array().unwrap().iter().flat_map(|section| section["rows"].as_array().unwrap()).filter(|row| row["kind"] == "character").map(|row| row["name"].as_str().unwrap()).collect(); assert!(names.contains(&"Maya") && names.contains(&"Ravi"), "{names:?}"); } #[test] fn all_of_a_characters_lines_in_order() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); let story = start(&handle); let one = id(&ask(&handle, "story.scene.new", json!({ "story": story }))["scene"]); ask(&handle, "story.scene.write", json!({ "id": one, "heading": "EXT. DEPOT - NIGHT", "body": "ANU\nLate again.\n\nKIRAN\nAlways.\n\nANU\n(quietly)\nGo home." })); let two = id(&ask(&handle, "story.scene.new", json!({ "story": story }))["scene"]); ask(&handle, "story.scene.write", json!({ "id": two, "heading": "INT. BUS - NIGHT", "body": "ANU\nSit." })); let anu = ask(&handle, "story.scenes", json!({ "story": story }))["cast"].as_array().unwrap().iter().find(|one| one["name"] == "Anu").unwrap()["id"].clone(); let lines = ask(&handle, "story.person.lines", json!({ "id": anu })); let said: Vec<&str> = lines["lines"].as_array().unwrap().iter().map(|one| one["text"].as_str().unwrap()).collect(); assert_eq!(said, ["Late again.", "Go home.", "Sit."]); assert_eq!(lines["lines"][1]["aside"], "quietly"); assert_eq!(lines["lines"][2]["number"], 2); assert_eq!(lines["name"], "Anu"); } #[test] fn a_characters_first_picture_is_their_face() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); let story = start(&handle); let scene = id(&ask(&handle, "story.scene.new", json!({ "story": story }))["scene"]); ask(&handle, "story.scene.write", json!({ "id": scene, "heading": "EXT. DEPOT - NIGHT", "body": "ANU\nLate again." })); let anu = ask(&handle, "story.scenes", json!({ "story": story }))["cast"][0]["id"].as_str().unwrap().to_string(); ask(&handle, "story.picture.add", json!({ "character": anu, "picture": "photo:aa" })); ask(&handle, "story.picture.add", json!({ "character": anu, "picture": "photo:bb" })); assert_eq!(ask(&handle, "story.scenes", json!({ "story": story }))["cast"][0]["picture"], "photo:aa"); assert_eq!(ask(&handle, "story.person", json!({ "id": anu }))["pictures"].as_array().unwrap().len(), 2); } fn scene_data(handle: &CottonHandle, story: &str, scene: &str) -> Value { ask(handle, "records.list", json!({ "table": "story_scene", "where": { "story": story }, "limit": 200 }))["records"] .as_array() .unwrap() .iter() .find(|row| row["id"] == scene) .map(|row| row["data"].clone()) .expect("the scene") } /// RULE (Story's launch, 23 Nov 2026): the lists read a scene's minutes as its kept figures, never reading every body /// again, so the kept figures stay true: a character added later, whose name now reads a line as speech, refigures /// the story's scenes, and the list says what a fresh reading says. #[test] fn a_character_added_later_refigures_the_kept_figures() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); let story = start(&handle); let scene = id(&ask(&handle, "story.scene.new", json!({ "story": story }))["scene"]); // "Asha" in mixed case is a cue only once Asha is a character. let body = "Rain on the shelter roof.\n\nAsha\nIs it very late? The last bus went an hour ago and the road is empty.\n\nA dog barks."; ask(&handle, "story.scene.write", json!({ "id": scene, "heading": "EXT. BUS STOP - NIGHT", "body": body })); assert_eq!(scene_data(&handle, &story, &scene)["speakers"], json!(""), "no cue while Asha is unknown"); ask(&handle, "story.character.add", json!({ "story": story, "name": "Asha" })); let kept = scene_data(&handle, &story, &scene); assert_eq!(kept["speakers"], json!("Asha"), "the kept figures read Asha's line as speech"); let fresh = cotton_core::cotton_rules::story::figures::of_scene("EXT. BUS STOP - NIGHT", body, &["Asha".to_string()]); assert_eq!(kept["minutes"].as_f64(), Some(fresh.minutes)); let listed = ask(&handle, "story.list", json!({})); assert_eq!(listed["stories"][0]["minutes"].as_f64(), Some(fresh.minutes), "the list says the kept minutes"); let scenes = ask(&handle, "story.scenes", json!({ "story": story })); let row = scenes["rows"].as_array().unwrap().iter().find(|row| row["kind"] == "scene").unwrap(); assert_eq!(row["minutes"].as_f64(), Some(fresh.minutes), "the scene list says the kept minutes"); }