ForcePilot/backend/test/unit/channels/sdk/test_normalizer.py

89 lines
2.7 KiB
Python
Raw Normal View History

from __future__ import annotations
import pytest
from yuxi.channels.sdk.normalizer import (
extract_mentions,
extract_urls,
normalize_timestamp,
normalize_user_id,
)
class TestNormalizeTimestamp:
def test_int_seconds(self):
assert normalize_timestamp(1715000000) == 1715000000.0
def test_int_milliseconds(self):
ts = 1715000000000
assert normalize_timestamp(ts) == 1715000000000.0
def test_float_seconds(self):
assert normalize_timestamp(1715000000.5) == 1715000000.5
def test_string_timestamp(self):
assert normalize_timestamp("1715000000.123") == 1715000000.123
def test_invalid_string_returns_zero(self):
assert normalize_timestamp("not-a-number") == 0.0
def test_none_returns_zero(self):
assert normalize_timestamp(None) == 0.0 # type: ignore[arg-type]
class TestNormalizeUserId:
def test_string_id(self):
assert normalize_user_id("user123") == "user123"
def test_int_id(self):
assert normalize_user_id(12345) == "12345"
def test_strips_whitespace(self):
assert normalize_user_id(" user123 ") == "user123"
class TestExtractMentions:
def test_extracts_single_mention(self):
result = extract_mentions("hello @alice how are you")
assert result == ["alice"]
def test_extracts_multiple_mentions(self):
result = extract_mentions("@alice @bob please review")
assert result == ["alice", "bob"]
def test_no_mentions(self):
result = extract_mentions("no mentions here")
assert result == []
def test_filter_by_bot_names(self):
text = "@bot1 @user1 @bot2"
result = extract_mentions(text, bot_names=["bot1", "bot2"])
assert result == ["bot1", "bot2"]
def test_partial_bot_name_match(self):
text = "@mybot @otherbot"
result = extract_mentions(text, bot_names=["mybot"])
assert result == ["mybot"]
class TestExtractUrls:
def test_extracts_http_url(self):
result = extract_urls("check http://example.com for info")
assert result == ["http://example.com"]
def test_extracts_https_url(self):
result = extract_urls("visit https://example.com/path?q=1 now")
assert result == ["https://example.com/path?q=1"]
def test_extracts_multiple_urls(self):
text = "see http://a.com and https://b.com/page"
result = extract_urls(text)
assert result == ["http://a.com", "https://b.com/page"]
def test_no_urls(self):
result = extract_urls("no urls in this text")
assert result == []
def test_url_in_parentheses(self):
result = extract_urls("link (https://example.com) here")
assert result == ["https://example.com"]