Repository navigation
Expand file tree
/
Copy pathredditText.test.ts
More file actions
79 lines (77 loc) · 2.89 KB
/
Copy pathredditText.test.ts
File metadata and controls
79 lines (77 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { describe, expect, it } from "vitest";
import { redditVideo } from "./api.fixture";
import {
displayedRedditVideos,
redditVideoKey,
withoutRedditVideoLinks,
withoutRedditVideoUrls,
} from "./redditText";
import type { RedditVideo } from "./types";
const url = "https://v.redd.it/portrait";
const video: RedditVideo = redditVideo({
url: "/assets/saved.mp4",
source_url: `${url}/HLSPlaylist.m3u8?a=old`,
poster_url: null,
remote: false,
});
const displayed = displayedRedditVideos([video], new Set());
describe("represented Reddit video links", () => {
it("matches the public link, rotating manifest, packaged video and local archive", () => {
for (const value of [
url,
`${url}/HLSPlaylist.m3u8?a=new`,
"https://packaged-media.redd.it/portrait/pb/video.mp4?signature=new",
"v.redd.it/portrait",
video.url,
]) {
expect(displayed.has(redditVideoKey(value) ?? "")).toBe(true);
expect(withoutRedditVideoUrls(value, displayed)).toBe("");
}
});
it("keeps other clips, deceptive hosts and descriptive links", () => {
for (const value of [
"https://v.redd.it/other",
"https://v.redd.it.evil.test/portrait",
"https://example.com/portrait",
]) {
expect(withoutRedditVideoUrls(value, displayed)).toBe(value);
}
const html = withoutRedditVideoLinks(
`<p>A clip <a href="${url}">${url}</a></p><p><a href="${url}">the discussion</a></p><p>Plain: ${url}</p><p><a href="https://v.redd.it/other">https://v.redd.it/other</a></p>`,
displayed,
);
expect(html).toContain("the discussion");
expect(html).toContain("https://v.redd.it/other");
expect(html).not.toContain(`>${url}<`);
expect(html).not.toContain(`Plain: ${url}`);
});
it("retains code and image links, removes empty URL paragraphs, and sanitizes imported HTML", () => {
const html = withoutRedditVideoLinks(
`<p><a href="${url}">${url}</a><br></p><p><a href="${url}"><img src="/assets/poster.jpg"></a></p><pre>${url}</pre><script>alert(1)</script>`,
displayed,
);
expect(html).not.toContain("<br>");
expect(html).toContain("<img");
expect(html).toContain(`<pre>${url}</pre>`);
expect(html).not.toContain("script");
});
it("restores failed references and leaves links alone when no usable player represents them", () => {
for (const assets of [[], [{ ...video, url: "javascript:alert(1)" }]]) {
const keys = displayedRedditVideos(assets, new Set());
expect(withoutRedditVideoUrls(url, keys)).toBe(url);
}
const keys = displayedRedditVideos(
[
video,
{
...video,
url: "/assets/other.mp4",
source_url: "https://v.redd.it/other/HLSPlaylist.m3u8",
},
],
new Set([video.url]),
);
expect(withoutRedditVideoUrls(url, keys)).toBe(url);
expect(withoutRedditVideoUrls("https://v.redd.it/other", keys)).toBe("");
});
});