import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { parseMarcXml } from './marc.ts';
const fx = (n: string) => readFileSync(join(import.meta.dirname, 'fixtures', n), 'utf8');
test('SRU wrapped record', () => {
const [r] = parseMarcXml(fx('loc-isbn-9781593278281.xml'));
assert.deepEqual(r!.lcc, [{ raw: 'QA76.73.R87 K53 2018', complete: true }]);
assert.match(r!.meta.title!, /Rust/);
assert.ok(r!.meta.authors!.some((a) => a.endsWith('Klabnik')));
assert.equal(r!.meta.year, 2018);
assert.ok(r!.lccn);
});
test('bare permalink record', () => {
const [r] = parseMarcXml(fx('loc-lccn-88005934.xml'));
assert.deepEqual(r!.lcc, [{ raw: 'QA76.73.C15 K47 1988', complete: true }]);
assert.equal(r!.lccn, '88005934');
});
test('zero records', () => {
assert.deepEqual(parseMarcXml(fx('loc-isbn-9780999999998.xml')), []);
});
test('single datafield/subfield objects, several 050s, other prefixes, 082, 264', () => {
const xml = `
PS3558.E63
PZ3.H4186Du
813/.54
Dune /
Ace,2005.
`;
const [r] = parseMarcXml(xml);
assert.deepEqual(r!.lcc, [{ raw: 'PS3558.E63', complete: false }, { raw: 'PZ3.H4186 Du', complete: true }]);
assert.equal(r!.dewey, '813.54');
assert.equal(r!.meta.title, 'Dune');
assert.equal(r!.meta.publisher, 'Ace');
assert.equal(r!.meta.year, 2005);
});
test('garbage is no records', () => {
assert.deepEqual(parseMarcXml('
nope'), []);
});
test('non-text records (a DVD of the book) contribute no call number', () => {
const rec = (type: string) => `01234c${type}m a2200337 a 4500
DVE9004.R69(viewing copy)
Harry Potter`;
assert.deepEqual(parseMarcXml(rec('g'))[0]!.lcc, []);
assert.equal(parseMarcXml(rec('g'))[0]!.meta.title, 'Harry Potter');
assert.equal(parseMarcXml(rec('a'))[0]!.lcc.length, 1);
});
test('authors: 100 only, dates and commas stripped, shown as Forename Surname', () => {
const xml = `
Herbert, Frank,1920-1986.
Dune /
Whelan, Michael,1950-`;
const [r] = parseMarcXml(xml);
assert.deepEqual(r!.meta.authors, ['Frank Herbert']);
assert.deepEqual(r!.names, ['Herbert, Frank', 'Whelan, Michael']);
});
test('authors: 700 only stands in when there is no 100; dates inside $a are removed', () => {
const xml = `
Anthology
Brooks, Frederick P., 1931-
King, Martin Luther, Jr.,`;
const [r] = parseMarcXml(xml);
assert.deepEqual(r!.meta.authors, ['Frederick P. Brooks', 'Martin Luther King Jr.']);
});
test('the Rust fixture shows a plain author name', () => {
const [r] = parseMarcXml(fx('loc-isbn-9781593278281.xml'));
assert.ok(r!.meta.authors!.every((a) => !a.includes(',') && !/\d/.test(a)), String(r!.meta.authors));
});