|
| 1 | +import { describe, expect, it } from "bun:test" |
| 2 | +import { pickBestVideoVariantUrl } from "./twitter-utils" |
| 3 | + |
| 4 | +describe("pickBestVideoVariantUrl", () => { |
| 5 | + it("returns an empty string when there are no variants", () => { |
| 6 | + expect(pickBestVideoVariantUrl(undefined)).toBe("") |
| 7 | + expect(pickBestVideoVariantUrl([])).toBe("") |
| 8 | + }) |
| 9 | + |
| 10 | + it("picks the highest-bitrate mp4, not the first variant", () => { |
| 11 | + const variants = [ |
| 12 | + { |
| 13 | + url: "https://video.twimg.com/playlist.m3u8", |
| 14 | + content_type: "application/x-mpegURL", |
| 15 | + }, |
| 16 | + { |
| 17 | + url: "https://video.twimg.com/low.mp4", |
| 18 | + content_type: "video/mp4", |
| 19 | + bitrate: 256000, |
| 20 | + }, |
| 21 | + { |
| 22 | + url: "https://video.twimg.com/high.mp4", |
| 23 | + content_type: "video/mp4", |
| 24 | + bitrate: 2176000, |
| 25 | + }, |
| 26 | + { |
| 27 | + url: "https://video.twimg.com/mid.mp4", |
| 28 | + content_type: "video/mp4", |
| 29 | + bitrate: 832000, |
| 30 | + }, |
| 31 | + ] |
| 32 | + expect(pickBestVideoVariantUrl(variants)).toBe( |
| 33 | + "https://video.twimg.com/high.mp4", |
| 34 | + ) |
| 35 | + }) |
| 36 | + |
| 37 | + it("does not return the HLS playlist when mp4 renditions exist", () => { |
| 38 | + const variants = [ |
| 39 | + { |
| 40 | + url: "https://video.twimg.com/playlist.m3u8", |
| 41 | + content_type: "application/x-mpegURL", |
| 42 | + }, |
| 43 | + { |
| 44 | + url: "https://video.twimg.com/only.mp4", |
| 45 | + content_type: "video/mp4", |
| 46 | + bitrate: 632000, |
| 47 | + }, |
| 48 | + ] |
| 49 | + expect(pickBestVideoVariantUrl(variants)).toBe( |
| 50 | + "https://video.twimg.com/only.mp4", |
| 51 | + ) |
| 52 | + }) |
| 53 | + |
| 54 | + it("falls back to the first variant when no mp4 is present", () => { |
| 55 | + const variants = [ |
| 56 | + { |
| 57 | + url: "https://video.twimg.com/playlist.m3u8", |
| 58 | + content_type: "application/x-mpegURL", |
| 59 | + }, |
| 60 | + ] |
| 61 | + expect(pickBestVideoVariantUrl(variants)).toBe( |
| 62 | + "https://video.twimg.com/playlist.m3u8", |
| 63 | + ) |
| 64 | + }) |
| 65 | + |
| 66 | + it("detects mp4 by extension when content_type is absent", () => { |
| 67 | + const variants = [ |
| 68 | + { url: "https://video.twimg.com/240/vid.mp4?tag=12" }, |
| 69 | + { url: "https://video.twimg.com/720/vid.mp4?tag=12", bitrate: 2176000 }, |
| 70 | + ] |
| 71 | + expect(pickBestVideoVariantUrl(variants)).toBe( |
| 72 | + "https://video.twimg.com/720/vid.mp4?tag=12", |
| 73 | + ) |
| 74 | + }) |
| 75 | +}) |
0 commit comments