Files
oembed-graphics/test/providers.test.js
Aiden Wilson 959f6590c3
Some checks failed
Build & Push Docker (latest) / build (push) Has been cancelled
Build & Push Docker (latest) / verify (push) Has been cancelled
conversion to type script
2026-05-29 23:55:31 +10:00

48 lines
1.7 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { buildOembedUrl } from "../dist/src/oembed.js";
import { findProvider, flattenProviders, wildcardToRegExp } from "../dist/src/providers.js";
test("wildcard provider schemes match target URLs", () => {
const regex = wildcardToRegExp("https://*.example.com/watch*");
assert.equal(regex.test("https://video.example.com/watch?v=abc"), true);
assert.equal(regex.test("https://example.org/watch?v=abc"), false);
});
test("flattens providers and finds a matching provider", () => {
const providers = flattenProviders([
{
provider_name: "Example",
provider_url: "https://example.com",
endpoints: [
{
schemes: ["https://*.example.com/watch*"],
url: "https://example.com/oembed.{format}",
},
],
},
]);
const provider = findProvider("https://video.example.com/watch?id=1", providers);
assert.equal(provider.providerName, "Example");
assert.equal(provider.endpoint, "https://example.com/oembed.json");
});
test("builds oEmbed provider request URLs", () => {
const url = buildOembedUrl("https://example.com/oembed", "https://video.example.com/watch?id=1", 500, 480);
assert.equal(url.searchParams.get("url"), "https://video.example.com/watch?id=1");
assert.equal(url.searchParams.get("format"), "json");
assert.equal(url.searchParams.get("maxwidth"), "500");
assert.equal(url.searchParams.get("maxheight"), "480");
});
test("caps all oEmbed maxwidth values to 500", () => {
const url = buildOembedUrl("https://example.com/oembed", "https://video.example.com/watch?id=1", 1280);
assert.equal(url.searchParams.get("maxwidth"), "500");
});