test_full_capture.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from __future__ import annotations
  2. import json
  3. from pathlib import Path
  4. from production_build_agents.observability.full_capture import (
  5. expand_global_data_result,
  6. expand_global_data_state,
  7. expand_node_update,
  8. load_observation_record,
  9. )
  10. def test_full_capture_reads_json_and_markdown_records(
  11. tmp_path: Path,
  12. ) -> None:
  13. input_path = tmp_path / "production_final.json"
  14. report_path = tmp_path / "PreprocessReport.v1.md"
  15. input_path.write_text(
  16. json.dumps(
  17. {
  18. "title": "完整标题",
  19. "image": "https://internal.example/image.png",
  20. },
  21. ensure_ascii=False,
  22. ),
  23. encoding="utf-8",
  24. )
  25. report_path.write_text("# 完整预处理报告", encoding="utf-8")
  26. state = {
  27. "run_id": "full-run",
  28. "input_path": str(input_path),
  29. "preprocess_report_path": str(report_path),
  30. "plan_history": {
  31. "1": {
  32. "json_uri": str(input_path),
  33. "remote_uri": "https://internal.example/reference.json",
  34. }
  35. },
  36. "event_log": ["完整事件"],
  37. }
  38. observed = expand_global_data_state(state)
  39. assert observed["state"] == state
  40. assert observed["records"]["input_path"]["content"]["title"] == (
  41. "完整标题"
  42. )
  43. assert observed["records"]["preprocess_report_path"]["content"] == (
  44. "# 完整预处理报告"
  45. )
  46. assert (
  47. observed["records"]["plan_history.1.json_uri"]["content"]["title"]
  48. == "完整标题"
  49. )
  50. assert "plan_history.1.remote_uri" not in observed["records"]
  51. def test_full_capture_preserves_node_state_error_and_media_url(
  52. tmp_path: Path,
  53. ) -> None:
  54. delivery_path = tmp_path / "GlobalDataStageDelivery.v1.json"
  55. delivery_path.write_text(
  56. json.dumps(
  57. {
  58. "body": "真实正文",
  59. "video_url": "https://internal.example/final.mp4",
  60. },
  61. ensure_ascii=False,
  62. ),
  63. encoding="utf-8",
  64. )
  65. state = {"run_id": "full-run", "status": "RUNNING"}
  66. error = RuntimeError("真实内部错误内容")
  67. observed = expand_node_update(
  68. "finalize_global_data",
  69. state,
  70. {
  71. "status": "FAILED",
  72. "error": "模型返回失败",
  73. "global_data_delivery_path": str(delivery_path),
  74. },
  75. exception=error,
  76. )
  77. assert observed["state_after"]["error"] == "模型返回失败"
  78. assert observed["exception"]["message"] == "真实内部错误内容"
  79. assert (
  80. observed["records"]["global_data_delivery_path"]["content"][
  81. "video_url"
  82. ]
  83. == "https://internal.example/final.mp4"
  84. )
  85. def test_full_capture_result_includes_full_final_state(
  86. tmp_path: Path,
  87. ) -> None:
  88. result = {
  89. "run_id": "full-run",
  90. "status": "COMPLETED",
  91. "event_log": ["计划完成", "阶段验收通过"],
  92. "task_records": {
  93. "Task1": {
  94. "executor_summary": "完整执行摘要",
  95. "validator_summary": "完整校验摘要",
  96. }
  97. },
  98. }
  99. observed = expand_global_data_result(
  100. result,
  101. execution_mode="fresh",
  102. graph_invoked=True,
  103. )
  104. assert observed["result"] == result
  105. assert observed["execution_mode"] == "fresh"
  106. assert observed["graph_invoked"] is True
  107. def test_binary_record_is_not_embedded(tmp_path: Path) -> None:
  108. media_path = tmp_path / "video.mp4"
  109. media_path.write_bytes(b"\x00\x01\x02")
  110. observed = load_observation_record(media_path)
  111. assert observed is not None
  112. assert observed["path"] == str(media_path)
  113. assert observed["content_omitted"] == "non_text_record"
  114. assert "content" not in observed