from __future__ import annotations import pytest from yuxi.channels.adapters.mattermost.normalizer import ( check_bot_mentioned, extract_mentions, parse_channel_json, parse_post_json, ) class TestParsePostJson: def test_parse_json_string(self): data = {"post": '{"id":"abc","user_id":"u1","message":"hello"}'} result = parse_post_json(data) assert result == {"id": "abc", "user_id": "u1", "message": "hello"} def test_parse_already_dict(self): post = {"id": "abc", "user_id": "u1"} data = {"post": post} result = parse_post_json(data) assert result == post def test_parse_invalid_json_returns_empty(self): data = {"post": "not-json"} result = parse_post_json(data) assert result == {} def test_parse_missing_key_returns_empty(self): result = parse_post_json({}) assert result == {} def test_parse_empty_string_returns_empty(self): result = parse_post_json({"post": ""}) assert result == {} class TestParseChannelJson: def test_parse_json_string(self): data = {"channel": '{"type":"O","name":"general"}'} result = parse_channel_json(data) assert result == {"type": "O", "name": "general"} def test_parse_already_dict(self): channel = {"type": "D", "name": "dm"} data = {"channel": channel} result = parse_channel_json(data) assert result == channel def test_parse_invalid_json_returns_empty(self): result = parse_channel_json({"channel": "bad"}) assert result == {} def test_parse_missing_key_returns_empty(self): result = parse_channel_json({}) assert result == {} class TestExtractMentions: def test_single_mention(self): assert extract_mentions("hello @alice") == ["alice"] def test_multiple_mentions(self): result = extract_mentions("@alice @bob hi") assert result == ["alice", "bob"] def test_username_with_dot(self): result = extract_mentions("hey @alice.smith") assert result == ["alice.smith"] def test_username_with_hyphen(self): result = extract_mentions("hi @bot-name") assert result == ["bot-name"] def test_no_mentions(self): assert extract_mentions("hello world") == [] def test_empty_text(self): assert extract_mentions("") == [] assert extract_mentions(None) == [] class TestCheckBotMentioned: def test_mentioned(self): assert check_bot_mentioned("hey @yuxi help me", "yuxi") is True def test_not_mentioned(self): assert check_bot_mentioned("hey @alice help me", "yuxi") is False def test_empty_text(self): assert check_bot_mentioned("", "yuxi") is False assert check_bot_mentioned(None, "yuxi") is False def test_empty_username(self): assert check_bot_mentioned("hello", "") is False