EntityWriter.test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. })