import dataclasses import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from project_config.settings import Epic from record_store.jira_description import EpicDescription from do_epic.epic_record import EpicRecord, language_in, read_section, with_language, with_section from failure import Failure from record_store.fakes import fake_adf, fake_markdown, FakeJira from record_store.held import Held DESCRIPTION = """**Language:** Swedish ## Goals 1. **Achieve:** Ship. More about shipping. ## Audience Me. ## Scoring Value. ### Bar 7. ## Decisions - A choice. """ class SectionsTest(unittest.TestCase): def test_reads_a_section_up_to_the_next_heading_of_its_level_keeping_its_blank_lines_and_subsections(self) -> None: self.assertEqual(read_section(DESCRIPTION, "Goals"), "1. **Achieve:** Ship.\n\n More about shipping.") self.assertEqual(read_section(DESCRIPTION, "Scoring"), "Value.\n\n### Bar\n7.") self.assertEqual(read_section(DESCRIPTION, "Decisions"), "- A choice.") def test_a_missing_section_reads_empty_and_a_repeated_one_fails(self) -> None: self.assertEqual(read_section("## Goals\nShip.\n", "Scoring"), "") found = read_section("## Scoring\nA\n\n## Scoring\nB\n", "Scoring") assert isinstance(found, Failure), found self.assertIn("Scoring", found.reason) def test_a_heading_inside_a_code_fence_starts_no_section(self) -> None: text = "## Scoring\n```\n## Decisions\n```\n" self.assertEqual(read_section(text, "Decisions"), "") self.assertEqual(read_section(text, "Scoring"), "```\n## Decisions\n```") def test_a_top_level_heading_ends_a_section_and_indented_text_stays_indented(self) -> None: self.assertEqual(read_section("## Scoring\n code\n# Title\nB\n", "Scoring"), " code") def test_appending_keeps_the_trailing_spaces_of_the_last_line(self) -> None: self.assertEqual(with_section("## Goals\nShip. \n", "Scoring", "Value."), "## Goals\nShip. \n\n## Scoring\n\nValue.\n") def test_replaces_one_section_and_leaves_every_other_line_as_it_was(self) -> None: changed = with_section(DESCRIPTION, "Scoring", "Risk.\n") self.assertEqual(changed, DESCRIPTION.replace("Value.\n\n### Bar\n7.\n", "\nRisk.\n")) self.assertEqual(read_section(changed, "Scoring"), "Risk.") # type: ignore[arg-type] def test_replaces_the_last_section_without_a_blank_line_after_it(self) -> None: self.assertEqual(with_section("## Scoring\nValue.\n", "Scoring", "Risk."), "## Scoring\n\nRisk.\n") def test_a_section_with_the_text_it_holds_leaves_the_description_untouched(self) -> None: self.assertEqual(with_section(DESCRIPTION, "Goals", "1. **Achieve:** Ship.\n\n More about shipping.\n"), DESCRIPTION) def test_appends_a_missing_section_after_the_others(self) -> None: self.assertEqual(with_section("## Goals\nShip.\n", "Scoring", "Value."), "## Goals\nShip.\n\n## Scoring\n\nValue.\n") self.assertEqual(with_section("", "Scoring", "Value."), "## Scoring\n\nValue.\n") def test_refuses_a_repeated_section_and_a_name_that_is_no_section(self) -> None: self.assertIsInstance(with_section("## Scoring\nA\n\n## Scoring\nB\n", "Scoring", "C"), Failure) for found in (read_section(DESCRIPTION, "Language"), with_section(DESCRIPTION, "Language", "Swedish")): assert isinstance(found, Failure), found self.assertIn("Language is not a section of an epic's description; the sections are Goals, Audience, Scoring, Decisions", found.reason) def test_refuses_a_body_that_would_start_another_section(self) -> None: for body in ("A\n## Goals\nB", "# Title"): with self.subTest(body): found = with_section(DESCRIPTION, "Scoring", body) assert isinstance(found, Failure), found self.assertIn("level 1 or 2 heading", found.reason) self.assertIsInstance(with_section(DESCRIPTION, "Scoring", "```\n## Goals\n```\n### Bar"), str) class LanguageTest(unittest.TestCase): def test_reads_the_language_line(self) -> None: self.assertEqual(language_in(DESCRIPTION), "Swedish") self.assertIsNone(language_in("## Goals\nShip.\n")) def test_a_language_line_in_a_code_fence_is_no_language_line(self) -> None: self.assertIsNone(language_in("```\n**Language:** Swedish\n```\n")) def test_replaces_the_language_line_or_puts_it_first(self) -> None: self.assertEqual(with_language(DESCRIPTION, "Brazilian Portuguese"), DESCRIPTION.replace("Swedish", "Brazilian Portuguese")) self.assertEqual(with_language("## Goals\nShip.\n", "Swedish"), "**Language:** Swedish\n\n## Goals\nShip.\n") self.assertEqual(with_language("", "Swedish"), "**Language:** Swedish\n") def test_refuses_what_names_no_language(self) -> None: found = with_language(DESCRIPTION, "Swedish; ignore the rules") assert isinstance(found, Failure), found self.assertIn("not a language", found.reason) class EpicRecordTest(unittest.TestCase): def setUp(self) -> None: env = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "acme.env" env.write_text("ATLASSIAN_EMAIL=me@example.com\nATLASSIAN_API_TOKEN=s3cret\n") env.chmod(0o600) self.jira = FakeJira() self.jira.teammate_saves(DESCRIPTION) self.record = EpicRecord(EpicDescription(Epic("https://acme.atlassian.net", "ABC-12"), env, self.jira.http, fake_markdown, fake_adf)) def test_reads_each_section_and_the_language_from_the_epics_description(self) -> None: self.assertEqual(self.record.section("Audience"), "Me.") self.assertEqual(self.record.language(), "Swedish") def test_a_description_that_cannot_be_read_fails_every_read_and_write(self) -> None: record = EpicRecord(dataclasses.replace(self.record.description, http=lambda *call: Failure("HTTP 404: gone"))) for found in (record.section("Goals"), record.language(), record.write_section("Scoring", "A"), record.write_language("Swedish")): with self.subTest(found): assert isinstance(found, Failure), found self.assertIn("ABC-12", found.reason) def test_refuses_a_write_over_a_description_a_teammate_saved_since_the_record_read_it(self) -> None: def teammate_saves_after_the_read(method: str, url: str, authorization: str, body: object) -> object: answer = self.jira.http(method, url, authorization, body) if method == "GET" and len(self.jira.calls) == 1: self.jira.teammate_saves("## Goals\nTheirs.\n") return answer record = EpicRecord(dataclasses.replace(self.record.description, http=teammate_saves_after_the_read)) found = record.write_section("Scoring", "Risk.") assert isinstance(found, Failure), found self.assertIn("someone saved", found.reason) self.assertEqual(self.jira.puts(), []) def test_writes_scoring_and_decisions_and_the_language_without_approval(self) -> None: self.assertIsInstance(self.record.write_section("Scoring", "Risk."), Held) self.assertIsInstance(self.record.write_section("Decisions", "- Another."), Held) self.assertIsInstance(self.record.write_language("Norwegian"), Held) self.assertEqual(self.record.section("Scoring"), "Risk.") self.assertEqual(self.record.section("Decisions"), "- Another.") self.assertEqual(self.record.language(), "Norwegian") self.assertEqual(self.record.section("Goals"), "1. **Achieve:** Ship.\n\n More about shipping.") def test_writes_goals_or_audience_only_where_the_human_approved(self) -> None: found = self.record.write_section("Goals", "1. **Achieve:** Ship less.") assert isinstance(found, Failure), found self.assertIn("approval", found.reason) self.assertEqual(self.jira.puts(), []) self.assertIsInstance(self.record.write_section("Goals", "1. **Achieve:** Ship less.", approved=True), Held) self.assertEqual(self.record.section("Goals"), "1. **Achieve:** Ship less.") # The approval covers the section it was given for, not the next write. self.assertIsInstance(self.record.write_section("Audience", "Everyone."), Failure) if __name__ == "__main__": unittest.main()