read-character.test.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { test, before, after } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { run } from '../../src/commands/read-character.js'
  4. import { fixtureCtx } from './_helper.js'
  5. let ctx, cleanup
  6. before(async () => {
  7. ;({ ctx, cleanup } = await fixtureCtx())
  8. })
  9. after(async () => {
  10. await cleanup()
  11. })
  12. test('read-character --front-matter 返回 JSON 含姓名', async () => {
  13. const r = await run(['林晚'], { 'front-matter': true }, ctx)
  14. assert.equal(r.ok, true)
  15. const data = JSON.parse(r.output)
  16. assert.equal(data.姓名, '林晚')
  17. })
  18. test('read-character 默认返回完整(frontMatter + body)', async () => {
  19. const r = await run(['林晚'], {}, ctx)
  20. assert.equal(r.ok, true)
  21. const data = JSON.parse(r.output)
  22. assert.ok(data.frontMatter)
  23. assert.ok(data.body.includes('设定'))
  24. })
  25. test('read-character --section=设定 返回该小节', async () => {
  26. const r = await run(['林晚'], { section: '设定' }, ctx)
  27. assert.equal(r.ok, true)
  28. assert.ok(r.output.includes('外门弟子'))
  29. })
  30. test('read-character 缺正名 → ok=false', async () => {
  31. const r = await run([], {}, ctx)
  32. assert.equal(r.ok, false)
  33. })
  34. test('read-character 不存在的角色 → ok=false 且不崩', async () => {
  35. const r = await run(['查无此人'], { 'front-matter': true }, ctx)
  36. assert.equal(r.ok, false)
  37. assert.match(r.error, /不存在/)
  38. })