feat(ralph): interactive UX layer pe Discord și Telegram (W1)
Adaugă straturile interactive peste slash commands flat: **Discord (`src/adapters/discord_views.py`):** - `RalphRootView` — listă proiecte workspace cu emoji status + Refresh + Close - `RalphProjectView` — Propose / Vezi PRD / Aprobă tonight / Status / Stop / Înapoi - `RalphProposeModal` — TextInput pentru descriere feature - Critical pattern: `await interaction.response.defer(ephemeral=True)` în orice button callback cu I/O (eng review concern #2 — "Discord 3s timeout") - `/p slug` autocomplete din `~/workspace/` - `/l` afișează `RalphRootView` ephemeral **Telegram (`src/adapters/telegram_bot.py`):** - `cmd_ralph_l` (fără arg) trimite `InlineKeyboardMarkup` cu workspace + active - `callback_ralph` cu pattern `^ralph:` rutează: project, menu, refresh, close, propose, prd, status, approve, stop - Pentru "Propose feature" → set ralph_flow state cu step=input_description + `ForceReply()`; `handle_message` detectează state și rutează la `_ralph_propose` - Pasează `adapter_name="telegram"` la `route_message` **State management (`src/ralph_flow.py`):** - Atomic JSON peste `sessions/ralph_flow.json` (pattern reusat din claude_session) - Schema per (adapter, chat, user): `{step, project?, expires_at, ...}` - TTL 10 min default; `cleanup_expired()` și auto-drop la `get_state` pe expirate **Router (`src/router.py`):** - `route_message` primește `adapter_name` keyword arg - `_maybe_whatsapp_redirect` adaugă "💡 Pentru meniu interactiv folosește Discord sau Telegram" la mesajele de usage când adapter_name="whatsapp" - WhatsApp `_handle_chat` pasează `adapter_name="whatsapp"` **Tests:** - `test_ralph_flow.py` — 10 teste (round-trip, isolation, expiry, atomic write) - `test_router.py::TestRalphDispatch` — 3 teste (whatsapp redirect, discord no-redirect, usage message) Foundation pentru W2 (planning agent — STEP_IN_PLANNING reservat). Spike Step 0 PASS: skill subprocess + AskUserQuestion→text serialization confirmat empiric (vezi tasks/spike-planning-findings.md). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
108
tests/test_ralph_flow.py
Normal file
108
tests/test_ralph_flow.py
Normal file
@@ -0,0 +1,108 @@
|
||||
"""Tests for src/ralph_flow.py — short-lived per-user UX state."""
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import pytest
|
||||
|
||||
from src import ralph_flow
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _isolate_state_file(tmp_path, monkeypatch):
|
||||
"""Redirect state file to a tmp location for each test."""
|
||||
monkeypatch.setattr(ralph_flow, "_STATE_FILE", tmp_path / "ralph_flow.json")
|
||||
monkeypatch.setattr(ralph_flow, "SESSIONS_DIR", tmp_path)
|
||||
|
||||
|
||||
def test_get_state_returns_none_when_absent():
|
||||
assert ralph_flow.get_state("discord", "c1", "u1") is None
|
||||
|
||||
|
||||
def test_set_then_get_round_trip():
|
||||
ralph_flow.set_state(
|
||||
"discord", "c1", "u1",
|
||||
step=ralph_flow.STEP_INPUT_DESCRIPTION,
|
||||
project="roa2web",
|
||||
)
|
||||
state = ralph_flow.get_state("discord", "c1", "u1")
|
||||
assert state is not None
|
||||
assert state["step"] == ralph_flow.STEP_INPUT_DESCRIPTION
|
||||
assert state["project"] == "roa2web"
|
||||
assert "expires_at" in state
|
||||
|
||||
|
||||
def test_clear_state_removes_entry():
|
||||
ralph_flow.set_state("telegram", "42", "7", step="input_description")
|
||||
assert ralph_flow.clear_state("telegram", "42", "7") is True
|
||||
assert ralph_flow.get_state("telegram", "42", "7") is None
|
||||
# Second clear is a no-op
|
||||
assert ralph_flow.clear_state("telegram", "42", "7") is False
|
||||
|
||||
|
||||
def test_state_keyed_by_adapter_chat_user():
|
||||
"""Different adapters / chats / users have isolated state."""
|
||||
ralph_flow.set_state("discord", "c1", "u1", step="input_description", project="A")
|
||||
ralph_flow.set_state("telegram", "c1", "u1", step="input_description", project="B")
|
||||
ralph_flow.set_state("discord", "c2", "u1", step="input_description", project="C")
|
||||
ralph_flow.set_state("discord", "c1", "u2", step="input_description", project="D")
|
||||
|
||||
assert ralph_flow.get_state("discord", "c1", "u1")["project"] == "A"
|
||||
assert ralph_flow.get_state("telegram", "c1", "u1")["project"] == "B"
|
||||
assert ralph_flow.get_state("discord", "c2", "u1")["project"] == "C"
|
||||
assert ralph_flow.get_state("discord", "c1", "u2")["project"] == "D"
|
||||
|
||||
|
||||
def test_expired_state_returns_none_and_self_cleans(monkeypatch):
|
||||
"""get_state on an expired entry should return None and drop the entry."""
|
||||
# Set with 0s TTL — already expired
|
||||
ralph_flow.set_state(
|
||||
"discord", "c1", "u1",
|
||||
step="input_description",
|
||||
ttl_seconds=0,
|
||||
)
|
||||
assert ralph_flow.get_state("discord", "c1", "u1") is None
|
||||
# Verify entry was dropped from disk
|
||||
assert ralph_flow._load() == {}
|
||||
|
||||
|
||||
def test_cleanup_expired_drops_only_expired():
|
||||
ralph_flow.set_state("discord", "c1", "u1", step="x", ttl_seconds=0) # expired
|
||||
ralph_flow.set_state("discord", "c2", "u2", step="y", ttl_seconds=600) # fresh
|
||||
|
||||
dropped = ralph_flow.cleanup_expired()
|
||||
|
||||
assert dropped == 1
|
||||
assert ralph_flow.get_state("discord", "c1", "u1") is None
|
||||
assert ralph_flow.get_state("discord", "c2", "u2") is not None
|
||||
|
||||
|
||||
def test_set_state_overwrites_previous():
|
||||
ralph_flow.set_state("discord", "c1", "u1", step="step_a", project="P1")
|
||||
ralph_flow.set_state("discord", "c1", "u1", step="step_b", project="P2")
|
||||
state = ralph_flow.get_state("discord", "c1", "u1")
|
||||
assert state["step"] == "step_b"
|
||||
assert state["project"] == "P2"
|
||||
|
||||
|
||||
def test_set_state_extras_propagate():
|
||||
ralph_flow.set_state(
|
||||
"discord", "c1", "u1",
|
||||
step="x",
|
||||
custom_field="hello",
|
||||
nested={"a": 1},
|
||||
)
|
||||
state = ralph_flow.get_state("discord", "c1", "u1")
|
||||
assert state["custom_field"] == "hello"
|
||||
assert state["nested"] == {"a": 1}
|
||||
|
||||
|
||||
def test_corrupted_state_file_returns_empty(tmp_path):
|
||||
"""If state file is corrupt JSON, _load returns {} so get_state stays robust."""
|
||||
ralph_flow._STATE_FILE.write_text("not json {")
|
||||
assert ralph_flow.get_state("discord", "c1", "u1") is None
|
||||
|
||||
|
||||
def test_atomic_write_does_not_leave_temp_files(tmp_path):
|
||||
ralph_flow.set_state("discord", "c1", "u1", step="x")
|
||||
leftovers = [p for p in tmp_path.iterdir() if p.name.startswith(".ralph_flow_")]
|
||||
assert leftovers == []
|
||||
@@ -134,6 +134,31 @@ class TestStatusCommand:
|
||||
assert is_cmd is True
|
||||
|
||||
|
||||
# --- Ralph command dispatch ---
|
||||
|
||||
|
||||
class TestRalphDispatch:
|
||||
def test_p_without_args_returns_usage(self):
|
||||
response, is_cmd = route_message("ch-1", "user-1", "/p")
|
||||
assert "Folosire: /p" in response
|
||||
assert is_cmd is True
|
||||
|
||||
def test_whatsapp_appends_redirect_hint_on_usage(self):
|
||||
"""WhatsApp users see a redirect line pointing them to Discord/TG."""
|
||||
response, is_cmd = route_message(
|
||||
"ch-1", "user-1", "/p", adapter_name="whatsapp"
|
||||
)
|
||||
assert "Folosire: /p" in response
|
||||
assert "Discord sau Telegram" in response
|
||||
|
||||
def test_discord_does_not_get_whatsapp_redirect(self):
|
||||
response, is_cmd = route_message(
|
||||
"ch-1", "user-1", "/p", adapter_name="discord"
|
||||
)
|
||||
assert "Folosire: /p" in response
|
||||
assert "Discord sau Telegram" not in response
|
||||
|
||||
|
||||
# --- Unknown command ---
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user