chore(repo): remove dead code and useless tests (#3375)

This commit is contained in:
Hampus authored and GitHub committed 2026-10-10 21:43:29 +02:00
1 parent 7e177a1c90
commit 1cd62eb661
1391 files changed
+783 -120415

No files matched your search

-292
View File
@@ -199,295 +199,3 @@ fn host_matches(hostname: &str, allowed: &[&str]) -> bool {
.iter()
.any(|&a| hostname == a || hostname.ends_with(&format!(".{a}")))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn discover_json_endpoint() {
let html = r#"<html><head>
<link rel="alternate" type="application/json+oembed" href="https://e.com/oembed?url=x">
</head></html>"#;
let eps = discover_oembed_url(html);
assert_eq!(eps.len(), 1);
assert_eq!(eps[0].format, OEmbedFormat::Json);
assert_eq!(eps[0].url, "https://e.com/oembed?url=x");
}
#[test]
fn discover_xml_endpoint() {
let html = r#"<head><link rel="alternate" type="text/xml+oembed" href="https://e.com/o.xml"></head>"#;
let eps = discover_oembed_url(html);
assert_eq!(eps.len(), 1);
assert_eq!(eps[0].format, OEmbedFormat::Xml);
}
#[test]
fn discover_no_oembed() {
assert!(discover_oembed_url("<head></head>").is_empty());
}
#[test]
fn parse_oembed_json_photo() {
let j = r#"{"type":"photo","title":"Sunset","url":"https://e.com/img.jpg","width":800,"height":600}"#;
let r: OEmbedResponse = serde_json::from_str(j).unwrap();
assert_eq!(r.oembed_type.as_deref(), Some("photo"));
assert_eq!(r.title.as_deref(), Some("Sunset"));
}
#[test]
fn parse_oembed_json_video() {
let j = r#"{"type":"video","title":"Clip","html":"<iframe></iframe>","width":1280,"height":720}"#;
let r: OEmbedResponse = serde_json::from_str(j).unwrap();
assert_eq!(r.oembed_type.as_deref(), Some("video"));
assert!(r.html.is_some());
}
#[test]
fn parse_oembed_json_rich_and_link() {
let r: OEmbedResponse =
serde_json::from_str(r#"{"type":"rich","provider_name":"P"}"#).unwrap();
assert_eq!(r.oembed_type.as_deref(), Some("rich"));
let l: OEmbedResponse = serde_json::from_str(r#"{"type":"link","title":"P"}"#).unwrap();
assert_eq!(l.oembed_type.as_deref(), Some("link"));
}
#[test]
fn parse_oembed_xml_basic() {
let r = parse_oembed_xml("<oembed><type>photo</type><title>S</title></oembed>").unwrap();
assert_eq!(r.oembed_type.as_deref(), Some("photo"));
}
#[test]
fn parse_dimension_values() {
assert_eq!(parse_dimension(&serde_json::json!(480)), Some(480));
assert_eq!(parse_dimension(&serde_json::json!("720px")), Some(720));
assert_eq!(parse_dimension(&serde_json::json!(0)), None);
assert_eq!(parse_dimension(&serde_json::json!(5000)), Some(4096));
assert_eq!(parse_dimension(&serde_json::json!("5000px")), Some(4096));
}
#[test]
fn known_oembed_youtube() {
let ep = known_oembed_endpoint("youtube.com", "https://youtube.com/watch?v=abc");
assert!(ep.is_some());
assert!(ep.unwrap().url.contains("youtube.com/oembed"));
}
#[test]
fn known_oembed_unknown_host() {
assert!(known_oembed_endpoint("example.com", "https://example.com").is_none());
}
#[test]
fn known_oembed_all_providers_match_ts() {
let cases = [
("youtube.com", "youtube.com/oembed"),
("www.youtube.com", "youtube.com/oembed"),
("youtu.be", "youtube.com/oembed"),
("youtube-nocookie.com", "youtube.com/oembed"),
("vimeo.com", "vimeo.com/api/oembed.json"),
("soundcloud.com", "soundcloud.com/oembed"),
("spotify.com", "open.spotify.com/oembed"),
("open.spotify.com", "open.spotify.com/oembed"),
("twitter.com", "publish.twitter.com/oembed"),
("x.com", "publish.twitter.com/oembed"),
("tiktok.com", "tiktok.com/oembed"),
("www.tiktok.com", "tiktok.com/oembed"),
("reddit.com", "reddit.com/oembed"),
("www.reddit.com", "reddit.com/oembed"),
("codepen.io", "codepen.io/api/oembed"),
];
for (host, expected_substr) in cases {
let ep = known_oembed_endpoint(host, &format!("https://{host}/page"));
assert!(ep.is_some(), "expected known endpoint for {host}");
assert!(
ep.as_ref().unwrap().url.contains(expected_substr),
"endpoint for {host} should contain {expected_substr}, got {}",
ep.unwrap().url
);
}
}
#[test]
fn known_oembed_twitter_params_match_ts() {
let ep =
known_oembed_endpoint("twitter.com", "https://twitter.com/user/status/123").unwrap();
assert!(ep.url.contains("omit_script=true"));
assert!(ep.url.contains("dnt=true"));
}
#[test]
fn known_oembed_youtube_format_param() {
let ep = known_oembed_endpoint("youtube.com", "https://youtube.com/watch?v=abc").unwrap();
assert!(ep.url.contains("format=json"));
}
#[test]
fn known_oembed_subdomain_matching() {
assert!(
known_oembed_endpoint("music.youtube.com", "https://music.youtube.com/watch?v=abc")
.is_some()
);
assert!(
known_oembed_endpoint("m.soundcloud.com", "https://m.soundcloud.com/track").is_some()
);
assert!(known_oembed_endpoint("old.reddit.com", "https://old.reddit.com/r/test").is_some());
}
#[test]
fn known_oembed_url_encodes_source() {
let ep =
known_oembed_endpoint("youtube.com", "https://youtube.com/watch?v=abc&t=10").unwrap();
assert!(ep.url.contains("url="));
assert!(!ep.url.contains("url=https://youtube.com/watch?v=abc&t=10"));
}
#[test]
fn discover_both_json_and_xml() {
let html = r#"<head>
<link rel="alternate" type="application/json+oembed" href="https://e.com/j">
<link rel="alternate" type="text/xml+oembed" href="https://e.com/x">
</head>"#;
let eps = discover_oembed_url(html);
assert_eq!(eps.len(), 2);
assert_eq!(eps[0].format, OEmbedFormat::Json);
assert_eq!(eps[1].format, OEmbedFormat::Xml);
}
#[test]
fn discover_infers_xml_from_format_param() {
let html = r#"<head>
<link rel="alternate" type="application/json+oembed" href="https://e.com/oembed?format=xml">
</head>"#;
let eps = discover_oembed_url(html);
assert_eq!(eps[0].format, OEmbedFormat::Xml);
}
#[test]
fn discover_skips_link_without_oembed_type() {
let html = r#"<head>
<link rel="alternate" type="application/json" href="https://e.com/api">
</head>"#;
assert!(discover_oembed_url(html).is_empty());
}
#[test]
fn discover_skips_empty_href() {
let html = r#"<head>
<link rel="alternate" type="application/json+oembed" href="">
</head>"#;
assert!(discover_oembed_url(html).is_empty());
}
#[test]
fn parse_dimension_integer() {
assert_eq!(parse_dimension(&serde_json::json!(1280)), Some(1280));
}
#[test]
fn parse_dimension_string_with_px_suffix() {
assert_eq!(parse_dimension(&serde_json::json!("480px")), Some(480));
}
#[test]
fn parse_dimension_string_plain() {
assert_eq!(parse_dimension(&serde_json::json!("720")), Some(720));
}
#[test]
fn parse_dimension_zero_is_none() {
assert_eq!(parse_dimension(&serde_json::json!(0)), None);
assert_eq!(parse_dimension(&serde_json::json!("0")), None);
}
#[test]
fn parse_dimension_clamps_at_4096() {
assert_eq!(parse_dimension(&serde_json::json!(9999)), Some(4096));
assert_eq!(parse_dimension(&serde_json::json!("10000")), Some(4096));
}
#[test]
fn parse_dimension_negative_returns_none() {
assert_eq!(parse_dimension(&serde_json::json!(-1)), None);
}
#[test]
fn parse_dimension_non_numeric_string_returns_none() {
assert_eq!(parse_dimension(&serde_json::json!("abc")), None);
}
#[test]
fn parse_dimension_null_returns_none() {
assert_eq!(parse_dimension(&serde_json::json!(null)), None);
}
#[test]
fn parse_dimension_bool_returns_none() {
assert_eq!(parse_dimension(&serde_json::json!(true)), None);
}
#[test]
fn parse_oembed_xml_with_nested_tags() {
let xml = r#"<oembed>
<type>video</type>
<title>My Video</title>
<html><![CDATA[<iframe></iframe>]]></html>
<width>640</width>
<height>360</height>
<provider_name>Provider</provider_name>
<provider_url>https://provider.com</provider_url>
</oembed>"#;
let r = parse_oembed_xml(xml).unwrap();
assert_eq!(r.oembed_type.as_deref(), Some("video"));
assert_eq!(r.title.as_deref(), Some("My Video"));
assert_eq!(r.provider_name.as_deref(), Some("Provider"));
}
#[test]
fn parse_oembed_xml_empty_field_returns_none() {
let xml = "<oembed><title></title><type>link</type></oembed>";
let r = parse_oembed_xml(xml).unwrap();
assert!(r.title.is_none());
assert_eq!(r.oembed_type.as_deref(), Some("link"));
}
#[test]
fn parse_oembed_json_with_author() {
let j = r#"{
"type": "video",
"author_name": "Author",
"author_url": "https://author.com",
"provider_name": "Provider",
"provider_url": "https://provider.com",
"thumbnail_url": "https://e.com/thumb.jpg",
"thumbnail_width": 120,
"thumbnail_height": 90
}"#;
let r: OEmbedResponse = serde_json::from_str(j).unwrap();
assert_eq!(r.author_name.as_deref(), Some("Author"));
assert_eq!(r.author_url.as_deref(), Some("https://author.com"));
assert_eq!(r.thumbnail_url.as_deref(), Some("https://e.com/thumb.jpg"));
assert_eq!(
parse_dimension(r.thumbnail_width.as_ref().unwrap()),
Some(120)
);
}
#[test]
fn parse_oembed_json_missing_all_optional() {
let r: OEmbedResponse = serde_json::from_str("{}").unwrap();
assert!(r.oembed_type.is_none());
assert!(r.title.is_none());
assert!(r.html.is_none());
}
#[test]
fn parse_oembed_json_dimension_as_string() {
let j = r#"{"type": "video", "width": "640px", "height": "360"}"#;
let r: OEmbedResponse = serde_json::from_str(j).unwrap();
assert_eq!(parse_dimension(r.width.as_ref().unwrap()), Some(640));
assert_eq!(parse_dimension(r.height.as_ref().unwrap()), Some(360));
}
}