diff --git a/novem/comments.py b/novem/comments.py index d912982..809cb5a 100644 --- a/novem/comments.py +++ b/novem/comments.py @@ -13,7 +13,7 @@ from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Tuple -from .api_ref import NovemAPI +from .api_ref import Novem409, NovemAPI # Single-letter FQNP type codes to API path plurals _TYPE_MAP: Dict[str, str] = { @@ -567,6 +567,11 @@ def reply(self, text: str, title: Optional[str] = None) -> None: Args: text: The message body. title: Optional slug for the comment. Auto-generated if omitted. + + Raises: + Novem409: a comment with this slug already exists with different + text (e.g. a concurrent writer won the race). Repeating a + reply with the same text is a no-op. """ self._do_reply(text, title) @@ -652,8 +657,18 @@ def _do_reply(self, text: str, title: Optional[str] = None) -> str: create_path = f"{base}/{slug}" full_path = f"{base}/{my_ref}" - self.create(create_path) # 409 (already exists) returns False — fine, we'll update - self.write(f"{full_path}/msg", text) + msg_path = f"{full_path}/msg" + if not self.create(create_path): + # 409: the comment already exists. Writing over it is right when + # it is our own earlier attempt (same text, or created but never + # written), but not when another writer won the create race with + # different text -- that would silently replace their message. + existing = self.read(msg_path) + if existing.strip() == text.strip(): + return full_path + if existing.strip(): + raise Novem409(f"{full_path} already exists with different text") + self.write(msg_path, text) return full_path diff --git a/tests/test_comments.py b/tests/test_comments.py index f7d8a73..ec798f2 100644 --- a/tests/test_comments.py +++ b/tests/test_comments.py @@ -703,3 +703,42 @@ def test_message_lines_paragraph(): msg = "Just a simple paragraph." lines = _render_message_lines(msg, "| ", 80) assert lines == ["| Just a simple paragraph."] + + +def _reply_conflict_ctx(requests_mock, existing_msg): + import re + + base = os.path.dirname(os.path.abspath(__file__)) + config_file = f"{base}/test.conf" + + threads_re = re.compile(r".*/vis/plots/myplot/threads/.*") + requests_mock.register_uri("PUT", threads_re, status_code=409, json={"message": "exists"}) + requests_mock.register_uri("GET", re.compile(r".*/@sondov~agreed/msg$"), text=existing_msg) + post = requests_mock.register_uri("POST", threads_re, status_code=200) + + ctx = Context("/u/alice/p/myplot/c/@sen~topic", config_path=config_file) + ctx._me = "sondov" + return ctx, post + + +def test_context_reply_conflict_with_different_text_raises(requests_mock): + from novem.exceptions import Novem409 + + ctx, post = _reply_conflict_ctx(requests_mock, "someone else's reply") + with pytest.raises(Novem409, match="different text"): + ctx.reply("I agree!", title="agreed") + assert not post.called + + +def test_context_reply_conflict_with_same_text_is_noop(requests_mock): + ctx, post = _reply_conflict_ctx(requests_mock, "I agree!\n") + ctx.reply("I agree!", title="agreed") + assert not post.called + + +def test_context_reply_conflict_with_empty_msg_writes(requests_mock): + # created by an earlier attempt that never wrote its message + ctx, post = _reply_conflict_ctx(requests_mock, "") + ctx.reply("I agree!", title="agreed") + assert post.called + assert post.last_request.text == "I agree!"