test_extract_chapter_context.py 5.8 KB

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