"""WebSocket end-to-end episode test. Confirms the canonical OpenEnv multi-step transport works: one WebSocket connection == one episode == one persistent env on the server side. """ import json from fastapi.testclient import TestClient from server.app import app def test_websocket_full_episode(): client = TestClient(app) with client.websocket_connect("/ws") as ws: # Reset ws.send_text(json.dumps({ "type": "reset", "data": {"task_id": "github_pat_token_to_bearer"}, })) raw = json.loads(ws.receive_text()) payload = raw.get("data", raw) obs = payload.get("observation", payload) assert obs["scenario_id"] == "github_pat_token_to_bearer" plan = [ {"command": "dispatch_diff", "rationale": "diff first"}, {"command": "dispatch_patch", "payload": {"change_id": "ch_001", "file_path": "github_client.py"}, "rationale": "patch"}, {"command": "dispatch_test", "rationale": "test"}, {"command": "dispatch_rollback", "rationale": "rb"}, {"command": "submit", "rationale": "finalize"}, ] last_reward = None last_done = False for action in plan: ws.send_text(json.dumps({"type": "step", "data": action})) raw = json.loads(ws.receive_text()) payload = raw.get("data", raw) obs = payload.get("observation", payload) last_reward = payload.get("reward", obs.get("reward")) last_done = payload.get("done", obs.get("done")) if last_done: break assert last_done is True assert last_reward is not None assert last_reward > 0.6 def test_websocket_state_endpoint(): client = TestClient(app) with client.websocket_connect("/ws") as ws: ws.send_text(json.dumps({"type": "reset", "data": {"task_id": "github_pat_token_to_bearer"}})) ws.receive_text() ws.send_text(json.dumps({"type": "state", "data": {}})) raw = json.loads(ws.receive_text()) st = raw.get("data", raw) # State should at minimum include scenario_id (from APIShiftState) assert isinstance(st, dict)