/ clis / twitter / shared.test.js
shared.test.js
 1  import { describe, expect, it } from 'vitest';
 2  import { __test__ } from './shared.js';
 3  
 4  const { extractMedia } = __test__;
 5  
 6  describe('twitter extractMedia', () => {
 7      it('returns false + empty list when legacy has no media', () => {
 8          expect(extractMedia({})).toEqual({ has_media: false, media_urls: [] });
 9          expect(extractMedia(undefined)).toEqual({ has_media: false, media_urls: [] });
10          expect(extractMedia({ extended_entities: { media: [] } })).toEqual({
11              has_media: false,
12              media_urls: [],
13          });
14      });
15  
16      it('extracts photo urls from extended_entities', () => {
17          const result = extractMedia({
18              extended_entities: {
19                  media: [
20                      { type: 'photo', media_url_https: 'https://pbs.twimg.com/media/a.jpg' },
21                      { type: 'photo', media_url_https: 'https://pbs.twimg.com/media/b.jpg' },
22                  ],
23              },
24          });
25          expect(result.has_media).toBe(true);
26          expect(result.media_urls).toEqual([
27              'https://pbs.twimg.com/media/a.jpg',
28              'https://pbs.twimg.com/media/b.jpg',
29          ]);
30      });
31  
32      it('prefers mp4 variant for video and animated_gif', () => {
33          const result = extractMedia({
34              extended_entities: {
35                  media: [
36                      {
37                          type: 'video',
38                          media_url_https: 'https://pbs.twimg.com/media/thumb.jpg',
39                          video_info: {
40                              variants: [
41                                  { content_type: 'application/x-mpegURL', url: 'https://video.twimg.com/x.m3u8' },
42                                  { content_type: 'video/mp4', url: 'https://video.twimg.com/x.mp4' },
43                              ],
44                          },
45                      },
46                      {
47                          type: 'animated_gif',
48                          media_url_https: 'https://pbs.twimg.com/tweet_video_thumb/g.jpg',
49                          video_info: {
50                              variants: [
51                                  { content_type: 'video/mp4', url: 'https://video.twimg.com/g.mp4' },
52                              ],
53                          },
54                      },
55                  ],
56              },
57          });
58          expect(result.has_media).toBe(true);
59          expect(result.media_urls).toEqual([
60              'https://video.twimg.com/x.mp4',
61              'https://video.twimg.com/g.mp4',
62          ]);
63      });
64  
65      it('falls back to media_url_https when no mp4 variant is available', () => {
66          const result = extractMedia({
67              extended_entities: {
68                  media: [
69                      {
70                          type: 'video',
71                          media_url_https: 'https://pbs.twimg.com/media/thumb.jpg',
72                          video_info: { variants: [] },
73                      },
74                  ],
75              },
76          });
77          expect(result).toEqual({
78              has_media: true,
79              media_urls: ['https://pbs.twimg.com/media/thumb.jpg'],
80          });
81      });
82  
83      it('falls back to entities.media when extended_entities is missing', () => {
84          const result = extractMedia({
85              entities: {
86                  media: [
87                      { type: 'photo', media_url_https: 'https://pbs.twimg.com/media/c.jpg' },
88                  ],
89              },
90          });
91          expect(result).toEqual({
92              has_media: true,
93              media_urls: ['https://pbs.twimg.com/media/c.jpg'],
94          });
95      });
96  });