Files
AstrBot/tests/test_dify_api_client.py
LIghtJUNction dcaaf6286a test: comprehensive test coverage and type fixes
- Add 100+ new test files covering provider sources, platform adapters,
  agent runners, star/plugin system, knowledge base, core utils,
  pipeline, computer tools, builtin commands, and dashboard routes
- Fix Python type errors in sqlite.py (col() wrappers for SQLModel),
  astr_agent_tool_exec, core_lifecycle, star context/manager
- Fix TypeScript strict mode errors across 30+ dashboard Vue files
- Add import smoke tests, auth roundtrip, startup tests
- Add compile-all check and CLI entry test for AUR compatibility
- Restore BotMessageAccumulator and helpers lost in merge
2026-04-29 06:29:56 +08:00

57 lines
2.3 KiB
Python

"""Import smoke tests for DifyAPIClient."""
from unittest.mock import patch
import pytest
from astrbot.core.agent.runners.dify.dify_api_client import DifyAPIClient, _stream_sse
class TestDifyAPIClientImport:
"""Verify DifyAPIClient can be imported and instantiated."""
def test_class_importable(self):
"""DifyAPIClient should be importable."""
assert DifyAPIClient is not None
def test_instantiation_with_defaults(self):
"""DifyAPIClient should be instantiatable with only api_key."""
with patch("astrbot.core.agent.runners.dify.dify_api_client.ClientSession"):
client = DifyAPIClient(api_key="test-key")
assert isinstance(client, DifyAPIClient)
assert client.api_key == "test-key"
assert client.api_base == "https://api.dify.ai/v1"
def test_instantiation_with_custom_base(self):
"""DifyAPIClient should accept a custom api_base."""
with patch("astrbot.core.agent.runners.dify.dify_api_client.ClientSession"):
client = DifyAPIClient(api_key="test-key", api_base="https://custom.dify.com/v1")
assert isinstance(client, DifyAPIClient)
assert client.api_base == "https://custom.dify.com/v1"
def test_session_created_on_init(self):
"""DifyAPIClient creates a ClientSession on init."""
import aiohttp
with patch("astrbot.core.agent.runners.dify.dify_api_client.ClientSession", spec=aiohttp.ClientSession):
client = DifyAPIClient(api_key="test-key")
assert isinstance(client.session, aiohttp.ClientSession)
def test_headers_include_bearer_token(self):
"""Headers should include the Authorization bearer token."""
with patch("astrbot.core.agent.runners.dify.dify_api_client.ClientSession"):
client = DifyAPIClient(api_key="my-secret-key")
assert client.headers["Authorization"] == "Bearer my-secret-key"
@pytest.mark.asyncio
async def test_close_cleans_up_session(self):
"""close() should close the underlying session."""
client = DifyAPIClient(api_key="test-key")
assert not client.session.closed
await client.close()
assert client.session.closed
def test_stream_sse_is_callable(self):
"""_stream_sse helper should be importable and callable."""
assert callable(_stream_sse)