|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import datetime, timezone |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from quant_platform_kit.common.reconciliation_recovery import ReconciliationRecoveryTransitionPlan |
| 8 | + |
| 9 | +from scripts.publish_reconciliation_recovery_state_ledger import archive_recovery_state_ledger |
| 10 | +from scripts.reconciliation_recovery_state_ledger import ( |
| 11 | + RECOVERY_STATE_LEDGER_SCHEMA_VERSION, |
| 12 | + build_recovery_state_ledger, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def _digest(character: str) -> str: |
| 17 | + return character * 64 |
| 18 | + |
| 19 | + |
| 20 | +def _verification() -> dict[str, object]: |
| 21 | + plan = ReconciliationRecoveryTransitionPlan( |
| 22 | + recovery_id="ibkr-soxl-live-recovery", |
| 23 | + candidate_sha256=_digest("a"), |
| 24 | + confirmation_sha256=_digest("b"), |
| 25 | + baseline_id="soxl-ibkr-lkg-20260830", |
| 26 | + baseline_target_sha256=_digest("c"), |
| 27 | + expected_digests={ |
| 28 | + "positions_sha256": _digest("d"), |
| 29 | + "cash_sha256": _digest("e"), |
| 30 | + "open_orders_sha256": _digest("f"), |
| 31 | + "recent_executions_sha256": _digest("0"), |
| 32 | + "local_execution_ledger_sha256": _digest("1"), |
| 33 | + }, |
| 34 | + verified_at=datetime(2026, 8, 31, 1, 5, tzinfo=timezone.utc), |
| 35 | + ) |
| 36 | + return { |
| 37 | + "schema_version": "ibkr_reconciliation_recovery_verification.v1", |
| 38 | + "recovery_id": plan.recovery_id, |
| 39 | + "candidate_sha256": plan.candidate_sha256, |
| 40 | + "confirmation_sha256": plan.confirmation_sha256, |
| 41 | + "ready_for_atomic_state_transition": True, |
| 42 | + "findings": [], |
| 43 | + "transition_plan": plan.to_dict(), |
| 44 | + "policy": { |
| 45 | + "controller_mode": "verify_only", |
| 46 | + "no_order": True, |
| 47 | + "execution_authority_granted": False, |
| 48 | + "state_write_attempted": False, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +def test_build_state_ledger_requires_a_complete_non_executable_verification() -> None: |
| 54 | + ledger = build_recovery_state_ledger( |
| 55 | + verification=_verification(), |
| 56 | + service_name="interactive-brokers-live-service", |
| 57 | + ) |
| 58 | + |
| 59 | + assert ledger["schema_version"] == RECOVERY_STATE_LEDGER_SCHEMA_VERSION |
| 60 | + assert ledger["service_name"] == "interactive-brokers-live-service" |
| 61 | + assert set(ledger) == {"schema_version", "recovery_id", "service_name", "transition_plan"} |
| 62 | + |
| 63 | + invalid = _verification() |
| 64 | + invalid["ready_for_atomic_state_transition"] = False |
| 65 | + with pytest.raises(ValueError, match="not ready"): |
| 66 | + build_recovery_state_ledger( |
| 67 | + verification=invalid, |
| 68 | + service_name="interactive-brokers-live-service", |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def test_archive_state_ledger_uses_create_only_private_state_prefix() -> None: |
| 73 | + ledger = build_recovery_state_ledger( |
| 74 | + verification=_verification(), |
| 75 | + service_name="interactive-brokers-live-service", |
| 76 | + ) |
| 77 | + received: dict[str, object] = {} |
| 78 | + |
| 79 | + class Blob: |
| 80 | + def upload_from_string(self, payload: str, **kwargs: object) -> None: |
| 81 | + received["payload"] = payload |
| 82 | + received["kwargs"] = kwargs |
| 83 | + |
| 84 | + class Bucket: |
| 85 | + @staticmethod |
| 86 | + def blob(name: str) -> Blob: |
| 87 | + received["object_name"] = name |
| 88 | + return Blob() |
| 89 | + |
| 90 | + class Client: |
| 91 | + @staticmethod |
| 92 | + def bucket(name: str) -> Bucket: |
| 93 | + received["bucket_name"] = name |
| 94 | + return Bucket() |
| 95 | + |
| 96 | + archive = archive_recovery_state_ledger( |
| 97 | + ledger, |
| 98 | + state_ledger_uri="gs://private-bucket/reconciliation-recovery/ibkr/state/recovery-1.json", |
| 99 | + storage_client_factory=Client, |
| 100 | + ) |
| 101 | + |
| 102 | + assert archive["uri"] == "gs://private-bucket/reconciliation-recovery/ibkr/state/recovery-1.json" |
| 103 | + assert received["bucket_name"] == "private-bucket" |
| 104 | + assert received["object_name"] == "reconciliation-recovery/ibkr/state/recovery-1.json" |
| 105 | + assert received["kwargs"] == { |
| 106 | + "content_type": "application/json", |
| 107 | + "if_generation_match": 0, |
| 108 | + } |
| 109 | + |
| 110 | + with pytest.raises(ValueError, match="state prefix"): |
| 111 | + archive_recovery_state_ledger( |
| 112 | + ledger, |
| 113 | + state_ledger_uri="gs://private-bucket/reconciliation-recovery/ibkr/source/recovery-1.json", |
| 114 | + storage_client_factory=Client, |
| 115 | + ) |
0 commit comments