grep-story.test.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { test, before, after } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { run } from '../../src/commands/grep-story.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('grep-story 关键词命中正文,返回章号与匹配行', async () => {
  13. const r = await run(['玉佩'], {}, ctx)
  14. assert.equal(r.ok, true)
  15. const hits = JSON.parse(r.output)
  16. assert.ok(hits.length >= 1)
  17. assert.ok(hits.every((h) => h.匹配行.includes('玉佩')))
  18. assert.ok(hits.some((h) => h.章号 === 1))
  19. })
  20. test('grep-story 无命中 → ok 且空数组', async () => {
  21. const r = await run(['绝不存在的词xyz'], {}, ctx)
  22. assert.equal(r.ok, true)
  23. assert.deepEqual(JSON.parse(r.output), [])
  24. })
  25. test('grep-story --regex 正则检索命中', async () => {
  26. const r = await run([], { regex: '玉佩|藏书' }, ctx)
  27. assert.equal(r.ok, true)
  28. const hits = JSON.parse(r.output)
  29. assert.ok(hits.length >= 1)
  30. assert.ok(hits.every((h) => /玉佩|藏书/.test(h.匹配行)))
  31. })
  32. test('grep-story --regex 非法正则 → ok=false(不崩)', async () => {
  33. const r = await run([], { regex: '[invalid(' }, ctx)
  34. assert.equal(r.ok, false)
  35. })
  36. test('grep-story 缺关键词 → ok=false', async () => {
  37. const r = await run([], {}, ctx)
  38. assert.equal(r.ok, false)
  39. })