mediaSequence.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { describe, expect, it } from 'vitest'
  2. import { mediaItemsFromMessages, mediaUrlIdentity, previewItemFromUrl } from '@/components/library/mediaSequence'
  3. import type { UiMessage } from '@/components/messages/types'
  4. describe('conversation media sequence', () => {
  5. it('keeps visible image and video order while deduplicating the same URL', () => {
  6. const messages: UiMessage[] = [
  7. { id: 'one', role: 'assistant', content: '![第一张](/media/first.png)' },
  8. { id: 'two', role: 'assistant', content: '[视频](/media/second.mp4)', attachments: [{ id: 'a', name: '第一张.png', kind: 'image', url: '/media/first.png' }] },
  9. { id: 'three', role: 'assistant', content: '普通文本', attachments: [{ id: 'b', name: '第三张.webp', kind: 'image', url: '/media/third.webp' }] },
  10. { id: 'four', role: 'assistant', content: 'MEDIA:/media/fourth.png' },
  11. ]
  12. expect(mediaItemsFromMessages(messages).map(item => [item.name, item.kind, item.previewUrl])).toEqual([
  13. ['first.png', 'image', '/media/first.png'],
  14. ['second.mp4', 'video', '/media/second.mp4'],
  15. ['第三张.webp', 'image', '/media/third.webp'],
  16. ['fourth.png', 'image', '/media/fourth.png'],
  17. ])
  18. })
  19. it('infers full-view media kind from a clicked conversation link', () => {
  20. expect(previewItemFromUrl('片段.webm', '/media/clip.webm')).toMatchObject({ kind: 'video', previewUrl: '/media/clip.webm' })
  21. })
  22. it('gives rendered and source Chinese paths the same media identity', () => {
  23. expect(mediaUrlIdentity('/Users/samien/Agents/瑶儿/单视角图片.png')).toBe(
  24. mediaUrlIdentity('/Users/samien/Agents/%E7%91%B6%E5%84%BF/%E5%8D%95%E8%A7%86%E8%A7%92%E5%9B%BE%E7%89%87.png'),
  25. )
  26. })
  27. it('uses the same Profile-scoped URL and filename as rendered server images', () => {
  28. const items = mediaItemsFromMessages([
  29. { id: 'one', role: 'assistant', profile: 'yaoer', content: 'MEDIA:/Users/test/工作/图片.png\n![第二张](/Users/test/工作/第二张.png)' },
  30. ])
  31. expect(items.map(item => item.name)).toEqual(['图片.png', '第二张.png'])
  32. const url = new URL(items[0]!.previewUrl!, window.location.origin)
  33. expect(url.pathname).toBe('/api/files/download')
  34. expect(url.searchParams.get('path')).toBe('/Users/test/工作/图片.png')
  35. expect(url.searchParams.get('profile')).toBe('yaoer')
  36. expect(items.every(item => item.kind === 'image')).toBe(true)
  37. })
  38. it('matches retried media without merging different Profile authorizations', () => {
  39. const original='/api/files/download?path=%2Fwork%2Fa.png&preview=1&profile=one'
  40. expect(mediaUrlIdentity(original)).toBe(mediaUrlIdentity(original+'&_retry=123'))
  41. expect(mediaUrlIdentity(original)).not.toBe(mediaUrlIdentity(original.replace('profile=one','profile=two')))
  42. })
  43. })