test_extract_chapter_context.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import sys
  5. from pathlib import Path
  6. def test_extract_state_summary_accepts_dominant_key(tmp_path):
  7. scripts_dir = Path(__file__).resolve().parents[2]
  8. if str(scripts_dir) not in sys.path:
  9. sys.path.insert(0, str(scripts_dir))
  10. from extract_chapter_context import extract_state_summary
  11. state = {
  12. "progress": {"current_chapter": 12, "total_words": 12345},
  13. "protagonist_state": {
  14. "power": {"realm": "筑基", "layer": 2},
  15. "location": "宗门",
  16. "golden_finger": {"name": "系统", "level": 1},
  17. },
  18. "strand_tracker": {
  19. "history": [
  20. {"chapter": 10, "dominant": "quest"},
  21. {"chapter": 11, "dominant": "fire"},
  22. ]
  23. },
  24. }
  25. webnovel_dir = tmp_path / ".webnovel"
  26. webnovel_dir.mkdir(parents=True, exist_ok=True)
  27. (webnovel_dir / "state.json").write_text(json.dumps(state, ensure_ascii=False), encoding="utf-8")
  28. text = extract_state_summary(tmp_path)
  29. assert "Ch10:quest" in text
  30. assert "Ch11:fire" in text
  31. def test_extract_chapter_outline_supports_hyphen_filename(tmp_path):
  32. scripts_dir = Path(__file__).resolve().parents[2]
  33. if str(scripts_dir) not in sys.path:
  34. sys.path.insert(0, str(scripts_dir))
  35. from extract_chapter_context import extract_chapter_outline
  36. outline_dir = tmp_path / "大纲"
  37. outline_dir.mkdir(parents=True, exist_ok=True)
  38. (outline_dir / "第1卷-详细大纲.md").write_text("### 第1章:测试标题\n测试大纲", encoding="utf-8")
  39. outline = extract_chapter_outline(tmp_path, 1)
  40. assert "### 第1章:测试标题" in outline
  41. assert "测试大纲" in outline
  42. def test_build_chapter_context_payload_includes_contract_sections(tmp_path):
  43. scripts_dir = Path(__file__).resolve().parents[2]
  44. if str(scripts_dir) not in sys.path:
  45. sys.path.insert(0, str(scripts_dir))
  46. from extract_chapter_context import build_chapter_context_payload
  47. from data_modules.config import DataModulesConfig
  48. from data_modules.index_manager import IndexManager, ChapterReadingPowerMeta, ReviewMetrics
  49. cfg = DataModulesConfig.from_project_root(tmp_path)
  50. cfg.ensure_dirs()
  51. state = {
  52. "project": {"genre": "xuanhuan"},
  53. "progress": {"current_chapter": 3, "total_words": 9000},
  54. "protagonist_state": {
  55. "power": {"realm": "筑基", "layer": 2},
  56. "location": "宗门",
  57. "golden_finger": {"name": "系统", "level": 1},
  58. },
  59. "strand_tracker": {"history": [{"chapter": 2, "dominant": "quest"}]},
  60. "chapter_meta": {},
  61. "disambiguation_warnings": [],
  62. "disambiguation_pending": [],
  63. }
  64. (cfg.webnovel_dir / "state.json").write_text(json.dumps(state, ensure_ascii=False), encoding="utf-8")
  65. summaries_dir = cfg.webnovel_dir / "summaries"
  66. summaries_dir.mkdir(parents=True, exist_ok=True)
  67. (summaries_dir / "ch0002.md").write_text("## 剧情摘要\n上一章总结", encoding="utf-8")
  68. outline_dir = tmp_path / "大纲"
  69. outline_dir.mkdir(parents=True, exist_ok=True)
  70. (outline_dir / "第1卷 详细大纲.md").write_text("### 第3章:测试标题\n测试大纲", encoding="utf-8")
  71. refs_dir = tmp_path / ".claude" / "references"
  72. refs_dir.mkdir(parents=True, exist_ok=True)
  73. (refs_dir / "genre-profiles.md").write_text("## xuanhuan\n- 升级线清晰", encoding="utf-8")
  74. (refs_dir / "reading-power-taxonomy.md").write_text("## xuanhuan\n- 悬念钩优先", encoding="utf-8")
  75. idx = IndexManager(cfg)
  76. idx.save_chapter_reading_power(
  77. ChapterReadingPowerMeta(chapter=2, hook_type="悬念钩", hook_strength="strong", coolpoint_patterns=["身份掉马"])
  78. )
  79. idx.save_review_metrics(
  80. ReviewMetrics(start_chapter=1, end_chapter=2, overall_score=71, dimension_scores={"plot": 71})
  81. )
  82. payload = build_chapter_context_payload(tmp_path, 3)
  83. assert payload["context_contract_version"] == "v2"
  84. assert payload.get("context_weight_stage") in {"early", "mid", "late"}
  85. assert "writing_guidance" in payload
  86. assert isinstance(payload["writing_guidance"].get("guidance_items"), list)
  87. assert isinstance(payload["writing_guidance"].get("checklist"), list)
  88. assert isinstance(payload["writing_guidance"].get("checklist_score"), dict)
  89. assert payload["genre_profile"].get("genre") == "xuanhuan"
  90. def test_render_text_contains_writing_guidance_section(tmp_path):
  91. scripts_dir = Path(__file__).resolve().parents[2]
  92. if str(scripts_dir) not in sys.path:
  93. sys.path.insert(0, str(scripts_dir))
  94. from extract_chapter_context import _render_text
  95. payload = {
  96. "chapter": 10,
  97. "outline": "测试大纲",
  98. "previous_summaries": ["### 第9章摘要\n上一章"],
  99. "state_summary": "状态",
  100. "context_contract_version": "v2",
  101. "context_weight_stage": "early",
  102. "reader_signal": {"review_trend": {"overall_avg": 72}, "low_score_ranges": [{"start_chapter": 8, "end_chapter": 9}]},
  103. "genre_profile": {
  104. "genre": "xuanhuan",
  105. "genres": ["xuanhuan", "realistic"],
  106. "composite_hints": ["以玄幻主线推进,同时保留现实议题表达"],
  107. "reference_hints": ["升级线清晰"],
  108. },
  109. "writing_guidance": {
  110. "guidance_items": ["先修低分", "钩子差异化"],
  111. "checklist": [
  112. {
  113. "id": "fix_low_score_range",
  114. "label": "修复低分区间问题",
  115. "weight": 1.4,
  116. "required": True,
  117. "source": "reader_signal.low_score_ranges",
  118. "verify_hint": "至少完成1处冲突升级",
  119. }
  120. ],
  121. "checklist_score": {
  122. "score": 81.5,
  123. "completion_rate": 0.66,
  124. "required_completion_rate": 0.75,
  125. },
  126. },
  127. }
  128. text = _render_text(payload)
  129. assert "## 写作执行建议" in text
  130. assert "先修低分" in text
  131. assert "## Contract (v2)" in text
  132. assert "- 上下文阶段权重: early" in text
  133. assert "### 执行检查清单(可评分)" in text
  134. assert "- 总权重: 1.40" in text
  135. assert "[必做][w=1.4] 修复低分区间问题" in text
  136. assert "### 执行评分" in text
  137. assert "- 评分: 81.5" in text
  138. assert "- 复合题材: xuanhuan + realistic" in text