import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from do_epic.config import DoEpicConfig, load from failure import Failure from project_config.settings import Finish SETTINGS = """jira-url = "https://acme.atlassian.net" env-file = "~/jira.env" repoless-model = "claude-opus-5-5" """ class ConfigTest(unittest.TestCase): def setUp(self) -> None: made = tempfile.TemporaryDirectory() self.addCleanup(made.cleanup) self.home = pathlib.Path(made.name).resolve() self.folder = self.home / ".claude-projects" / "do-epic" self.folder.mkdir(parents=True) def write(self, settings: str | None = SETTINGS, rule: str | None = "A label that equals a repo's name names that repo.\n") -> None: if settings is not None: (self.folder / "config.toml").write_text(settings, encoding="utf-8") if rule is not None: (self.folder / "repo-rule.md").write_text(rule, encoding="utf-8") def failure(self) -> str: found = load(self.home) assert isinstance(found, Failure), found return found.reason def test_reads_the_settings_and_the_rule_and_defaults_the_clone_folder_and_language(self) -> None: self.write() self.assertEqual(load(self.home), DoEpicConfig(self.folder, "https://acme.atlassian.net", self.home / "jira.env", self.home / "code", "English", "claude-opus-5-5", "A label that equals a repo's name names that repo.\n")) def test_reads_the_teams_labels_for_its_repos(self) -> None: self.write(SETTINGS + '[label-repos]\ntwu-4 = "tenant-web-ui-4"\n"core-4" = "core-4"\n') found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual(dict(found.label_repos), {"twu-4": "tenant-web-ui-4", "core-4": "core-4"}) def test_names_each_entry_that_is_no_label_mapped_to_a_repo(self) -> None: self.write(SETTINGS + '[label-repos]\n"repo:api" = "api"\ndo-epic-human = "api"\nsplit-by-repo = "api"\ndo-epic-release-review = "api"\n"two words" = "api"\nweb = "../web"\ncore = 4\nlanguage = "Swedish"\n') reason = self.failure() for fault in ( "`label-repos` maps 'repo:api', a label do-epic gives a meaning of its own", "`label-repos` maps 'do-epic-human', a label do-epic gives a meaning of its own", "`label-repos` maps 'split-by-repo', a label do-epic gives a meaning of its own", "`label-repos` maps 'do-epic-release-review', a label do-epic gives a meaning of its own", "`label-repos` maps 'two words', which is no Jira label: a label holds no white space", "`language` stands below `[label-repos]`, so it is read as a label; move it above the table", "`label-repos` maps 'web' to '../web', not a repo's name in letters, digits, dots, hyphens and underscores", "`label-repos` maps 'core' to 4, not a repo's name", ): self.assertIn(fault, reason) self.assertEqual(reason.count("`label-repos` maps"), 7) def test_reads_a_template_for_a_sub_tasks_summary(self) -> None: self.write(SETTINGS + 'sub-task-summary = "{label}: {Summary}"\n') found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual(found.sub_task_summary, "{label}: {Summary}") def test_leaves_a_sub_tasks_summary_to_its_default_where_the_config_has_no_template(self) -> None: self.write() found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertIsNone(found.sub_task_summary) def test_refuses_a_template_that_is_no_one_line_with_summary_and_only_known_fields(self) -> None: for line, fault in ( ("sub-task-summary = 4", "`sub-task-summary` is 4, not one line of text"), ('sub-task-summary = "a\\nb {Summary}"', "not one line of text"), ('sub-task-summary = "{label}"', "`sub-task-summary` lacks {Summary}"), ('sub-task-summary = "{label}: {Summary} {repo}"', "names {repo}, and only {label} and {Summary} are known"), ('sub-task-summary = "{Summary:>9}"', "writes {Summary:>9} with something after the name; write {Summary} alone"), ('sub-task-summary = "{Summary} {label!r}"', "writes {label!r} with something after the name; write {label} alone"), ('sub-task-summary = "{Summary!}"', "`sub-task-summary` is '{Summary!}', which Python can't read as a template ("), ('sub-task-summary = "{Summary"', "`sub-task-summary` is '{Summary', which Python can't read as a template ("), ): with self.subTest(line=line): self.write(SETTINGS + line + "\n") self.assertIn(fault, self.failure()) def test_reads_a_template_of_plain_text_around_its_fields_and_a_brace_written_twice(self) -> None: for template in ("{Summary}", "{Summary}{{", "{Summary} for {label} in Jira"): with self.subTest(template=template): self.write(SETTINGS + f'sub-task-summary = "{template}"\n') found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual(found.sub_task_summary, template) def test_refuses_a_label_repos_that_is_no_table(self) -> None: self.write('label-repos = "twu-4"\n' + SETTINGS) self.assertIn("`label-repos` is 'twu-4', not a table of labels, such as `twu-4 = \"tenant-web-ui-4\"` under `[label-repos]`", self.failure()) def test_reads_a_clone_folder_and_language_the_config_sets(self) -> None: self.write(SETTINGS + 'clone-folder = "/srv/repos"\nlanguage = "Brazilian Portuguese"\n') found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual((found.clone_folder, found.language), (pathlib.Path("/srv/repos"), "Brazilian Portuguese")) def test_reads_how_a_finished_epic_closes_and_wants_both_names_or_neither(self) -> None: self.write(SETTINGS + 'jira-finish-transition = "Mark as done"\njira-finish-status = "Done"\n') found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual(found.finish, Finish("Mark as done", "Done")) self.write(SETTINGS + 'jira-finish-status = " "\n') self.assertIn('`jira-finish-status` is \' \', not the name Jira shows, such as "Done"; `jira-finish-status` is set, so `jira-finish-transition` must be too', self.failure()) def test_reads_the_unblocking_statuses_and_that_the_teams_automation_moves_issues(self) -> None: self.write(SETTINGS) found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual((found.unblocking, found.automation_moves), ((), False)) self.write(SETTINGS + 'jira-unblocking-statuses = ["Release Ready", "Done"]\njira-automation-moves-issues = true\n') found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual((found.unblocking, found.automation_moves), (("Release Ready", "Done"), True)) def test_the_issue_types_of_a_finding_and_a_release_review_are_task_where_left_out(self) -> None: self.write() found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual((found.finding_type, found.review_type), ("Task", "Task")) def test_reads_the_issue_types_of_a_finding_and_a_release_review(self) -> None: self.write(SETTINGS + 'jira-finding-type = "Feature"\njira-review-type = "Chore"\n') found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual((found.finding_type, found.review_type), ("Feature", "Chore")) def test_refuses_an_issue_type_that_is_no_name(self) -> None: for setting, example in (("jira-finding-type", "Feature"), ("jira-review-type", "Chore")): for value, shown in (("4", "4"), ('" "', "' '")): with self.subTest(setting=setting, value=value): self.write(SETTINGS + f"{setting} = {value}\n") self.assertIn(f'`{setting}` is {shown}, not the name Jira shows, such as "{example}"', self.failure()) def test_refuses_unblocking_statuses_that_are_no_list_of_names_and_a_switch_that_is_no_boolean(self) -> None: for value in ('"Done"', '["Done", " "]', "[]", "[1]"): with self.subTest(value=value): self.write(SETTINGS + f"jira-unblocking-statuses = {value}\n") self.assertIn('`jira-unblocking-statuses` is ', self.failure()) self.assertIn(', not a list of the status names Jira shows, such as ["Release Ready"]', self.failure()) self.write(SETTINGS + "jira-automation-moves-issues = true\n") self.assertIn("`jira-automation-moves-issues` is set, so `jira-unblocking-statuses` must name the status the automation gives an issue whose pull request merged", self.failure()) self.write(SETTINGS + 'jira-automation-moves-issues = "yes"\n') self.assertIn("`jira-automation-moves-issues` is 'yes', not true or false", self.failure()) def test_refuses_while_the_config_is_missing(self) -> None: self.write(None) self.assertIn(f"cannot read {self.folder / 'config.toml'}", self.failure()) def test_lists_the_settings_where_the_config_is_missing(self) -> None: self.write(None) self.assertIn("The settings are jira-url, env-file, repoless-model", self.failure()) def test_refuses_while_the_rule_is_missing(self) -> None: self.write(rule=None) self.assertIn(f"cannot read {self.folder / 'repo-rule.md'}", self.failure()) def test_refuses_while_the_rule_is_blank(self) -> None: self.write(rule=" \n") self.assertIn("`repo-rule.md` is blank", self.failure()) def test_a_missing_rule_does_not_hide_the_settings_faults(self) -> None: self.write(SETTINGS.replace("claude-opus-5-5", "x"), rule=None) self.assertIn("`repoless-model` is 'x'", self.failure()) def test_names_each_missing_setting(self) -> None: self.write("") reason = self.failure() for name in ("jira-url", "env-file", "repoless-model"): self.assertIn(f"`{name}` is missing", reason) def test_names_a_setting_it_does_not_know(self) -> None: self.write(SETTINGS + 'checkout = "~/code/acme"\n') self.assertIn("`checkout` is not a setting", self.failure()) def test_names_each_misspelt_value(self) -> None: for line, name in ( ('jira-url = "http://acme.atlassian.net"', "jira-url"), ('jira-url = "https://acme.atlassian.net/"', "jira-url"), ('env-file = "jira.env"', "env-file"), ('clone-folder = "code"', "clone-folder"), ('language = "sv_SE"', "language"), ('repoless-model = "opus"', "repoless-model"), ('repoless-model = "claude-opsu-5-5"', "repoless-model"), ): with self.subTest(line): self.write("\n".join([ln for ln in SETTINGS.splitlines() if not ln.startswith(name)] + [line])) self.assertIn(f"`{name}` is", self.failure()) def test_keeps_the_credentials_outside_every_config_folder_and_the_clone_folder(self) -> None: (self.home / ".claude-projects" / "acme").mkdir() for env_file in ("~/.claude-projects/do-epic/jira.env", "~/.claude-projects/acme/jira.env", "~/code/acme/jira.env"): with self.subTest(env_file): self.write(SETTINGS.replace("~/jira.env", env_file)) self.assertIn("`env-file` is", self.failure()) def test_keeps_the_credentials_outside_a_clone_folder_the_config_sets(self) -> None: self.write(SETTINGS.replace("~/jira.env", "~/clones/jira.env") + 'clone-folder = "~/clones"\n') self.assertIn("`env-file` is", self.failure()) def test_accepts_credentials_beside_the_config_folders(self) -> None: self.write(SETTINGS.replace("~/jira.env", "~/.claude-projects/do-epic.env")) found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual(found.env_file, self.home / ".claude-projects" / "do-epic.env") def test_refuses_while_the_routing_ladder_cannot_be_read(self) -> None: self.write() found = load(self.home, self.home / "no-ladder.json") assert isinstance(found, Failure), found self.assertIn("no-ladder.json", found.reason) def test_reports_every_fault_at_once(self) -> None: self.write('jira-url = "acme"\nrepoless-model = "x"\n') reason = self.failure() for part in ("`jira-url` is", "`env-file` is missing", "`repoless-model` is"): self.assertIn(part, reason) self.assertIn("`env-file` is missing; `jira-url` is", reason, "joined by semicolons") def test_names_the_models_on_the_ladder_where_the_model_is_off_it(self) -> None: ladder = self.home / "ladder.json" ladder.write_text('[{"model": "claude-sonnet-5-5", "effort": "low"}, {"model": "claude-opus-5-5", "effort": "low"}]', encoding="utf-8") self.write(SETTINGS.replace("claude-opus-5-5", "claude-haiku-5-5")) found = load(self.home, ladder) assert isinstance(found, Failure), found self.assertIn("not a model on the routing ladder: claude-opus-5-5, claude-sonnet-5-5.", found.reason) def test_reads_the_rule_and_the_settings_as_utf8(self) -> None: self.write(SETTINGS + 'language = "Svenska"\n', rule="Etiketten är repots namn.\n") found = load(self.home) assert isinstance(found, DoEpicConfig), found self.assertEqual(found.repo_rule, "Etiketten är repots namn.\n")