feat: add recommendation command and expand test coverage

This commit is contained in:
luofengyuan committed 2026-05-18 19:27:41 +08:00
1 parent 702280a416
commit f9267352cc
9 files changed
+684 -4

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
from __future__ import annotations
import io
import json
import sys
import contextlib
import importlib
def _run(mod, argv):
old = sys.argv[:]
try:
sys.argv = argv
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
rc = mod.main()
return rc, json.loads(buf.getvalue())
finally:
sys.argv = old
def test_discover_5sim_and_grizzly_branches(monkeypatch):
mod = importlib.import_module("scripts.sms_activation")
class FakeClient:
def _get(self, path):
assert path == "/v1/guest/countries"
return {"branch": "countries"}
def guest_products(self, country, operator):
return {"branch": "products", "country": country, "operator": operator}
def guest_prices(self, country, service):
return {"branch": "prices", "country": country, "service": service}
monkeypatch.setattr(mod, "get_client", lambda provider: FakeClient())
monkeypatch.setattr(mod, "read_state", lambda path: {})
rc, out = _run(mod, ["sms_activation.py", "--provider", "5sim", "discover"])
assert rc == 0 and out["branch"] == "countries"
rc, out = _run(
mod,
["sms_activation.py", "--provider", "5sim", "discover", "--country", "usa", "--operator", "any"],
)
assert rc == 0 and out["branch"] == "products"
rc, out = _run(
mod,
["sms_activation.py", "--provider", "5sim", "discover", "--country", "usa", "--service", "amazon"],
)
assert rc == 0 and out["branch"] == "prices"
# grizzly discover is note-style fallback branch
rc, out = _run(mod, ["sms_activation.py", "--provider", "grizzly", "discover"])
assert rc == 0
assert out["provider"] == "grizzly"
assert "limited" in out["note"]
def test_finish_cancel_ban_branches(monkeypatch):
mod = importlib.import_module("scripts.sms_activation")
class FiveClient:
def finish(self, order_id):
return {"op": "finish", "order_id": order_id}
def cancel(self, order_id):
return {"op": "cancel", "order_id": order_id}
def ban(self, order_id):
return {"op": "ban", "order_id": order_id}
class SmsClient:
def set_status(self, order_id, status):
return {"op": "set_status", "order_id": order_id, "status": status}
monkeypatch.setattr(mod, "read_state", lambda path: {})
monkeypatch.setattr(mod, "get_client", lambda provider: FiveClient())
rc, out = _run(mod, ["sms_activation.py", "--provider", "5sim", "finish", "--order-id", "1"])
assert rc == 0 and out["op"] == "finish"
rc, out = _run(mod, ["sms_activation.py", "--provider", "5sim", "cancel", "--order-id", "1"])
assert rc == 0 and out["op"] == "cancel"
rc, out = _run(mod, ["sms_activation.py", "--provider", "5sim", "ban", "--order-id", "1"])
assert rc == 0 and out["op"] == "ban"
monkeypatch.setattr(mod, "get_client", lambda provider: SmsClient())
rc, out = _run(mod, ["sms_activation.py", "--provider", "smsvn", "finish", "--order-id", "9"])
assert rc == 0 and out["status"] == 6
rc, out = _run(mod, ["sms_activation.py", "--provider", "smsvn", "cancel", "--order-id", "9"])
assert rc == 0 and out["status"] == 8
rc, out = _run(mod, ["sms_activation.py", "--provider", "smsvn", "ban", "--order-id", "9"])
assert rc == 0 and out["error"] == "ban_not_supported_by_default"