"""Arbre d'un livre de campagne → arcs, aventures, scènes, salles, sans IA.""" from __future__ import annotations from app.application.campaign_structure import ( all_scenes, campaign_from_outline, counts, is_skipped, lead, played_scenes, ) from app.application.pdf_structure import BOOKMARKS, BookOutline, OutlineNode LONG = "Texte d'introduction assez long pour faire une scène à lui seul. " * 4 def node(title: str, text: str = "", *children: OutlineNode, level: int = 1) -> OutlineNode: return OutlineNode(title, level, 1, text, tuple(children)) def outline(*nodes: OutlineNode) -> BookOutline: return BookOutline(BOOKMARKS, "", tuple(nodes)) def _names(arcs: list[dict]) -> list[tuple[str, list[str]]]: return [(chapter["name"], [scene["name"] for scene in chapter["scenes"]]) for arc in arcs for chapter in arc["chapters"]] def test_chapters_become_adventures_and_their_sections_scenes(): arcs = campaign_from_outline(outline( node("The Adventure", "", node("A Bloodied History", LONG), node("The Old Watchtower", LONG)), node("Leaving the Vale", "", node("Closing Time!", LONG)), )) assert [arc["name"] for arc in arcs] == ["Aventure principale"] assert _names(arcs) == [("The Adventure", ["A Bloodied History", "The Old Watchtower"]), ("Leaving the Vale", ["Closing Time!"])] scene = arcs[0]["chapters"][0]["scenes"][0] # Le texte de la scène est celui du livre ; rien d'autre n'est écrit ici. assert scene["gm_notes"] == LONG.strip() assert scene["player_narration"] == "" and scene["rooms"] == [] def test_the_fallback_synopsis_is_the_beginning_of_the_text(): text = "### Un intertitre\n\nLe pont enjambe la rivière. Au-delà, la tour de guet tient encore debout. " + "Suite. " * 60 arcs = campaign_from_outline(outline(node("Aventure", "", node("La tour", text), node("Autre", LONG)))) assert arcs[0]["chapters"][0]["scenes"][0]["description"].startswith("Le pont enjambe la rivière.") assert len(arcs[0]["chapters"][0]["scenes"][0]["description"]) <= 241 assert lead("") == "" and lead("| a | b |\n\n- puce") == "" def test_the_introduction_of_a_chapter_is_a_scene_of_its_own(): arcs = campaign_from_outline(outline(node("Chapitre 1", LONG, node("La foire", LONG))), overview_title="Présentation") assert _names(arcs) == [("Chapitre 1", ["Présentation", "La foire"])] def test_a_short_introduction_opens_the_first_scene(): arcs = campaign_from_outline(outline(node("Chapitre 1", "Quelques mots.", node("La foire", LONG), node("La nuit", LONG)))) scenes = arcs[0]["chapters"][0]["scenes"] assert [s["name"] for s in scenes] == ["La foire", "La nuit"] assert scenes[0]["gm_notes"].startswith("Quelques mots.\n\n" + LONG.strip()[:20]) def test_a_chapter_without_sections_is_one_scene(): arcs = campaign_from_outline(outline(node("Épilogue", LONG), node("Prologue", LONG))) assert _names(arcs) == [("Épilogue", ["Épilogue"]), ("Prologue", ["Prologue"])] def test_numbered_places_are_the_rooms_of_one_scene(): crypt = node("La crypte", "La crypte s'ouvre sous l'autel. " * 8, node("1. Entrée", "Une grille rouillée."), node("2. Salle des gardes", "Deux squelettes."), node("A3. Tombeau", "Le sarcophage du roi.", node("Trésor", "Une couronne.")), node("Suite de l'aventure", "Les héros ressortent.")) arcs = campaign_from_outline(outline(node("Chapitre 1", "", crypt, node("Le village", LONG)))) scene = arcs[0]["chapters"][0]["scenes"][0] assert scene["name"] == "La crypte" assert [room["name"] for room in scene["rooms"]] == ["1. Entrée", "2. Salle des gardes", "A3. Tombeau"] assert scene["rooms"][2]["description"] == "Le sarcophage du roi.\n\n### Trésor\n\nUne couronne." assert scene["rooms"][0]["enemies"] == "" and scene["rooms"][0]["loot"] == "" # Ce qui n'est pas un lieu numéroté reste dans le texte de la scène. assert "### Suite de l'aventure\n\nLes héros ressortent." in scene["gm_notes"] assert counts(arcs) == (1, 1, 2, 3) def test_a_dungeon_chapter_is_one_scene_with_its_rooms(): arcs = campaign_from_outline(outline( node("Cragmaw Hideout", "Le repaire des gobelins.", node("Area 1: Cave Mouth", "Un ruisseau. " * 12), node("Area 2: Goblin Blind", "Deux gobelins. " * 12)), node("Phandalin", LONG))) scenes = arcs[0]["chapters"][0]["scenes"] assert [s["name"] for s in scenes] == ["Cragmaw Hideout"] assert len(scenes[0]["rooms"]) == 2 and scenes[0]["gm_notes"] == "Le repaire des gobelins." def test_numbered_places_may_carry_a_hyphen(): temple = node("Elder Temple Map", LONG, node("T-10. Mole-man Lair", "Des hommes-taupes."), node("T-11. Main Temple Entrance", "Une double porte."), node("Concluding the Adventure", "Fin.")) arcs = campaign_from_outline(outline(node("The Elder Temple", "", temple, node("The Road", LONG)))) scene = arcs[0]["chapters"][0]["scenes"][0] assert [room["name"] for room in scene["rooms"]] == ["T-10. Mole-man Lair", "T-11. Main Temple Entrance"] assert "### Concluding the Adventure" in scene["gm_notes"] def test_a_dungeon_filed_under_a_region_keeps_its_rooms(): """Signets mal emboîtés : le donjon rangé parmi les lieux de la région qui y mène.""" temple = node("Elder Temple Map", LONG, node("T-10. Mole-man Lair", "Des hommes-taupes."), node("T-11. Main Temple Entrance", "Une double porte.")) region = node("The Elder Temple", "", node("W-3. Harkin's Ford", "Un gué."), node("W-4. Young Black Dragon", "Un dragon."), node("W-5. Bandit Camp", "Des bandits."), node("The Road Back", "Le retour."), temple) arcs = campaign_from_outline(outline(region, node("Appendix", LONG))) wilderness, dungeon = arcs[0]["chapters"][0]["scenes"] assert (wilderness["name"], len(wilderness["rooms"])) == ("The Elder Temple", 3) assert "### The Road Back" in wilderness["gm_notes"] and "Mole-man" not in wilderness["gm_notes"] assert dungeon["name"] == "Elder Temple Map" assert [room["name"] for room in dungeon["rooms"]] == ["T-10. Mole-man Lair", "T-11. Main Temple Entrance"] def test_a_section_that_is_too_long_is_given_by_its_subsections(): big = "Un long passage. " * 600 arcs = campaign_from_outline(outline(node("La vallée", "", node( "Le col", LONG, node("Le village (C)", big), node("La forêt (D)", big)), node("Fin", LONG)))) assert _names(arcs)[0] == ("La vallée", ["Le col", "Le col › Le village (C)", "Le col › La forêt (D)", "Fin"]) def test_a_short_section_keeps_its_subsections_as_subheadings(): arcs = campaign_from_outline(outline(node("La vallée", "", node( "Le col", "Le col est gardé.", node("Le pont", "Un pont de corde.")), node("Fin", LONG)))) assert arcs[0]["chapters"][0]["scenes"][0]["gm_notes"] == "Le col est gardé.\n\n### Le pont\n\nUn pont de corde." def test_parts_of_the_book_become_arcs(): arcs = campaign_from_outline(outline( node("Introduction", LONG), node("Part 1: The Arrival", "", node("The Road", "", node("Ambush", LONG))), node("Part 2: To Hunt the Devil", LONG, node("The Cliffs", "", node("The Lair", LONG))), ), overview_title="Overview") assert [arc["name"] for arc in arcs] == ["Introduction", "Part 1: The Arrival", "Part 2: To Hunt the Devil"] assert [c["name"] for c in arcs[1]["chapters"]] == ["The Road"] # Le texte d'introduction d'une partie fait une aventure de présentation. assert [c["name"] for c in arcs[2]["chapters"]] == ["Overview", "The Cliffs"] assert all(arc["type"] == "LINEAR" for arc in arcs) def test_one_part_alone_does_not_make_arcs(): arcs = campaign_from_outline(outline(node("Part 1", "", node("Scène", LONG)), node("Épilogue", LONG))) assert [arc["name"] for arc in arcs] == ["Aventure principale"] def test_cover_credits_contents_and_legal_pages_are_left_out(): arcs = campaign_from_outline(outline( node("Cover", LONG), node("Crédits", LONG), node("Table des matières", LONG), node("Hall of the Rainbow Mage Cover PDF 5e", "ISBN 978."), node("L'aventure", "", node("Scène", LONG)), node("Legal Appendix", LONG), node("Carte du donjon", "Légende."), node("Index", LONG))) assert _names(arcs) == [("L'aventure", ["Scène"])] assert is_skipped("Open Game License v1.0a") and is_skipped("livre.pdf") and not is_skipped("Introduction") assert is_skipped("Legal Aappendix") and not is_skipped("Legends of the Vale") def test_a_node_that_holds_the_whole_book_is_only_a_wrapper(): """Signets mal rangés : tout le livre sous un nom de fichier, ou sous « Crédits ».""" book = node("Lost Crypt 5e PDF Download.pdf", "", node("Introduction", LONG), node("Crypt Entrance", LONG), node("The Lower Halls", LONG)) arcs = campaign_from_outline(outline(node("Lost Crypt 5e PDF Cover", "Couverture."), book, node("Legal Appendix", LONG))) assert [name for name, _ in _names(arcs)] == ["Introduction", "Crypt Entrance", "The Lower Halls"] under_credits = node("Crédits", "Auteurs : ...", node("Les secrets de Calice", LONG), node("La vallée des ours", LONG), node("Les marais", LONG)) arcs = campaign_from_outline(outline(under_credits)) assert [name for name, _ in _names(arcs)] == ["Les secrets de Calice", "La vallée des ours", "Les marais"] def test_appendices_go_to_an_arc_of_their_own_as_a_few_reading_scenes(): creature = "Petite bête, non alignée. Classe d'armure 12. " * 6 # ≈ 280 caractères bestiary = node("Appendix II: Creatures", "", node("A", "", node("Animated Chain", creature), node("Demon Doll", creature)), node("B", "", node("Bat, Vampire", creature))) items = node("Appendix VI: New Magic Items", "Quelques objets.", node("Endless Rope", creature)) arcs = campaign_from_outline(outline(node("Chapter 1", "", node("The Village", LONG)), bestiary, items), appendix_arc="Annexes") assert [(arc["name"], arc["type"]) for arc in arcs] == [("Aventure principale", "LINEAR"), ("Annexes", "HUB")] assert arcs[1]["reference"] is True and "reference" not in arcs[0] # Le bestiaire tient en une scène de lecture, pas une scène par créature. creatures, magic = arcs[1]["chapters"] assert [scene["name"] for scene in creatures["scenes"]] == ["A – B"] scene = creatures["scenes"][0] assert scene["description"] == "A, B" assert "### A\n\n#### Animated Chain\n\n" + creature.strip() in scene["gm_notes"] assert "#### Bat, Vampire" in scene["gm_notes"] # Une annexe d'une seule entrée garde son nom ; son court texte d'introduction l'ouvre. assert [scene["name"] for scene in magic["scenes"]] == ["Endless Rope"] assert magic["scenes"][0]["gm_notes"].startswith("Quelques objets.\n\n" + creature[:20]) assert [scene["name"] for scene in played_scenes(arcs)] == ["The Village"] assert len(all_scenes(arcs)) == 3 def test_a_long_appendix_is_cut_in_several_reading_scenes_in_the_order_of_the_book(): entry = "Un profil de créature. " * 300 # ≈ 6 900 caractères bestiary = node("Bestiaire", LONG, *(node(f"Créature {i}", entry) for i in range(1, 6))) arcs = campaign_from_outline(outline(node("L'aventure", "", node("Scène", LONG)), bestiary)) scenes = arcs[1]["chapters"][0]["scenes"] assert [scene["name"] for scene in scenes] == [ "Présentation", "Créature 1 – Créature 2", "Créature 3 – Créature 4", "Créature 5"] assert scenes[1]["description"] == "Créature 1, Créature 2" def test_reference_titles_count_only_at_the_end_of_the_book(): arcs = campaign_from_outline(outline( node("Monsters", "", node("The Lair", LONG)), node("The Road", LONG), node("NEW CREATURES", LONG), node("New Magic Items", LONG))) assert [arc["name"] for arc in arcs] == ["Aventure principale", "Annexes"] assert [chapter["name"] for chapter in arcs[0]["chapters"]] == ["Monsters", "The Road"] assert [chapter["name"] for chapter in arcs[1]["chapters"]] == ["NEW CREATURES", "New Magic Items"] def test_a_book_made_only_of_appendices_is_left_as_it_is(): arcs = campaign_from_outline(outline(node("Appendix A", LONG), node("Bestiary", LONG))) assert [arc["name"] for arc in arcs] == ["Aventure principale"] assert played_scenes(arcs) == all_scenes(arcs) def test_two_scenes_with_the_same_name_are_told_apart(): arcs = campaign_from_outline(outline(node("Chapitre", "", node("Rencontre", LONG), node("Rencontre", LONG)))) assert _names(arcs) == [("Chapitre", ["Rencontre", "Rencontre (2)"])] def test_a_book_without_matter_gives_nothing(): assert campaign_from_outline(outline(node("Cover", LONG), node("Index", LONG))) == [] assert campaign_from_outline(BookOutline("none")) == [] assert all_scenes([]) == []