1
0

EntityWriter.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { EntityWriter } from '../../../src/storage/adapters/EntityWriter.js'
  4. import { makeRepo, cleanup, read } from '../_tmprepo.js'
  5. const 角色卡 = `---
  6. 姓名: 林晚
  7. 状态: 在世
  8. 位置: 青云宗
  9. 境界: 练气三层
  10. 最后变更章: 1
  11. ---
  12. ## 设定
  13. 外门弟子。
  14. `
  15. test('updateCharacter 改 front matter,保留正文', async () => {
  16. const root = await makeRepo({ '定稿/设定/角色/林晚.md': 角色卡 })
  17. try {
  18. const r = await new EntityWriter(root).updateCharacter('林晚', {
  19. 位置: '北境雪原',
  20. 境界: '练气五层',
  21. 最后变更章: 152,
  22. })
  23. assert.equal(r.ok, true)
  24. const c = await read(root, '定稿/设定/角色/林晚.md')
  25. assert.match(c, /位置: 北境雪原/)
  26. assert.match(c, /境界: 练气五层/)
  27. assert.match(c, /最后变更章: 152/)
  28. assert.match(c, /外门弟子/)
  29. } finally {
  30. await cleanup(root)
  31. }
  32. })
  33. test('updateCharacter 不存在的角色 → ok=false(不崩)', async () => {
  34. const root = await makeRepo({})
  35. try {
  36. const r = await new EntityWriter(root).updateCharacter('查无此人', { 状态: '死亡' })
  37. assert.equal(r.ok, false)
  38. } finally {
  39. await cleanup(root)
  40. }
  41. })
  42. const 名册 = '| 正名 | 别名 | 类型 | 首现章 |\n|---|---|---|---|\n| 林晚 | 晚晚 | character | 1 |\n'
  43. test('upsertRosterRow 新增名册行', async () => {
  44. const root = await makeRepo({ '定稿/设定/名册.md': 名册 })
  45. try {
  46. const r = await new EntityWriter(root).upsertRosterRow({
  47. 正名: '神秘老者',
  48. 别名: '黑衣人',
  49. 类型: 'character',
  50. 首现章: 1,
  51. })
  52. assert.equal(r.ok, true)
  53. const c = await read(root, '定稿/设定/名册.md')
  54. assert.match(c, /\| 林晚 \|/)
  55. assert.match(c, /\| 神秘老者 \| 黑衣人 \| character \| 1 \|/)
  56. } finally {
  57. await cleanup(root)
  58. }
  59. })
  60. test('upsertRosterRow 已存在则更新该行(不重复)', async () => {
  61. const root = await makeRepo({ '定稿/设定/名册.md': 名册 })
  62. try {
  63. const r = await new EntityWriter(root).upsertRosterRow({
  64. 正名: '林晚',
  65. 别名: '晚晚, 林师妹',
  66. 类型: 'character',
  67. 首现章: 1,
  68. })
  69. assert.equal(r.ok, true)
  70. const c = await read(root, '定稿/设定/名册.md')
  71. assert.match(c, /\| 林晚 \| 晚晚, 林师妹 \| character \| 1 \|/)
  72. assert.equal((c.match(/\| 林晚 \|/g) || []).length, 1)
  73. } finally {
  74. await cleanup(root)
  75. }
  76. })
  77. test('A11:upsert 更新行时保留作者手加的额外列', async () => {
  78. const 带备注名册 =
  79. '| 正名 | 别名 | 类型 | 首现章 | 备注 |\n|---|---|---|---|---|\n| 林晚 | 晚晚 | 角色 | 1 | 主角,勿动 |\n'
  80. const root = await makeRepo({ '定稿/设定/名册.md': 带备注名册 })
  81. try {
  82. const r = await new EntityWriter(root).upsertRosterRow({
  83. 正名: '林晚',
  84. 别名: '晚晚, 小晚',
  85. 类型: '角色',
  86. 首现章: 1,
  87. })
  88. assert.equal(r.ok, true)
  89. const c = await read(root, '定稿/设定/名册.md')
  90. assert.match(c, /\| 林晚 \| 晚晚, 小晚 \| 角色 \| 1 \| 主角,勿动 \|/, `备注列不能丢:\n${c}`)
  91. } finally {
  92. await cleanup(root)
  93. }
  94. })