1
0

read-chapter.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-chapter.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-chapter --front-matter 返回 JSON 含标题', async () => {
  13. const r = await run(['1'], { 'front-matter': true }, ctx)
  14. assert.equal(r.ok, true)
  15. const data = JSON.parse(r.output)
  16. assert.equal(data.title ?? data.标题, '开局')
  17. })
  18. test('read-chapter --tail=N 返回正文末尾 N 字', async () => {
  19. const r = await run(['1'], { tail: '6' }, ctx)
  20. assert.equal(r.ok, true)
  21. assert.equal([...r.output].length, 6)
  22. })
  23. test('read-chapter 默认返回正文(不含 front matter)', async () => {
  24. const r = await run(['1'], {}, ctx)
  25. assert.equal(r.ok, true)
  26. assert.ok(!r.output.includes('章号:'))
  27. assert.ok(r.output.includes('林晚'))
  28. })
  29. test('read-chapter 不存在的章号 → ok=false 且不崩', async () => {
  30. const r = await run(['999'], { 'front-matter': true }, ctx)
  31. assert.equal(r.ok, false)
  32. assert.match(r.error, /不存在/)
  33. })
  34. test('read-chapter 非数字章号 → ok=false', async () => {
  35. const r = await run(['abc'], {}, ctx)
  36. assert.equal(r.ok, false)
  37. assert.match(r.error, /数字/)
  38. })