From 60bc76c8769d77a02bf69990e8b3299c2782c36a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sun, 15 Nov 2026 11:36:54 +0100 Subject: [PATCH] Test the page store's failures, Confluence's error messages and where project-record keeps the version --- bin/project_record/main.py | 9 +++- bin/project_record/test_project_record.py | 12 ++++++ bin/record_store/confluence_page.py | 8 ++-- bin/record_store/test_confluence_page.py | 51 +++++++++++++++++++++-- 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/bin/project_record/main.py b/bin/project_record/main.py index 5512c952..ffb46c3a 100644 --- a/bin/project_record/main.py +++ b/bin/project_record/main.py @@ -28,6 +28,7 @@ import sys from failure import Failure from record_store.stores import FileStore, goals_store, local_file, RECORDS, RecordStore, store, stored from project_config.main import located +from project_config.settings import ProjectConfig # Not git's verbs, such as checkout and commit: those read as acting on the repo. COMMANDS = ("open", "save") @@ -46,7 +47,7 @@ def main(argv: list[str]) -> int: return 2 home = pathlib.Path.home() config = located(None, home) - done = config if isinstance(config, Failure) else act(argv[0], goals_store(config) if argv[1] == GOALS else store(config, argv[1], home / COPIES / config.key / f"{argv[1]}.version"), home / COPIES / config.key / f"{argv[1]}.md") + done = config if isinstance(config, Failure) else act(argv[0], *opened(config, argv[1], home)) if isinstance(done, Failure): print(f"project-record: {done.reason}", file=sys.stderr) return 1 @@ -54,6 +55,12 @@ def main(argv: list[str]) -> int: return 0 +def opened(config: ProjectConfig, name: str, home: pathlib.Path) -> tuple[RecordStore, pathlib.Path]: + """The store of the record called name, and the copy it gets where it isn't kept as a file. A store that keeps the version it read, for a later save, keeps it beside the copy.""" + copy = home / COPIES / config.key / f"{name}.md" + return (goals_store(config) if name == GOALS else store(config, name, copy.with_suffix(".version"))), copy + + def act(command: str, record: RecordStore, copy: pathlib.Path) -> str | Failure: """What command, one of COMMANDS, prints for record. copy is where a record kept outside files gets its file.""" if command == "open": diff --git a/bin/project_record/test_project_record.py b/bin/project_record/test_project_record.py index 66f3c6f4..0568c3ae 100644 --- a/bin/project_record/test_project_record.py +++ b/bin/project_record/test_project_record.py @@ -8,13 +8,25 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from project_config.test_project_config import configured +from record_store.confluence_page import ConfluencePage from failure import Failure from project_record import main from record_store.fakes import MemoryStore +from project_config.settings import Page, ProjectConfig TOOL = pathlib.Path(__file__).resolve().parents[1] / "project-record" +class Opened(unittest.TestCase): + def test_a_record_on_a_confluence_page_keeps_the_version_it_read_beside_its_copy(self): + page = Page("https://acme.atlassian.net", "2293761", "confluence:https://acme.atlassian.net/wiki/spaces/ENG/pages/2293761") + config = ProjectConfig("acme", pathlib.Path("/home/me/.claude-projects/acme"), pathlib.Path("/code/acme"), {}, pathlib.Path("/home/me/acme.env"), pages={"decisions": page}) + record, copy = main.opened(config, "decisions", pathlib.Path("/home/me")) + self.assertEqual(copy, pathlib.Path("/home/me/.local/state/project-record/acme/decisions.md")) + assert isinstance(record, ConfluencePage), record + self.assertEqual(record.kept, pathlib.Path("/home/me/.local/state/project-record/acme/decisions.version")) + + class Act(unittest.TestCase): def setUp(self) -> None: self.copy = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()), "acme", "decisions.md") diff --git a/bin/record_store/confluence_page.py b/bin/record_store/confluence_page.py index 47c8e575..6b1323d2 100644 --- a/bin/record_store/confluence_page.py +++ b/bin/record_store/confluence_page.py @@ -7,11 +7,11 @@ import pathlib import typing from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused -from failure import Failure -from plan_store.jira_epic import basic_auth, Http, ToAdf, ToMarkdown, TIMEOUT -from project_config.settings import Page -from rest_client import json_request +from plan_store.jira_epic import basic_auth, Http, TIMEOUT, ToAdf, ToMarkdown from rest_client.json_request import ErrorAnswer +from failure import Failure +from rest_client import json_request +from project_config.settings import Page from text_files.file_writes import replace_file # What Confluence answers to a write whose version isn't the one after the version it holds. diff --git a/bin/record_store/test_confluence_page.py b/bin/record_store/test_confluence_page.py index 1b50a624..6dbd60ec 100644 --- a/bin/record_store/test_confluence_page.py +++ b/bin/record_store/test_confluence_page.py @@ -7,11 +7,11 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) +from record_store.confluence_page import ConfluencePage, error_message +from rest_client.json_request import ErrorAnswer from failure import Failure from project_config.settings import Page -from record_store.confluence_page import ConfluencePage from record_store.contract import RecordStoreContract -from rest_client.json_request import ErrorAnswer PAGE = Page("https://acme.atlassian.net", "2293761", "confluence:https://acme.atlassian.net/wiki/spaces/ENG/pages/2293761") URL = "https://acme.atlassian.net/wiki/api/v2/pages/2293761" @@ -56,7 +56,7 @@ class ConfluenceCase(unittest.TestCase): self.env = folder / "acme.env" self.env.write_text("ATLASSIAN_EMAIL=me@example.com\nATLASSIAN_API_TOKEN=s3cret\n") self.env.chmod(0o600) - self.kept = folder / "state" / "decisions.version" + self.kept = folder / "state" / "acme" / "decisions.version" self.confluence = FakeConfluence() def new_store(self, kept: pathlib.Path | None = None) -> ConfluencePage: @@ -103,6 +103,7 @@ class ConfluencePageTest(ConfluenceCase): self.assertIsNone(self.new_store(self.kept).write("first\n")) self.assertIsNone(self.new_store(self.kept).write("second\n")) self.assertEqual(self.new_store().read(), "second\n") + self.assertEqual(self.confluence.title, "Decisions") def test_a_write_with_no_read_replaces_the_page_as_it_stands(self) -> None: self.confluence.teammate_saves("earlier\n") @@ -111,13 +112,55 @@ class ConfluencePageTest(ConfluenceCase): self.assertEqual(self.new_store().read(), "replaced\n") def test_fails_on_an_answer_not_shaped_as_a_page(self) -> None: - for answer in ({}, {"title": "Decisions", "version": {}, "body": {}}, ["not", "a", "page"]): + for answer in ({}, {"title": "Decisions", "version": {}, "body": {"atlas_doc_format": {"value": ""}}}, {"version": {"number": 3}, "body": {"atlas_doc_format": {"value": ""}}}, ["not", "a", "page"]): with self.subTest(answer): store = ConfluencePage(PAGE, self.env, None, lambda *_: answer, to_markdown, to_adf) read = store.read() assert isinstance(read, Failure), read self.assertIn("not in the shape Confluence Cloud's REST API version 2 documents", read.reason) + def test_fails_on_a_page_without_a_body_in_adf(self) -> None: + for body in ({}, "text", {"atlas_doc_format": "text"}, {"atlas_doc_format": {"value": 3}}): + with self.subTest(body): + answer = {"title": "Decisions", "version": {"number": 3}, "body": body} + store = ConfluencePage(PAGE, self.env, None, lambda *_: answer, to_markdown, to_adf) + self.assertEqual(store.read(), Failure(f"{PAGE.spelling} is not in the shape Confluence Cloud's REST API version 2 documents: it holds no body in atlas_doc_format")) + + def test_fails_on_a_body_that_is_not_json(self) -> None: + self.confluence.value = "{not json" + self.assertEqual(self.new_store().read(), Failure(f"{PAGE.spelling} holds a body that is not JSON")) + + def test_fails_naming_the_page_where_the_text_cannot_be_converted(self) -> None: + self.confluence.teammate_saves("text\n") + store = ConfluencePage(PAGE, self.env, None, self.confluence.http, lambda _: Failure("the shim is down"), lambda _: Failure("the shim is down")) + self.assertEqual(store.read(), Failure(f"cannot convert {PAGE.spelling} to markdown: the shim is down")) + self.assertEqual(store.write("text\n"), Failure(f"cannot convert the text for {PAGE.spelling} to ADF: the shim is down")) + + def test_a_write_confluence_refuses_otherwise_fails_naming_the_page_and_not_a_teammate(self) -> None: + answers = iter([{"title": "Decisions", "version": {"number": 3}}, ErrorAnswer("PUT /wiki/api/v2/pages/2293761 answered 403: Forbidden", 403)]) + store = ConfluencePage(PAGE, self.env, None, lambda *_: next(answers), to_markdown, to_adf) + self.assertEqual(store.write("text\n"), Failure(f"cannot write {PAGE.spelling}: PUT /wiki/api/v2/pages/2293761 answered 403: Forbidden")) + + def test_fails_where_its_file_cannot_keep_the_version(self) -> None: + self.kept.mkdir(parents=True) + read = self.new_store(self.kept).read() + assert isinstance(read, Failure), read + self.assertTrue(read.reason.startswith(f"cannot write {self.kept}: "), read.reason) + + def test_fails_on_a_kept_version_it_cannot_read_and_writes_nothing(self) -> None: + self.kept.parent.mkdir(parents=True) + self.kept.write_text("{}") + written = self.new_store(self.kept).write("text\n") + assert isinstance(written, Failure), written + self.assertTrue(written.reason.startswith(f"cannot read the version of {PAGE.spelling} that {self.kept} keeps: "), written.reason) + self.assertEqual(self.confluence.calls, []) + + def test_reads_confluences_messages_from_an_error_answer(self) -> None: + self.assertEqual(error_message({"errors": [{"status": 409, "title": "Version must be incremented"}, {"title": "and more"}, "odd"]}), "Version must be incremented; and more") + self.assertEqual(error_message({"message": "Not Found"}), "Not Found") + self.assertEqual(error_message({"errors": "odd"}), "") + self.assertEqual(error_message(["odd"]), "") + def test_fails_naming_the_page_when_confluence_answers_an_error(self) -> None: store = ConfluencePage(PAGE, self.env, None, lambda *_: ErrorAnswer("GET /wiki/api/v2/pages/2293761 answered 404: Not Found", 404), to_markdown, to_adf) self.assertEqual(store.read(), Failure(f"cannot read {PAGE.spelling}: GET /wiki/api/v2/pages/2293761 answered 404: Not Found"))