ChapterWriter.test.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { ChapterWriter } from '../../../src/storage/adapters/ChapterWriter.js'
  4. import { makeRepo, cleanup, read } from '../_tmprepo.js'
  5. test('ChapterWriter.writeChapter 写出定稿正文(防呆 front matter + 正文)', async () => {
  6. const root = await makeRepo()
  7. try {
  8. const w = new ChapterWriter(root)
  9. const fm = {
  10. 章号: 152,
  11. 标题: '北境的雪',
  12. 卷: 5,
  13. 字数: 3120,
  14. 章定位: '推进',
  15. 伏笔: ['埋下 伏笔-058'],
  16. }
  17. const r = await w.writeChapter(152, fm, '正文第一段。\n')
  18. assert.equal(r.ok, true)
  19. const content = await read(root, '定稿/正文/0152-北境的雪.md')
  20. assert.match(content, /^---\n/)
  21. assert.match(content, /标题: 北境的雪/)
  22. assert.match(content, /伏笔:\n {2}- 埋下 伏笔-058/) // 块列表防呆,两空格缩进
  23. assert.match(content, /正文第一段。/)
  24. } finally {
  25. await cleanup(root)
  26. }
  27. })
  28. test('ChapterWriter.writeChapter 危险值加引号(防呆)', async () => {
  29. const root = await makeRepo()
  30. try {
  31. const w = new ChapterWriter(root)
  32. const r = await w.writeChapter(1, { 章号: 1, 标题: '123', 卷: 1 }, '正文')
  33. assert.equal(r.ok, true)
  34. const content = await read(root, '定稿/正文/0001-123.md')
  35. assert.match(content, /标题: "123"/) // 纯数字串加引号
  36. } finally {
  37. await cleanup(root)
  38. }
  39. })