import-draft.spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { afterAll, describe, expect, it } from 'vitest'
  2. import * as fs from 'node:fs'
  3. import * as os from 'node:os'
  4. import * as path from 'node:path'
  5. import {
  6. assembleMaterials,
  7. confirmOutline,
  8. listChapterDrafts,
  9. parseDocument,
  10. seedMinDesign,
  11. writeCandidate,
  12. } from '@webnovel/core'
  13. import { importAuthorDraft } from '../src/index'
  14. const roots: string[] = []
  15. function mkDir(prefix: string): string {
  16. const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix))
  17. roots.push(dir)
  18. return dir
  19. }
  20. afterAll(() => { for (const root of roots) { try { fs.rmSync(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 }) } catch { /* Windows 句柄延迟释放,%TEMP% 残留无害 */ } } })
  21. const key = { 卷: 1, 章: 1, 章名: '开篇任务' } as const
  22. const HARD = '开场必须点名主角现身'
  23. function readyBook(root: string): void {
  24. seedMinDesign(root)
  25. for (const name of ['故事线', '人物弧线', '承诺', '线索', '时间线'] as const) {
  26. fs.mkdirSync(path.join(root, '账本'), { recursive: true })
  27. fs.writeFileSync(path.join(root, '账本', `${name}.md`), `# ${name}\n`, 'utf-8')
  28. }
  29. const body = [
  30. '# 章细纲', '', '## 定位段', '', '### 来源窗口项及拆并关系', '开篇任务,单章承接', '',
  31. '### 章节功能', '', `- 〔硬〕${HARD}`, '',
  32. '### 视角与焦点', '主角视角', '', '### 时空锚定', '城门口,清晨', '',
  33. '### 起止边界', '从抵达城门到发现异状', '', '### 故事线与承诺分配', '推进主线承诺', '',
  34. '### 信息边界', '只披露主角所见', '', '### 情绪与节奏目标', '紧张后留钩子', '',
  35. '### 前置条件核对结果', '已核对来源与窗口,前置设定均非留白', '',
  36. '## 细纲段', '', '### 单元 1', '',
  37. '- 目标: 开场', '- 人物: 主角', '- 时空: 城门口', '- 行动/冲突: 盘问与发现', '- 信息披露: 异常线索', '- 状态变化: 从平静到警觉', '',
  38. ].join('\n')
  39. writeCandidate(root, { 卷: 1, 章: 1, 章名: key.章名, 来源引用: ['作品契约/契约.md@1'], body })
  40. const r = confirmOutline(root, key)
  41. if (!r.ok) throw new Error(`confirm failed:${r.gaps.join(';')}`)
  42. const a = assembleMaterials(root, key)
  43. if (!a.ok) throw new Error(`assemble failed:${a.gaps.join(';')}`)
  44. }
  45. describe('作者稿导入(A9)', () => {
  46. it('整章自写:落 稿N+1.md,生成模块 作者手写,角色 草稿,不覆盖既有稿', () => {
  47. const root = mkDir('webnovel-import-1-')
  48. readyBook(root)
  49. const r1 = importAuthorDraft(root, {
  50. 卷: 1, 章: 1, 章名: key.章名,
  51. 正文: `作者亲写的第一章。${HARD}。\n\n## 草稿候选事实\n\n- 事实:主角揣着半枚铜符\n`,
  52. 角色: '草稿',
  53. 生成模块: '作者手写',
  54. })
  55. expect(r1.ok).toBe(true)
  56. expect(r1.relPath).toBe(`草稿区/草稿/卷01-${key.章名}/稿1.md`)
  57. const doc = parseDocument(fs.readFileSync(path.join(root, r1.relPath), 'utf-8'))
  58. expect(doc.ok).toBe(true)
  59. if (doc.ok) {
  60. expect(doc.data.fields['生成模块']).toBe('作者手写')
  61. expect(doc.data.fields['角色']).toBe('草稿')
  62. expect(doc.data.fields['选定']).toBe(false)
  63. expect(doc.data.body).toContain('事实:主角揣着半枚铜符')
  64. }
  65. const r2 = importAuthorDraft(root, {
  66. 卷: 1, 章: 1, 章名: key.章名,
  67. 正文: '第二版作者稿。',
  68. 角色: '草稿',
  69. 生成模块: '作者手写',
  70. })
  71. expect(r2.ok).toBe(true)
  72. expect(r2.relPath).toBe(`草稿区/草稿/卷01-${key.章名}/稿2.md`)
  73. expect(fs.readFileSync(path.join(root, r1.relPath), 'utf-8')).toContain('作者亲写的第一章')
  74. })
  75. it('作者手改:角色 待审稿 时旧待审稿降级、父版本指向被改的稿、选定唯一', () => {
  76. const root = mkDir('webnovel-import-2-')
  77. readyBook(root)
  78. fs.mkdirSync(path.join(root, `草稿区/草稿/卷01-${key.章名}`), { recursive: true })
  79. fs.writeFileSync(
  80. path.join(root, `草稿区/草稿/卷01-${key.章名}/稿1.md`),
  81. `---\n角色: 待审稿\n选定: true\n版本: 1\n父版本: null\n生成模块: 写稿\n---\nAI 初稿。${HARD}。\n`,
  82. 'utf-8',
  83. )
  84. const r = importAuthorDraft(root, {
  85. 卷: 1, 章: 1, 章名: key.章名,
  86. 正文: `作者改过的版本。${HARD}。`,
  87. 角色: '待审稿',
  88. 生成模块: '作者手改',
  89. 父版本: 1,
  90. })
  91. expect(r.ok).toBe(true)
  92. const drafts = listChapterDrafts(root, key)
  93. expect(drafts.filter((d) => d.角色 === '待审稿').map((d) => d.file)).toEqual(['稿2.md'])
  94. expect(drafts.filter((d) => d.选定).map((d) => d.file)).toEqual(['稿2.md'])
  95. const 新稿 = drafts.find((d) => d.file === '稿2.md')!
  96. const doc = parseDocument(新稿.text)
  97. expect(doc.ok).toBe(true)
  98. if (doc.ok) {
  99. expect(doc.data.fields['生成模块']).toBe('作者手改')
  100. expect(doc.data.fields['父版本']).toBe(1)
  101. expect(doc.data.fields['版本']).toBe(2)
  102. }
  103. const 旧稿 = drafts.find((d) => d.file === '稿1.md')!
  104. expect(旧稿.角色).toBe('草稿')
  105. expect(旧稿.选定).toBe(false)
  106. })
  107. it('空正文被拒;未确认细纲不拒绝(R1 播报)', () => {
  108. const root = mkDir('webnovel-import-3-')
  109. readyBook(root)
  110. expect(importAuthorDraft(root, { 卷: 1, 章: 1, 章名: key.章名, 正文: ' ', 角色: '草稿', 生成模块: '作者手写' }).ok).toBe(false)
  111. const r = importAuthorDraft(root, { 卷: 2, 章: 2, 章名: '未确认章', 正文: '正文', 角色: '草稿', 生成模块: '作者手写' })
  112. expect(r.ok).toBe(true)
  113. if (r.ok) expect(r.播报).toContain('细纲未确认')
  114. })
  115. })