Coverage for src/copytrading_engine/host/pipe/handlers/manual.py: 62%

46 statements  

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

1"""Manual corrections, orders, and lot sales: save, preview, confirm, and read back.""" 

2 

3from copytrading_engine.execution.domain.manual_commands import ManualCommandPageRequest 

4from copytrading_engine.host.pipe.requests import ( 

5 ConfirmLotSaleRequest, 

6 ConfirmManualOrdersRequest, 

7 GetManualCommandRequest, 

8 ListManualCommandsRequest, 

9 PipeRequest, 

10 PreviewLotSaleRequest, 

11 PreviewManualOrderRequest, 

12 RequestHandler, 

13 SaveManualCorrectionRequest, 

14) 

15from copytrading_engine.host.pipe.responses import reply 

16from copytrading_engine.host.pipe.services import TradingServices 

17 

18 

19def _copying_not_started(request: PipeRequest) -> bytes: 

20 """The by-hand steps need accounts that run; before Start Copying there are none.""" 

21 return reply( 

22 request.version, request.request_id, error="unavailable", reason="copying_not_started" 

23 ) 

24 

25 

26class ManualOrderHandlers: 

27 """Save corrections and preview, confirm, and read back manual orders.""" 

28 

29 def __init__(self, *, trading: TradingServices | None) -> None: 

30 self._trading = trading 

31 

32 def handlers(self) -> dict[type[PipeRequest], RequestHandler]: 

33 return { 

34 SaveManualCorrectionRequest: self._on_save_manual_correction, 

35 PreviewManualOrderRequest: self._on_preview_manual_order, 

36 ConfirmManualOrdersRequest: self._on_confirm_manual_orders, 

37 PreviewLotSaleRequest: self._on_preview_lot_sale, 

38 ConfirmLotSaleRequest: self._on_confirm_lot_sale, 

39 GetManualCommandRequest: self._on_get_manual_command, 

40 ListManualCommandsRequest: self._on_list_manual_commands, 

41 } 

42 

43 async def _on_save_manual_correction(self, request: SaveManualCorrectionRequest) -> bytes: 

44 if self._trading is None: 44 ↛ 45line 44 didn't jump to line 45 because the condition on line 44 was never true

45 return _copying_not_started(request) 

46 outcome = await self._trading.manual.save_manual_correction(request.correction) 

47 return reply( 

48 request.version, 

49 request.request_id, 

50 ok={"type": "manual_correction", "correction": outcome.model_dump(mode="json")}, 

51 ) 

52 

53 async def _on_preview_manual_order(self, request: PreviewManualOrderRequest) -> bytes: 

54 if self._trading is None: 

55 return _copying_not_started(request) 

56 preview = await self._trading.manual.preview_manual_order(request.preview) 

57 return reply( 

58 request.version, 

59 request.request_id, 

60 ok={"type": "manual_preview", "preview": preview.model_dump(mode="json")}, 

61 ) 

62 

63 async def _on_confirm_manual_orders(self, request: ConfirmManualOrdersRequest) -> bytes: 

64 if self._trading is None: 64 ↛ 65line 64 didn't jump to line 65 because the condition on line 64 was never true

65 return _copying_not_started(request) 

66 outcome = await self._trading.manual.confirm_manual_orders(tuple(request.commands)) 

67 return reply( 

68 request.version, 

69 request.request_id, 

70 ok={"type": "manual_commands", "commands": outcome.model_dump(mode="json")}, 

71 ) 

72 

73 async def _on_preview_lot_sale(self, request: PreviewLotSaleRequest) -> bytes: 

74 if self._trading is None: 

75 return reply(request.version, request.request_id, error="unavailable") 

76 preview = await self._trading.manual.preview_lot_sale(request.preview) 

77 return reply( 

78 request.version, 

79 request.request_id, 

80 ok={"type": "lot_sale_preview", "preview": preview.model_dump(mode="json")}, 

81 ) 

82 

83 async def _on_confirm_lot_sale(self, request: ConfirmLotSaleRequest) -> bytes: 

84 if self._trading is None: 

85 return reply(request.version, request.request_id, error="unavailable") 

86 result = await self._trading.manual.confirm_lot_sale(request.sale) 

87 return reply( 

88 request.version, 

89 request.request_id, 

90 ok={"type": "lot_sale", "sale": result.model_dump(mode="json")}, 

91 ) 

92 

93 async def _on_get_manual_command(self, request: GetManualCommandRequest) -> bytes: 

94 if self._trading is None: 94 ↛ 95line 94 didn't jump to line 95 because the condition on line 94 was never true

95 return _copying_not_started(request) 

96 result = await self._trading.manual.manual_command_result( 

97 request.account_id, request.command_id 

98 ) 

99 return reply( 

100 request.version, 

101 request.request_id, 

102 ok={"type": "manual_command", "command": result.model_dump(mode="json")}, 

103 ) 

104 

105 async def _on_list_manual_commands(self, request: ListManualCommandsRequest) -> bytes: 

106 if self._trading is None: 106 ↛ 107line 106 didn't jump to line 107 because the condition on line 106 was never true

107 return _copying_not_started(request) 

108 page = await self._trading.operator.list_manual_commands( 

109 ManualCommandPageRequest( 

110 account_id=request.account_id, 

111 source_id=request.source_id, 

112 before_command_id=request.before_command_id, 

113 limit=request.limit, 

114 ) 

115 ) 

116 return reply( 

117 request.version, 

118 request.request_id, 

119 ok={"type": "manual_command_page", "commands": page.model_dump(mode="json")}, 

120 )