95 lines
4.3 KiB
Python
95 lines
4.3 KiB
Python
"""Autonomous search: coin rotation, leaders first, simulator connections, history."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
|
|
import autoscan as au # noqa: E402
|
|
import leadlag as ll # noqa: E402
|
|
|
|
|
|
def target(symbol, cex="MEXC", chain="solana", cex_symbol=None):
|
|
return ll.Target(symbol, symbol.lower(), cex, cex_symbol or f"{symbol}_USDT", 1.0, chain, chain, f"POOL{symbol}",
|
|
"TOKEN", "pumpswap", 500)
|
|
|
|
|
|
def result(symbol, verdict=au.LEAD_VERDICT):
|
|
return ll.LeadResult(symbol, "MEXC", "solana/pumpswap", 900, 100, 100, 30, {0: 0.1}, 2, 0.0, {5: 0.1},
|
|
{5: 0.6}, 2.5, 70.0, 0, verdict)
|
|
|
|
|
|
class AutoscanTests(unittest.TestCase):
|
|
def test_rotation_covers_everyone_and_leaders_come_first(self):
|
|
cands = [target(s) for s in "ABCDEFG"]
|
|
first, off = au.choose_targets(cands, [], 0, 3)
|
|
second, off = au.choose_targets(cands, [], off, 3)
|
|
third, off = au.choose_targets(cands, [], off, 3)
|
|
seen = [t.symbol for t in first + second + third]
|
|
self.assertEqual(set(seen), set("ABCDEFG"))
|
|
with_leader, _ = au.choose_targets(cands, ["F"], 0, 3)
|
|
self.assertEqual(with_leader[0].symbol, "F")
|
|
self.assertEqual(len({t.symbol for t in with_leader}), 3)
|
|
|
|
def test_simulator_connection_mapping(self):
|
|
conn = au.simulator_connection(target("CLAUDIA"))
|
|
self.assertEqual(conn, {"lead_market": "raydium", "lead_ticker": "CLAUDIAUSDTM", "lead_pool": "POOLCLAUDIA",
|
|
"follow_exchange": "mexc", "follow_ticker": "CLAUDIA_USDT"})
|
|
self.assertEqual(au.simulator_connection(target("X", cex="Binance", cex_symbol="1000XUSDT"))["follow_ticker"],
|
|
"1000XUSDT")
|
|
self.assertIsNone(au.simulator_connection(target("X", cex="Gate")))
|
|
self.assertIsNone(au.simulator_connection(target("X", chain="bsc")))
|
|
|
|
def test_add_connection_keeps_existing_and_skips_duplicates(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder) / "lead_connections.json"
|
|
old = {"lead_market": "futures", "lead_ticker": "MARSCOINUSDTM", "lead_pool": "", "follow_exchange": "bybit",
|
|
"follow_ticker": "MARSCOINUSDT"}
|
|
path.write_text(json.dumps([old]), encoding="utf-8")
|
|
conn = au.simulator_connection(target("SI"))
|
|
self.assertTrue(au.add_connection(conn, path))
|
|
self.assertFalse(au.add_connection(conn, path))
|
|
saved = json.loads(path.read_text(encoding="utf-8"))
|
|
self.assertEqual(saved, [old, conn])
|
|
|
|
def test_loop_survives_the_recording_and_errors(self):
|
|
"""record() sets the stop event it is given; the loop must keep running for the next cycle."""
|
|
import threading
|
|
from unittest import mock
|
|
calls = {"n": 0}
|
|
loop_stop = threading.Event()
|
|
|
|
def fake_cycle(st, *args):
|
|
calls["n"] += 1
|
|
given = threading.Event()
|
|
given.set() # what record() does with its own event
|
|
if calls["n"] == 2:
|
|
raise RuntimeError("boom") # an error in a cycle is logged, not fatal
|
|
if calls["n"] >= 3:
|
|
loop_stop.set()
|
|
|
|
with tempfile.TemporaryDirectory() as folder, \
|
|
mock.patch.object(au, "_cycle", fake_cycle), mock.patch.object(au, "LOG_FILE", Path(folder) / "l.log"), \
|
|
mock.patch.object(loop_stop, "wait", lambda s: None):
|
|
au.run(1, 1, 1, 0.15, stop=loop_stop)
|
|
self.assertEqual(calls["n"], 3)
|
|
|
|
def test_history_and_leaders(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder) / "h.csv"
|
|
targets = {s: target(s) for s in ("A", "B", "C")}
|
|
au.append_history(1, [result("A"), result("B", "одновременно / нет опережения")], targets, {"A": "да"}, path)
|
|
au.append_history(2, [result("C")], targets, {}, path)
|
|
self.assertEqual(set(au.read_leaders(path)), {"A", "C"})
|
|
self.assertEqual(au.last_cycle(path), 2)
|
|
self.assertIn("да", path.read_text(encoding="utf-8-sig"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|