mediaMarkdown.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { describe, expect, it } from 'vitest'
  2. import { normalizeAssistantMediaMarkdown } from '@/utils/mediaMarkdown'
  3. import MarkdownIt from 'markdown-it'
  4. function destinations(value: string): string[] {
  5. return new MarkdownIt().parse(value, {}).flatMap(token => (token.children ?? []).flatMap(child => {
  6. const path = child.type === 'image' ? child.attrGet('src') : child.type === 'link_open' ? child.attrGet('href') : null
  7. return path ? [decodeURIComponent(path)] : []
  8. }))
  9. }
  10. describe('historical assistant MEDIA compatibility', () => {
  11. it('converts image and non-image paths to valid Markdown with round-trip filenames', () => {
  12. const rendered = normalizeAssistantMediaMarkdown('MEDIA:/Users/samien/图片 文件.jpg\nMEDIA:/tmp/报告.docx')
  13. expect(destinations(rendered)).toEqual(['/Users/samien/图片 文件.jpg', '/tmp/报告.docx'])
  14. expect(rendered).toContain('![图片 文件.jpg]')
  15. })
  16. it('accepts quoted paths with Chinese, spaces, quotes, and backticks', () => {
  17. const paths = ["/Users/瑶儿/带 空格 '引号'.png", '/tmp/含`反引号`.pdf']
  18. const rendered = normalizeAssistantMediaMarkdown(paths.map(path => `MEDIA:"${path}"`).join('\n'))
  19. expect(destinations(rendered)).toEqual(paths)
  20. })
  21. it('waits for a streaming path boundary and accepts a closed quoted path', () => {
  22. expect(normalizeAssistantMediaMarkdown('MEDIA:/tmp/正在生成.png', true)).toBe('MEDIA:/tmp/正在生成.png')
  23. expect(destinations(normalizeAssistantMediaMarkdown('MEDIA:/tmp/正在生成.png\n下一行', true))).toEqual(['/tmp/正在生成.png'])
  24. expect(destinations(normalizeAssistantMediaMarkdown('MEDIA:`/tmp/已完整.png`', true))).toEqual(['/tmp/已完整.png'])
  25. })
  26. it('does not transform code, headings, tables, URLs, or traversal paths', () => {
  27. const input = [
  28. '```text',
  29. 'MEDIA:/tmp/inside-code.png',
  30. '```',
  31. '`MEDIA:/tmp/inline.png`',
  32. '# MEDIA:/tmp/title.png',
  33. '| MEDIA:/tmp/table.png | value |',
  34. 'MEDIA:https://example.com/remote.png',
  35. 'MEDIA:/tmp/../secret.png',
  36. ].join('\n')
  37. expect(normalizeAssistantMediaMarkdown(input)).toBe(input)
  38. })
  39. })