Coverage for src/copytrading_engine/host/self_test/model.py: 77%
53 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 workflow values and invariants shared by all engine adapters."""
3from dataclasses import dataclass
4from decimal import Decimal
5from enum import StrEnum
6from fractions import Fraction
7from typing import Literal
9from copytrading_engine.host.errors import InvalidCommand
11ALLOWED_DESTINATIONS = frozenset({"self-test-a", "self-test-b"})
14class Stage(StrEnum):
15 CAPTURED = "captured"
16 PARSED = "parsed"
17 COMPLETED = "completed"
18 FAILED = "failed"
21@dataclass(frozen=True)
22class SubmitSelfTest:
23 command_id: str
24 text: str
25 destination_ids: tuple[str, ...]
27 def __post_init__(self) -> None:
28 if not self.command_id or len(self.command_id) > 128: 28 ↛ 29line 28 didn't jump to line 29 because the condition on line 28 was never true
29 raise InvalidCommand("command_id must contain 1 to 128 characters")
30 if not self.text: 30 ↛ 31line 30 didn't jump to line 31 because the condition on line 30 was never true
31 raise InvalidCommand("text must not be empty")
32 if not self.destination_ids: 32 ↛ 33line 32 didn't jump to line 33 because the condition on line 32 was never true
33 raise InvalidCommand("at least one self-test destination is required")
34 if len(set(self.destination_ids)) != len(self.destination_ids): 34 ↛ 35line 34 didn't jump to line 35 because the condition on line 34 was never true
35 raise InvalidCommand("destination_ids must not contain duplicates")
36 if any(destination not in ALLOWED_DESTINATIONS for destination in self.destination_ids):
37 raise InvalidCommand("destination is outside the self-test allowlist")
40@dataclass(frozen=True)
41class DestinationOutcome:
42 account_id: str
43 result: Literal["simulated"] = "simulated"
45 def __post_init__(self) -> None:
46 if self.account_id not in ALLOWED_DESTINATIONS: 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true
47 raise InvalidCommand("outcome account is outside the self-test allowlist")
50@dataclass(frozen=True)
51class WorkflowView:
52 command_id: str
53 stage: Stage
54 outcomes: tuple[DestinationOutcome, ...]
55 trace_id: str
57 def __post_init__(self) -> None:
58 if not self.command_id or not self.trace_id: 58 ↛ 59line 58 didn't jump to line 59 because the condition on line 58 was never true
59 raise InvalidCommand("workflow identity values must not be empty")
60 if self.stage is Stage.COMPLETED and not self.outcomes: 60 ↛ 61line 60 didn't jump to line 61 because the condition on line 60 was never true
61 raise InvalidCommand("completed self-tests must have outcomes")
62 if self.stage is not Stage.COMPLETED and self.outcomes: 62 ↛ 63line 62 didn't jump to line 63 because the condition on line 62 was never true
63 raise InvalidCommand("only completed self-tests may have outcomes")
66@dataclass(frozen=True)
67class WorkflowAcceptance:
68 """The committed workflow and whether this transaction created it."""
70 workflow: WorkflowView
71 newly_accepted: bool
74@dataclass(frozen=True)
75class ParsedSelfTest:
76 symbol: str
77 action: Literal["buy"]
78 quantity: Fraction
79 unit_price: Decimal
82@dataclass(frozen=True)
83class ParseRejected:
84 command_id: str
85 reason: Literal["unsupported_self_test"] = "unsupported_self_test"
88@dataclass(frozen=True)
89class SelfTestAccepted:
90 command_id: str
91 trace_id: str
94@dataclass(frozen=True)
95class SelfTestParsed:
96 command_id: str
97 parsed: ParsedSelfTest
100@dataclass(frozen=True)
101class SelfTestCompleted:
102 command_id: str
103 outcomes: tuple[DestinationOutcome, ...]