import { html, stubNetwork } from "../../helpers.ts";
import { assert, assertEquals, assertFalse } from "@std/assert";
import { fetchRichContent } from "../../../services/rich-content-service.ts";
import { soundcloudProvider } from "../../../remote/providers/soundcloud.ts";
const embedOf = (url: string) =>
`https://w.soundcloud.com/player/?url=${
encodeURIComponent(url)
}&visual=true&auto_play=false`;
Deno.test("soundcloud provider", async () => {
using _ = stubNetwork(() =>
html(`
`)
);
const sc = await fetchRichContent("https://soundcloud.com/artist/track");
assertEquals(sc?.type, "soundcloud");
assertEquals(sc?.siteName, "SoundCloud");
assertEquals(sc?.title, "Track");
assertEquals(sc?.description, "Listen to Track");
assertEquals(sc?.embedUrl, embedOf("https://soundcloud.com/artist/track"));
});
Deno.test("soundcloud provider: a short link embeds the page it leads to", async () => {
using _ = stubNetwork((url) =>
url.hostname === "on.soundcloud.com"
? new Response(null, {
status: 302,
headers: {
location: "https://m.soundcloud.com/artist/track?si=abc&utm_source=x",
},
})
: html(``)
);
const sc = await fetchRichContent("https://on.soundcloud.com/AbCd");
assertEquals(sc?.type, "soundcloud");
assertEquals(sc?.url, "https://on.soundcloud.com/AbCd");
assertEquals(sc?.embedUrl, embedOf("https://soundcloud.com/artist/track"));
});
Deno.test("soundcloud provider: the page's og:url names what is embedded", async () => {
using _ = stubNetwork(() =>
html(
``,
)
);
const sc = await fetchRichContent("https://soundcloud.com/artist/real?in=x");
assertEquals(sc?.embedUrl, embedOf("https://soundcloud.com/artist/real"));
});
Deno.test("soundcloud provider: SoundCloud's hosts only", () => {
for (
const url of [
"https://soundcloud.com/a/t",
"https://www.soundcloud.com/a/t",
"https://m.soundcloud.com/a/t",
"https://on.soundcloud.com/AbCd",
]
) {
assert(soundcloudProvider.matches(url), url);
}
assertFalse(
soundcloudProvider.matches("https://soundcloud.com.evil.example/"),
);
assertFalse(
soundcloudProvider.matches("https://api.soundcloud.com/tracks/1"),
);
});