72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
from helpers import (
|
|
build_transcript_block,
|
|
chunk_message,
|
|
command_channel_error,
|
|
summarize_error,
|
|
)
|
|
|
|
|
|
def test_chunk_message_splits_long_text_under_limit():
|
|
text = "A" * 4500
|
|
chunks = chunk_message(text, limit=1900)
|
|
assert len(chunks) == 3
|
|
assert all(len(c) <= 1900 for c in chunks)
|
|
assert "".join(chunks) == text
|
|
|
|
|
|
def test_chunk_message_keeps_short_text_single_chunk():
|
|
text = "hello world"
|
|
assert chunk_message(text, limit=1900) == [text]
|
|
|
|
|
|
def test_summarize_error_prefers_json_message():
|
|
body = {"error": {"message": "bad request"}}
|
|
assert summarize_error(body, fallback="x") == "bad request"
|
|
|
|
|
|
def test_summarize_error_falls_back_to_string():
|
|
assert summarize_error({"unexpected": True}, fallback="fallback") == "fallback"
|
|
|
|
|
|
def test_summarize_error_compacts_cloudflare_html():
|
|
html = """
|
|
<!DOCTYPE html>
|
|
<html><head><title>openrouter.ai | 502: Bad gateway</title></head>
|
|
<body><h1>Bad gateway</h1><span class="code-label">Error code 502</span></body></html>
|
|
"""
|
|
|
|
assert summarize_error(None, fallback=html) == "Cloudflare/OpenRouter returned HTTP 502 Bad gateway"
|
|
|
|
|
|
def test_summarize_error_truncates_long_plain_text():
|
|
message = summarize_error(None, fallback="x" * 600)
|
|
|
|
assert len(message) < 260
|
|
assert message.endswith("…")
|
|
|
|
|
|
def test_build_transcript_block_labels_transcript():
|
|
block = build_transcript_block("Chris", "Discussed deadlines")
|
|
assert block == "[Chris]\nDiscussed deadlines"
|
|
|
|
|
|
def test_command_channel_error_allows_matching_channel():
|
|
assert command_channel_error(current_channel_id=123, allowed_channel_id=123) is None
|
|
|
|
|
|
def test_command_channel_error_blocks_when_not_configured():
|
|
assert command_channel_error(current_channel_id=123, allowed_channel_id=None) == (
|
|
"⚠️ This bot doesn't have a command channel yet. Use `/set_output` first."
|
|
)
|
|
|
|
|
|
def test_command_channel_error_blocks_wrong_channel():
|
|
assert command_channel_error(current_channel_id=123, allowed_channel_id=456) == (
|
|
"⚠️ This command only works in <#456>."
|
|
)
|