router.test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import path from 'node:path'
  4. import os from 'node:os'
  5. import { promises as fs } from 'node:fs'
  6. import { execFile } from 'node:child_process'
  7. import { promisify } from 'node:util'
  8. import { CacheManager } from '../../src/cache/index.js'
  9. import { determineNextState } from '../../src/state-machine/index.js'
  10. const execFileAsync = promisify(execFile)
  11. // 造 git 书仓库 + 缓存。files = {相对路径: 内容};committed=true 时初始全部提交
  12. async function makeGitBook(files, { commit = true } = {}) {
  13. const root = await fs.mkdtemp(path.join(os.tmpdir(), 'wnw-sm-'))
  14. const git = (a) => execFileAsync('git', a, { cwd: root })
  15. await git(['init', '-q'])
  16. await git(['config', 'user.email', 't@example.com'])
  17. await git(['config', 'user.name', 'test'])
  18. await fs.writeFile(path.join(root, '.gitignore'), '.cache/\n工作区/\n', 'utf8')
  19. for (const [rel, content] of Object.entries(files)) {
  20. const full = path.join(root, rel)
  21. await fs.mkdir(path.dirname(full), { recursive: true })
  22. await fs.writeFile(full, content, 'utf8')
  23. }
  24. if (commit) {
  25. await git(['add', '-A'])
  26. await git(['commit', '-q', '-m', 'init'])
  27. }
  28. const dbDir = await fs.mkdtemp(path.join(os.tmpdir(), 'wnw-sm-db-'))
  29. const cache = new CacheManager(path.join(dbDir, 'index.db'))
  30. await cache.ensureReady(root)
  31. return {
  32. root,
  33. git,
  34. ctx: { repoPath: root, cache },
  35. cleanup: async () => {
  36. await cache.close()
  37. await fs.rm(root, { recursive: true, force: true })
  38. await fs.rm(dbDir, { recursive: true, force: true })
  39. },
  40. }
  41. }
  42. const ch = (n, vol = 1, pos = '推进') =>
  43. `---\n章号: ${n}\n标题: 第${n}章\n卷: ${vol}\n字数: 100\n章定位: ${pos}\n钩子: 危机钩-强\n情绪定位: 铺垫\n---\n正文。`
  44. const healthyBook = (extra = {}) => ({
  45. 'book.yaml': 'spec_version: "7.0"\n书名: 测\n卷规模: 40\n体检周期: 50\n',
  46. '大纲/总纲.md': '# 总纲\n## 结局\nx',
  47. '定稿/正文/0001-第1章.md': ch(1),
  48. ...extra,
  49. })
  50. test('序1:无 book.yaml → 建书引导', async () => {
  51. const { ctx, cleanup } = await makeGitBook({ '大纲/占位.md': 'x' })
  52. try {
  53. const r = await determineNextState(ctx)
  54. assert.equal(r.序, 1)
  55. assert.equal(r.state, 'create-book')
  56. } finally {
  57. await cleanup()
  58. }
  59. })
  60. test('序6:健康书、无异常 → 起草新章细纲', async () => {
  61. const { ctx, cleanup } = await makeGitBook(healthyBook())
  62. try {
  63. const r = await determineNextState(ctx)
  64. assert.equal(r.序, 6, `实际:${JSON.stringify(r)}`)
  65. assert.equal(r.state, 'draft-outline')
  66. } finally {
  67. await cleanup()
  68. }
  69. })
  70. test('序0:源文件解析失败 → 修复确认', async () => {
  71. const { ctx, cleanup } = await makeGitBook(
  72. healthyBook({ '定稿/正文/0002-坏章.md': '---\n章号: 2\n标题: [未闭合\n卷: : :\n---\n正文' })
  73. )
  74. try {
  75. const r = await determineNextState(ctx)
  76. assert.equal(r.序, 0)
  77. assert.equal(r.state, 'repair-confirm')
  78. } finally {
  79. await cleanup()
  80. }
  81. })
  82. test('序2:定稿有未登记手改 → 提议补登', async () => {
  83. const { ctx, root, cleanup } = await makeGitBook(healthyBook())
  84. try {
  85. // 提交后手改一个已跟踪文件(不提交)
  86. await fs.writeFile(path.join(root, '定稿/正文/0001-第1章.md'), ch(1) + '\n手改了一句。', 'utf8')
  87. const r = await determineNextState(ctx)
  88. assert.equal(r.序, 2)
  89. assert.equal(r.state, 'relink-manual-edits')
  90. } finally {
  91. await cleanup()
  92. }
  93. })
  94. test('序3:工作区有未完成草稿 → 断点续跑', async () => {
  95. const { ctx, root, cleanup } = await makeGitBook(healthyBook())
  96. try {
  97. await fs.mkdir(path.join(root, '工作区'), { recursive: true })
  98. await fs.writeFile(path.join(root, '工作区/草稿-A.md'), '半成品草稿', 'utf8')
  99. const r = await determineNextState(ctx)
  100. assert.equal(r.序, 3)
  101. assert.equal(r.state, 'resume')
  102. } finally {
  103. await cleanup()
  104. }
  105. })
  106. test('序4:卷末章 → 卷复盘', async () => {
  107. const { ctx, cleanup } = await makeGitBook({
  108. 'book.yaml': 'spec_version: "7.0"\n书名: 测\n卷规模: 2\n体检周期: 50\n',
  109. '大纲/总纲.md': '# 总纲',
  110. '定稿/正文/0001-第1章.md': ch(1),
  111. '定稿/正文/0002-第2章.md': ch(2),
  112. })
  113. try {
  114. const r = await determineNextState(ctx)
  115. assert.equal(r.序, 4, `实际:${JSON.stringify(r)}`)
  116. assert.equal(r.state, 'volume-review')
  117. } finally {
  118. await cleanup()
  119. }
  120. })
  121. test('序5:到体检周期 → 体检', async () => {
  122. const { ctx, cleanup } = await makeGitBook({
  123. 'book.yaml': 'spec_version: "7.0"\n书名: 测\n卷规模: 3\n体检周期: 2\n',
  124. '大纲/总纲.md': '# 总纲',
  125. '定稿/正文/0001-第1章.md': ch(1),
  126. '定稿/正文/0002-第2章.md': ch(2),
  127. })
  128. try {
  129. const r = await determineNextState(ctx)
  130. assert.equal(r.序, 5, `实际:${JSON.stringify(r)}`)
  131. assert.equal(r.state, 'health-check')
  132. } finally {
  133. await cleanup()
  134. }
  135. })
  136. test('命中即停:手改(序2) + 工作区草稿(序3) 同时存在 → 先报序2', async () => {
  137. const { ctx, root, cleanup } = await makeGitBook(healthyBook())
  138. try {
  139. await fs.writeFile(path.join(root, '定稿/正文/0001-第1章.md'), ch(1) + '\n手改。', 'utf8')
  140. await fs.mkdir(path.join(root, '工作区'), { recursive: true })
  141. await fs.writeFile(path.join(root, '工作区/草稿-A.md'), '草稿', 'utf8')
  142. const r = await determineNextState(ctx)
  143. assert.equal(r.序, 2)
  144. } finally {
  145. await cleanup()
  146. }
  147. })