Coverage for src/copytrading_engine/execution/adapters/restore_preparer.py: 80%
24 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"""Apply the durable manual-disabled transition to an isolated restored ledger."""
3from __future__ import annotations
5import datetime as dt
6import uuid
7from pathlib import Path
9from copytrading_engine.execution.adapters.sqlite_ledger import Store
10from copytrading_engine.execution.application.ledger import TradingLedger
11from copytrading_engine.execution.application.ports import NoOpObserver
12from copytrading_engine.execution.domain.lifecycle import AccountControlCommand
15def prepare_restored_account(path: Path) -> None:
16 """Persist an audited disabled/manual control before a candidate can be activated."""
17 store = Store(path)
18 try:
19 row = store.db.execute(
20 "SELECT environment, account_id FROM identity WHERE singleton=1"
21 ).fetchone()
22 if row is None or row[0] not in {"paper", "live"} or not isinstance(row[1], str): 22 ↛ 23line 22 didn't jump to line 23 because the condition on line 22 was never true
23 raise ValueError("restored account identity is incomplete")
24 environment, account_id = row
25 store.bind_identity(account_id, environment)
26 ledger = TradingLedger(store, NoOpObserver())
27 result = ledger.account_control(
28 AccountControlCommand(
29 command_id=str(uuid.uuid4()),
30 account_id=account_id,
31 action="restore_manual",
32 ),
33 dt.datetime.now(dt.UTC),
34 local_account_id=account_id,
35 )
36 if (result.entry_permission, result.recovery_preference) != ("disabled", "manual"): 36 ↛ 37line 36 didn't jump to line 37 because the condition on line 36 was never true
37 raise RuntimeError("restored account did not enter manual-disabled state")
38 checkpoint = store.db.execute("PRAGMA wal_checkpoint(TRUNCATE)").fetchone()
39 if checkpoint is None or checkpoint[0] != 0: 39 ↛ 40line 39 didn't jump to line 40 because the condition on line 39 was never true
40 raise RuntimeError("restored account journal could not be checkpointed")
41 finally:
42 store.close()