Files
OpenJarvis/tests/channels/test_mattermost.py
T
Jon Saad-FalconandClaude Opus 4.6 8d538cd1b0 Add orchestrator training, channels, LiteLLM engine, and simplify learning taxonomy
Major changes across parallel sessions:

- Add orchestrator SFT & GRPO training subpackage (learning/orchestrator/)
  with episode types, multi-objective reward, prompt registry, policy model,
  RL environment, and registered learning policies
- Add structured THOUGHT/TOOL/INPUT/FINAL_ANSWER mode to OrchestratorAgent
- Add 15 channel backends (Discord, Slack, Telegram, Email, Webhook, IRC,
  Matrix, Teams, WhatsApp, Signal, Mattermost, BlueBubbles, Feishu,
  Google Chat, Webchat) with channel tools and config
- Add LiteLLM engine backend for unified LLM provider access
- Add RLM agent and REPL tool
- Remove ToolLearningPolicy — learning taxonomy now only targets
  Intelligence (LM weights/routing) and Agents (logic/ICL/tool strategies)
- Rename SFTPolicy to SFTRouterPolicy (backward-compat alias kept)
- Remove OpenClaw agent infrastructure (openclaw*.py, openclaw_bridge.py)
- Fix async streaming tests (asyncio.run vs deprecated get_event_loop)
- Fix server channel route tests (pytest.importorskip for optional fastapi)
- Track uv.lock for reproducibility
- Update CLAUDE.md and docs to reflect all changes

1676 tests pass, 37 skipped.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 18:32:32 +00:00

154 lines
5.4 KiB
Python

"""Tests for the MattermostChannel adapter."""
from __future__ import annotations
import os
from unittest.mock import MagicMock, patch
import pytest
from openjarvis.channels._stubs import ChannelStatus
from openjarvis.channels.mattermost import MattermostChannel
from openjarvis.core.events import EventBus, EventType
from openjarvis.core.registry import ChannelRegistry
@pytest.fixture(autouse=True)
def _register_mattermost():
"""Re-register after any registry clear."""
if not ChannelRegistry.contains("mattermost"):
ChannelRegistry.register_value("mattermost", MattermostChannel)
class TestRegistration:
def test_registry_key(self):
assert ChannelRegistry.contains("mattermost")
def test_channel_id(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
assert ch.channel_id == "mattermost"
class TestInit:
def test_defaults(self):
ch = MattermostChannel()
assert ch._url == ""
assert ch._token == ""
assert ch._status == ChannelStatus.DISCONNECTED
def test_constructor_param(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
assert ch._url == "https://mattermost.example.com"
assert ch._token == "test-token"
def test_env_var_fallback(self):
with patch.dict(os.environ, {"MATTERMOST_URL": "https://env.example.com", "MATTERMOST_TOKEN": "env-token"}):
ch = MattermostChannel()
assert ch._url == "https://env.example.com"
assert ch._token == "env-token"
def test_constructor_overrides_env(self):
with patch.dict(os.environ, {"MATTERMOST_URL": "https://env.example.com", "MATTERMOST_TOKEN": "env-token"}):
ch = MattermostChannel(url="https://explicit.example.com", token="explicit-token")
assert ch._url == "https://explicit.example.com"
assert ch._token == "explicit-token"
class TestSend:
def test_send_success(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
mock_response = MagicMock()
mock_response.status_code = 200
with patch("httpx.post", return_value=mock_response) as mock_post:
result = ch.send("channel-id-123", "Hello!")
assert result is True
mock_post.assert_called_once()
call_args = mock_post.call_args
url = call_args[0][0]
assert url.endswith("/api/v4/posts")
def test_send_with_conversation_id(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
mock_response = MagicMock()
mock_response.status_code = 200
with patch("httpx.post", return_value=mock_response) as mock_post:
result = ch.send("channel-id-123", "Hello!", conversation_id="root-123")
assert result is True
call_args = mock_post.call_args
payload = call_args[1]["json"]
assert "root_id" in payload
assert payload["root_id"] == "root-123"
def test_send_failure(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
mock_response = MagicMock()
mock_response.status_code = 400
mock_response.text = "Bad Request"
with patch("httpx.post", return_value=mock_response):
result = ch.send("channel-id-123", "Hello!")
assert result is False
def test_send_exception(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
with patch("httpx.post", side_effect=ConnectionError("refused")):
result = ch.send("channel-id-123", "Hello!")
assert result is False
def test_send_no_token(self):
ch = MattermostChannel()
result = ch.send("channel-id-123", "Hello!")
assert result is False
def test_send_publishes_event(self):
bus = EventBus(record_history=True)
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token", bus=bus)
mock_response = MagicMock()
mock_response.status_code = 200
with patch("httpx.post", return_value=mock_response):
ch.send("channel-id-123", "Hello!")
event_types = [e.event_type for e in bus.history]
assert EventType.CHANNEL_MESSAGE_SENT in event_types
class TestListChannels:
def test_list_channels(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
assert ch.list_channels() == ["mattermost"]
class TestStatus:
def test_disconnected_initially(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
assert ch.status() == ChannelStatus.DISCONNECTED
def test_no_url_connect_error(self):
ch = MattermostChannel()
ch.connect()
assert ch.status() == ChannelStatus.ERROR
class TestOnMessage:
def test_on_message(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
handler = MagicMock()
ch.on_message(handler)
assert handler in ch._handlers
class TestDisconnect:
def test_disconnect(self):
ch = MattermostChannel(url="https://mattermost.example.com", token="test-token")
ch._status = ChannelStatus.CONNECTED
ch.disconnect()
assert ch.status() == ChannelStatus.DISCONNECTED