|
| 1 | +import json |
| 2 | +from types import SimpleNamespace |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from rlm.v100 import paper_learning, spot_bootstrap |
| 7 | +from rlm.v100.common import atomic_json |
| 8 | +from tests.test_v100_paper_learning import settings |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def observer(tmp_path, monkeypatch): |
| 13 | + from rlm.v100 import market_adapters, provider_registry, reward_policy |
| 14 | + |
| 15 | + ledger = tmp_path / "research/paper/ledger.sqlite3" |
| 16 | + ledger.parent.mkdir(parents=True) |
| 17 | + ledger.touch() |
| 18 | + state = { |
| 19 | + "instruments": { |
| 20 | + "BTC": { |
| 21 | + "feed_id": "kraken:XBTUSD", |
| 22 | + "fee_profile": "old", |
| 23 | + "market": "crypto", |
| 24 | + "product": "spot", |
| 25 | + } |
| 26 | + }, |
| 27 | + "fee_profiles": {"old": {"valid_until": "2020-01-01T00:00:00+00:00"}}, |
| 28 | + } |
| 29 | + notes, quotes = [], [] |
| 30 | + |
| 31 | + class Book: |
| 32 | + def __init__(self, root): |
| 33 | + pass |
| 34 | + |
| 35 | + def state(self): |
| 36 | + return state |
| 37 | + |
| 38 | + def note(self, branch, payload): |
| 39 | + notes.append(payload) |
| 40 | + |
| 41 | + def close(self): |
| 42 | + pass |
| 43 | + |
| 44 | + monkeypatch.setattr(paper_learning, "PaperBook", Book) |
| 45 | + monkeypatch.setattr(paper_learning, "poll_crypto", lambda *a: quotes.append("price")) |
| 46 | + monkeypatch.setattr(provider_registry, "poll", lambda *a: None) |
| 47 | + monkeypatch.setattr(market_adapters, "poll", lambda *a: None) |
| 48 | + monkeypatch.setattr(reward_policy, "train", lambda *a: {"state": "no labels"}) |
| 49 | + monkeypatch.setattr(paper_learning, "scheduled_reports", lambda *a: []) |
| 50 | + now = [1791748000.0] |
| 51 | + monkeypatch.setattr(paper_learning, "time", SimpleNamespace(time=lambda: now[0])) |
| 52 | + config = tmp_path / "settings.json" |
| 53 | + atomic_json(config, {**settings(), "crypto": True}) |
| 54 | + session = paper_learning.PaperLearning(tmp_path, config) |
| 55 | + return session, state, notes, quotes, now |
| 56 | + |
| 57 | + |
| 58 | +def test_fee_source_failure_preserves_observations_and_backs_off(observer, monkeypatch): |
| 59 | + session, state, notes, quotes, now = observer |
| 60 | + calls = [] |
| 61 | + |
| 62 | + def unavailable(*args, **kwargs): |
| 63 | + calls.append(1) |
| 64 | + raise spot_bootstrap.FeeSourceUnavailable("Archived unrecognized fee table") |
| 65 | + |
| 66 | + monkeypatch.setattr(spot_bootstrap, "prepare", unavailable) |
| 67 | + session.tick() |
| 68 | + session.tick() |
| 69 | + session.check() |
| 70 | + assert calls == [1] and quotes == ["price", "price"] |
| 71 | + assert state["fee_profiles"]["old"]["valid_until"] == "2020-01-01T00:00:00+00:00" |
| 72 | + assert session.failure is None and notes[0]["status"] == "fee-refresh-blocked" |
| 73 | + assert session.fee_retry_after == now[0] + 3600 |
| 74 | + now[0] += 3601 |
| 75 | + session.tick() |
| 76 | + assert len(calls) == 2 |
| 77 | + |
| 78 | + |
| 79 | +def test_non_fee_integrity_errors_are_not_downgraded(observer, monkeypatch): |
| 80 | + session, _, _, quotes, _ = observer |
| 81 | + |
| 82 | + def invalid(*a, **k): |
| 83 | + raise ValueError("Spot execution rules changed") |
| 84 | + |
| 85 | + monkeypatch.setattr(spot_bootstrap, "prepare", invalid) |
| 86 | + with pytest.raises(ValueError, match="rules changed"): |
| 87 | + session.tick() |
| 88 | + assert not quotes |
| 89 | + |
| 90 | + |
| 91 | +def test_repaired_fee_source_is_retried_without_resetting_session(observer, monkeypatch): |
| 92 | + session, _, _, quotes, now = observer |
| 93 | + calls = [] |
| 94 | + |
| 95 | + def refresh(*a, **k): |
| 96 | + calls.append(1) |
| 97 | + if len(calls) == 1: |
| 98 | + raise spot_bootstrap.FeeSourceUnavailable("Temporary layout failure") |
| 99 | + return {"status": "paper spot sources configured"} |
| 100 | + |
| 101 | + monkeypatch.setattr(spot_bootstrap, "prepare", refresh) |
| 102 | + session.tick() |
| 103 | + now[0] += 3601 |
| 104 | + session.tick() |
| 105 | + session.check() |
| 106 | + assert len(calls) == 2 and len(quotes) == 2 and session.failure is None |
| 107 | + |
| 108 | + |
| 109 | +def test_public_fee_locale_and_redirect_receipt_are_exact(tmp_path, monkeypatch): |
| 110 | + state = {"currency": "PLN", "instruments": {}, "fee_profiles": {}} |
| 111 | + |
| 112 | + class Book: |
| 113 | + def __init__(self, root): |
| 114 | + pass |
| 115 | + |
| 116 | + def state(self): |
| 117 | + return state |
| 118 | + |
| 119 | + def close(self): |
| 120 | + pass |
| 121 | + |
| 122 | + monkeypatch.setattr(spot_bootstrap, "PaperBook", Book) |
| 123 | + seen = [] |
| 124 | + |
| 125 | + def page(url, **kwargs): |
| 126 | + seen.append((url, kwargs)) |
| 127 | + return "https://www.kraken.com/features/fee-schedule", "<p>Unrecognized publication</p>" |
| 128 | + |
| 129 | + monkeypatch.setattr(spot_bootstrap, "download_page", page) |
| 130 | + with pytest.raises(spot_bootstrap.FeeSourceUnavailable, match="archived source"): |
| 131 | + spot_bootstrap.prepare(tmp_path, refresh=True) |
| 132 | + assert seen[0][1]["language"] == "en-US" |
| 133 | + receipt = json.loads((tmp_path / "research/paper/fee-source-status.json").read_text()) |
| 134 | + assert receipt["source_url"] == "https://www.kraken.com/features/fee-schedule" |
| 135 | + assert receipt["state"] == "blocked" |
0 commit comments