sessionOutline.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { describe, expect, it } from 'vitest'
  2. import type { UiMessage } from '@/components/messages/types'
  3. import { buildSessionOutline } from '@/utils/sessionOutline'
  4. describe('session outline', () => {
  5. it('keeps each user turn and all assistant H1-H3 headings before the next turn', () => {
  6. const messages: UiMessage[] = [
  7. { id: 'u1', role: 'user', content: '请给我一份完整方案' },
  8. { id: 'a1', role: 'assistant', content: '# 结论\n正文\n## 实施步骤' },
  9. { id: 'a2', role: 'assistant', content: '### 风险\n说明' },
  10. { id: 'u2', role: 'user', content: '再补充测试' },
  11. { id: 'a3', role: 'assistant', content: '## 测试计划' },
  12. ]
  13. expect(buildSessionOutline(messages).map(item => [item.type, item.level, item.content])).toEqual([
  14. ['user', 0, '请给我一份完整方案'],
  15. ['heading', 1, '结论'],
  16. ['heading', 2, '实施步骤'],
  17. ['heading', 3, '风险'],
  18. ['user', 0, '再补充测试'],
  19. ['heading', 2, '测试计划'],
  20. ])
  21. })
  22. it('ignores reasoning, fenced-code headings, tools, and timeline system events', () => {
  23. const messages: UiMessage[] = [
  24. { id: 'system', role: 'user', content: '模型切换', timelineKind: 'system' },
  25. { id: 'u1', role: 'user', content: '<think>内部</think>真实问题' },
  26. { id: 'a1', role: 'assistant', content: '<think># 隐藏标题</think>\n```md\n# 代码标题\n```\n## 可见标题' },
  27. { id: 'tool', role: 'tool', content: '# 工具标题' },
  28. ]
  29. expect(buildSessionOutline(messages).map(item => item.content)).toEqual(['真实问题', '可见标题'])
  30. })
  31. it('truncates long user questions to fifty characters', () => {
  32. const content = '这是一条很长的用户问题'.repeat(8)
  33. const [item] = buildSessionOutline([{ id: 'u1', role: 'user', content }])
  34. expect(item?.content).toHaveLength(51)
  35. expect(item?.content.endsWith('…')).toBe(true)
  36. })
  37. })