character-context.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { assembleCharacterContext } from '../../src/dto/character-context.js'
  4. import { makeGitBook } from '../state-machine/_helper.js'
  5. const charCard = `---
  6. 姓名: 林晚
  7. 别名:
  8. - 晚晚
  9. 状态: 在世
  10. 位置: 青云宗
  11. 境界: 练气三层
  12. 持有:
  13. - 青霜剑
  14. 最后变更章: 1
  15. ---
  16. ## 设定
  17. 外门弟子,心性坚韧。`
  18. test('assembleCharacterContext:从角色卡 front matter 组装 DTO', async () => {
  19. const { ctx, cleanup } = await makeGitBook({
  20. 'book.yaml': 'spec_version: "7.0"\n书名: 测\n',
  21. '定稿/设定/角色/林晚.md': charCard,
  22. })
  23. try {
  24. const r = await assembleCharacterContext(ctx, '林晚')
  25. assert.equal(r.ok, true)
  26. assert.equal(r.context.正名, '林晚')
  27. assert.equal(r.context.境界, '练气三层')
  28. assert.equal(r.context.状态, '在世')
  29. assert.equal(r.context.位置, '青云宗')
  30. assert.deepEqual(r.context.别名, ['晚晚'])
  31. assert.deepEqual(r.context.持有, ['青霜剑'])
  32. } finally {
  33. await cleanup()
  34. }
  35. })
  36. test('assembleCharacterContext:DTO 不泄漏文件路径(不变量:AI 永不见文件结构)', async () => {
  37. const { ctx, cleanup } = await makeGitBook({
  38. 'book.yaml': 'spec_version: "7.0"\n书名: 测\n',
  39. '定稿/设定/角色/林晚.md': charCard,
  40. })
  41. try {
  42. const r = await assembleCharacterContext(ctx, '林晚')
  43. const json = JSON.stringify(r.context)
  44. assert.ok(!json.includes('定稿'), 'DTO 不应含定稿路径')
  45. assert.ok(!json.includes('.md'), 'DTO 不应含文件名')
  46. assert.ok(!json.includes(ctx.repoPath), 'DTO 不应含仓库绝对路径')
  47. } finally {
  48. await cleanup()
  49. }
  50. })
  51. test('assembleCharacterContext:角色不存在 → ok=false 不抛', async () => {
  52. const { ctx, cleanup } = await makeGitBook({ 'book.yaml': '书名: 测\n' })
  53. try {
  54. const r = await assembleCharacterContext(ctx, '查无此人')
  55. assert.equal(r.ok, false)
  56. assert.equal(r.context, null)
  57. } finally {
  58. await cleanup()
  59. }
  60. })