修复平仓后查仓失败仍卖币,并恢复期权说明页。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-07 16:19:18 +08:00
parent a23fb7cbdc
commit a45b5d1b0b
4 changed files with 222 additions and 14 deletions
+60
View File
@@ -35,5 +35,65 @@ class TestSellCapLogic(unittest.TestCase):
self.assertAlmostEqual(sell_amt, 0.006)
class TestMaybeSellAfterCloseGuard(unittest.TestCase):
"""平仓后卖回:持仓查询失败/空响应绝不卖币."""
def test_none_positions_no_sell(self):
from lib.options.options_coin_open_lib import maybe_sell_spot_after_close
called = {"sell": 0}
def _fetch(_ex):
return None
cfg = {
"fetch_option_positions": _fetch,
"get_db": lambda: (_ for _ in ()).throw(AssertionError("不应开库卖币")),
}
out = maybe_sell_spot_after_close(
cfg, object(), inst_id="ETH-USD-260701-2500-C"
)
self.assertIsInstance(out, dict)
self.assertFalse(out.get("ok"))
self.assertTrue(out.get("skipped"))
self.assertIn("空响应", str(out.get("msg") or ""))
self.assertEqual(called["sell"], 0)
def test_fetch_raises_no_sell(self):
from lib.options.options_coin_open_lib import maybe_sell_spot_after_close
def _fetch(_ex):
raise RuntimeError("network down")
cfg = {
"fetch_option_positions": _fetch,
"get_db": lambda: (_ for _ in ()).throw(AssertionError("不应开库卖币")),
}
out = maybe_sell_spot_after_close(
cfg, object(), inst_id="ETH-USD-260701-2500-C"
)
self.assertIsInstance(out, dict)
self.assertFalse(out.get("ok"))
self.assertTrue(out.get("skipped"))
self.assertIn("持仓查询失败", str(out.get("msg") or ""))
def test_still_open_skips_sell(self):
from lib.options.options_coin_open_lib import maybe_sell_spot_after_close
inst = "ETH-USD-260701-2500-C"
def _fetch(_ex):
return [{"instId": inst, "pos": "1"}]
cfg = {
"fetch_option_positions": _fetch,
"get_db": lambda: (_ for _ in ()).throw(AssertionError("有仓不应卖币")),
}
out = maybe_sell_spot_after_close(cfg, object(), inst_id=inst)
self.assertTrue(out.get("ok"))
self.assertTrue(out.get("skipped"))
self.assertIn("仍有持仓", str(out.get("msg") or ""))
if __name__ == "__main__":
unittest.main()