8abccf7ffe
tracker-policy-pr / check-pr (pull_request) Successful in 12s
test / integration-docker (pull_request) Successful in 19s
lint / lint (push) Failing after 55s
test / unit (pull_request) Successful in 2m8s
test / integration-firecracker (pull_request) Successful in 3m25s
test / coverage (pull_request) Successful in 39s
test / publish-infra (pull_request) Has been skipped
Turn the loose queue/audit functions in orchestrator/supervisor/queue.py into
methods on a concrete Supervisor service. The Orchestrator now owns an
injectable `self._supervisor` and calls `self._supervisor.write_proposal(...)`
etc., instead of module-level free functions — the supervise dependency is
explicit and mockable, and a Supervisor can be scoped to a `db_path` (defaults
to the host DB) so tests can point it at a temp database.
- Supervisor (in the package __init__) drops the ABC and gains write_proposal,
read_proposal, list_pending_proposals, list_all_pending_proposals,
write_response, read_response, archive_all_proposals, write_audit_entry,
read_audit_entries, plus the launch-time prepare().
- render_diff / sha256_hex are pure and stateless, so they move to
orchestrator/supervisor/util.py (re-exported from the facade) rather than
becoming methods.
- queue.py is deleted; service.py + the supervise tests call through a
Supervisor instance.
Full unit suite green (2243).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
165 lines
5.9 KiB
Python
165 lines
5.9 KiB
Python
"""Unit: supervise queue/audit error + edge branches (coverage ratchet,
|
|
ADR 0004). Complements test_supervise.py with the malformed-input and
|
|
fallback paths."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from bot_bottle.orchestrator import supervisor as supervise
|
|
from bot_bottle.store.audit_store import AuditStore
|
|
from bot_bottle.paths import bot_bottle_root
|
|
from bot_bottle.orchestrator.store.queue_store import QueueStore
|
|
from bot_bottle.orchestrator.supervisor import (
|
|
AuditEntry,
|
|
Proposal,
|
|
STATUS_APPROVED,
|
|
Supervisor,
|
|
TOOL_EGRESS_ALLOW,
|
|
)
|
|
|
|
_SV = Supervisor()
|
|
|
|
|
|
def _proposal() -> Proposal:
|
|
return Proposal.new(
|
|
bottle_slug="slug",
|
|
tool=TOOL_EGRESS_ALLOW,
|
|
proposed_file="x",
|
|
justification="j",
|
|
current_file_hash="h",
|
|
)
|
|
|
|
|
|
class TestPathHelpers(unittest.TestCase):
|
|
def test_bot_bottle_root(self) -> None:
|
|
self.assertTrue(str(bot_bottle_root()).endswith(".bot-bottle"))
|
|
|
|
|
|
class TestReadMalformed(unittest.TestCase):
|
|
def test_read_proposal_missing_row(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
with patch.dict("os.environ", {"HOME": d}):
|
|
QueueStore("slug").migrate()
|
|
with self.assertRaises(FileNotFoundError):
|
|
_SV.read_proposal("slug", "p")
|
|
|
|
def test_read_response_missing_row(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
with patch.dict("os.environ", {"HOME": d}):
|
|
QueueStore("slug").migrate()
|
|
with self.assertRaises(FileNotFoundError):
|
|
_SV.read_response("slug", "p")
|
|
|
|
def test_list_pending_reads_db_only(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
with patch.dict("os.environ", {"HOME": d}):
|
|
QueueStore("slug").migrate()
|
|
_SV.write_proposal(_proposal())
|
|
pending = _SV.list_pending_proposals("slug")
|
|
self.assertEqual(1, len(pending))
|
|
self.assertEqual("slug", pending[0].bottle_slug)
|
|
|
|
def test_list_pending_skips_when_response_present(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
with patch.dict("os.environ", {"HOME": d}):
|
|
QueueStore("slug").migrate()
|
|
p = _proposal()
|
|
_SV.write_proposal(p)
|
|
_SV.write_response("slug", supervise.Response(
|
|
proposal_id=p.id,
|
|
status=STATUS_APPROVED,
|
|
notes="",
|
|
))
|
|
self.assertEqual([], _SV.list_pending_proposals("slug"))
|
|
|
|
|
|
class TestReadAuditEntries(unittest.TestCase):
|
|
def test_missing_log_returns_empty(self) -> None:
|
|
with tempfile.TemporaryDirectory() as home, \
|
|
patch.dict("os.environ", {"HOME": home}):
|
|
self.assertEqual([], _SV.read_audit_entries("egress", "nope"))
|
|
|
|
def test_reads_entries_from_db(self) -> None:
|
|
with tempfile.TemporaryDirectory() as home, \
|
|
patch.dict("os.environ", {"HOME": home}):
|
|
AuditStore().migrate()
|
|
_SV.write_audit_entry(AuditEntry(
|
|
timestamp="t",
|
|
bottle_slug="slug",
|
|
component="egress",
|
|
operator_action="approve",
|
|
operator_notes="",
|
|
justification="",
|
|
diff="",
|
|
))
|
|
_SV.write_audit_entry(AuditEntry(
|
|
timestamp="t",
|
|
bottle_slug="other",
|
|
component="egress",
|
|
operator_action="reject",
|
|
operator_notes="",
|
|
justification="",
|
|
diff="",
|
|
))
|
|
entries = _SV.read_audit_entries("egress", "slug")
|
|
self.assertEqual(1, len(entries))
|
|
self.assertEqual("approve", entries[0].operator_action)
|
|
|
|
|
|
class TestStoreGuardBranches(unittest.TestCase):
|
|
"""Direct QueueStore / AuditStore construction and early-return guard branches."""
|
|
|
|
def test_queue_store_explicit_db_path(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db = Path(d) / "q.db"
|
|
store = QueueStore("key", db_path=db)
|
|
store.migrate()
|
|
self.assertTrue(db.is_file())
|
|
self.assertEqual(db, store.db_path)
|
|
|
|
def test_queue_store_missing_db_list_pending_returns_empty(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db = Path(d) / "q.db"
|
|
store = QueueStore("key", db_path=db)
|
|
store.migrate()
|
|
db.unlink()
|
|
self.assertEqual([], store.list_pending_proposals())
|
|
|
|
def test_queue_store_missing_db_list_all_returns_empty(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db = Path(d) / "q.db"
|
|
store = QueueStore("key", db_path=db)
|
|
store.migrate()
|
|
db.unlink()
|
|
self.assertEqual([], store.list_all_pending_proposals())
|
|
|
|
def test_queue_store_chmod_oserror_is_swallowed(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db = Path(d) / "q.db"
|
|
store = QueueStore("key", db_path=db)
|
|
with patch("pathlib.Path.chmod", side_effect=OSError("ro")):
|
|
store.migrate() # must not raise
|
|
|
|
def test_audit_store_missing_db_read_returns_empty(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db = Path(d) / "a.db"
|
|
store = AuditStore(db_path=db)
|
|
store.migrate()
|
|
db.unlink()
|
|
self.assertEqual([], store.read_audit_entries("egress", "slug"))
|
|
|
|
def test_audit_store_chmod_oserror_is_swallowed(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db = Path(d) / "a.db"
|
|
store = AuditStore(db_path=db)
|
|
with patch("pathlib.Path.chmod", side_effect=OSError("ro")):
|
|
store.migrate() # must not raise
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|