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

136 lines
4.0 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
filterUrlsByExtensions,
parseExtensionsOption,
parseIntOption,
sliceUrls,
} from "../src/utils/slice.js";
const sample = [
"https://x.test/c.jpg",
"https://x.test/a.jpg",
"https://x.test/b.jpg",
"https://x.test/e.jpg",
"https://x.test/d.jpg",
];
test("sliceUrls sorts deterministically with no slice options", () => {
assert.deepEqual(sliceUrls(sample), [
"https://x.test/a.jpg",
"https://x.test/b.jpg",
"https://x.test/c.jpg",
"https://x.test/d.jpg",
"https://x.test/e.jpg",
]);
});
test("sliceUrls applies offset only", () => {
assert.deepEqual(sliceUrls(sample, { offset: 2 }), [
"https://x.test/c.jpg",
"https://x.test/d.jpg",
"https://x.test/e.jpg",
]);
});
test("sliceUrls applies limit only", () => {
assert.deepEqual(sliceUrls(sample, { limit: 2 }), [
"https://x.test/a.jpg",
"https://x.test/b.jpg",
]);
});
test("sliceUrls applies offset and limit together", () => {
assert.deepEqual(sliceUrls(sample, { offset: 1, limit: 2 }), [
"https://x.test/b.jpg",
"https://x.test/c.jpg",
]);
});
test("sliceUrls returns [] when offset exceeds length", () => {
assert.deepEqual(sliceUrls(sample, { offset: 99 }), []);
});
test("sliceUrls works with a Set", () => {
const set = new Set(sample);
assert.deepEqual(sliceUrls(set, { limit: 1 }), ["https://x.test/a.jpg"]);
});
test("sliceUrls clamps negative offset to 0", () => {
assert.deepEqual(sliceUrls(sample, { offset: -5, limit: 1 }), [
"https://x.test/a.jpg",
]);
});
test("sliceUrls treats limit=0 as empty", () => {
assert.deepEqual(sliceUrls(sample, { limit: 0 }), []);
});
test("parseIntOption returns undefined when missing", () => {
assert.equal(parseIntOption(undefined, "--limit"), undefined);
assert.equal(parseIntOption("", "--limit"), undefined);
});
test("parseIntOption parses integers", () => {
assert.equal(parseIntOption("0", "--offset"), 0);
assert.equal(parseIntOption("42", "--limit"), 42);
});
test("parseIntOption rejects non-integers", () => {
assert.throws(() => parseIntOption("3.14", "--limit"), /must be an integer/);
assert.throws(() => parseIntOption("abc", "--offset"), /must be an integer/);
});
test("parseIntOption rejects negatives", () => {
assert.throws(() => parseIntOption("-1", "--limit"), /must be >= 0/);
});
test("parseExtensionsOption returns undefined for missing/blank", () => {
assert.equal(parseExtensionsOption(undefined), undefined);
assert.equal(parseExtensionsOption(""), undefined);
assert.equal(parseExtensionsOption(" , , "), undefined);
});
test("parseExtensionsOption normalizes case, dots, and whitespace", () => {
const set = parseExtensionsOption(" .JPG, png ,.WebP");
assert.deepEqual([...(set ?? [])].sort(), ["jpg", "png", "webp"]);
});
test("filterUrlsByExtensions returns input unchanged when no filter", () => {
const urls = ["https://x.test/a.jpg", "https://x.test/b.png"];
assert.deepEqual(filterUrlsByExtensions(urls, undefined), urls);
});
test("filterUrlsByExtensions keeps only matching extensions", () => {
const urls = [
"https://x.test/a.jpg",
"https://x.test/b.png",
"https://x.test/c.JPG",
"https://x.test/d.gif",
];
const set = parseExtensionsOption("jpg");
assert.deepEqual(filterUrlsByExtensions(urls, set), [
"https://x.test/a.jpg",
"https://x.test/c.JPG",
]);
});
test("filterUrlsByExtensions ignores query string and fragment", () => {
const urls = [
"https://x.test/a.jpg?v=2",
"https://x.test/b.png#frag",
"https://x.test/c.gif?x=1#y",
];
const set = parseExtensionsOption("jpg,png");
assert.deepEqual(filterUrlsByExtensions(urls, set), [
"https://x.test/a.jpg?v=2",
"https://x.test/b.png#frag",
]);
});
test("filterUrlsByExtensions drops URLs without an extension", () => {
const urls = ["https://x.test/no-ext", "https://x.test/a.jpg"];
const set = parseExtensionsOption("jpg");
assert.deepEqual(filterUrlsByExtensions(urls, set), ["https://x.test/a.jpg"]);
});