1
0

f1-seams.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // 书名含 Windows 非法字符 → 前置拦截并指路 --dir(P2-4)
  140. const badName = await jsonFile(os.tmpdir(), `wnw-pb-bad-${process.pid}.json`, {
  141. book: { spec_version: '7.0', 书名: '冒险:开始' },
  142. 总纲: '# 总纲\nx',
  143. 卷纲: '# 第1卷\ny',
  144. })
  145. const rBad = await persistBook([], { file: badName }, ctx)
  146. assert.equal(rBad.ok, false)
  147. assert.ok(rBad.error.includes('--dir'), '报错应指路 --dir 而不是 mkdir 深处报天书')
  148. const rDir = await persistBook([], { file: badName, dir: '冒险开始' }, ctx)
  149. assert.equal(rDir.ok, true, rDir.error)
  150. } finally {
  151. await fs.rm(workdir, { recursive: true, force: true })
  152. }
  153. })
  154. test('persist-book:书仓库直启落 cwd,不登记(开发/测试兼容)', async () => {
  155. const { root, ctx, cleanup } = await makeGitBook({ 'book.yaml': BOOK })
  156. try {
  157. const p = await jsonFile(os.tmpdir(), `wnw-pb2-${process.pid}.json`, {
  158. book: { spec_version: '7.0', 书名: '测' },
  159. 总纲: '# 总纲\nx',
  160. 卷纲: '# 第1卷\ny',
  161. })
  162. const r = await persistBook([], { file: p }, ctx)
  163. assert.equal(r.ok, true, r.error)
  164. await fs.access(path.join(root, 'AGENTS.md'))
  165. } finally {
  166. await cleanup()
  167. }
  168. })
  169. const charCard = `---\n姓名: 林晚\n状态: 在世\n位置: 青云宗\n境界: 练气三层\n---\n## 设定\n玉佩线索。`
  170. test('review-input:落 工作区/审稿输入.json(含草稿全文与章号)', async () => {
  171. const { root, ctx, cleanup } = await makeGitBook({
  172. 'book.yaml': BOOK,
  173. '定稿/设定/角色/林晚.md': charCard,
  174. '工作区/草稿-A.md': '林晚握紧玉佩。',
  175. })
  176. try {
  177. const r = await reviewInput(['1'], {}, ctx)
  178. assert.equal(r.ok, true, r.error)
  179. const raw = await fs.readFile(path.join(root, '工作区', '审稿输入.json'), 'utf8')
  180. const input = JSON.parse(raw)
  181. assert.equal(input.章号, 1)
  182. assert.ok(input.草稿全文.includes('玉佩'))
  183. assert.ok(input.相关角色.some((c) => c.姓名 === '林晚' || c.正名 === '林晚'))
  184. const gone = await reviewInput(['1'], { draft: '工作区/没有.md' }, ctx)
  185. assert.equal(gone.ok, false)
  186. // --draft 传绝对路径也要能读(P2-2:resolve 而非 join)
  187. const abs = await reviewInput(['1'], { draft: path.join(root, '工作区', '草稿-A.md') }, ctx)
  188. assert.equal(abs.ok, true, abs.error)
  189. } finally {
  190. await cleanup()
  191. }
  192. })
  193. test('save-review:两审 JSON 入库落审稿单;schema 不过人话报错', async () => {
  194. const { root, ctx, cleanup } = await makeGitBook({
  195. 'book.yaml': BOOK,
  196. '工作区/草稿-A.md': '林晚握紧玉佩。',
  197. })
  198. try {
  199. const good = await jsonFile(os.tmpdir(), `wnw-sr-${process.pid}.json`, {
  200. 事实审查: { chapter: 1, issues: [] },
  201. 编辑审: {
  202. chapter: 1,
  203. issues: [
  204. {
  205. severity: 'high',
  206. category: 'pacing',
  207. location: '第1段',
  208. description: '开头太平',
  209. evidence: '首段无钩子',
  210. fix_hint: '前移冲突',
  211. blocking: false,
  212. },
  213. ],
  214. },
  215. 章摘要: '林晚得玉佩。',
  216. })
  217. const r = await saveReview(['1'], { file: good }, ctx)
  218. assert.equal(r.ok, true, r.error)
  219. assert.ok(r.output.includes('1 个问题'))
  220. const md = await fs.readFile(path.join(root, '工作区', '审稿.md'), 'utf8')
  221. assert.ok(md.includes('开头太平') && md.includes('林晚得玉佩'))
  222. await fs.access(path.join(root, '工作区', '评审报告', '事实审查.json'))
  223. const bad = await jsonFile(os.tmpdir(), `wnw-sr-bad-${process.pid}.json`, {
  224. 事实审查: { chapter: 1, issues: [{ severity: '不存在的级别' }] },
  225. 编辑审: { chapter: 1, issues: [] },
  226. })
  227. const rb = await saveReview(['1'], { file: bad }, ctx)
  228. assert.equal(rb.ok, false)
  229. assert.ok(rb.error.includes('schema'))
  230. const noDraft = await saveReview(['1'], { file: good, draft: '工作区/无.md' }, ctx)
  231. assert.equal(noDraft.ok, false)
  232. assert.ok(noDraft.error.includes('草稿'))
  233. // --draft 传绝对路径也要能读(P2-2:resolve 而非 join)
  234. const absDraft = await saveReview(
  235. ['1'],
  236. { file: good, draft: path.join(root, '工作区', '草稿-A.md') },
  237. ctx
  238. )
  239. assert.equal(absDraft.ok, true, absDraft.error)
  240. } finally {
  241. await cleanup()
  242. }
  243. })
  244. test('finalize:--payload 定稿入档 + commit;章号不一致防呆', async () => {
  245. const { root, ctx, cleanup } = await makeGitBook({
  246. 'book.yaml': BOOK,
  247. '工作区/草稿-A.md': '林晚突破。',
  248. })
  249. try {
  250. const payload = await jsonFile(os.tmpdir(), `wnw-fz-${process.pid}.json`, {
  251. frontMatter: { 章号: 1, 标题: '突破', 卷: 1, 字数: 5, 章定位: '推进' },
  252. body: '林晚突破。',
  253. summary: '林晚突破练气四层。',
  254. commitLines: {},
  255. workspaceFiles: ['工作区/草稿-A.md'],
  256. })
  257. const r = await finalizeCmd(['1'], { payload }, ctx)
  258. assert.equal(r.ok, true, r.error)
  259. assert.ok(r.output.includes('已定稿'))
  260. await fs.access(path.join(root, '定稿', '正文', '0001-突破.md'))
  261. const mismatch = await jsonFile(os.tmpdir(), `wnw-fz-mm-${process.pid}.json`, {
  262. chapterNum: 2,
  263. frontMatter: { 章号: 2, 标题: 'x' },
  264. })
  265. const rm = await finalizeCmd(['1'], { payload: mismatch }, ctx)
  266. assert.equal(rm.ok, false)
  267. assert.ok(rm.error.includes('不一致'))
  268. } finally {
  269. await cleanup()
  270. }
  271. })