Coverage for src/copytrading_engine/sources/recovery.py: 76%
50 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
1"""Immutable values exchanged by ingestion recovery boundaries."""
3from collections.abc import Mapping
4from dataclasses import dataclass
5from datetime import datetime
6from types import MappingProxyType
7from typing import Literal, cast
9from pydantic import JsonValue
11Bootstrap = Literal["earliest_retained", "recent_only"]
14@dataclass(frozen=True, slots=True)
15class RecoveryStart:
16 last_id: int
17 bootstrap: Bootstrap
19 def __post_init__(self) -> None:
20 if type(self.last_id) is not int or self.last_id < 0: 20 ↛ 21line 20 didn't jump to line 21 because the condition on line 20 was never true
21 raise ValueError("Recovery cursor must be a non-negative integer")
22 _validate_bootstrap(self.bootstrap)
25@dataclass(frozen=True, slots=True)
26class RecoveryProgress:
27 channel_id: str
28 last_id: int
29 bootstrap: Bootstrap
30 updated_at: datetime
32 def __post_init__(self) -> None:
33 if not self.channel_id: 33 ↛ 34line 33 didn't jump to line 34 because the condition on line 33 was never true
34 raise ValueError("Recovery channel ID cannot be empty")
35 if type(self.last_id) is not int or self.last_id < 0: 35 ↛ 36line 35 didn't jump to line 36 because the condition on line 35 was never true
36 raise ValueError("Recovery cursor must be a non-negative integer")
37 _validate_bootstrap(self.bootstrap)
38 if self.updated_at.tzinfo is None or self.updated_at.utcoffset() is None:
39 raise ValueError("Recovery update time must be timezone-aware")
42@dataclass(frozen=True, slots=True)
43class RejectedCapture:
44 payload: Mapping[str, JsonValue]
45 reason: str
47 def __post_init__(self) -> None:
48 if not isinstance(self.payload, Mapping): 48 ↛ 49line 48 didn't jump to line 49 because the condition on line 48 was never true
49 raise TypeError("Rejected capture payload must be a mapping")
50 object.__setattr__(self, "payload", cast(Mapping[str, JsonValue], _freeze(self.payload)))
53def _validate_bootstrap(bootstrap: str) -> None:
54 if bootstrap not in {"earliest_retained", "recent_only"}: 54 ↛ 55line 54 didn't jump to line 55 because the condition on line 54 was never true
55 raise ValueError("Unknown recovery bootstrap mode")
58def _freeze(value: object) -> JsonValue:
59 if isinstance(value, Mapping):
60 if any(not isinstance(key, str) for key in value): 60 ↛ 61line 60 didn't jump to line 61 because the condition on line 60 was never true
61 raise TypeError("Rejected capture object keys must be strings")
62 return cast(
63 JsonValue,
64 MappingProxyType({key: _freeze(item) for key, item in value.items()}),
65 )
66 if isinstance(value, list | tuple):
67 return cast(JsonValue, tuple(_freeze(item) for item in value))
68 if value is None or isinstance(value, str | int | float | bool): 68 ↛ 70line 68 didn't jump to line 70 because the condition on line 68 was always true
69 return cast(JsonValue, value)
70 raise TypeError("Rejected capture payload must contain only JSON values")
73def plain_json(value: object) -> JsonValue:
74 """Return owned JSON containers for private persistence serialization."""
75 if isinstance(value, Mapping):
76 return {key: plain_json(item) for key, item in value.items()}
77 if isinstance(value, list | tuple): 77 ↛ 78line 77 didn't jump to line 78 because the condition on line 77 was never true
78 return [plain_json(item) for item in value]
79 if value is None or isinstance(value, str | int | float | bool): 79 ↛ 81line 79 didn't jump to line 81 because the condition on line 79 was always true
80 return cast(JsonValue, value)
81 raise TypeError("Rejected capture payload must contain only JSON values")