Coverage for src/robot_dispatch/repositories/protocols.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-07 17:18 +0000

1from __future__ import annotations 

2 

3from collections.abc import Callable 

4from typing import Any, Protocol, runtime_checkable 

5 

6from robot_dispatch.db.models import OutboxMessage, Robot, Task, TaskEvent 

7 

8 

9@runtime_checkable 

10class TaskRepository(Protocol): 

11 async def add(self, task: Task) -> Task: ... 

12 async def get(self, task_id: Any) -> Task | None: ... 

13 async def get_by_external_id(self, external_id: str) -> Task | None: ... 

14 async def lock_ready(self, *, now: Any, limit: int) -> list[Task]: ... 

15 async def lock_expired_leases(self, *, now: Any, limit: int) -> list[Task]: ... 

16 async def list( 

17 self, 

18 *, 

19 statuses: list[str] | None = None, 

20 robot_id: Any | None = None, 

21 task_type: str | None = None, 

22 limit: int = 50, 

23 offset: int = 0, 

24 ) -> list[Task]: ... 

25 async def count_by_status(self) -> dict[str, int]: ... 

26 

27 

28@runtime_checkable 

29class RobotRepository(Protocol): 

30 async def add(self, robot: Robot) -> Robot: ... 

31 async def get(self, robot_id: Any) -> Robot | None: ... 

32 async def get_by_name(self, name: str) -> Robot | None: ... 

33 async def list(self) -> list[Robot]: ... 

34 async def find_idle_for(self, *, task_type: str, now: Any, offline_after: int) -> Robot | None: ... 

35 

36 

37@runtime_checkable 

38class EventRepository(Protocol): 

39 async def add(self, event: TaskEvent) -> TaskEvent: ... 

40 async def list_for_task(self, task_id: Any, *, limit: int = 200) -> list[TaskEvent]: ... 

41 

42 

43@runtime_checkable 

44class OutboxRepository(Protocol): 

45 async def add(self, message: OutboxMessage) -> OutboxMessage: ... 

46 async def lock_pending(self, *, now: Any, limit: int) -> list[OutboxMessage]: ... 

47 async def mark_sent(self, message: OutboxMessage, sent_at: Any) -> None: ... 

48 async def mark_failed( 

49 self, message: OutboxMessage, error: str, available_at: Any, attempts: int 

50 ) -> None: ... 

51 

52 

53@runtime_checkable 

54class UnitOfWork(Protocol): 

55 tasks: TaskRepository 

56 robots: RobotRepository 

57 events: EventRepository 

58 outbox: OutboxRepository 

59 

60 async def __aenter__(self) -> UnitOfWork: ... 

61 async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None: ... 

62 async def commit(self) -> None: ... 

63 async def rollback(self) -> None: ... 

64 

65 

66UnitOfWorkFactory = Callable[[], UnitOfWork]