package com.cottondb.story import com.cottondb.story.ink.Home import com.cottondb.story.ink.InkState import com.cottondb.story.ink.Page import com.cottondb.story.ink.Scene import com.cottondb.story.ink.Scripts import com.cottondb.story.ink.Sheet import com.cottondb.story.script.Script import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test /** Story's reading of the core's answers for the Ink screens (`story.scenes`, `story.list`, `story.scene`, the pages), and where back goes. */ class StoryLogicTest { private fun json(s: String): JsonObject = Json.parseToJsonElement(s).jsonObject @Test fun a_second_bring_in_waits_for_the_first() { val state = InkState() var read = 0 var during: String? = null val first = state.bringingIn("Importing your script…") { during = state.bringing assertFalse("a second pick is not read meanwhile", state.bringingIn("Importing your script…") { read++ }) read++ } assertTrue(first) assertEquals("Importing your script…", during) assertEquals(1, read) assertNull("the home is itself again once the core answers", state.bringing) runCatching { state.bringingIn("Importing your script…") { error("refused") } } assertNull("a refusal lets go too", state.bringing) } @Test fun the_script_is_read_as_the_core_lays_it() { val script = Script.of(json("""{"paper":{"name":"a4","width_pt":595,"height_pt":842,"margins_pt":{"top":72,"bottom":72,"left":108,"right":72}}, "title_page":{"title":"The Night Bus","credit":"Written by","author":"A. Writer","lines":["Draft 1"]}, "pages":[{"number":1,"lines":[{"kind":"heading","text":"INT. BUS - NIGHT","indent":0,"scene":{"id":"s1","number":"1"}},{"kind":"character","text":"ANU","indent":22}]},{"number":2,"lines":[{"kind":"dialogue","text":"Go.","indent":10}]}], "scenes":[{"id":"s1","number":"1","page":1}]}""")) assertEquals("a4", script.paper.name) assertEquals("The Night Bus", script.title.title) assertEquals("s1", script.pages[1].lines[0].scene) assertEquals(22, script.pages[0].lines[1].indent) } @Test fun the_home_draws_the_cast_and_the_scenes_alone() { val home = Home.of(json("""{"story":{"id":"st","title":"The Night Bus","summary":"3 scenes · 4 min"}, "cast":[{"id":"a","name":"Anu","initial":"A","picture":null,"lines":5},{"id":"b","name":"Kiran","initial":"K","picture":"photos/ab.jpg","lines":2},{"id":"c","name":"","initial":"","lines":0}], "rows":[{"kind":"scene","id":"x","number":1,"title":"Depot · night","minutes_word":"2 min","line":"Anu waits under the one light."}, {"kind":"interval","at":1.5,"words":"INTERVAL"}, {"kind":"sequence","id":"q","title":"The ride","folded":false}, {"kind":"scene","id":"y","number":2,"title":"Bus · night","minutes_word":null,"line":null}]}""")) assertEquals("3 scenes · 4 min", home.count) assertEquals(listOf("Anu", "Kiran"), home.cast.map { it.name }) assertNull(home.cast[0].picture); assertEquals("photos/ab.jpg", home.cast[1].picture) assertEquals(listOf("x", "y"), home.scenes.map { it.id }) assertEquals("Anu waits under the one light.", home.scenes[0].line) assertNull(home.scenes[1].length) } @Test fun your_scripts_mark_the_one_worked_on() { val scripts = Scripts.of(json("""{"stories":[{"id":"a","title":"The Night Bus","summary":"3 scenes · 4 min","current":true},{"id":"b","title":"","summary":"0 scenes · 0 min","current":false}]}""")) assertTrue(scripts.scripts[0].current); assertFalse(scripts.scripts[1].current) } @Test fun a_scene_reads_its_speech_its_guess_and_where_and_when() { val scene = Scene.of(json("""{"id":"s","story":"st","title":"Depot · night","slugline":"EXT. DEPOT - NIGHT", "lines":[{"id":"l1","kind":"action","text":"Rain.","label":"Action: Rain."},{"id":"l2","kind":"dialogue","text":"Late again.","who":"ANU","character":"a","initial":"A","aside":"not looking up","label":"Anu says: Late again."}], "chips":[{"id":"a","name":"Anu","initial":"A","picture":null,"lines":1},{"id":"b","name":"Kiran","initial":"K","lines":0}], "pick":{"next":{"kind":"character","character":"b"}}, "composer":{"hint":"Write a line…"}, "picker":{"title":"Where and when","hint":"Where are we?","place":"Depot","used_label":"Places you have used","used":["Depot","Bus"], "setting":"EXT.","settings":[{"setting":"INT.","word":"Inside"},{"setting":"EXT.","word":"Outside"}], "band":"night","times":[{"band":"day","word":"Day"},{"band":"night","word":"Night"}],"summary":null,"summary_hint":"What happens here?","done":"Done"}}""")) assertEquals("b", scene.next) assertTrue(scene.lines[1].speech); assertFalse(scene.lines[0].speech) assertEquals("not looking up", scene.lines[1].aside) assertEquals(listOf("Depot", "Bus"), scene.where.used) assertEquals("EXT.", scene.where.setting) assertTrue(scene.placed) assertEquals("Kiran", scene.person("b")?.name) } @Test fun back_closes_the_sheet_then_the_screen_and_never_the_home() { val state = InkState() state.home("st") state.open(Page.Scene("s")) state.sheet = Sheet.More assertTrue(state.back()); assertNull(state.sheet); assertEquals(Page.Scene("s"), state.page) assertTrue(state.back()); assertNull(state.page) assertFalse(state.back()) } @Test fun listen_gives_each_speaker_a_voice_and_the_rest_to_the_narrator() { val script = Script.of(json("""{"pages":[ {"number":1,"lines":[{"kind":"heading","text":"EXT. DEPOT - NIGHT","scene":{"id":"s1","number":"1"}},{"kind":"action","text":"Rain on the"},{"kind":"action","text":"tin roof."}, {"kind":"character","text":"ANU"},{"kind":"parenthetical","text":"(not looking up)"},{"kind":"dialogue","text":"Late again."}]}, {"number":2,"lines":[{"kind":"character","text":"KIRAN"},{"kind":"dialogue","text":"Always."},{"kind":"character","text":"ANU (CONT'D)"},{"kind":"dialogue","text":"Go."}]}]}""")) val said = com.cottondb.story.listen.spokenOf(script) assertEquals(listOf("EXT. DEPOT - NIGHT", "Rain on the tin roof.", "Late again.", "Always.", "Go."), said.map { it.text }) assertEquals(listOf(0, 0, 1, 2, 1), said.map { it.voice }) val later = com.cottondb.story.listen.spokenOf(script, from = 2) assertEquals(listOf(2, 1), later.map { it.voice }) } @Test fun what_changed_since_a_draft_is_read_by_identity_and_counted_by_scenes() { val changes = com.cottondb.story.ink.Changes.of(json("""{"scenes":[ {"scene":"a","number_then":1,"number_now":1,"change":"same","heading_then":"INT. BUS - NIGHT","heading_now":"INT. BUS - NIGHT","lines":{"added":0,"removed":0,"changed":0}}, {"scene":"b","number_then":2,"number_now":3,"change":"moved","heading_now":"EXT. DEPOT - DAWN","lines":{"added":0,"removed":0,"changed":0}}, {"scene":"c","number_then":3,"change":"removed","heading_then":"INT. TEA STALL - DAY","lines":{"added":0,"removed":4,"changed":0}}, {"scene":"d","number_now":2,"change":"added","heading_now":"ఇల్లు - రాత్రి","lines":{"added":3,"removed":0,"changed":0}}, {"scene":"e","number_then":4,"number_now":4,"change":"changed","lines":{"added":1,"removed":0,"changed":2}}], "since":{"scenes_added":1,"scenes_removed":1,"scenes_changed":1,"lines_added":4,"lines_removed":4,"lines_changed":2}}""")) assertEquals(listOf("b", "c", "d", "e"), changes.shown.map { it.scene }) assertEquals(3, changes.since.scenes) assertNull(changes.scenes[2].numberNow); assertEquals(3, changes.scenes[2].numberThen) assertEquals(2, changes.scenes[4].changed) assertEquals("ఇల్లు - రాత్రి", changes.scenes[3].headingNow) } @Test fun a_script_brought_in_says_its_scenes_and_the_check_for_a_printed_one() { val pdf = com.cottondb.story.ink.BroughtIn.of(json("""{"story":"st","scenes":12,"characters":4,"places":3,"from":"pdf","check":"Read it through."}""")) assertEquals("st", pdf.story); assertEquals(12, pdf.scenes); assertEquals("Read it through.", pdf.check) val fountain = com.cottondb.story.ink.BroughtIn.of(json("""{"story":"st2","scenes":1,"from":"fountain"}""")) assertNull(fountain.check) } @Test fun pictures_keep_their_day_and_the_first_without_one_is_the_face() { val pictures = com.cottondb.story.ink.Picture.all(json("""{"pictures":[ {"id":"p1","picture":"photos/a.jpg","day":null},{"id":"p2","picture":"photos/b.jpg","day":2},{"id":"p3","picture":"","day":null},{"id":"p4","picture":"photos/c.jpg"}]}""")) assertEquals(listOf("p1", "p2", "p4"), pictures.map { it.id }) assertEquals(listOf("p1", "p4"), pictures.filter { it.day == null }.map { it.id }) assertEquals(2, pictures[1].day) } @Test fun an_initial_is_the_first_whole_letter_in_any_script() { assertEquals("R", com.cottondb.story.ink.firstLetter("Ravi")) // A Telugu syllable with its vowel sign, a Hindi one, a letter with a combining accent: never half of one. assertEquals("రా", com.cottondb.story.ink.firstLetter("రాము")) assertEquals("मी", com.cottondb.story.ink.firstLetter("मीरा")) assertEquals("e\u0301", com.cottondb.story.ink.firstLetter("e\u0301lise")) assertEquals("", com.cottondb.story.ink.firstLetter(" ")) } @Test fun a_shared_file_keeps_its_title_in_any_script() { assertEquals("రాత్రి బస్సు", com.cottondb.story.share.fileName("రాత్రి బస్సు")) assertEquals("A_B", com.cottondb.story.share.fileName("A/B")) assertEquals("script", com.cottondb.story.share.fileName(" ")) } @Test fun a_character_who_has_not_spoken_says_so_and_never_zero_lines() { assertEquals("character.speaks.zero", com.cottondb.story.ink.speaksKey(0)) assertEquals("character.speaks.one", com.cottondb.story.ink.speaksKey(1)) assertEquals("character.speaks.other", com.cottondb.story.ink.speaksKey(8)) } @Test fun the_send_and_views_sheets_close_before_the_screen_under_them() { val state = InkState() state.home("st") state.open(Page.Read(null)) state.sheet = Sheet.Send("The Night Bus") assertTrue(state.back()); assertNull(state.sheet); assertEquals(Page.Read(null), state.page) state.sheet = Sheet.Views assertTrue(state.back()); assertNull(state.sheet) state.open(Page.Changes("d1", "Second draft")) assertTrue(state.back()); assertEquals(Page.Read(null), state.page) } @Test fun scripts_still_coming_are_never_nothing_here_yet() { val coming = com.cottondb.story.ink.Current.of(json("""{"story":null,"stories":0,"arriving":true,"arriving_line":"Loading your data…"}""")) assertEquals("Loading your data…", coming.arriving) val state = InkState() state.come(coming) assertTrue(state.known); assertNull(state.story); assertEquals("Loading your data…", state.arriving) // Asked again: still coming, then nothing came (the empty screen), then a script came. state.come(com.cottondb.story.ink.Current.of(json("""{"story":null,"arriving":false}"""))) assertNull(state.arriving) state.come(com.cottondb.story.ink.Current.of(json("""{"story":{"id":"st"},"arriving":false}"""))) assertEquals("st", state.story) } @Test fun the_bar_reads_its_action_and_note_switches_and_the_guess() { val scene = Scene.of(json("""{"id":"s","story":"st","title":"Depot · night","slugline":"EXT. DEPOT - NIGHT","lines":[],"chips":[], "pick":{"next":{"kind":"note","character":null},"sections":[{"key":"characters","rows":[]}], "switches":[{"kind":"action","name":"Action","suggested":false},{"kind":"note","name":"Note","suggested":true}],"label":"Next line"}, "composer":{"hint":"Write a line…","send":"Send"},"picker":{}}""")) assertEquals(listOf("action", "note"), scene.switches.map { it.kind }) assertEquals("note", scene.nextKind) assertNull(scene.next) assertEquals("Send", scene.sendWord) } /** Sixty invented characters: the scene's own speakers first, the rest by lines spoken, found by typing. */ @Test fun the_speaker_chooser_puts_the_scene_first_and_finds_among_sixty() { val cast = (1..60).map { i -> com.cottondb.story.ink.Person("p$i", "Person %02d".format(i), "P", null, lines = (i * 7) % 61) } val scene = listOf( Scene.Line("l1", Scene.DIALOGUE, "Hello.", "PERSON 03", "p3", "P", null, ""), Scene.Line("l2", Scene.ACTION, "They sit.", null, "p60", null, null, ""), Scene.Line("l3", Scene.DIALOGUE, "Sit down.", "PERSON 40", "p40", "P", null, ""), Scene.Line("l4", Scene.DIALOGUE, "Again.", "PERSON 03", "p3", "P", null, ""), ) val shown = com.cottondb.story.ink.Chooser.order(cast, scene) assertEquals(60, shown.size) // In this scene: Person 40 (280 % 61 = 36 lines) before Person 03 (21); an action's character is not a speaker. assertEquals(listOf("p40", "p3"), shown.take(2).map { it.id }) val rest = shown.drop(2) assertEquals(rest.sortedByDescending { it.lines }.map { it.id }, rest.map { it.id }) assertTrue(com.cottondb.story.ink.Chooser.finds(cast)) assertFalse(com.cottondb.story.ink.Chooser.finds(cast.take(8))) assertEquals(listOf("p50", "p51", "p52", "p53", "p54", "p55", "p56", "p57", "p58", "p59").sorted(), com.cottondb.story.ink.Chooser.order(cast, scene, " person 5").map { it.id }.sorted()) assertEquals(listOf("p40"), com.cottondb.story.ink.Chooser.order(cast, scene, "SON 40").map { it.id }) } }