1
0

f1-seams.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import os from 'node:os'
  4. import path from 'node:path'
  5. import { promises as fs } from 'node:fs'
  6. import { makeGitBook, chapter } from '../state-machine/_helper.js'
  7. import { run as nextRun } from '../../src/commands/next.js'
  8. import { run as persistOutline } from '../../src/commands/persist-outline.js'
  9. import { run as persistVolumeReview } from '../../src/commands/persist-volume-review.js'
  10. import { run as persistRepair } from '../../src/commands/persist-repair.js'
  11. import { run as persistBook } from '../../src/commands/persist-book.js'
  12. import { run as reviewInput } from '../../src/commands/review-input.js'
  13. import { run as saveReview } from '../../src/commands/save-review.js'
  14. import { run as finalizeCmd } from '../../src/commands/finalize.js'
  15. import { readBooksRegistry } from '../../src/session/index.js'
  16. const BOOK = 'spec_version: "7.0"\n书名: 测\n卷规模: 40\n体检周期: 50\n'
  17. async function jsonFile(dir, name, data) {
  18. const p = path.join(dir, name)
  19. await fs.writeFile(p, JSON.stringify(data, null, 2), 'utf8')
  20. return p
  21. }
  22. test('next --json:输出完整状态机 DTO(F1 C1)', async () => {
  23. const { ctx, cleanup } = await makeGitBook({ 'book.yaml': BOOK, '定稿/正文/0001-起.md': chapter(1) })
  24. try {
  25. const r = await nextRun([], { json: true }, ctx)
  26. assert.equal(r.ok, true)
  27. const dto = JSON.parse(r.output)
  28. for (const key of ['ok', 'gitHealth', '序', 'state', 'needsAI', 'message', 'dto']) {
  29. assert.ok(key in dto, `缺字段 ${key}`)
  30. }
  31. assert.equal(typeof dto.序, 'number')
  32. // 缺省输出仍是人读
  33. const human = await nextRun([], {}, ctx)
  34. assert.ok(human.output.includes('【当前状态】'))
  35. } finally {
  36. await cleanup()
  37. }
  38. })
  39. test('persist-outline:--file 落细纲;缺文件/坏 JSON/缺字段人话报错', async () => {
  40. const { root, ctx, cleanup } = await makeGitBook({ 'book.yaml': BOOK })
  41. try {
  42. const p = await jsonFile(os.tmpdir(), `wnw-ol-${process.pid}.json`, { 细纲: '## 本章要写到的事\n突破。' })
  43. const r = await persistOutline([], { file: p }, ctx)
  44. assert.equal(r.ok, true)
  45. const content = await fs.readFile(path.join(root, '工作区', '细纲.md'), 'utf8')
  46. assert.ok(content.includes('突破'))
  47. const miss = await persistOutline([], {}, ctx)
  48. assert.equal(miss.ok, false)
  49. assert.ok(miss.error.includes('--file'))
  50. const gone = await persistOutline([], { file: path.join(os.tmpdir(), '不存在.json') }, ctx)
  51. assert.equal(gone.ok, false)
  52. assert.ok(gone.error.includes('读不到'))
  53. const badPath = path.join(os.tmpdir(), `wnw-bad-${process.pid}.json`)
  54. await fs.writeFile(badPath, '{坏的', 'utf8')
  55. const bad = await persistOutline([], { file: badPath }, ctx)
  56. assert.equal(bad.ok, false)
  57. assert.ok(bad.error.includes('JSON'))
  58. const empty = await jsonFile(os.tmpdir(), `wnw-empty-${process.pid}.json`, {})
  59. const noField = await persistOutline([], { file: empty }, ctx)
  60. assert.equal(noField.ok, false)
  61. assert.ok(noField.error.includes('细纲'))
  62. } finally {
  63. await cleanup()
  64. }
  65. })
  66. test('persist-volume-review:卷摘要+下卷卷纲落盘;坏卷号报错', async () => {
  67. const { root, ctx, cleanup } = await makeGitBook({ 'book.yaml': BOOK })
  68. try {
  69. const p = await jsonFile(os.tmpdir(), `wnw-vr-${process.pid}.json`, {
  70. 卷号: 1,
  71. 卷摘要: '# 第一卷\n入门完毕。',
  72. 下卷卷纲: '# 第2卷\n出山。',
  73. })
  74. const r = await persistVolumeReview([], { file: p }, ctx)
  75. assert.equal(r.ok, true)
  76. await fs.access(path.join(root, '定稿', '摘要', '卷摘要', '第01卷.md'))
  77. await fs.access(path.join(root, '大纲', '卷纲', '第02卷.md'))
  78. const bad = await jsonFile(os.tmpdir(), `wnw-vr-bad-${process.pid}.json`, { 卷号: 0, 卷摘要: 'x' })
  79. const rb = await persistVolumeReview([], { file: bad }, ctx)
  80. assert.equal(rb.ok, false)
  81. assert.ok(rb.error.includes('卷号'))
  82. } finally {
  83. await cleanup()
  84. }
  85. })
  86. test('persist-repair:修检测失败清单内文件;清单外拒绝', async () => {
  87. const { root, ctx, cleanup } = await makeGitBook({ 'book.yaml': BOOK })
  88. try {
  89. // 造一个解析失败的伏笔条目(检测后进入 allowedFiles)
  90. const brokenRel = '大纲/伏笔/伏笔-001-试.md'
  91. await fs.mkdir(path.join(root, '大纲', '伏笔'), { recursive: true })
  92. await fs.writeFile(path.join(root, brokenRel), '---\na: [未闭合\n---\n正文', 'utf8')
  93. const fixed = '---\nid: 伏笔-001\n短题: 试\n状态: 进行\n---\n正文'
  94. const p = await jsonFile(os.tmpdir(), `wnw-rp-${process.pid}.json`, {
  95. repairs: [{ file: brokenRel, content: fixed }],
  96. })
  97. const r = await persistRepair([], { file: p }, ctx)
  98. assert.equal(r.ok, true, r.error)
  99. const after = await fs.readFile(path.join(root, brokenRel), 'utf8')
  100. assert.ok(after.includes('id: 伏笔-001'))
  101. // 清单外文件(book.yaml 好好的)→ 拒绝
  102. const evil = await jsonFile(os.tmpdir(), `wnw-rp-evil-${process.pid}.json`, {
  103. repairs: [{ file: 'book.yaml', content: '---\nx: 1\n---\n' }],
  104. })
  105. const re = await persistRepair([], { file: evil }, ctx)
  106. assert.equal(re.ok, false)
  107. assert.ok(re.error.includes('拒绝'))
  108. } finally {
  109. await cleanup()
  110. }
  111. })
  112. test('persist-book:工作目录模式建书+指路 AGENTS.md+登记置当前;同名防覆盖', async () => {
  113. const workdir = await fs.mkdtemp(path.join(os.tmpdir(), 'wnw-pb-'))
  114. try {
  115. await fs.mkdir(path.join(workdir, '.webnovel'), { recursive: true })
  116. const p = await jsonFile(os.tmpdir(), `wnw-pb-${process.pid}.json`, {
  117. book: { spec_version: '7.0', 书名: '剑起青云', 卷规模: 40 },
  118. 总纲: '# 总纲\n## 结局\n登顶。',
  119. 卷纲: '# 第1卷\n入门。',
  120. })
  121. const ctx = { workdir, repoPath: null }
  122. const r = await persistBook([], { file: p }, ctx)
  123. assert.equal(r.ok, true, r.error)
  124. const repo = path.join(workdir, '剑起青云')
  125. await fs.access(path.join(repo, 'book.yaml'))
  126. const agents = await fs.readFile(path.join(repo, 'AGENTS.md'), 'utf8')
  127. assert.ok(agents.includes('工作目录') && agents.includes('剑起青云'), '指路 AGENTS.md 应指回工作目录')
  128. const reg = await readBooksRegistry(workdir)
  129. assert.equal(reg.books.length, 1)
  130. assert.equal(reg.books[0].当前, true)
  131. // 同名再建 → 防覆盖
  132. const again = await persistBook([], { file: p }, ctx)
  133. assert.equal(again.ok, false)
  134. assert.ok(again.error.includes('不覆盖'))
  135. // 目录名不合法
  136. const badDir = await persistBook([], { file: p, dir: '../逃逸' }, ctx)
  137. assert.equal(badDir.ok, false)
  138. assert.ok(badDir.error.includes('不合法'))
  139. } finally {
  140. await fs.rm(workdir, { recursive: true, force: true })
  141. }
  142. })
  143. test('persist-book:书仓库直启落 cwd,不登记(开发/测试兼容)', async () => {
  144. const { root, ctx, cleanup } = await makeGitBook({ 'book.yaml': BOOK })
  145. try {
  146. const p = await jsonFile(os.tmpdir(), `wnw-pb2-${process.pid}.json`, {
  147. book: { spec_version: '7.0', 书名: '测' },
  148. 总纲: '# 总纲\nx',
  149. 卷纲: '# 第1卷\ny',
  150. })
  151. const r = await persistBook([], { file: p }, ctx)
  152. assert.equal(r.ok, true, r.error)
  153. await fs.access(path.join(root, 'AGENTS.md'))
  154. } finally {
  155. await cleanup()
  156. }
  157. })
  158. const charCard = `---\n姓名: 林晚\n状态: 在世\n位置: 青云宗\n境界: 练气三层\n---\n## 设定\n玉佩线索。`
  159. test('review-input:落 工作区/审稿输入.json(含草稿全文与章号)', async () => {
  160. const { root, ctx, cleanup } = await makeGitBook({
  161. 'book.yaml': BOOK,
  162. '定稿/设定/角色/林晚.md': charCard,
  163. '工作区/草稿-A.md': '林晚握紧玉佩。',
  164. })
  165. try {
  166. const r = await reviewInput(['1'], {}, ctx)
  167. assert.equal(r.ok, true, r.error)
  168. const raw = await fs.readFile(path.join(root, '工作区', '审稿输入.json'), 'utf8')
  169. const input = JSON.parse(raw)
  170. assert.equal(input.章号, 1)
  171. assert.ok(input.草稿全文.includes('玉佩'))
  172. assert.ok(input.相关角色.some((c) => c.姓名 === '林晚' || c.正名 === '林晚'))
  173. const gone = await reviewInput(['1'], { draft: '工作区/没有.md' }, ctx)
  174. assert.equal(gone.ok, false)
  175. } finally {
  176. await cleanup()
  177. }
  178. })
  179. test('save-review:两审 JSON 入库落审稿单;schema 不过人话报错', async () => {
  180. const { root, ctx, cleanup } = await makeGitBook({
  181. 'book.yaml': BOOK,
  182. '工作区/草稿-A.md': '林晚握紧玉佩。',
  183. })
  184. try {
  185. const good = await jsonFile(os.tmpdir(), `wnw-sr-${process.pid}.json`, {
  186. 事实审查: { chapter: 1, issues: [] },
  187. 编辑审: {
  188. chapter: 1,
  189. issues: [
  190. {
  191. severity: 'high',
  192. category: 'pacing',
  193. location: '第1段',
  194. description: '开头太平',
  195. evidence: '首段无钩子',
  196. fix_hint: '前移冲突',
  197. blocking: false,
  198. },
  199. ],
  200. },
  201. 章摘要: '林晚得玉佩。',
  202. })
  203. const r = await saveReview(['1'], { file: good }, ctx)
  204. assert.equal(r.ok, true, r.error)
  205. assert.ok(r.output.includes('1 个问题'))
  206. const md = await fs.readFile(path.join(root, '工作区', '审稿.md'), 'utf8')
  207. assert.ok(md.includes('开头太平') && md.includes('林晚得玉佩'))
  208. await fs.access(path.join(root, '工作区', '评审报告', '事实审查.json'))
  209. const bad = await jsonFile(os.tmpdir(), `wnw-sr-bad-${process.pid}.json`, {
  210. 事实审查: { chapter: 1, issues: [{ severity: '不存在的级别' }] },
  211. 编辑审: { chapter: 1, issues: [] },
  212. })
  213. const rb = await saveReview(['1'], { file: bad }, ctx)
  214. assert.equal(rb.ok, false)
  215. assert.ok(rb.error.includes('schema'))
  216. const noDraft = await saveReview(['1'], { file: good, draft: '工作区/无.md' }, ctx)
  217. assert.equal(noDraft.ok, false)
  218. assert.ok(noDraft.error.includes('草稿'))
  219. } finally {
  220. await cleanup()
  221. }
  222. })
  223. test('finalize:--payload 定稿入档 + commit;章号不一致防呆', async () => {
  224. const { root, ctx, cleanup } = await makeGitBook({
  225. 'book.yaml': BOOK,
  226. '工作区/草稿-A.md': '林晚突破。',
  227. })
  228. try {
  229. const payload = await jsonFile(os.tmpdir(), `wnw-fz-${process.pid}.json`, {
  230. frontMatter: { 章号: 1, 标题: '突破', 卷: 1, 字数: 5, 章定位: '推进' },
  231. body: '林晚突破。',
  232. summary: '林晚突破练气四层。',
  233. commitLines: {},
  234. workspaceFiles: ['工作区/草稿-A.md'],
  235. })
  236. const r = await finalizeCmd(['1'], { payload }, ctx)
  237. assert.equal(r.ok, true, r.error)
  238. assert.ok(r.output.includes('已定稿'))
  239. await fs.access(path.join(root, '定稿', '正文', '0001-突破.md'))
  240. const mismatch = await jsonFile(os.tmpdir(), `wnw-fz-mm-${process.pid}.json`, {
  241. chapterNum: 2,
  242. frontMatter: { 章号: 2, 标题: 'x' },
  243. })
  244. const rm = await finalizeCmd(['1'], { payload: mismatch }, ctx)
  245. assert.equal(rm.ok, false)
  246. assert.ok(rm.error.includes('不一致'))
  247. } finally {
  248. await cleanup()
  249. }
  250. })