|
| 1 | +# SPDX-FileCopyrightText: 2026 The RISE Project |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +"""Accepted command completion reactions are durable and self-healing.""" |
| 4 | +import json |
| 5 | +from pathlib import Path |
| 6 | +import tempfile |
| 7 | +import unittest |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | +from upstreaming import command_reactions |
| 11 | + |
| 12 | + |
| 13 | +class FakeApi: |
| 14 | + def __init__(self, existing=False): |
| 15 | + self.existing = existing |
| 16 | + self.posts = [] |
| 17 | + |
| 18 | + def request(self, method, path, body=None): |
| 19 | + if method == "GET": |
| 20 | + return ([{"content": "+1", "user": {"login": "riseproject-dev[bot]"}}] |
| 21 | + if self.existing else []) |
| 22 | + self.posts.append((path, body)) |
| 23 | + return {"content": "+1", "user": {"login": "riseproject-dev[bot]"}} |
| 24 | + |
| 25 | + |
| 26 | +class TestCommandReactions(unittest.TestCase): |
| 27 | + def root(self, td): |
| 28 | + root = Path(td) |
| 29 | + directory = root / "upstreaming-state/commands" |
| 30 | + directory.mkdir(parents=True) |
| 31 | + accepted = {"command_id": "comment-7", "comment_id": 7, "result": "accepted"} |
| 32 | + rejected = {"command_id": "comment-8", "comment_id": 8, "result": "rejected"} |
| 33 | + (directory / "comment-7.json").write_text(json.dumps(accepted)) |
| 34 | + (directory / "comment-8.json").write_text(json.dumps(rejected)) |
| 35 | + return root |
| 36 | + |
| 37 | + def test_backfills_accepted_receipts_and_ignores_rejections(self): |
| 38 | + with tempfile.TemporaryDirectory() as td: |
| 39 | + api = FakeApi() |
| 40 | + with patch("upstreaming.command_reactions.Api", return_value=api): |
| 41 | + result = command_reactions.publish(self.root(td), "token") |
| 42 | + self.assertEqual(result, [{"command_id": "comment-7", "comment_id": 7, "status": "created"}]) |
| 43 | + self.assertEqual(api.posts[0][1], {"content": "+1"}) |
| 44 | + self.assertIn("/issues/comments/7/reactions", api.posts[0][0]) |
| 45 | + |
| 46 | + def test_existing_app_completion_is_idempotent(self): |
| 47 | + with tempfile.TemporaryDirectory() as td: |
| 48 | + api = FakeApi(existing=True) |
| 49 | + with patch("upstreaming.command_reactions.Api", return_value=api): |
| 50 | + result = command_reactions.publish(self.root(td), "token") |
| 51 | + self.assertEqual(result[0]["status"], "existing") |
| 52 | + self.assertEqual(api.posts, []) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + unittest.main() |
0 commit comments