fix: harden residual flow boundaries and pending recovery

This commit is contained in:
github-actions[bot]
2026-07-14 17:21:54 +00:00
parent 447b1253dc
commit 778d6822a2
7 changed files with 350 additions and 808 deletions
+35
View File
@@ -0,0 +1,35 @@
import json
import os
import tempfile
import unittest
from account_outputs import retry_pending_file
class PendingRecoveryTests(unittest.TestCase):
def test_retry_is_idempotent_after_target_was_already_written(self):
with tempfile.TemporaryDirectory() as directory:
pending = os.path.join(directory, "accounts.txt.pending.jsonl")
target = os.path.join(directory, "accounts.txt")
record = {"email": "a@example.com", "password": "pw", "sso": "token"}
with open(pending, "w", encoding="utf-8") as handle:
handle.write(json.dumps(record) + "\n")
with open(target, "w", encoding="utf-8") as handle:
handle.write("a@example.com----pw----token\n")
summary = retry_pending_file(pending)
self.assertEqual(summary["restored"], 1)
with open(target, "r", encoding="utf-8") as handle:
self.assertEqual(handle.readlines(), ["a@example.com----pw----token\n"])
self.assertFalse(os.path.exists(pending))
def test_rejects_same_input_and_output_path(self):
with tempfile.TemporaryDirectory() as directory:
pending = os.path.join(directory, "pending.jsonl")
with open(pending, "w", encoding="utf-8") as handle:
handle.write("{}\n")
with self.assertRaises(ValueError):
retry_pending_file(pending, output_path=pending)
if __name__ == "__main__":
unittest.main()
+42
View File
@@ -113,6 +113,48 @@ class RegistrationFlowTests(unittest.TestCase):
self.assertEqual(batch.success_count, 1)
self.assertTrue(any("observer 执行失败" in line for line in logs))
def test_cleanup_failure_does_not_change_success_statistics(self):
fake = FakeOps()
ops = fake.operations()
def cleanup(reason):
if "已成功" in reason:
raise RuntimeError("cleanup failed")
fake.events.append(("cleanup", reason))
ops.cleanup = cleanup
batch = run_batch(2, self.callbacks(), lambda *args: None, ops, cleanup_interval=1)
self.assertEqual(batch.success_count, 2)
self.assertEqual(batch.fail_count, 0)
self.assertEqual(batch.processed_count, 2)
def test_cancel_during_between_account_sleep_ends_normally(self):
fake = FakeOps()
ops = fake.operations()
ops.sleep = lambda seconds: (_ for _ in ()).throw(Cancelled())
batch = run_batch(2, self.callbacks(), lambda *args: None, ops)
self.assertTrue(batch.cancelled)
self.assertEqual(batch.success_count, 1)
self.assertEqual(batch.processed_count, 1)
def test_final_cleanup_failure_does_not_hide_original_error(self):
fake = FakeOps()
ops = fake.operations()
ops.start_browser = lambda: (_ for _ in ()).throw(RuntimeError("original start error"))
ops.cleanup = lambda reason: (_ for _ in ()).throw(RuntimeError("cleanup error"))
logs = []
with self.assertRaisesRegex(RuntimeError, "original start error"):
run_batch(1, self.callbacks(logs), lambda *args: None, ops)
self.assertTrue(any("清理失败" in line for line in logs))
def test_postprocessing_exceptions_become_warnings(self):
fake = FakeOps()
ops = fake.operations()
ops.add_tokens = lambda sso, email: (_ for _ in ()).throw(RuntimeError("pool down"))
ops.export_cpa = lambda email, password, sso: (_ for _ in ()).throw(RuntimeError("cpa down"))
batch = run_batch(1, self.callbacks(), lambda *args: None, ops)
self.assertEqual(batch.success_count, 1)
self.assertEqual(batch.fail_count, 0)
self.assertEqual(batch.postprocess_warning_count, 1)
if __name__ == "__main__":
unittest.main()