Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

3.2 KB
Raw Blame
from __future__ import annotations

import sys
import unittest
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from config.v2_config import SlackConfig
from modules.im.base import MessageContext
from modules.im.slack import SlackApiError, SlackBot, _SLACK_MARKDOWN_TEXT_LIMIT


class SlackMarkdownFooterTests(unittest.IsolatedAsyncioTestCase):
    async def test_long_result_folds_subtext_into_body(self):
        """A 12k-30k inline Slack result falls back to the legacy mrkdwn path,
        which can't carry a context-footer block; the show_duration footnote must
        be folded onto the body instead of silently dropped (Codex P2)."""
        slack = SlackBot(SlackConfig(bot_token="xoxb-test"))
        slack._ensure_clients = lambda: None
        captured = {}

        async def fake_send(context, text, parse_mode=None, reply_to=None):
            captured["text"] = text
            return "ts-1"

        slack.send_message = fake_send
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack")
        big = "x" * (_SLACK_MARKDOWN_TEXT_LIMIT + 1)

        message_id = await slack.send_markdown_message(context, big, subtext="✅ ⏱️ 5s · 🪙 1.2k tok")

        self.assertEqual(message_id, "ts-1")
        self.assertEqual(captured["text"], f"{big}\n\n✅ ⏱️ 5s · 🪙 1.2k tok")

    async def test_long_result_without_subtext_unchanged(self):
        slack = SlackBot(SlackConfig(bot_token="xoxb-test"))
        slack._ensure_clients = lambda: None
        captured = {}

        async def fake_send(context, text, parse_mode=None, reply_to=None):
            captured["text"] = text
            return "ts-2"

        slack.send_message = fake_send
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack")
        big = "y" * (_SLACK_MARKDOWN_TEXT_LIMIT + 1)

        await slack.send_markdown_message(context, big)

        self.assertEqual(captured["text"], big)


    async def test_block_rejection_fallback_folds_subtext(self):
        """When Slack rejects the native markdown block (invalid_blocks etc.), the
        legacy-mrkdwn recovery must fold the footnote onto the body so the
        show_duration/token footer survives (Codex P2)."""
        slack = SlackBot(SlackConfig(bot_token="xoxb-test"))
        slack._ensure_clients = lambda: None
        slack._is_markdown_block_rejection = lambda err: True
        captured = {}

        async def raise_rejection(context, kwargs, log_label=None):
            raise SlackApiError("invalid_blocks", {"ok": False, "error": "invalid_blocks"})

        async def fake_send(context, text, parse_mode=None, reply_to=None):
            captured["text"] = text
            return "ts-3"

        slack._post_message_with_dm_recovery = raise_rejection
        slack.send_message = fake_send
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack")

        message_id = await slack.send_markdown_message(context, "Answer body", subtext="✅ ⏱️ 5s · 🪙 1.2k tok")

        self.assertEqual(message_id, "ts-3")
        self.assertEqual(captured["text"], "Answer body\n\n✅ ⏱️ 5s · 🪙 1.2k tok")


if __name__ == "__main__":
    unittest.main()