Files
AstrBot/tests/unit/test_provider_stats.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

67 lines
2.2 KiB
Python

from types import SimpleNamespace
from unittest.mock import MagicMock
import pytest
from sqlmodel import select
from astrbot.core import db_helper
from astrbot.core.agent.response import AgentStats
from astrbot.core.db.po import Conversation, ProviderStat
from astrbot.core.pipeline.process_stage.method.agent_sub_stages import internal
from astrbot.core.provider.entities import LLMResponse, ProviderRequest, TokenUsage
@pytest.mark.asyncio
async def test_record_internal_agent_stats_persists_provider_stat(
temp_db,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setattr(db_helper, "insert_provider_stat", temp_db.insert_provider_stat)
event = MagicMock(unified_msg_origin="webchat:FriendMessage:session-42")
req = ProviderRequest(
conversation=Conversation(cid="conv-123", platform_id="test", user_id="test"),
)
stats = AgentStats(
token_usage=TokenUsage(input_other=11, input_cached=3, output=7),
start_time=100.0,
end_time=108.5,
time_to_first_token=0.6,
)
provider = SimpleNamespace(
provider_config={"id": "provider-1"},
meta=lambda: SimpleNamespace(id="provider-1", type="openai"),
get_model=lambda: "gpt-4.1",
)
agent_runner = MagicMock()
agent_runner.provider = provider
agent_runner.stats = stats
agent_runner.was_aborted.return_value = False
final_resp = LLMResponse(role="assistant")
await internal._record_internal_agent_stats(
event,
req,
agent_runner,
final_resp,
)
async with temp_db.get_db() as session:
result = await session.execute(select(ProviderStat))
records = result.scalars().all()
assert len(records) == 1
record = records[0]
assert record.agent_type == "internal"
assert record.status == "completed"
assert record.umo == "webchat:FriendMessage:session-42"
assert record.conversation_id == "conv-123"
assert record.provider_id == "provider-1"
assert record.provider_model == "gpt-4.1"
assert record.token_input_other == 11
assert record.token_input_cached == 3
assert record.token_output == 7
assert record.start_time == 100.0
assert record.end_time == 108.5
assert record.time_to_first_token == 0.6