test_run_service_post_archive.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from __future__ import annotations
  2. from content_agent import run_service
  3. from content_agent.integrations.runtime_files import LocalRuntimeFileStore
  4. from content_agent.run_service import RunService
  5. from content_agent.schemas import RunStartRequest
  6. class EventRecordingRuntime(LocalRuntimeFileStore):
  7. def append_run_event_records(self, run_id, policy_run_id, rows):
  8. prepared = [
  9. {**row, "run_id": run_id, "policy_run_id": row.get("policy_run_id", policy_run_id)}
  10. for row in rows
  11. ]
  12. self.append_jsonl(run_id, "run_events.jsonl", prepared)
  13. def test_post_run_oss_archive_triggers_for_real_pending_records(monkeypatch, tmp_path):
  14. runtime = EventRecordingRuntime(tmp_path)
  15. runtime.prepare_run("run_001")
  16. runtime.append_jsonl(
  17. "run_001",
  18. "content_media_records.jsonl",
  19. [
  20. {
  21. "run_id": "run_001",
  22. "policy_run_id": "policy_001",
  23. "platform": "kuaishou",
  24. "platform_content_id": "content_001",
  25. "content_media_status": "oss_upload_pending",
  26. "play_url": "https://source.example/video.mp4",
  27. "raw_payload": {},
  28. }
  29. ],
  30. )
  31. service = object.__new__(RunService)
  32. service.runtime = runtime
  33. service._start_background_thread = lambda target, name: target()
  34. calls: list[str] = []
  35. def fake_archive(runtime_arg, run_id):
  36. calls.append(run_id)
  37. return [
  38. {
  39. "platform_content_id": "content_001",
  40. "content_media_status": "oss_uploaded",
  41. }
  42. ]
  43. monkeypatch.setattr(run_service.oss_archive, "archive_pending_for_run", fake_archive)
  44. service._trigger_post_run_oss_archive(
  45. {"run_id": "run_001", "policy_run_id": "policy_001"},
  46. RunStartRequest(platform="kuaishou", platform_mode="real"),
  47. )
  48. events = runtime.read_jsonl("run_001", "run_events.jsonl")
  49. assert calls == ["run_001"]
  50. assert [event["event_id"] for event in events] == [
  51. "oss_archive_post_run_started",
  52. "oss_archive_post_run_completed",
  53. ]
  54. assert events[0]["raw_payload"]["pending_due_count"] == 1
  55. assert events[1]["raw_payload"]["content_media_status_counts"] == {"oss_uploaded": 1}
  56. def test_post_run_oss_archive_failure_is_recorded_without_raising(monkeypatch, tmp_path):
  57. runtime = EventRecordingRuntime(tmp_path)
  58. runtime.prepare_run("run_001")
  59. runtime.append_jsonl(
  60. "run_001",
  61. "content_media_records.jsonl",
  62. [
  63. {
  64. "run_id": "run_001",
  65. "policy_run_id": "policy_001",
  66. "platform": "shipinhao",
  67. "platform_content_id": "content_001",
  68. "content_media_status": "oss_upload_pending",
  69. "play_url": "https://source.example/video.mp4",
  70. "raw_payload": {},
  71. }
  72. ],
  73. )
  74. service = object.__new__(RunService)
  75. service.runtime = runtime
  76. service._start_background_thread = lambda target, name: target()
  77. def fail_archive(runtime_arg, run_id):
  78. raise TimeoutError("archive stuck")
  79. monkeypatch.setattr(run_service.oss_archive, "archive_pending_for_run", fail_archive)
  80. service._trigger_post_run_oss_archive(
  81. {"run_id": "run_001", "policy_run_id": "policy_001"},
  82. RunStartRequest(platform="shipinhao", platform_mode="real"),
  83. )
  84. events = runtime.read_jsonl("run_001", "run_events.jsonl")
  85. assert [event["event_id"] for event in events] == [
  86. "oss_archive_post_run_started",
  87. "oss_archive_post_run_failed",
  88. ]
  89. assert events[1]["error_code"] == "OSS_ARCHIVE_POST_RUN_FAILED"
  90. assert events[1]["raw_payload"]["exception_type"] == "TimeoutError"
  91. def test_post_run_oss_archive_skips_mock_and_non_pending_records(monkeypatch, tmp_path):
  92. runtime = EventRecordingRuntime(tmp_path)
  93. runtime.prepare_run("run_001")
  94. runtime.append_jsonl(
  95. "run_001",
  96. "content_media_records.jsonl",
  97. [
  98. {
  99. "run_id": "run_001",
  100. "policy_run_id": "policy_001",
  101. "platform_content_id": "content_uploaded",
  102. "content_media_status": "oss_uploaded",
  103. "play_url": "https://source.example/video.mp4",
  104. }
  105. ],
  106. )
  107. service = object.__new__(RunService)
  108. service.runtime = runtime
  109. service._start_background_thread = lambda target, name: target()
  110. calls: list[str] = []
  111. monkeypatch.setattr(
  112. run_service.oss_archive,
  113. "archive_pending_for_run",
  114. lambda runtime_arg, run_id: calls.append(run_id),
  115. )
  116. service._trigger_post_run_oss_archive(
  117. {"run_id": "run_001", "policy_run_id": "policy_001"},
  118. RunStartRequest(platform="douyin", platform_mode="real"),
  119. )
  120. service._trigger_post_run_oss_archive(
  121. {"run_id": "run_001", "policy_run_id": "policy_001"},
  122. RunStartRequest(platform="douyin", platform_mode="mock"),
  123. )
  124. assert calls == []
  125. assert runtime.read_jsonl("run_001", "run_events.jsonl") == []