import threading import unittest from types import SimpleNamespace from unittest.mock import patch from rpa_engine.credential import credential_egress_mismatch, validate_im_session class CredentialResponsivenessTests(unittest.IsolatedAsyncioTestCase): def test_legacy_egress_marker_comparison_is_diagnostic(self): legacy = '{"cookies": []}' self.assertFalse(credential_egress_mismatch(legacy, "")) self.assertTrue(credential_egress_mismatch(legacy, "47.96.154.74")) def test_stamped_egress_marker_comparison(self): stamped = ( '{"cookies": [], ' '"credential_egress_public_ip": "47.96.154.74"}' ) self.assertFalse(credential_egress_mismatch(stamped, "47.96.154.74")) self.assertTrue(credential_egress_mismatch(stamped, "116.62.23.103")) self.assertTrue(credential_egress_mismatch(stamped, "")) async def test_uid_lookup_does_not_block_event_loop(self): event_loop_thread_id = threading.get_ident() lookup_thread_ids = [] def get_uid(): lookup_thread_ids.append(threading.get_ident()) return 123456 session = SimpleNamespace( my_uid=0, can_direct_im=lambda: True, ) auth = SimpleNamespace( get_uid=get_uid, is_sign_ready=lambda: True, ) with patch( "rpa_engine.credential.DouyinAuth.from_im_session", return_value=auth, ): result = await validate_im_session( session, _bypass_global_limit=True, ) self.assertTrue(result[0]) self.assertEqual(len(lookup_thread_ids), 1) self.assertNotEqual( lookup_thread_ids[0], event_loop_thread_id, "the synchronous UID lookup ran on the event-loop thread", ) self.assertEqual(session.my_uid, 123456) if __name__ == "__main__": unittest.main()