From 1898db985f708f0ff54d04d2d77303c633bc1ba2 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 7 May 2026 00:35:07 +0300 Subject: Fix podcast episode pagination arguments (p0) --- web/js/tests/podcast-episodes-api.test.js | 74 +++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 web/js/tests/podcast-episodes-api.test.js (limited to 'web/js/tests') diff --git a/web/js/tests/podcast-episodes-api.test.js b/web/js/tests/podcast-episodes-api.test.js new file mode 100644 index 0000000..d46d782 --- /dev/null +++ b/web/js/tests/podcast-episodes-api.test.js @@ -0,0 +1,74 @@ +import { API } from '../api.js'; + +const failures = []; +const requested = []; + +function assert(cond, msg) { + if (!cond) failures.push(msg || 'assertion failed'); +} + +globalThis.fetch = async (url) => { + requested.push(url); + return new Response('[]', { + status: 200, + headers: { 'content-type': 'application/json' }, + }); +}; + +globalThis.location = { pathname: '/', href: '' }; + +async function capturesUrl(fn) { + requested.length = 0; + await fn(); + return requested[0]; +} + +function paramsFrom(url) { + return new URL(url, 'http://player.test').searchParams; +} + +async function testDefaults() { + const params = paramsFrom(await capturesUrl(() => API.podcastEpisodes(5))); + assert(params.get('limit') === '50', 'default limit should be 50'); + assert(params.get('offset') === '0', 'default offset should be 0'); +} + +async function testSecondArgumentIsOffset() { + const params = paramsFrom(await capturesUrl(() => API.podcastEpisodes(5, 0))); + assert(params.get('limit') === '50', 'single numeric argument should keep default limit'); + assert(params.get('offset') === '0', 'single numeric argument should set explicit offset'); +} + +async function testOptionsObject() { + const params = paramsFrom(await capturesUrl(() => API.podcastEpisodes(5, { limit: 25, offset: 75 }))); + assert(params.get('limit') === '25', 'options object should set limit'); + assert(params.get('offset') === '75', 'options object should set offset'); +} + +async function testOptionsObjectKeepsZeroLimit() { + const params = paramsFrom(await capturesUrl(() => API.podcastEpisodes(5, { limit: 0 }))); + assert(params.get('limit') === '0', 'options object should preserve explicit zero limit'); + assert(params.get('offset') === '0', 'options object should default offset to 0'); +} + +async function testLegacyLimitOffset() { + const params = paramsFrom(await capturesUrl(() => API.podcastEpisodes(5, 10, 20))); + assert(params.get('limit') === '10', 'legacy third-argument form should preserve limit'); + assert(params.get('offset') === '20', 'legacy third-argument form should preserve offset'); +} + +console.log('Running podcast episodes API pagination tests...'); +await testDefaults(); +await testSecondArgumentIsOffset(); +await testOptionsObject(); +await testOptionsObjectKeepsZeroLimit(); +await testLegacyLimitOffset(); + +if (failures.length) { + console.error('FAILURES:'); + failures.forEach((m) => console.error(' - ' + m)); + process.exit(1); +} else { + console.log('All podcast episodes API pagination tests passed.'); + process.exit(0); +} -- cgit v1.2.3