Files
mulit-agent/mulit_agent/tests/test_controller_session.py
T

47 lines
1.7 KiB
Python

import pytest
from mulit_agent_app.config import AgentConfig
from mulit_agent_app.infrastructure.controller_session import ControllerSession
def test_https_address_is_converted_to_wss():
assert ControllerSession._websocket_url("https://192.168.1.10:8443") == "wss://192.168.1.10:8443"
def test_insecure_address_is_rejected():
with pytest.raises(ValueError):
ControllerSession._websocket_url("http://192.168.1.10:8443")
def test_current_socket_close_schedules_a_reconnect(tmp_path):
config = AgentConfig(
tmp_path,
tmp_path / "settings.json",
tmp_path / "agent.sqlite3",
tmp_path / "agent.log",
controller_url="wss://10.0.0.9:8443",
agent_id="agent-id",
certificate_fingerprint="fingerprint",
)
session = ControllerSession(config, object()) # type: ignore[arg-type]
socket = object()
scheduled: list[bool] = []
session._socket = socket # type: ignore[assignment]
session._schedule_reconnect = lambda: scheduled.append(True) # type: ignore[method-assign]
session._on_close(socket, None, None) # type: ignore[arg-type]
assert scheduled == [True]
def test_replaced_socket_close_does_not_schedule_an_extra_reconnect(tmp_path):
config = AgentConfig(tmp_path, tmp_path / "settings.json", tmp_path / "agent.sqlite3", tmp_path / "agent.log")
session = ControllerSession(config, object()) # type: ignore[arg-type]
scheduled: list[bool] = []
session._socket = object() # type: ignore[assignment]
session._schedule_reconnect = lambda: scheduled.append(True) # type: ignore[method-assign]
session._on_close(object(), None, None) # type: ignore[arg-type]
assert scheduled == []