test_card_data.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from __future__ import annotations
  2. from copy import deepcopy
  3. from fastapi.testclient import TestClient
  4. from app import main
  5. client = TestClient(main.app)
  6. def _event_detail(bundle, event_id: int):
  7. event = next(item for item in bundle["events"] if int(item["id"]) == event_id)
  8. input_content = deepcopy(event.get("inputData"))
  9. output_content = deepcopy(event.get("agentOutputData"))
  10. if event_id == 50:
  11. input_content = {
  12. "task": "范围:文章结构\n方法:补齐因果链\n目标:形成三段式文章结构\n避免:不要虚构事实"
  13. }
  14. output_content = {"summary": "实现完成"}
  15. if output_content is None and event.get("output_preview") is not None:
  16. output_content = event.get("output_preview")
  17. return {
  18. **deepcopy(event),
  19. "input": {"contentType": "json", "content": input_content, "truncated": False}
  20. if input_content is not None else None,
  21. "output": {"contentType": "json", "content": output_content, "truncated": False}
  22. if output_content is not None else None,
  23. }
  24. def test_card_data_protocol_and_primary_kinds(monkeypatch, current_bundle):
  25. bundle = deepcopy(current_bundle)
  26. monkeypatch.setattr(main.provider, "load_bundle", lambda _build_id: deepcopy(bundle))
  27. monkeypatch.setattr(main.provider, "load_event_detail", lambda _build_id, event_id: _event_detail(bundle, event_id))
  28. objective = client.get("/api/script-builds/88001/card-data/run%3Aobjective")
  29. assert objective.status_code == 200
  30. assert objective.json()["relationKind"] == "business-record"
  31. assert objective.json()["businessOutputs"][0]["source"] == {
  32. "kind": "database",
  33. "label": "script_build_record",
  34. "ref": "run:objective",
  35. "fieldPath": "script_direction",
  36. }
  37. task = client.get(
  38. "/api/script-builds/88001/card-data/round%3A1%3Abranch%3A1%3Atask"
  39. )
  40. assert task.status_code == 200
  41. assert task.json()["relationKind"] == "single-event"
  42. assert "不要虚构事实" in task.json()["businessInputs"][0]["value"]
  43. assert task.json()["runtime"]["units"][0]["eventRef"] == "event:50"
  44. tradeoff = client.get(
  45. "/api/script-builds/88001/card-data/data-decision%3A11"
  46. )
  47. assert tradeoff.status_code == 200
  48. assert tradeoff.json()["relationKind"] == "business-record"
  49. assert tradeoff.json()["businessInputs"][0]["relation"] == "explicit-basis"
  50. assert tradeoff.json()["runtime"]["units"] == []
  51. final = client.get(
  52. "/api/script-builds/88001/card-data/run%3Afinal-result"
  53. )
  54. assert final.status_code == 200
  55. assert final.json()["cardKind"] == "final-result"
  56. assert final.json()["relationKind"] == "calculated"
  57. assert next(item for item in final.json()["businessOutputs"] if item["id"] == "final:artifact-scale")["value"] == {
  58. "paragraphs": 3,
  59. "elements": 2,
  60. "links": 1,
  61. }
  62. def test_retrieval_agent_loads_full_parent_and_query_event_bodies(monkeypatch, current_bundle):
  63. bundle = deepcopy(current_bundle)
  64. monkeypatch.setattr(main.provider, "load_bundle", lambda _build_id: deepcopy(bundle))
  65. monkeypatch.setattr(main.provider, "load_event_detail", lambda _build_id, event_id: _event_detail(bundle, event_id))
  66. response = client.get(
  67. "/api/script-builds/88001/card-data/retrieval-agent%3A52"
  68. )
  69. assert response.status_code == 200
  70. payload = response.json()
  71. assert payload["cardKind"] == "retrieval-agent"
  72. assert payload["relationKind"] == "aggregate"
  73. assert payload["businessInputs"][0]["value"] == "查找可说明因果结构的解构 Case"
  74. assert [item["eventRef"] for item in payload["runtime"]["units"]] == [
  75. "event:52",
  76. "event:53",
  77. "event:54",
  78. ]
  79. assert payload["businessOutputs"][1]["value"] == "初筛出两个可用的三段式案例。"
  80. def test_unknown_card_data_ref_is_404(monkeypatch, current_bundle):
  81. monkeypatch.setattr(main.provider, "load_bundle", lambda _build_id: deepcopy(current_bundle))
  82. response = client.get("/api/script-builds/88001/card-data/not-a-card")
  83. assert response.status_code == 404