diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-09 11:11:49 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-09 11:11:49 +0300 |
| commit | 8c32cd117abe79f4ee6cff3a950ffc35c95faeff (patch) | |
| tree | 5b63a4789351da4f2d9c1cd2e7007f67d43b3371 /web/js/tests | |
| parent | 59c8870061465142306e7a6e5d3fcb344686cfe9 (diff) | |
Refine media browsing and set covers
Diffstat (limited to 'web/js/tests')
| -rw-r--r-- | web/js/tests/media-pagination.test.js | 56 | ||||
| -rw-r--r-- | web/js/tests/search-parser.test.js | 33 |
2 files changed, 89 insertions, 0 deletions
diff --git a/web/js/tests/media-pagination.test.js b/web/js/tests/media-pagination.test.js new file mode 100644 index 0000000..0d599fc --- /dev/null +++ b/web/js/tests/media-pagination.test.js @@ -0,0 +1,56 @@ +import { paginateItems, setMediaPageSize } from '../views/media-grid.js'; +import { state } from '../state.js'; + +const failures = []; + +function assert(cond, msg) { + if (!cond) failures.push(msg || 'assertion failed'); +} + +function items(count) { + return Array.from({ length: count }, (_, i) => i + 1); +} + +function testDefaultPageSize() { + setMediaPageSize(undefined); + const first = paginateItems(items(101), 0); + assert(first.items.length === 100, 'default first page should contain 100 items'); + assert(first.hasNext, 'default first page should have next'); + assert(!first.hasPrev, 'default first page should not have previous'); + + const second = paginateItems(items(101), 1); + assert(second.items.length === 1, 'default second page should contain remaining item'); + assert(second.hasPrev, 'default second page should have previous'); + assert(!second.hasNext, 'default second page should not have next'); +} + +function testConfiguredPageSize() { + state.mediaPage = 2; + setMediaPageSize(25); + const page = paginateItems(items(60), 1); + assert(state.mediaPage === 0, 'changing page size should reset current media page'); + assert(page.items.length === 25, 'configured page should contain configured number of items'); + assert(page.start === 25, 'configured second page should start at item offset 25'); + assert(page.end === 50, 'configured second page should end at item offset 50'); +} + +function testClampsOutOfRangePage() { + setMediaPageSize(25); + const page = paginateItems(items(60), 99); + assert(page.page === 2, 'page should clamp to the last page'); + assert(page.items.length === 10, 'last page should contain remaining items'); +} + +console.log('Running media pagination tests...'); +testDefaultPageSize(); +testConfiguredPageSize(); +testClampsOutOfRangePage(); + +if (failures.length) { + console.error('FAILURES:'); + failures.forEach((m) => console.error(' - ' + m)); + process.exit(1); +} else { + console.log('All media pagination tests passed.'); + process.exit(0); +} diff --git a/web/js/tests/search-parser.test.js b/web/js/tests/search-parser.test.js new file mode 100644 index 0000000..8b8e287 --- /dev/null +++ b/web/js/tests/search-parser.test.js @@ -0,0 +1,33 @@ +import { parseQuery } from '../search.js'; + +const failures = []; + +function assert(cond, msg) { + if (!cond) failures.push(msg || 'assertion failed'); +} + +function testSetFilter() { + const parsed = parseQuery('set:yoga'); + assert(parsed.set === 'yoga', 'set token should parse set name'); + assert(parsed.search === '', 'set token should not become text search'); +} + +function testQuotedSetFilter() { + const parsed = parseQuery('set:"Yoga Flow" type:video morning'); + assert(parsed.set === 'Yoga Flow', 'quoted set token should preserve spaces'); + assert(parsed.type === 'video', 'other filters should still parse'); + assert(parsed.search === 'morning', 'free text should still parse'); +} + +console.log('Running search parser tests...'); +testSetFilter(); +testQuotedSetFilter(); + +if (failures.length) { + console.error('FAILURES:'); + failures.forEach((m) => console.error(' - ' + m)); + process.exit(1); +} else { + console.log('All search parser tests passed.'); + process.exit(0); +} |
