Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _runtime_error_notification_message(exc: Exception) -> str:
error_text = f"{type(exc).__name__}: {exc}"
if len(error_text) > 1200:
error_text = error_text[:1197] + "..."
is_health_check = request.path in {"/session-check", "/probe"}
is_health_check = request.path == "/probe"
if str(os.getenv("NOTIFY_LANG") or "").strip().lower().startswith("zh"):
return "\n".join(
(
Expand Down Expand Up @@ -348,8 +348,6 @@ def smoke():
return jsonify({"ok": False, "error": str(exc)}), 500


@app.post("/session-check")
@app.get("/session-check")
def session_check():
if not _flag("FIRSTRADE_RUN_SESSION_CHECK_ON_HTTP"):
return (
Expand Down Expand Up @@ -392,7 +390,6 @@ def session_check():
)


@app.post("/")
@app.post("/run")
@app.get("/run")
def run_strategy():
Expand Down Expand Up @@ -437,11 +434,9 @@ def run_strategy():
)


@app.post("/precheck")
@app.get("/precheck")
@app.post("/dry-run")
@app.get("/dry-run")
def precheck():
def dry_run():
try:
return jsonify(
_run_strategy_cycle_with_report(
Expand Down
49 changes: 7 additions & 42 deletions tests/test_request_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ def route_methods():

def test_cloud_run_route_contracts_are_registered():
assert route_methods() == {
"/": ["GET", "POST"],
"/": ["GET"],
"/profiles": ["GET"],
"/smoke": ["GET"],
"/session-check": ["GET", "POST"],
"/run": ["GET", "POST"],
"/precheck": ["GET", "POST"],
"/dry-run": ["GET", "POST"],
"/probe": ["GET", "POST"],
"/static/<path:filename>": ["GET"],
Expand Down Expand Up @@ -68,17 +66,17 @@ def test_run_endpoint_calls_strategy_cycle_when_gate_enabled(monkeypatch):
assert response.get_json() == {"ok": True, "action_done": False}


def test_session_check_endpoint_is_disabled_without_explicit_http_gate(monkeypatch):
def test_probe_endpoint_is_disabled_without_explicit_http_gate(monkeypatch):
monkeypatch.delenv("FIRSTRADE_RUN_SESSION_CHECK_ON_HTTP", raising=False)
client = main.app.test_client()

response = client.get("/session-check")
response = client.get("/probe")

assert response.status_code == 403
assert response.get_json()["ok"] is False


def test_session_check_endpoint_calls_service_when_gate_enabled(monkeypatch):
def test_probe_endpoint_calls_service_when_gate_enabled(monkeypatch):
monkeypatch.setenv("FIRSTRADE_RUN_SESSION_CHECK_ON_HTTP", "true")
sent_messages = []
monkeypatch.setattr(
Expand All @@ -89,7 +87,7 @@ def test_session_check_endpoint_calls_service_when_gate_enabled(monkeypatch):
monkeypatch.setattr(main, "build_sender", lambda *_args, **_kwargs: sent_messages.append)
client = main.app.test_client()

response = client.post("/session-check")
response = client.post("/probe")

assert response.status_code == 200
assert response.get_json() == {
Expand All @@ -100,26 +98,7 @@ def test_session_check_endpoint_calls_service_when_gate_enabled(monkeypatch):
assert sent_messages == []


def test_probe_alias_calls_session_check_service_when_gate_enabled(monkeypatch):
monkeypatch.setenv("FIRSTRADE_RUN_SESSION_CHECK_ON_HTTP", "true")
monkeypatch.setattr(
main,
"run_session_check",
lambda: {"ok": True, "session_reused": False, "snapshot_persisted": True},
)
client = main.app.test_client()

response = client.post("/probe")

assert response.status_code == 200
assert response.get_json() == {
"ok": True,
"session_reused": False,
"snapshot_persisted": True,
}


def test_session_check_endpoint_notifies_only_on_error(monkeypatch):
def test_probe_endpoint_notifies_only_on_error(monkeypatch):
monkeypatch.setenv("FIRSTRADE_RUN_SESSION_CHECK_ON_HTTP", "true")
monkeypatch.setenv("TELEGRAM_TOKEN", "token-1")
monkeypatch.setenv("GLOBAL_TELEGRAM_CHAT_ID", "chat-1")
Expand All @@ -139,7 +118,7 @@ def send(message):
monkeypatch.setattr(main, "build_sender", fake_build_sender)
client = main.app.test_client()

response = client.post("/session-check")
response = client.post("/probe")

assert response.status_code == 500
payload = response.get_json()
Expand All @@ -152,17 +131,6 @@ def send(message):
assert "RuntimeError: session denied" in sent_messages[0][2]


def test_root_post_calls_strategy_cycle_when_gate_enabled(monkeypatch):
monkeypatch.setenv("FIRSTRADE_RUN_STRATEGY_ON_HTTP", "true")
monkeypatch.setattr(main, "_run_strategy_cycle_with_report", lambda **_kwargs: {"ok": True, "action_done": False})
client = main.app.test_client()

response = client.post("/")

assert response.status_code == 200
assert response.get_json() == {"ok": True, "action_done": False}


def test_run_endpoint_notifies_telegram_on_strategy_cycle_error(monkeypatch):
sent_messages = []

Expand Down Expand Up @@ -266,12 +234,9 @@ def fake_run_strategy_cycle_with_report(**kwargs):
monkeypatch.setattr(main, "run_session_check", lambda: {"ok": True, "session_reused": True})
client = main.app.test_client()

precheck_response = client.post("/precheck")
dry_run_response = client.post("/dry-run")
probe_response = client.post("/probe")

assert precheck_response.status_code == 200
assert precheck_response.get_json()["ok"] is True
assert dry_run_response.status_code == 200
assert dry_run_response.get_json()["ok"] is True
assert observed == {
Expand Down