index.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import path from 'node:path'
  4. import { promises as fs } from 'node:fs'
  5. import {
  6. readBatch,
  7. stagedFacts,
  8. stageChapter,
  9. judgeStop,
  10. judgeBatchQuality,
  11. rejectFrom,
  12. restageReview,
  13. discardBatch,
  14. 章状态,
  15. } from '../../src/staging/index.js'
  16. import { repoCtx } from '../commands/_helper.js'
  17. // —— fixture:已有 2 章定稿的书,批次从第 3 章起 ——
  18. const 定稿章 = (num) =>
  19. [
  20. '---',
  21. `章号: ${num}`,
  22. `标题: 第${num}章`,
  23. '卷: 1',
  24. `书内时间: 1023春月初${num}`,
  25. '字数: 100',
  26. '章定位: 推进',
  27. '钩子: 危机钩-强',
  28. '情绪定位: 铺垫',
  29. '---',
  30. '',
  31. `第${num}章正文。`,
  32. ].join('\n')
  33. function bookFiles({ 批次大小 = 3, 卷纲 = true } = {}) {
  34. const files = {
  35. 'book.yaml': `spec_version: "7.0"\n书名: 连写测试\n类型: 玄幻\n每章目标字数: 3000\n卷规模: 40\n连写批次大小: ${批次大小}\n`,
  36. '定稿/正文/0001-第1章.md': 定稿章(1),
  37. '定稿/正文/0002-第2章.md': 定稿章(2),
  38. }
  39. if (卷纲) files['大纲/卷纲/第01卷.md'] = '# 第01卷\n第三章到第五章:追查黑影。\n'
  40. return files
  41. }
  42. const 审稿单 = (num) => `# 第 ${num} 章审稿单\n\n> 完整两审模式(事实审查/编辑审各自独立上下文)。\n> 共 0 个问题:0 阻断。\n`
  43. function mkPayload(num, { 标题 = `第${num}章`, 钩子 = '危机钩-强', 书内时间 = `1023夏月初${num}`, 伏笔 = null, 收卷 = null, body = null } = {}) {
  44. const frontMatter = {
  45. 章号: num,
  46. 标题,
  47. 卷: 1,
  48. 视角: '林晚',
  49. 字数: 100,
  50. 章定位: '推进',
  51. 钩子,
  52. 情绪定位: '铺垫',
  53. }
  54. if (书内时间) frontMatter.书内时间 = 书内时间
  55. if (伏笔) frontMatter.伏笔 = 伏笔
  56. if (收卷) frontMatter.收卷 = 收卷
  57. return {
  58. frontMatter,
  59. body: body ?? `林晚在第${num}章继续追查,线索一路指向后山。夜色渐深,她的脚步没有停。`,
  60. summary: `第${num}章摘要。`,
  61. commitLines: {},
  62. workspaceFiles: [],
  63. }
  64. }
  65. async function stage(ctx, num, opts) {
  66. await fs.mkdir(path.join(ctx.repoPath, '工作区'), { recursive: true })
  67. await fs.writeFile(path.join(ctx.repoPath, '工作区', '审稿.md'), 审稿单(num), 'utf8')
  68. return stageChapter(ctx, { chapterNum: num, payload: mkPayload(num, opts) })
  69. }
  70. test('stage 首章:三件套落位、meta 记录、工作区清、停止未命中', async () => {
  71. const { ctx, cleanup } = await repoCtx(null, bookFiles())
  72. try {
  73. const r = await stage(ctx, 3, { 伏笔: ['推进 伏笔-001'] })
  74. assert.equal(r.ok, true, r.error)
  75. assert.equal(r.停止.stop, false, JSON.stringify(r.停止))
  76. const dir = path.join(ctx.repoPath, '工作区', '待定稿', '0003-第3章')
  77. for (const f of ['草稿.md', '定稿包.json', '审稿单.md']) {
  78. await fs.access(path.join(dir, f))
  79. }
  80. const batch = await readBatch(ctx.repoPath)
  81. assert.equal(batch.exists, true)
  82. assert.deepEqual(
  83. batch.章列表.map((x) => [x.章号, x.状态]),
  84. [[3, 章状态.待审收]]
  85. )
  86. // 本章工作区文件已清(审稿单搬进批次目录)
  87. await assert.rejects(() => fs.access(path.join(ctx.repoPath, '工作区', '审稿.md')))
  88. } finally {
  89. await cleanup()
  90. }
  91. })
  92. test('stage 跳章拒绝、重章覆盖(改标题清旧目录)', async () => {
  93. const { ctx, cleanup } = await repoCtx(null, bookFiles())
  94. try {
  95. const skip = await stage(ctx, 5)
  96. assert.equal(skip.ok, false)
  97. assert.match(skip.error, /连续.*第 3 章/)
  98. assert.equal((await stage(ctx, 3)).ok, true)
  99. const again = await stage(ctx, 3, { 标题: '改名章' })
  100. assert.equal(again.ok, true, again.error)
  101. const batch = await readBatch(ctx.repoPath)
  102. assert.equal(batch.章列表.length, 1)
  103. assert.equal(batch.章列表[0].标题, '改名章')
  104. await assert.rejects(() => fs.access(path.join(ctx.repoPath, '工作区', '待定稿', '0003-第3章')))
  105. } finally {
  106. await cleanup()
  107. }
  108. })
  109. test('stage 无审稿单拒绝,零写入', async () => {
  110. const { ctx, cleanup } = await repoCtx(null, bookFiles())
  111. try {
  112. const r = await stageChapter(ctx, { chapterNum: 3, payload: mkPayload(3) })
  113. assert.equal(r.ok, false)
  114. assert.match(r.error, /审稿/)
  115. await assert.rejects(() => fs.access(path.join(ctx.repoPath, '工作区', '待定稿')))
  116. } finally {
  117. await cleanup()
  118. }
  119. })
  120. test('停止条件:批次写满', async () => {
  121. const { ctx, cleanup } = await repoCtx(null, bookFiles({ 批次大小: 3 }))
  122. try {
  123. assert.equal((await stage(ctx, 3, { 伏笔: ['推进 伏笔-001'] })).停止.stop, false)
  124. assert.equal((await stage(ctx, 4, { 伏笔: ['推进 伏笔-001'] })).停止.stop, false)
  125. const r = await stage(ctx, 5, { 伏笔: ['推进 伏笔-001'] })
  126. assert.equal(r.停止.stop, true)
  127. assert.ok(r.停止.reasons.some((x) => x.includes('写满')), JSON.stringify(r.停止))
  128. } finally {
  129. await cleanup()
  130. }
  131. })
  132. test('停止条件:收卷声明终止批次', async () => {
  133. const { ctx, cleanup } = await repoCtx(null, bookFiles({ 批次大小: 8 }))
  134. try {
  135. const r = await stage(ctx, 3, { 收卷: '是', 伏笔: ['推进 伏笔-001'] })
  136. assert.equal(r.停止.stop, true)
  137. assert.ok(r.停止.reasons.some((x) => x.includes('收卷')), JSON.stringify(r.停止))
  138. } finally {
  139. await cleanup()
  140. }
  141. })
  142. test('停止条件:卷纲耗尽(当前卷无卷纲文件)', async () => {
  143. const { ctx, cleanup } = await repoCtx(null, bookFiles({ 批次大小: 8, 卷纲: false }))
  144. try {
  145. const r = await stage(ctx, 3, { 伏笔: ['推进 伏笔-001'] })
  146. assert.equal(r.停止.stop, true)
  147. assert.ok(r.停止.reasons.some((x) => x.includes('卷纲')), JSON.stringify(r.停止))
  148. } finally {
  149. await cleanup()
  150. }
  151. })
  152. test('停止条件:连续 3 章无条目变动(默认上限),有变动章重置计数', async () => {
  153. const { ctx, cleanup } = await repoCtx(null, bookFiles({ 批次大小: 8 }))
  154. try {
  155. assert.equal((await stage(ctx, 3)).停止.stop, false)
  156. assert.equal((await stage(ctx, 4)).停止.stop, false)
  157. const r5 = await stage(ctx, 5)
  158. assert.equal(r5.停止.stop, true)
  159. assert.ok(r5.停止.reasons.some((x) => x.includes('无条目变动')), JSON.stringify(r5.停止))
  160. // 第 5 章重暂存并带条目变动 → 尾部计数归零,不再停
  161. const r5b = await stage(ctx, 5, { 伏笔: ['推进 伏笔-001'] })
  162. assert.equal(r5b.停止.stop, false, JSON.stringify(r5b.停止))
  163. } finally {
  164. await cleanup()
  165. }
  166. })
  167. // —— 批次质检(纯函数)——
  168. const 质检章 = (num, { 钩子 = '危机钩-强', 书内时间 = 'x', body = '正文。' } = {}) => ({
  169. 章号: num,
  170. frontMatter: { 钩子, 书内时间 },
  171. body,
  172. })
  173. test('批次质检:弱钩尾达上限', async () => {
  174. const staged = [1, 2, 3].map((n) => 质检章(n, { 钩子: '悬念钩-弱' }))
  175. const q = judgeBatchQuality(staged, null, { 连续弱钩上限: 3 })
  176. assert.equal(q.过线, false)
  177. assert.ok(q.原因.some((x) => x.includes('弱钩')), JSON.stringify(q))
  178. // 尾部只有 2 章弱钩 → 过线
  179. const staged2 = [质检章(1), 质检章(2, { 钩子: '悬念钩-弱' }), 质检章(3, { 钩子: '悬念钩-弱' })]
  180. assert.equal(judgeBatchQuality(staged2, null, { 连续弱钩上限: 3 }).过线, true)
  181. })
  182. test('批次质检:缺书内时间列出章号', async () => {
  183. const q = judgeBatchQuality([质检章(3), 质检章(4, { 书内时间: '' })], null, {})
  184. assert.equal(q.过线, false)
  185. assert.ok(q.原因.some((x) => x.includes('第 4 章') && x.includes('书内时间')), JSON.stringify(q))
  186. })
  187. test('批次质检:句式 vs 基线 29% 不停 31% 停;无基线跳过', async () => {
  188. const 字池 =
  189. '天地玄黄宇宙洪荒日月盈昃辰宿列张寒来暑往秋收冬藏闰余成岁律吕调阳云腾致雨露结为霜金生丽水玉出昆冈剑号巨阙珠称夜光果珍李柰菜重芥姜海咸河淡鳞潜羽翔龙师火帝鸟官人皇始制文字乃服衣裳推位让国有虞陶唐吊民伐罪周发殷汤坐朝问道垂拱平章爱育黎首臣伏戎羌遐迩一体率宾归王鸣凤在竹白驹食场'
  190. const bodyOf = (lengths) => {
  191. let pos = 0
  192. const parts = []
  193. for (const n of lengths) {
  194. parts.push(字池.slice(pos, pos + n))
  195. pos += n
  196. }
  197. return parts.join('。') + '。'
  198. }
  199. const base = { avg_sentence_length: 10, sentence_length_variance: 0 }
  200. const at29 = [质检章(3, { body: bodyOf([12, 13, 13, 13, 13, 13, 13, 13, 13, 13]) })]
  201. assert.equal(judgeBatchQuality(at29, base, {}).过线, true, '偏 29% 不停')
  202. const at31 = [质检章(3, { body: bodyOf([13, 13, 13, 13, 13, 13, 13, 13, 13, 14]) })]
  203. const q = judgeBatchQuality(at31, base, {})
  204. assert.equal(q.过线, false, '偏 31% 停')
  205. assert.ok(q.原因.some((x) => x.includes('平均句长')), JSON.stringify(q))
  206. assert.equal(judgeBatchQuality(at31, null, {}).过线, true, '无基线句式判据跳过')
  207. })
  208. // —— 打回 / 重审 / 丢弃 / 对账 ——
  209. test('rejectFrom:K 打回清工件,K+1..N 受影响;restageReview 回待审收', async () => {
  210. const { ctx, cleanup } = await repoCtx(null, bookFiles({ 批次大小: 8 }))
  211. try {
  212. for (const n of [3, 4, 5]) await stage(ctx, n, { 伏笔: ['推进 伏笔-001'] })
  213. const r = await rejectFrom(ctx.repoPath, 4)
  214. assert.equal(r.ok, true, r.error)
  215. assert.deepEqual(r.受影响, [5])
  216. const batch = await readBatch(ctx.repoPath)
  217. assert.deepEqual(
  218. batch.章列表.map((x) => [x.章号, x.状态]),
  219. [
  220. [3, 章状态.待审收],
  221. [4, 章状态.打回],
  222. [5, 章状态.受影响],
  223. ]
  224. )
  225. // 打回章工件已清
  226. await assert.rejects(() =>
  227. fs.access(path.join(ctx.repoPath, '工作区', '待定稿', '0004-第4章', '草稿.md'))
  228. )
  229. // 打回章不能只重审
  230. const bad = await restageReview(ctx.repoPath, 4)
  231. assert.equal(bad.ok, false)
  232. assert.match(bad.error, /重写/)
  233. // 受影响章重审:新审稿单 → 待审收
  234. await fs.writeFile(path.join(ctx.repoPath, '工作区', '审稿.md'), 审稿单(5), 'utf8')
  235. const ok5 = await restageReview(ctx.repoPath, 5)
  236. assert.equal(ok5.ok, true, ok5.error)
  237. assert.equal((await readBatch(ctx.repoPath)).章列表.find((x) => x.章号 === 5).状态, 章状态.待审收)
  238. } finally {
  239. await cleanup()
  240. }
  241. })
  242. test('批次.json 损坏 → 按目录重建,保守标记受影响', async () => {
  243. const { ctx, cleanup } = await repoCtx(null, bookFiles())
  244. try {
  245. await stage(ctx, 3, { 伏笔: ['推进 伏笔-001'] })
  246. await fs.writeFile(path.join(ctx.repoPath, '工作区', '待定稿', '批次.json'), '{{{', 'utf8')
  247. const batch = await readBatch(ctx.repoPath)
  248. assert.equal(batch.exists, true)
  249. assert.equal(batch.章列表[0].状态, 章状态.受影响)
  250. assert.ok(batch.warnings.some((w) => w.includes('批次.json')), JSON.stringify(batch.warnings))
  251. } finally {
  252. await cleanup()
  253. }
  254. })
  255. test('discardBatch:整批丢弃,工作区批次消失', async () => {
  256. const { ctx, cleanup } = await repoCtx(null, bookFiles())
  257. try {
  258. await stage(ctx, 3)
  259. const r = await discardBatch(ctx.repoPath)
  260. assert.equal(r.ok, true)
  261. assert.equal(r.章数, 1)
  262. assert.equal((await readBatch(ctx.repoPath)).exists, false)
  263. await assert.rejects(() => fs.access(path.join(ctx.repoPath, '工作区', '待定稿')))
  264. } finally {
  265. await cleanup()
  266. }
  267. })
  268. test('stagedFacts:声明与定稿包合并出条目/名册/时间线/信息差事实', async () => {
  269. const { ctx, cleanup } = await repoCtx(null, bookFiles({ 批次大小: 8 }))
  270. try {
  271. await fs.mkdir(path.join(ctx.repoPath, '工作区'), { recursive: true })
  272. await fs.writeFile(path.join(ctx.repoPath, '工作区', '审稿.md'), 审稿单(3), 'utf8')
  273. const p = mkPayload(3, { 伏笔: ['埋下 伏笔-009'] })
  274. p.threadCreates = [
  275. { id: '伏笔-009', 短题: '古钟', frontMatter: { 强度: '中', 状态: '进行', 开启章: 3 }, body: '## 描述\n古钟。\n' },
  276. ]
  277. p.rosterUpserts = [{ 正名: '古钟长老', 别名: '钟叟, 老钟', 类型: 'character', 首现章: 3 }]
  278. p.characterUpdates = [{ name: '林晚', updates: { 最后变更章: 3 } }]
  279. p.timelineRows = [{ volumeNum: 1, row: { 章: 3, 书内时间: '夏月初三', 一句话事件: '闻古钟', 在场: '林晚' } }]
  280. p.secretWrites = [{ id: '信息差-002-钟声', frontMatter: { 读者已知: false, 登记章: 3, 关键词: ['古钟'] }, content: '## 内容\n钟声有古怪。\n' }]
  281. const r = await stageChapter(ctx, { chapterNum: 3, payload: p })
  282. assert.equal(r.ok, true, r.error)
  283. const facts = await stagedFacts(ctx.repoPath)
  284. assert.equal(facts.exists, true)
  285. assert.equal(facts.chapters.length, 1)
  286. const t = facts.threads.get('伏笔-009')
  287. assert.ok(t, '声明+定稿包都指向的新条目必须在')
  288. assert.equal(t.新开, true)
  289. assert.equal(t.状态, '进行')
  290. assert.ok(facts.newEntities.has('古钟长老'))
  291. assert.ok(facts.newAliases.has('钟叟') && facts.newAliases.has('老钟'))
  292. assert.deepEqual(facts.characterUpdates.get('林晚'), { 最后变更章: 3 })
  293. assert.equal(facts.timelineRows.length, 1)
  294. assert.equal(facts.secretWrites.length, 1)
  295. assert.equal(facts.总字数, 100)
  296. } finally {
  297. await cleanup()
  298. }
  299. })