persist.test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { execFile } from 'node:child_process'
  7. import { promisify } from 'node:util'
  8. import {
  9. persistRepair,
  10. persistCreateBook,
  11. persistVolumeReview,
  12. persistDraftOutline,
  13. } from '../../src/state-machine/persist.js'
  14. async function tmpRepo() {
  15. const root = await fs.mkdtemp(path.join(os.tmpdir(), 'wnw-persist-'))
  16. return { ctx: { repoPath: root }, root, cleanup: () => fs.rm(root, { recursive: true, force: true }) }
  17. }
  18. const read = (root, rel) => fs.readFile(path.join(root, rel), 'utf8')
  19. const exec = promisify(execFile)
  20. test('persistCreateBook(P0-2)→ git init + core.quotepath=false + .gitignore(书仓库工程化)', async () => {
  21. const { ctx, root, cleanup } = await tmpRepo()
  22. try {
  23. const r = await persistCreateBook(ctx, {
  24. book: { spec_version: '7.0', 书名: '测' },
  25. 总纲: '# 总纲',
  26. 卷纲: '# 第1卷',
  27. })
  28. assert.equal(r.ok, true, r.error)
  29. const { stdout: inside } = await exec('git', ['rev-parse', '--is-inside-work-tree'], { cwd: root })
  30. assert.equal(inside.trim(), 'true', '建书应 git init 出一个仓库')
  31. const { stdout: qp } = await exec('git', ['config', 'core.quotepath'], { cwd: root })
  32. assert.equal(qp.trim(), 'false', '应设 core.quotepath=false(spec quality §3.3)')
  33. const gi = await read(root, '.gitignore')
  34. assert.ok(gi.includes('.cache/') && gi.includes('工作区/'), '.gitignore 应 ignore .cache 与 工作区')
  35. } finally { await cleanup() }
  36. })
  37. test('persistCreateBook(P0-2)→ 已有 .gitignore 追加不覆盖', async () => {
  38. const { ctx, root, cleanup } = await tmpRepo()
  39. try {
  40. await fs.writeFile(path.join(root, '.gitignore'), 'node_modules/\n', 'utf8')
  41. const r = await persistCreateBook(ctx, { book: { 书名: '测' }, 总纲: '# 总纲', 卷纲: '# 第1卷' })
  42. assert.equal(r.ok, true, r.error)
  43. const gi = await read(root, '.gitignore')
  44. assert.ok(gi.includes('node_modules/'), '不应覆盖既有条目')
  45. assert.ok(gi.includes('.cache/') && gi.includes('工作区/'))
  46. } finally { await cleanup() }
  47. })
  48. test('persistDraftOutline(序6)→ 写 工作区/细纲.md', async () => {
  49. const { ctx, root, cleanup } = await tmpRepo()
  50. try {
  51. const r = await persistDraftOutline(ctx, { 细纲: '## 本章要写到的事\n林晚突破。\n' })
  52. assert.equal(r.ok, true)
  53. assert.match(await read(root, '工作区/细纲.md'), /林晚突破/)
  54. } finally { await cleanup() }
  55. })
  56. test('persistCreateBook(序1)→ 写 book.yaml + 总纲 + 第一卷卷纲', async () => {
  57. const { ctx, root, cleanup } = await tmpRepo()
  58. try {
  59. const r = await persistCreateBook(ctx, {
  60. book: { spec_version: '7.0', 书名: '剑起青云', 题材: '仙侠' },
  61. 总纲: '# 总纲\n主角逆袭。',
  62. 卷纲: '# 第1卷\n入门。',
  63. })
  64. assert.equal(r.ok, true)
  65. assert.match(await read(root, 'book.yaml'), /剑起青云/)
  66. assert.match(await read(root, '大纲/总纲.md'), /逆袭/)
  67. assert.match(await read(root, '大纲/第01卷.md'), /入门/)
  68. } finally { await cleanup() }
  69. })
  70. test('persistVolumeReview(序4)→ 写卷摘要 + 下卷卷纲', async () => {
  71. const { ctx, root, cleanup } = await tmpRepo()
  72. try {
  73. const r = await persistVolumeReview(ctx, { 卷号: 1, 卷摘要: '第一卷收束。', 下卷卷纲: '# 第2卷\n新地图。' })
  74. assert.equal(r.ok, true)
  75. assert.match(await read(root, '定稿/摘要/卷摘要/01.md'), /收束/)
  76. assert.match(await read(root, '大纲/第02卷.md'), /新地图/)
  77. } finally { await cleanup() }
  78. })
  79. test('persistRepair(序0)→ 仅写失败清单内的文件,内容须能解析', async () => {
  80. const { ctx, root, cleanup } = await tmpRepo()
  81. try {
  82. const target = '定稿/正文/0001-起.md'
  83. await fs.mkdir(path.join(root, '定稿/正文'), { recursive: true })
  84. await fs.writeFile(path.join(root, target), '---\n坏: yaml: :\n---\n正文', 'utf8')
  85. const good = '---\n章号: 1\n标题: 起\n---\n正文'
  86. const r = await persistRepair(ctx, { repairs: [{ file: target, content: good }] }, { allowedFiles: [target] })
  87. assert.equal(r.ok, true)
  88. assert.deepEqual(r.written, [target])
  89. assert.match(await read(root, target), /章号: 1/)
  90. } finally { await cleanup() }
  91. })
  92. test('persistRepair:拒绝写不在失败清单内的文件(安全网,防 AI 任意写)', async () => {
  93. const { ctx, root, cleanup } = await tmpRepo()
  94. try {
  95. const r = await persistRepair(
  96. ctx,
  97. { repairs: [{ file: '定稿/正文/9999-注入.md', content: '---\n章号: 9\n---\nx' }] },
  98. { allowedFiles: ['定稿/正文/0001-起.md'] }
  99. )
  100. assert.equal(r.ok, false)
  101. await assert.rejects(() => read(root, '定稿/正文/9999-注入.md'))
  102. } finally { await cleanup() }
  103. })
  104. test('persistRepair:修复内容仍解析失败 → ok=false 不写', async () => {
  105. const { ctx, root, cleanup } = await tmpRepo()
  106. try {
  107. const target = '定稿/正文/0001-起.md'
  108. const r = await persistRepair(
  109. ctx,
  110. { repairs: [{ file: target, content: '---\n仍坏: : :\n---\nx' }] },
  111. { allowedFiles: [target] }
  112. )
  113. assert.equal(r.ok, false)
  114. } finally { await cleanup() }
  115. })