test_pool.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import json
  2. from pathlib import Path
  3. from platforms.chatgpt.pool import load_token_record, update_token_record, write_token_record
  4. def test_write_token_record_uses_single_pool_defaults(tmp_path: Path) -> None:
  5. path = write_token_record({"email": "user@example.com", "access_token": "tok"}, tmp_path)
  6. payload = json.loads(path.read_text(encoding="utf-8"))
  7. assert payload["email"] == "user@example.com"
  8. assert payload["source"] == "register"
  9. assert payload["backup_written"] is True
  10. assert payload["cpa_sync_status"] == "pending"
  11. assert "candidate_state" not in payload
  12. assert "last_recycled_at" not in payload
  13. assert "in_main_pool" not in payload
  14. assert "promoted_at" not in payload
  15. def test_load_token_record_backfills_runtime_metadata_without_candidate_fields(tmp_path: Path) -> None:
  16. path = tmp_path / "user@example.com.json"
  17. path.write_text(json.dumps({"email": "user@example.com", "access_token": "tok"}), encoding="utf-8")
  18. payload = load_token_record(path)
  19. assert payload["backup_written"] is True
  20. assert payload["cpa_sync_status"] == "pending"
  21. assert payload["health_status"] == "unknown"
  22. assert payload["last_probe_result"] == ""
  23. assert "candidate_state" not in payload
  24. assert "in_main_pool" not in payload
  25. def test_update_token_record_rewrites_file_atomically(tmp_path: Path) -> None:
  26. path = tmp_path / "user@example.com.json"
  27. path.write_text(json.dumps({"email": "user@example.com", "access_token": "tok"}), encoding="utf-8")
  28. payload = update_token_record(
  29. path,
  30. cpa_sync_status="synced",
  31. last_cpa_sync_at="2026-03-28T12:00:00+08:00",
  32. health_status="good",
  33. )
  34. assert payload["backup_written"] is True
  35. assert payload["cpa_sync_status"] == "synced"
  36. assert payload["health_status"] == "good"
  37. assert payload["last_cpa_sync_at"] == "2026-03-28T12:00:00+08:00"
  38. assert "in_main_pool" not in payload
  39. assert not path.with_name(f"{path.name}.tmp").exists()
  40. def test_write_token_record_marks_add_phone_accounts_for_warmup(tmp_path: Path) -> None:
  41. path = write_token_record(
  42. {
  43. "email": "user@example.com",
  44. "access_token": "tok",
  45. "registration_post_create_gate": "add_phone",
  46. },
  47. tmp_path,
  48. )
  49. payload = json.loads(path.read_text(encoding="utf-8"))
  50. assert payload["warmup_required"] is True
  51. assert payload["warmup_state"] == "pending"
  52. assert payload["warmup_passed"] is False