e2e.test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 { migrateV6 } from '../../src/migrate/index.js'
  9. import { run as migrateCmd } from '../../src/commands/migrate.js'
  10. import { CacheManager } from '../../src/cache/index.js'
  11. import { determineNextState } from '../../src/state-machine/index.js'
  12. import { loadBooks } from '../../src/session/index.js'
  13. import { tempV6, tempV6Sqlite, inlineFixture } from './_v6.js'
  14. const execFileAsync = promisify(execFile)
  15. async function tempWorkdir() {
  16. const workdir = await fs.mkdtemp(path.join(os.tmpdir(), 'wnw-workdir-'))
  17. await fs.mkdir(path.join(workdir, '.webnovel'), { recursive: true })
  18. return { ctx: { workdir, packageRoot: null }, cleanup: () => fs.rm(workdir, { recursive: true, force: true }) }
  19. }
  20. /** 目录树指纹:相对路径 → 内容长度(源只读断言用)。 */
  21. async function treeFingerprint(root) {
  22. const map = new Map()
  23. async function walk(dir) {
  24. for (const ent of await fs.readdir(dir, { withFileTypes: true })) {
  25. const full = path.join(dir, ent.name)
  26. if (ent.isDirectory()) await walk(full)
  27. else map.set(path.relative(root, full), (await fs.readFile(full)).length)
  28. }
  29. }
  30. await walk(root)
  31. return map
  32. }
  33. async function readTree(root) {
  34. const parts = []
  35. async function walk(dir) {
  36. for (const ent of await fs.readdir(dir, { withFileTypes: true })) {
  37. if (ent.name === '.git' || ent.name === '.cache') continue
  38. const full = path.join(dir, ent.name)
  39. if (ent.isDirectory()) await walk(full)
  40. else parts.push(await fs.readFile(full, 'utf8'))
  41. }
  42. }
  43. await walk(root)
  44. return parts.join('\n')
  45. }
  46. test('AC2 端到端(inline):迁移落位、git 单 commit、缓存可删重建、next 进正常写章流程', async () => {
  47. const { ctx, cleanup } = await tempWorkdir()
  48. const v6 = await tempV6(inlineFixture)
  49. try {
  50. const r = await migrateV6(ctx, v6.v6Path)
  51. assert.equal(r.ok, true, r.error)
  52. const repo = path.join(ctx.workdir, '剑碎虚空')
  53. // 结构合规:正文/摘要/条目/名册/时间线/卷纲/工程件
  54. for (const p of [
  55. '定稿/正文/0001-残剑出鞘.md', '定稿/正文/0002-第2章.md', '定稿/正文/0003-剑灵初醒.md',
  56. '定稿/摘要/章摘要/0001.md', '大纲/伏笔/伏笔-001-残剑剑柄内藏半张古.md',
  57. '定稿/设定/名册.md', '定稿/设定/角色/陆沉.md', '定稿/设定/时间线/第01卷.md',
  58. '大纲/总纲.md', '大纲/卷纲/第01卷.md', 'book.yaml', 'AGENTS.md', '.gitignore',
  59. '工作区/迁移报告.md',
  60. ]) {
  61. await fs.access(path.join(repo, p))
  62. }
  63. // git:提交链压成单个 init commit,工作树净(工作区被 ignore)
  64. const { stdout: cnt } = await execFileAsync('git', ['rev-list', '--count', 'HEAD'], { cwd: repo })
  65. assert.equal(cnt.trim(), '1')
  66. const { stdout: st } = await execFileAsync('git', ['status', '--porcelain'], { cwd: repo })
  67. assert.equal(st.trim(), '')
  68. // 删缓存全量重建一致(不变量 2)
  69. await fs.rm(path.join(repo, '.cache'), { recursive: true, force: true })
  70. const cache = new CacheManager(path.join(repo, '.cache', 'index.db'))
  71. try {
  72. await cache.ensureReady(repo)
  73. const rows = await cache.query('SELECT COUNT(*) AS n FROM chapters', [])
  74. assert.equal(rows[0].n, 3)
  75. // AC7 口径顺检:迁移不产生指纹(体检才产)
  76. assert.equal((await cache.query('SELECT COUNT(*) AS n FROM fingerprints', []))[0].n, 0)
  77. // next 直接进正常流程:序 6 起草第 4 章
  78. const next = await determineNextState({ repoPath: repo, cache, workdir: ctx.workdir })
  79. assert.equal(next.序, 6, JSON.stringify(next))
  80. assert.equal(next.dto.nextChapter, 4)
  81. } finally {
  82. await cache.close()
  83. }
  84. // books.jsonl 登记 + 当前书
  85. const books = await loadBooks(ctx.workdir)
  86. assert.ok(books.books.some((b) => b.书名 === '剑碎虚空'))
  87. } finally {
  88. await v6.cleanup()
  89. await cleanup()
  90. }
  91. })
  92. test('AC2 端到端(sqlite 形态)+ AC5 报告三节', async () => {
  93. const { ctx, cleanup } = await tempWorkdir()
  94. const v6 = await tempV6Sqlite()
  95. try {
  96. const r = await migrateV6(ctx, v6.v6Path)
  97. assert.equal(r.ok, true, r.error)
  98. const report = await fs.readFile(path.join(ctx.workdir, '潮汐之下', '工作区', '迁移报告.md'), 'utf8')
  99. assert.match(report, /## 迁了什么/)
  100. assert.match(report, /- 章数:2/)
  101. assert.match(report, /## 待校对/)
  102. assert.match(report, /## 如实丢弃/)
  103. assert.match(report, /index\.db 实体已转正文件/)
  104. assert.match(report, /数据形态:state\.json 精简 \+ index\.db/)
  105. } finally {
  106. await v6.cleanup()
  107. await cleanup()
  108. }
  109. })
  110. test('AC3 不丢字:v6 每类文本在 v7 产物或待校对区可寻回', async () => {
  111. const { ctx, cleanup } = await tempWorkdir()
  112. const v6 = await tempV6(inlineFixture)
  113. try {
  114. const r = await migrateV6(ctx, v6.v6Path)
  115. assert.equal(r.ok, true, r.error)
  116. const tree = await readTree(path.join(ctx.workdir, '剑碎虚空'))
  117. for (const text of [
  118. '晨雾未散,陆沉背着那柄锈迹斑斑的残剑走进演武场', // 正文(平坦带标题)
  119. '藏经阁的木梯吱呀作响', // 正文(遗留无标题)
  120. '淤塞多年的气海竟被撕开一道细缝', // 正文(卷内 3 位)
  121. '陆沉携残剑入演武场受三长老盘问', // 章摘要
  122. '残剑剑柄内藏半张古图', // 伏笔 content
  123. '外门大比', // active_threads → 卷纲
  124. '三长老着人盯梢陆沉,尚未收线', // scratchpad open_loops → 待校对
  125. '陆家灭门夜唯一遗物', // scratchpad story_facts → 待校对
  126. '战斗段落短句连用,收在动作定格', // patterns → 文风候选
  127. '剑灵反哺', // state_changes → 实体变更史
  128. '藏经阁初见,苏素予其残卷', // structured_relationships → 角色卡关系
  129. '外门三千弟子,藏经阁七层', // 设定集
  130. '陆沉集齐古图,于虚空裂隙斩落伪天道', // 总纲
  131. '剑灵初醒,破至练气五层', // 时间线事件
  132. ]) {
  133. assert.ok(tree.includes(text), `丢字:找不到「${text}」`)
  134. }
  135. } finally {
  136. await v6.cleanup()
  137. await cleanup()
  138. }
  139. })
  140. test('AC4 回退演练:中途失败工作目录零残留、源 v6 未被改动;残留临时目录会被清扫', async () => {
  141. const { ctx, cleanup } = await tempWorkdir()
  142. const v6 = await tempV6(inlineFixture)
  143. try {
  144. const before = await treeFingerprint(v6.v6Path)
  145. // 预埋一个上次中断的残留临时目录
  146. await fs.mkdir(path.join(ctx.workdir, '.migrate-tmp-99999', '定稿'), { recursive: true })
  147. const r = await migrateV6(ctx, v6.v6Path, { _faultBeforeRename: true })
  148. assert.equal(r.ok, false)
  149. assert.match(r.error, /工作目录已恢复原样/)
  150. const left = await fs.readdir(ctx.workdir)
  151. assert.ok(!left.some((n) => n.startsWith('.migrate-tmp-')), `残留:${left}`)
  152. assert.ok(!left.includes('剑碎虚空'))
  153. assert.deepEqual(await treeFingerprint(v6.v6Path), before) // 源逐文件未动
  154. } finally {
  155. await v6.cleanup()
  156. await cleanup()
  157. }
  158. })
  159. test('目标目录已存在拒绝(--dir 另起名可走);命令壳用法提示', async () => {
  160. const { ctx, cleanup } = await tempWorkdir()
  161. const v6 = await tempV6(inlineFixture)
  162. try {
  163. await fs.mkdir(path.join(ctx.workdir, '剑碎虚空'))
  164. const clash = await migrateV6(ctx, v6.v6Path)
  165. assert.equal(clash.ok, false)
  166. assert.match(clash.error, /已有「剑碎虚空」/)
  167. const alt = await migrateCmd([v6.v6Path], { dir: '剑碎虚空-旧稿' }, ctx)
  168. assert.equal(alt.ok, true, alt.error)
  169. await fs.access(path.join(ctx.workdir, '剑碎虚空-旧稿', 'book.yaml'))
  170. const noArg = await migrateCmd([], {}, ctx)
  171. assert.equal(noArg.ok, false)
  172. assert.match(noArg.error, /用法/)
  173. } finally {
  174. await v6.cleanup()
  175. await cleanup()
  176. }
  177. })