Coverage for src/copytrading_engine/control/copying.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-16 16:55 +0000

1"""Whether CopyTrading copies, in the five words the owner sees everywhere. 

2 

3The engine keeps every detail; what it says to the owner is one of: Copying, Sells only, Paused 

4(only when the owner or an approved agent paused), Stopped (a problem stopped it, and the detail 

5says which), or Not started. The app's `CopyingState` makes the same choice from the same facts. 

6""" 

7 

8from dataclasses import dataclass 

9 

10from copytrading_engine.control.wire import CopyingWord 

11 

12# An account the engine could not open or keep: a problem, never the owner's choice. 

13UNHELD = frozenset( 

14 { 

15 "account_unavailable", 

16 "outside_open_orders", 

17 "broker_account_inactive", 

18 "account_in_use", 

19 "alpaca_account_shared", 

20 "record_unreadable", 

21 "broker_keys_rejected", 

22 "broker_unreachable", 

23 } 

24) 

25 

26 

27@dataclass(frozen=True, slots=True) 

28class Copying: 

29 """One of the five words, and a code for the line under it: why it stopped, what it waits 

30 for, or a warning while it still copies.""" 

31 

32 word: CopyingWord 

33 detail: str | None = None 

34 

35 

36def overall(state: str, error_code: str | None) -> Copying: 

37 """Copying as a whole, from the engine's processing state.""" 

38 match state: 

39 case "running": 

40 return Copying("copying") 

41 case "degraded": 

42 # No code means the start is still finishing; a code is a warning while it copies. 

43 return ( 

44 Copying("copying", error_code) if error_code else Copying("not_started", "starting") 

45 ) 

46 case "starting": 

47 return Copying("not_started", "starting") 

48 case "pausing": 

49 return Copying("copying", "pausing") 

50 case "failed": 

51 return Copying("stopped", error_code or "processing_unavailable") 

52 case _: 

53 return Copying("paused") 

54 

55 

56def account( 

57 *, 

58 processing_state: str | None, 

59 active_configuration: bool, 

60 entry_permission: str, 

61 readiness: str, 

62 error_code: str | None = None, 

63 run_state: str | None = None, 

64) -> Copying: 

65 """One account. `processing_state` is None when only the account's record is at hand, whose 

66 readiness already says when copying is paused.""" 

67 if not active_configuration: 

68 return Copying("not_started") 

69 if run_state == "failed" or readiness in UNHELD: 

70 return Copying("stopped", error_code if error_code in UNHELD else readiness) 

71 if processing_state == "failed": 

72 return Copying("stopped", "processing_unavailable") 

73 if processing_state in {"paused", "pausing"} or readiness == "processing_stopped": 

74 return Copying("paused") 

75 if entry_permission == "paused": 

76 return Copying("sells_only", "account_paused") 

77 if readiness == "manual_resume_required": 

78 return Copying("sells_only", "manual_resume_required") 

79 if entry_permission != "enabled": 

80 return Copying("not_started") 

81 match readiness: 

82 case "enabled": 

83 return Copying("copying") 

84 case "enabled_waiting_for_session" | "enabled_coins_only": 

85 return Copying("copying", readiness) 

86 case "recovery_pending" | "reconciliation_unavailable" | "session_unavailable": 

87 return Copying("copying", readiness) 

88 case _: 

89 return Copying("copying", readiness if readiness != "unavailable" else None)