"""Tests for file-based queues: pending_orders TTL and recent_sells cleanup.""" import json import time import src.common as common import src.order as order def test_pending_orders_ttl_cleanup(tmp_path, monkeypatch): pending_file = tmp_path / "pending_orders.json" monkeypatch.setattr(order, "PENDING_ORDERS_FILE", str(pending_file)) # Seed with stale entry (older than TTL 24h) stale_ts = time.time() - (25 * 3600) with open(pending_file, "w", encoding="utf-8") as f: json.dump([{"token": "old", "order": {}, "timestamp": stale_ts}], f) # Write new entry order._write_pending_order("new", {"x": 1}, pending_file=str(pending_file)) with open(pending_file, encoding="utf-8") as f: data = json.load(f) tokens = {entry["token"] for entry in data} assert "old" not in tokens # stale removed assert "new" in tokens def test_recent_sells_ttl_cleanup(tmp_path, monkeypatch): recent_file = tmp_path / "recent_sells.json" monkeypatch.setattr(common, "RECENT_SELLS_FILE", str(recent_file)) # Seed with stale entry older than 48h (2x default cooldown) stale_ts = time.time() - (49 * 3600) fresh_ts = time.time() - (1 * 3600) with open(recent_file, "w", encoding="utf-8") as f: json.dump({"KRW-BTC": stale_ts, "KRW-ETH": fresh_ts}, f) can_buy_eth = common.can_buy("KRW-ETH", cooldown_hours=24) can_buy_btc = common.can_buy("KRW-BTC", cooldown_hours=24) # ETH still in cooldown (fresh timestamp) assert can_buy_eth is False # BTC stale entry pruned -> allowed to buy assert can_buy_btc is True with open(recent_file, encoding="utf-8") as f: data = json.load(f) assert "KRW-BTC" not in data assert "KRW-ETH" in data