Files
wp-media-rewind/tests/media.test.ts
T

38 lines
1.4 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { buildMediaUrlRegex, DEFAULT_MEDIA_EXTENSIONS } from "../src/utils/media.js";
test("default regex matches typical WordPress upload URLs", () => {
const re = buildMediaUrlRegex(DEFAULT_MEDIA_EXTENSIONS);
const text =
"see <img src=\"https://site.test/wp-content/uploads/2020/01/photo.jpg\"/> and " +
"https://site.test/wp-content/uploads/2020/01/doc.pdf?v=2 and ignore https://site.test/page";
const matches = text.match(re) ?? [];
assert.deepEqual(matches, [
"https://site.test/wp-content/uploads/2020/01/photo.jpg",
"https://site.test/wp-content/uploads/2020/01/doc.pdf?v=2",
]);
});
test("regex is case-insensitive on extension", () => {
const re = buildMediaUrlRegex(["jpg"]);
assert.ok(re.test("https://x.test/a.JPG"));
});
test("regex respects custom extensions only", () => {
const re = buildMediaUrlRegex(["xyz"]);
assert.equal("https://x.test/a.jpg".match(re), null);
assert.ok(re.test("https://x.test/a.xyz"));
});
test("buildMediaUrlRegex throws on empty list", () => {
assert.throws(() => buildMediaUrlRegex([]), /at least one extension/);
});
test("regex stops at quotes and whitespace", () => {
const re = buildMediaUrlRegex(["png"]);
const text = `'https://x.test/a.png' "https://x.test/b.png"`;
const matches = text.match(re) ?? [];
assert.deepEqual(matches, ["https://x.test/a.png", "https://x.test/b.png"]);
});