81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import AsyncMock
|
|
|
|
|
|
BACKEND_DIR = Path(__file__).resolve().parents[1]
|
|
os.environ["KEFU_DB_TYPE"] = "sqlite"
|
|
os.environ["KEFU_DATABASE_URL"] = ""
|
|
os.environ["KEFU_DB_PATH"] = str(BACKEND_DIR / "kefu.db")
|
|
if str(BACKEND_DIR) not in sys.path:
|
|
sys.path.insert(0, str(BACKEND_DIR))
|
|
|
|
from rpa_engine.douyin_im.http_client import DouyinImHttpClient
|
|
from rpa_engine.douyin_im.session import DouyinImSession
|
|
|
|
|
|
class ConversationPollBandwidthTests(unittest.IsolatedAsyncioTestCase):
|
|
def _make_client(self) -> DouyinImHttpClient:
|
|
return DouyinImHttpClient(
|
|
DouyinImSession(cookies={"sessionid": "test"}, my_uid=10001),
|
|
account_id=9,
|
|
)
|
|
|
|
async def test_terminal_token_error_does_not_probe_other_payloads(self):
|
|
client = self._make_client()
|
|
client._request = AsyncMock(
|
|
return_value={
|
|
"status_code": 500,
|
|
"error_desc": "empty token",
|
|
"body": {},
|
|
}
|
|
)
|
|
|
|
self.assertEqual(await client.get_conversations(), [])
|
|
client._request.assert_awaited_once()
|
|
self.assertEqual(client._request.await_args.args[0], "POST")
|
|
|
|
async def test_successful_empty_response_stops_after_first_payload(self):
|
|
client = self._make_client()
|
|
client._request = AsyncMock(
|
|
return_value={"status_code": 0, "body": {"conversation_list": []}}
|
|
)
|
|
|
|
self.assertEqual(await client.get_conversations(), [])
|
|
client._request.assert_awaited_once()
|
|
|
|
async def test_parameter_error_can_fall_through_to_compatible_payload(self):
|
|
client = self._make_client()
|
|
client._request = AsyncMock(
|
|
side_effect=[
|
|
{"status_code": 400, "error_desc": "invalid parameter"},
|
|
{"status_code": 0, "body": {"conversation_list": []}},
|
|
]
|
|
)
|
|
|
|
self.assertEqual(await client.get_conversations(), [])
|
|
self.assertEqual(client._request.await_count, 2)
|
|
self.assertTrue(
|
|
all(call.args[0] == "POST" for call in client._request.await_args_list)
|
|
)
|
|
|
|
async def test_get_fallback_only_runs_after_transport_failure(self):
|
|
client = self._make_client()
|
|
client._request = AsyncMock(
|
|
side_effect=[None, {"status_code": 0, "body": {}}]
|
|
)
|
|
|
|
self.assertEqual(await client.get_conversations(), [])
|
|
self.assertEqual(
|
|
[call.args[0] for call in client._request.await_args_list],
|
|
["POST", "GET"],
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|