import pytest from starlette.websockets import WebSocketDisconnect from app.services.editor import python_lsp def test_python_editor_session(client, monkeypatch): session = python_lsp.PythonEditorSession( root_path=python_lsp.WORKSPACE_ROOT, file_path=python_lsp.WORKSPACE_ROOT / python_lsp.ENTRY_FILE, ) monkeypatch.setattr( "app.api.editor.router.python_lsp.lsp_binary", lambda: "/bin/lsp" ) monkeypatch.setattr( "app.api.editor.router.python_lsp.get_session", lambda: session ) response = client.get("/proxy/editor/python/session") assert response.status_code == 200 assert response.json() == { "root_uri": session.root_uri, "file_uri": session.file_uri, } def test_python_editor_socket_rejects_missing_token(client): with pytest.raises(WebSocketDisconnect) as error: with client.websocket_connect("/proxy/editor/python/ws"): pass assert error.value.code == 4401 def test_python_editor_socket_accepts_token(client, monkeypatch): accepted = {"value": False} async def fake_authenticate(_: object) -> None: return None async def fake_bridge(websocket, session) -> None: accepted["value"] = True await websocket.send_text(session.file_uri) await websocket.close() monkeypatch.setattr( "app.api.editor.router.authenticate_editor_socket", fake_authenticate, ) monkeypatch.setattr( "app.api.editor.router.python_lsp.lsp_binary", lambda: "/bin/lsp" ) monkeypatch.setattr("app.api.editor.router.python_lsp.bridge", fake_bridge) with client.websocket_connect("/proxy/editor/python/ws?token=test-token") as socket: assert socket.receive_text().endswith("/proxy_addon.py") assert accepted["value"] is True