rebuilder.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises'
  6. import { CacheManager } from '../../src/cache/index.js'
  7. // 在临时目录构造一个最小书仓库,files 是 { 相对路径: 内容 }
  8. async function makeRepo(files) {
  9. const root = await mkdtemp(path.join(os.tmpdir(), 'wnw-rebuild-'))
  10. for (const [rel, content] of Object.entries(files)) {
  11. const full = path.join(root, rel)
  12. await mkdir(path.dirname(full), { recursive: true })
  13. await writeFile(full, content, 'utf8')
  14. }
  15. return root
  16. }
  17. const 名册仅林晚 =
  18. '| 正名 | 别名 | 类型 | 首现章 |\n|------|------|------|--------|\n| 林晚 | 晚晚 | character | 1 |\n'
  19. test('角色卡不在名册里也必须入 entities(upsert,不丢数据)', async () => {
  20. const root = await makeRepo({
  21. 'book.yaml': 'spec_version: "7.0"\n书名: 测试\n',
  22. '定稿/设定/名册.md': 名册仅林晚,
  23. '定稿/设定/角色/独行客.md':
  24. '---\n姓名: 独行客\n状态: 在世\n位置: 荒原\n境界: 元婴\n最后变更章: 5\n---\n## 设定\n来历不明。\n',
  25. })
  26. const cache = new CacheManager(path.join(root, '.cache', 'index.db'))
  27. try {
  28. await cache.ensureReady(root)
  29. const rows = await cache.query('SELECT * FROM entities WHERE id = ?', ['独行客'])
  30. assert.equal(rows.length, 1, '独行客有角色卡但不在名册,必须 upsert 入 entities,不能丢')
  31. assert.equal(rows[0].status, '在世')
  32. assert.equal(rows[0].realm, '元婴')
  33. } finally {
  34. await cache.close()
  35. await rm(root, { recursive: true, force: true })
  36. }
  37. })
  38. test('名册中的角色被角色卡补全字段(upsert 走 UPDATE 分支)', async () => {
  39. const root = await makeRepo({
  40. 'book.yaml': 'spec_version: "7.0"\n书名: 测试\n',
  41. '定稿/设定/名册.md': 名册仅林晚,
  42. '定稿/设定/角色/林晚.md':
  43. '---\n姓名: 林晚\n状态: 在世\n位置: 青云宗\n境界: 练气三层\n最后变更章: 1\n---\n## 设定\n外门弟子。\n',
  44. })
  45. const cache = new CacheManager(path.join(root, '.cache', 'index.db'))
  46. try {
  47. await cache.ensureReady(root)
  48. const rows = await cache.query('SELECT * FROM entities WHERE id = ?', ['林晚'])
  49. assert.equal(rows.length, 1, '林晚不应因 upsert 而重复')
  50. assert.equal(rows[0].realm, '练气三层', '角色卡的境界应补进名册建立的行')
  51. } finally {
  52. await cache.close()
  53. await rm(root, { recursive: true, force: true })
  54. }
  55. })
  56. test('履历引用不存在的章节 → 记 warning,不阻断重建(AC10)', async () => {
  57. const root = await makeRepo({
  58. 'book.yaml': 'spec_version: "7.0"\n书名: 测试\n',
  59. '定稿/设定/名册.md': 名册仅林晚,
  60. '定稿/正文/0001-开局.md': '---\n章号: 1\n标题: 开局\n卷: 1\n字数: 100\n章定位: 推进\n---\n正文。',
  61. '大纲/伏笔/伏笔-001-test.md':
  62. '---\n强度: 高\n状态: 进行\n开启章: 1\n---\n## 履历\n- 第999章:推进——引用不存在的章\n',
  63. })
  64. const cache = new CacheManager(path.join(root, '.cache', 'index.db'))
  65. try {
  66. const result = await cache.rebuildFromSource(root)
  67. assert.equal(result.ok, true, '履历章节不存在只警告,不阻断')
  68. assert.ok(
  69. result.warnings.some((w) => w.includes('999')),
  70. `应有引用 999 章的 warning,实际:${JSON.stringify(result.warnings)}`
  71. )
  72. } finally {
  73. await cache.close()
  74. await rm(root, { recursive: true, force: true })
  75. }
  76. })
  77. test('别名冲突 → 报 error 并拒绝重建(AC10)', async () => {
  78. const root = await makeRepo({
  79. 'book.yaml': 'spec_version: "7.0"\n书名: 测试\n',
  80. // 「影」同时是林晚和神秘老者的别名 → 冲突
  81. '定稿/设定/名册.md':
  82. '| 正名 | 别名 | 类型 | 首现章 |\n|------|------|------|--------|\n' +
  83. '| 林晚 | 影 | character | 1 |\n| 神秘老者 | 影 | character | 1 |\n',
  84. })
  85. const cache = new CacheManager(path.join(root, '.cache', 'index.db'))
  86. try {
  87. const result = await cache.rebuildFromSource(root)
  88. assert.equal(result.ok, false, '别名冲突必须拒绝重建')
  89. assert.ok(
  90. result.errors.some((e) => e.includes('影')),
  91. `应有别名冲突 error,实际:${JSON.stringify(result.errors)}`
  92. )
  93. } finally {
  94. await cache.close()
  95. await rm(root, { recursive: true, force: true })
  96. }
  97. })