test_script_table_projection.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from app.inspector_projection import project_artifact_detail
  2. def test_script_table_projection_normalizes_facets_links_and_element_references():
  3. artifact = {
  4. "sourceOrigin": "database",
  5. "paragraphs": [
  6. {
  7. "id": 1,
  8. "rowId": 1,
  9. "effectiveId": 1,
  10. "paragraph_index": 2,
  11. "level": 1,
  12. "name": "机制拆解",
  13. "content_range": {"图片": ["1", "2"], "标题": True, "正文": "第二段"},
  14. "theme": "通过[元素:10]解释机制",
  15. "theme_elements": [{"维度类型": "主", "维度": "核心竞争力", "原子点": "份额换订单"}],
  16. "form": "案例拆解",
  17. "form_elements": [],
  18. "function": "承接上文",
  19. "function_elements": [],
  20. "feeling": "建立可信度",
  21. "feeling_elements": [],
  22. "full_description": "通过[元素:10]解释机制,并保留[元素:999]。",
  23. }
  24. ],
  25. "elements": [
  26. {"id": 10, "rowId": 10, "effectiveId": 10, "name": "份额换订单", "dimension_primary": "核心竞争力", "dimension_secondary": "商业模式"}
  27. ],
  28. "paragraphElements": [{"id": 100, "paragraph_id": 1, "element_id": 10}],
  29. }
  30. payload = project_artifact_detail(431, "artifact:base:current", artifact)
  31. table = payload["scriptTable"]
  32. assert table["counts"] == {"paragraphs": 1, "elements": 1, "links": 1}
  33. assert table["paragraphs"][0]["contentRange"] == ["图1-2", "标题", "正文: 第二段"]
  34. assert table["paragraphs"][0]["sections"]["theme"]["dimensions"][0] == {
  35. "type": "主",
  36. "name": "核心竞争力",
  37. "value": "份额换订单",
  38. }
  39. parts = table["paragraphs"][0]["fullDescriptionParts"]
  40. assert parts[1] == {"type": "element", "id": 10, "name": "份额换订单", "resolved": True, "text": "[元素:10]"}
  41. assert parts[3]["resolved"] is False
  42. assert table["paragraphs"][0]["sections"]["theme"]["descriptionParts"][1] == {
  43. "type": "element",
  44. "id": 10,
  45. "name": "份额换订单",
  46. "resolved": True,
  47. "text": "[元素:10]",
  48. }
  49. assert table["elements"][0]["paragraphs"] == [{"id": 1, "index": 2, "name": "机制拆解"}]
  50. assert payload["changes"]["sections"][0]["content"] == "1 个段落、1 个元素、1 条关联"
  51. assert "artifact" not in payload["technical"]
  52. def test_script_table_keeps_children_beside_their_real_parent():
  53. def paragraph(row_id, index, name, parent_id=None, level=1):
  54. return {
  55. "id": row_id,
  56. "rowId": row_id,
  57. "effectiveId": row_id,
  58. "paragraph_index": index,
  59. "parent_id": parent_id,
  60. "level": level,
  61. "name": name,
  62. }
  63. artifact = {
  64. "sourceOrigin": "database",
  65. "paragraphs": [
  66. paragraph(1, 1, "第一章"),
  67. paragraph(2, 1, "第一章子段", parent_id=1, level=2),
  68. paragraph(3, 2, "第二章"),
  69. paragraph(4, 1, "第二章子段", parent_id=3, level=2),
  70. ],
  71. "elements": [],
  72. "paragraphElements": [],
  73. }
  74. table = project_artifact_detail(1, "artifact:base:current", artifact)["scriptTable"]
  75. assert [row["name"] for row in table["paragraphs"]] == [
  76. "第一章",
  77. "第一章子段",
  78. "第二章",
  79. "第二章子段",
  80. ]
  81. assert [row["depth"] for row in table["paragraphs"]] == [0, 1, 0, 1]
  82. assert table["maxDepth"] == 2
  83. def test_frozen_candidate_snapshot_restores_nested_paragraphs_and_embedded_links():
  84. artifact = {
  85. "sourceOrigin": "database",
  86. "historicalAccuracy": "exact",
  87. "artifactContext": {"type": "candidate", "branchStatus": "merged"},
  88. "snapshot": {
  89. "stats": {"paragraph_count": 2, "element_count": 1, "link_count": 2},
  90. "paragraphs": [
  91. {
  92. "id": 1,
  93. "paragraph_index": 1,
  94. "level": 1,
  95. "name": "主段落",
  96. "linked_elements": [{"link_id": 10, "element_id": 100}],
  97. "sub_paragraphs": [
  98. {
  99. "id": 2,
  100. "paragraph_index": 1,
  101. "level": 2,
  102. "name": "子段落",
  103. "linked_elements": [],
  104. "linked_element_ids": [100],
  105. }
  106. ],
  107. }
  108. ],
  109. "elements": [{"id": 100, "name": "候选元素"}],
  110. },
  111. }
  112. payload = project_artifact_detail(1, "artifact:branch:7", artifact)
  113. table = payload["scriptTable"]
  114. assert table["counts"] == {"paragraphs": 2, "elements": 1, "links": 2}
  115. assert [row["name"] for row in table["paragraphs"]] == ["主段落", "子段落"]
  116. assert [row["depth"] for row in table["paragraphs"]] == [0, 1]
  117. assert table["elements"][0]["paragraphs"] == [
  118. {"id": 1, "index": 1, "name": "主段落"},
  119. {"id": 2, "index": 1, "name": "子段落"},
  120. ]
  121. assert payload["businessSections"][0]["title"] == "候选脚本规模"
  122. assert payload["changes"]["summary"] == "候选脚本内容"