volume-summary.spec.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { afterAll, describe, expect, it } from 'vitest'
  2. import * as fs from 'node:fs'
  3. import * as path from 'node:path'
  4. import * as os from 'node:os'
  5. import { computeVolumeSummaryCandidate, parseDocument, paths, prepareVolumeSummary, seedMinDesign, serializeDocument, writeFileAtomic } from '../src/index'
  6. import { removeSync } from '../src/repo/remove'
  7. const roots: string[] = []
  8. function mkBook(): string {
  9. const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'webnovel-volsum-'))
  10. roots.push(dir)
  11. seedMinDesign(dir)
  12. return dir
  13. }
  14. afterAll(() => { for (const r of roots) { try { fs.rmSync(r, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 }) } catch { /* Windows 句柄延迟释放 */ } } })
  15. /** 卷一 N 章定稿;章摘要按 needSummaries 决定是否写齐;另附账本线索与卷纲卷末兑现。 */
  16. function mkVolumeBook(root: string, chapters: number, opts: { 缺摘要?: number[] } = {}): void {
  17. writeFileAtomic(root, paths.卷纲(1), [
  18. '# 卷纲', '', '卷01|1 案/3 章,单章约 100 字', '',
  19. '## 叙事结构 〔已确认〕', '', '三章立规矩', '',
  20. '## 弧线 〔已确认〕', '', 'x', '',
  21. '## 线索推进 〔已确认〕', '', 'x', '',
  22. '## 卷末兑现 〔已确认〕', '', '- 结案 1 案,真凶点明。', '- 主角付出代价。', '',
  23. ].join('\n'))
  24. for (let n = 1; n <= chapters; n += 1) {
  25. const name = `第${n}案`
  26. const fin = path.join(root, `定稿/卷01/${String(n).padStart(4, '0')}-${name}.md`)
  27. fs.mkdirSync(path.dirname(fin), { recursive: true })
  28. fs.writeFileSync(fin, serializeDocument({ 状态: '已定稿' }, `第${n}章正文`), 'utf-8')
  29. if (opts.缺摘要?.includes(n)) continue
  30. const sum = path.join(root, paths.章摘要(1, n, name))
  31. fs.mkdirSync(path.dirname(sum), { recursive: true })
  32. fs.writeFileSync(sum, `# 章摘要\n\n${name}摘要:灯灭之因第${n}步。`, 'utf-8')
  33. }
  34. for (const name of ['故事线', '人物弧线', '承诺', '时间线'] as const) {
  35. writeFileAtomic(root, paths.账本(name), `# ${name}\n`)
  36. }
  37. writeFileAtomic(root, paths.账本('线索'), '# 线索\n\n## 铜托座\n状态:已收\n埋设点:第0001章\n\n### 正文\n铜托座有新磕痕,与灯竿同源。\n\n## 糖纸\n状态:已埋\n埋设点:第0002章\n\n### 正文\n甜味引向第二案。\n')
  38. }
  39. describe('卷摘要候选与落盘(任务21, 件一/AC2)', () => {
  40. it('章摘要齐全 → 候选含章摘要汇编/线索收束/卷末兑现;只算不写', () => {
  41. const root = mkBook()
  42. mkVolumeBook(root, 3)
  43. const before = fs.existsSync(path.join(root, paths.卷摘要(1)))
  44. const r = computeVolumeSummaryCandidate(root, 1)
  45. expect(r.ok).toBe(true)
  46. if (!r.ok) return
  47. expect(r.候选).toContain('第1案摘要')
  48. expect(r.候选).toContain('第3案摘要')
  49. expect(r.候选).toContain('铜托座〔已收〕')
  50. expect(r.候选).toContain('糖纸〔已埋〕')
  51. expect(r.候选).toContain('结案 1 案,真凶点明')
  52. expect(r.章摘要来源).toHaveLength(3)
  53. // 只算不写:不产卷摘要文件
  54. expect(fs.existsSync(path.join(root, paths.卷摘要(1)))).toBe(before)
  55. })
  56. it('缺章摘要 → 如实报缺清单,不产候选不落盘', () => {
  57. const root = mkBook()
  58. mkVolumeBook(root, 3, { 缺摘要: [2] })
  59. const r = computeVolumeSummaryCandidate(root, 1)
  60. expect(r.ok).toBe(false)
  61. if (r.ok) return
  62. expect(r.缺章摘要).toEqual([2])
  63. expect(r.reason).toContain('报缺')
  64. expect(fs.existsSync(path.join(root, paths.卷摘要(1)))).toBe(false)
  65. })
  66. it('无定稿章 → 如实报缺', () => {
  67. const root = mkBook()
  68. const r = computeVolumeSummaryCandidate(root, 1)
  69. expect(r.ok).toBe(false)
  70. if (r.ok) return
  71. expect(r.缺章摘要).toEqual([])
  72. expect(r.reason).toContain('无定稿章')
  73. })
  74. it('落盘:首次 v1、原样重跑复用版本、正文变化 v2 父版本 1', () => {
  75. const root = mkBook()
  76. mkVolumeBook(root, 3)
  77. const c = computeVolumeSummaryCandidate(root, 1)
  78. if (!c.ok) throw new Error('候选生成失败')
  79. const op1 = prepareVolumeSummary(root, 1, c.候选)
  80. fs.mkdirSync(path.join(root, path.dirname(paths.卷摘要(1))), { recursive: true })
  81. fs.writeFileSync(path.join(root, op1.relPath), op1.content, 'utf-8')
  82. expect(op1.result.版本).toBe(1)
  83. expect(op1.result.首次写入).toBe(true)
  84. const doc1 = parseDocument(fs.readFileSync(path.join(root, paths.卷摘要(1)), 'utf-8'))
  85. expect(doc1.ok && doc1.data.fields['状态']).toBe('已确认')
  86. expect(doc1.ok && doc1.data.fields['生成模块']).toBe('卷摘要确认')
  87. const op2 = prepareVolumeSummary(root, 1, c.候选)
  88. expect(op2.result.版本).toBe(1)
  89. expect(op2.result.首次写入).toBe(false)
  90. expect(op2.content).toBe(fs.readFileSync(path.join(root, paths.卷摘要(1)), 'utf-8'))
  91. const op3 = prepareVolumeSummary(root, 1, `${c.候选}\n补一行修订。\n`)
  92. expect(op3.result.版本).toBe(2)
  93. const doc3 = parseDocument(op3.content)
  94. expect(doc3.ok && doc3.data.fields['父版本']).toBe(1)
  95. })
  96. })
  97. describe('卷摘要事实边界与账本失败(F21-1/F21-2)', () => {
  98. function mkLedgerBook(root: string, chapters: number, ledgerText: string | null): void {
  99. mkVolumeBook(root, chapters)
  100. // 账本五件齐(故事线/人物弧线/承诺/时间线空壳,线索按用例覆写或删除)
  101. for (const name of ['故事线', '人物弧线', '承诺', '时间线'] as const) {
  102. writeFileAtomic(root, paths.账本(name), `# ${name}\n`)
  103. }
  104. if (ledgerText === null) {
  105. removeSync(path.join(root, paths.账本('线索')))
  106. } else {
  107. writeFileAtomic(root, paths.账本('线索'), ledgerText)
  108. }
  109. }
  110. it('①后卷新增秘密不入旧卷摘要(埋设点第9章超过卷末)', () => {
  111. const root = mkBook()
  112. mkLedgerBook(root, 1, '# 线索\n\n## 秘密\n状态:已埋\n埋设点:第0009章\n\n### 正文\nFUTURE_ONLY_SECRET。\n')
  113. const r = computeVolumeSummaryCandidate(root, 1)
  114. expect(r.ok).toBe(true)
  115. if (!r.ok) return
  116. expect(r.候选).not.toContain('FUTURE_ONLY_SECRET')
  117. expect(r.候选).not.toContain('秘密')
  118. })
  119. it('②同一线索后卷已收/更新正文,旧卷摘要保留当时状态与正文(不回流不消失)', () => {
  120. const root = mkBook()
  121. mkLedgerBook(root, 3, [
  122. '# 线索', '',
  123. '## 灯油账', '状态:已埋', '埋设点:第0002章', '',
  124. '### 正文', '油里掺水,账上多记三成。', '',
  125. '## 灯油账', '状态:已收', '埋设点:第0002章', '实际落点:定稿/卷02/0009-第九章.md', '',
  126. '### 正文', 'FUTURE_STATE_后卷已收正文。', '',
  127. ].join('\n'))
  128. const r = computeVolumeSummaryCandidate(root, 1)
  129. expect(r.ok).toBe(true)
  130. if (!r.ok) return
  131. expect(r.候选).toContain('灯油账〔已埋〕')
  132. expect(r.候选).toContain('油里掺水')
  133. expect(r.候选).not.toContain('FUTURE_STATE')
  134. expect(r.候选).not.toContain('灯油账〔已收〕')
  135. })
  136. it('③卷内正常线索与合理延续线索(已收/已示)均保留', () => {
  137. const root = mkBook()
  138. mkLedgerBook(root, 3, [
  139. '# 线索', '',
  140. '## 铜灯钩', '状态:已收', '埋设点:第0001章', '',
  141. '### 正文', '钩上新划痕。', '',
  142. '## 灯油账', '状态:已示', '埋设点:第0002章', '预期兑现区间:第4-6章', '',
  143. '### 正文', '账目存疑,卷二再接。', '',
  144. ].join('\n'))
  145. const r = computeVolumeSummaryCandidate(root, 1)
  146. expect(r.ok).toBe(true)
  147. if (!r.ok) return
  148. expect(r.候选).toContain('铜灯钩〔已收〕')
  149. expect(r.候选).toContain('灯油账〔已示〕')
  150. expect(r.候选).toContain('账目存疑')
  151. })
  152. it('④无法判定发生时点的条目报边界不明,不静默当作卷内事实', () => {
  153. const root = mkBook()
  154. mkLedgerBook(root, 1, '# 线索\n\n## 无根线索\n状态:已埋\n计划来源:大纲/分卷布局.md@1\n\n### 正文\n无时点。\n')
  155. const r = computeVolumeSummaryCandidate(root, 1)
  156. expect(r.ok).toBe(true)
  157. if (!r.ok) return
  158. expect(r.线索边界不明).toEqual(['无根线索'])
  159. expect(r.候选).not.toContain('无根线索〔已埋〕')
  160. expect(r.候选).toContain('边界不明')
  161. })
  162. it('⑤删线索账本(missing) → 不产候选不落盘,报可操作缺失信息', () => {
  163. const root = mkBook()
  164. mkLedgerBook(root, 1, null)
  165. const r = computeVolumeSummaryCandidate(root, 1)
  166. expect(r.ok).toBe(false)
  167. if (r.ok) return
  168. expect(r.reason).toContain('账本')
  169. expect(fs.existsSync(path.join(root, paths.卷摘要(1)))).toBe(false)
  170. })
  171. it('⑥账本损坏(parse-error) → 不产候选,报解析信息', () => {
  172. const root = mkBook()
  173. mkLedgerBook(root, 1, '# 线索\n\n## 坏条目\n状态:已埋\n状态:已示\n\n### 正文\n重复字段。\n')
  174. const r = computeVolumeSummaryCandidate(root, 1)
  175. expect(r.ok).toBe(false)
  176. if (r.ok) return
  177. expect(r.reason).toContain('解析')
  178. expect(fs.existsSync(path.join(root, paths.卷摘要(1)))).toBe(false)
  179. })
  180. it('⑦部分账本可读但整体 ok:false(他卷账本损坏) → 仍不产候选', () => {
  181. const root = mkBook()
  182. mkLedgerBook(root, 1, '# 线索\n\n## 铜灯钩\n状态:已收\n埋设点:第0001章\n\n### 正文\n钩上新划痕。\n')
  183. writeFileAtomic(root, paths.账本('故事线'), '# 故事线\n\n## 坏\n状态:进行中\n状态:已结束\n')
  184. const r = computeVolumeSummaryCandidate(root, 1)
  185. expect(r.ok).toBe(false)
  186. if (r.ok) return
  187. expect(r.reason).toContain('解析')
  188. })
  189. it('⑧成功空线索 → 候选如实写无线索(ok:true)', () => {
  190. const root = mkBook()
  191. mkLedgerBook(root, 1, '# 线索\n')
  192. const r = computeVolumeSummaryCandidate(root, 1)
  193. expect(r.ok).toBe(true)
  194. if (!r.ok) return
  195. expect(r.候选).toContain('无已收或进行中线索')
  196. expect(r.线索边界不明).toEqual([])
  197. })
  198. })