Files
kefu/deploy/review-assistant-20260917/append_protocol_cancel_tests.py
T
2026-09-21 10:34:06 +08:00

82 lines
4.4 KiB
Python

from pathlib import Path
p=Path(r'C:\wechat_rpa\test_protocol_review_assistant.py')
s=p.read_text(encoding='utf-8')
s=s.replace(" def close(self): self.closed = True\n", " def close(self): self.closed = True\n def cancel_review(self, state):\n conv = state.get('conv_id')\n if self.pending.get(conv) == state.get('assistant_review_id'):\n self.pending.pop(conv, None)\n return True\n", 1)
s=s.replace(" self.coordinator = ReviewAssistantCoordinator(\n", " self.settings = settings\n self.coordinator = ReviewAssistantCoordinator(\n", 1)
extra=''' def test_explicit_cancel_closes_old_reminder_without_auto_resume(self):
state = self.review()
old_id = state['assistant_review_id']
result = self.bot.cancel_pending_replies([self.key])
self.assertEqual(result['deleted'], [self.key])
self.assertFalse(self.coordinator.has_pending(CONV))
self.assertFalse(self.coordinator.resumed(CONV))
self.assertFalse(self.bot._assistant_cancellations)
self.add(3)
self.bot.poll_once()
self.assertTrue(self.bot._pending[self.key]['awaiting_review'])
self.assertNotEqual(self.bot._pending[self.key]['assistant_review_id'], old_id)
self.assertFalse(self.sender.calls)
def test_explicit_handoff_closes_reminder_and_preserves_evidence(self):
state = self.review()
result = self.bot.handoff_pending_replies([self.key])
self.assertEqual(result['handed_off'], [self.key])
self.assertFalse(self.coordinator.has_pending(CONV))
self.assertFalse(self.coordinator.resumed(CONV))
self.assertEqual(self.bot._manual_handoffs[self.key]['state']['assistant_review_id'], state['assistant_review_id'])
self.assertFalse(self.sender.calls)
def test_cancel_queue_save_failure_does_not_cancel_existing_review(self):
state = self.review()
with mock.patch.object(self.bot, '_save', side_effect=OSError('synthetic full disk')):
with self.assertRaises(OSError):
self.bot.cancel_pending_replies([self.key])
self.assertIs(self.bot._pending[self.key], state)
self.assertTrue(self.coordinator.has_pending(CONV))
self.assertFalse(self.bot._assistant_cancellations)
def test_cancel_store_failure_retries_after_restart_before_new_question(self):
state = self.review()
self.bot.send_mode = 'auto'
with mock.patch.object(self.coordinator, 'cancel_review', return_value=False):
self.bot.cancel_pending_replies([self.key])
self.assertIn(state['assistant_review_id'], self.bot._assistant_cancellations)
self.add(3, text='customer after explicit cancellation')
self.bot.poll_once()
self.assertEqual(self.bot._pending[self.key]['stage'], 'queued')
self.assertFalse(self.sender.calls)
restored = self.make_bot()
restored.send_mode = 'auto'
restored.poll_once()
self.assertFalse(restored._assistant_cancellations)
self.assertFalse(self.coordinator.has_pending(CONV))
self.assertFalse(self.coordinator.resumed(CONV))
self.assertEqual(len(self.sender.calls), 1)
def test_cancel_works_after_configuration_is_disabled(self):
self.review()
self.settings['review_assistant_accounts'][ACCOUNT]['enabled'] = False
self.bot.cancel_pending_replies([self.key])
self.assertFalse(self.bot._assistant_cancellations)
self.settings['review_assistant_accounts'][ACCOUNT]['enabled'] = True
self.assertFalse(self.coordinator.has_pending(CONV))
def test_recovered_confirmed_local_approval_closes_old_reminder(self):
state = self.review()
self.bot.approve_pending_replies([self.key])
state['send_state'] = 'sending'
self.bot._save()
self.sender.lookup = mock.Mock(return_value={'status': 'confirmed', 'result': {
'status': 'confirmed', 'serverId': '789'}})
restored = self.make_bot()
self.assertNotIn(self.key, restored._pending)
self.assertFalse(self.coordinator.has_pending(CONV))
self.assertFalse(self.coordinator.resumed(CONV))
self.assertFalse(self.sender.calls)
'''
s=s.replace("if __name__ == '__main__':\n",extra+"if __name__ == '__main__':\n",1)
p.write_text(s,encoding='utf-8',newline='\n')
print('Added six cancellation and confirmed-restart protocol integration tests')