Files
booksorter/src/web/shelve.test.ts
T

25 lines
1.3 KiB
TypeScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import type { Book } from '../shared/types.ts';
import { composeNumber, findNeighbours } from './shelve.ts';
test('a number is composed from the neighbour class, a new cutter and the year', () => {
assert.equal(composeNumber('GV199.42.U8 T74 2016', ['Nancy Pallister'], 'Hiking', 2025), 'GV199.42.U8 P35 2025');
assert.equal(composeNumber('TX719.C454 2001', ['Samin Nosrat'], 'Salt fat acid heat', 2017), 'TX719.N67 2017');
});
test('no year given leaves it off; an unreadable neighbour gives nothing', () => {
assert.equal(composeNumber('TX719.C454 2001', ['Samin Nosrat'], 'x', null), 'TX719.N67');
assert.equal(composeNumber('garbage', ['A'], 'x', 2000), null);
});
test('neighbours: matches by title or author, only books with a readable number, capped', () => {
const mk = (id: number, title: string, callNumber: string | null) =>
({ id, title, authors: ['Ann Author'], callNumber } as Book);
const books = [mk(1, 'Hiking', 'GV199.42 .U8'), mk(2, 'Hiking notes', null), mk(3, 'Other', 'QA76 .X')];
assert.deepEqual(findNeighbours(books, 'hik').map(b => b.id), [1]);
assert.deepEqual(findNeighbours(books, 'author').map(b => b.id), [1, 3]);
assert.deepEqual(findNeighbours(books, 'author', 1).length, 1);
assert.deepEqual(findNeighbours(books, ' '), []);
});