test: add coverage for orchestrator + gitea client (diff gate 77% → 98%)
Three new unit test modules: - tests/unit/test_contrib_gitea_client.py — GiteaClient (urllib mocked) and GiteaForge delegation - tests/unit/orchestrator/test_main.py — __main__ run/status commands - tests/unit/orchestrator/test_bootstrap.py — _token, BotBottleStateStore, _to_forge_state/_to_record, make_forge, make_sidecar, build Augments to existing suites: - test_events: non-"created" comment action ignored - test_lifecycle: _iso_now callable, untracked-issue comment ignored, untracked-PR closed ignored (covers _find_by_pr return-None path) - test_runner: destroy command, _default_run via subprocess mock - test_sidecar: _jsonable dataclass/list branches, OpLog.read on missing file, drain_done_events on corrupted file, socket _Handler invalid-JSON and empty-line paths, serve() with pre-existing socket path - test_watchdog: _loop body covered by patching _TICK_SECS to 0.01s - test_webhook: unknown GET path returns 404 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import json
|
||||
import socket
|
||||
import tempfile
|
||||
@@ -12,6 +13,7 @@ from pathlib import Path
|
||||
from bot_bottle.orchestrator.sidecar import (
|
||||
ForgeSidecar,
|
||||
OpLog,
|
||||
_jsonable,
|
||||
drain_done_events,
|
||||
serve,
|
||||
write_done_event,
|
||||
@@ -66,6 +68,29 @@ class SidecarDispatchTest(unittest.TestCase):
|
||||
self.assertFalse(resp["ok"])
|
||||
|
||||
|
||||
class JsonableTest(unittest.TestCase):
|
||||
def test_plain_value_passthrough(self):
|
||||
self.assertEqual(42, _jsonable(42))
|
||||
self.assertEqual("s", _jsonable("s"))
|
||||
|
||||
def test_dataclass_converted_to_dict(self):
|
||||
@dataclasses.dataclass
|
||||
class Thing:
|
||||
x: int
|
||||
y: str = "hi"
|
||||
self.assertEqual({"x": 99, "y": "hi"}, _jsonable(Thing(x=99)))
|
||||
|
||||
def test_list_recursed(self):
|
||||
self.assertEqual([1, 2, 3], _jsonable([1, 2, 3]))
|
||||
|
||||
def test_list_of_dataclasses(self):
|
||||
@dataclasses.dataclass
|
||||
class Item:
|
||||
v: int
|
||||
result = _jsonable([Item(v=1), Item(v=2)])
|
||||
self.assertEqual([{"v": 1}, {"v": 2}], result)
|
||||
|
||||
|
||||
class QueueTest(unittest.TestCase):
|
||||
def test_drain_removes_events(self):
|
||||
tmp = Path(self.enterContext(tempfile.TemporaryDirectory())) # pylint: disable=consider-using-with
|
||||
@@ -76,8 +101,34 @@ class QueueTest(unittest.TestCase):
|
||||
def test_drain_missing_dir(self):
|
||||
self.assertEqual([], drain_done_events(Path("/nonexistent/queue")))
|
||||
|
||||
def test_drain_skips_corrupted_file(self):
|
||||
tmp = Path(self.enterContext(tempfile.TemporaryDirectory())) # pylint: disable=consider-using-with
|
||||
(tmp / "done-bad.json").write_text("not json", encoding="utf-8")
|
||||
events = drain_done_events(tmp)
|
||||
self.assertEqual([], events)
|
||||
# The corrupted file is removed by the finally block.
|
||||
self.assertFalse((tmp / "done-bad.json").exists())
|
||||
|
||||
|
||||
class OpLogReadTest(unittest.TestCase):
|
||||
def test_read_missing_file_returns_empty(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
log = OpLog(Path(tmp) / "sub" / "ops.jsonl")
|
||||
# File not written yet — read() should return [].
|
||||
self.assertEqual([], log.read())
|
||||
|
||||
|
||||
class SocketServerTest(unittest.TestCase):
|
||||
def _make_server(self, tmp: Path):
|
||||
sock = tmp / "s.sock"
|
||||
if len(str(sock)) > 100:
|
||||
self.skipTest("temp socket path too long for AF_UNIX")
|
||||
sidecar = ForgeSidecar(
|
||||
forge=FakeForge(), op_log=OpLog(tmp / "ops.jsonl"),
|
||||
queue_dir=tmp / "q", run_key=("o", "r", 1),
|
||||
)
|
||||
return serve(sidecar, sock), sock
|
||||
|
||||
def test_round_trip_over_unix_socket(self):
|
||||
tmp = tempfile.mkdtemp()
|
||||
sock = Path(tmp) / "s.sock"
|
||||
@@ -104,5 +155,50 @@ class SocketServerTest(unittest.TestCase):
|
||||
self.assertEqual(3, resp["result"]["number"])
|
||||
|
||||
|
||||
def test_handler_invalid_json_returns_error(self):
|
||||
tmp = Path(tempfile.mkdtemp())
|
||||
srv, sock = self._make_server(tmp)
|
||||
t = threading.Thread(target=srv.handle_request, daemon=True)
|
||||
t.start()
|
||||
try:
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
client.connect(str(sock))
|
||||
client.sendall(b"not valid json!\n")
|
||||
line = client.makefile().readline()
|
||||
client.close()
|
||||
finally:
|
||||
t.join(timeout=5)
|
||||
srv.server_close()
|
||||
resp = json.loads(line)
|
||||
self.assertFalse(resp["ok"])
|
||||
self.assertIn("invalid json", resp["error"])
|
||||
|
||||
def test_handler_empty_line_closes_silently(self):
|
||||
tmp = Path(tempfile.mkdtemp())
|
||||
srv, sock = self._make_server(tmp)
|
||||
t = threading.Thread(target=srv.handle_request, daemon=True)
|
||||
t.start()
|
||||
try:
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
client.connect(str(sock))
|
||||
client.close() # immediate EOF -> readline() returns b""
|
||||
finally:
|
||||
t.join(timeout=5)
|
||||
srv.server_close()
|
||||
|
||||
def test_serve_removes_existing_socket_path(self):
|
||||
tmp = Path(tempfile.mkdtemp())
|
||||
sock = tmp / "existing.sock"
|
||||
if len(str(sock)) > 100:
|
||||
self.skipTest("temp socket path too long for AF_UNIX")
|
||||
sock.touch() # pre-existing file at socket path
|
||||
sidecar = ForgeSidecar(
|
||||
forge=FakeForge(), op_log=OpLog(tmp / "ops.jsonl"),
|
||||
queue_dir=tmp / "q", run_key=("o", "r", 1),
|
||||
)
|
||||
srv = serve(sidecar, sock) # should unlink the pre-existing file
|
||||
srv.server_close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user