📄 src/isbn/isbn.test.ts
D-OPEN SOVEREIGN
1/*
2 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
3 * Non-Profit Public Library — The World Library
4 * 
5 * D-SAFE Certification issued by POND Enterprise.
6 * Published under the D-Open Code Sovereign Licence v1.0 framework
7 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
8 * https://registry.world.bibliotech.com/licence
9 * Source code donated to DataPond D-Library by it's author: data pond.
10 * 
11 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
12 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
13 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
14 */
15import { describe, expect, it } from 'vitest';
16import { ISBNAdapter } from './index';
17
18describe('ISBNAdapter.normalize', () => {
19  it('strips hyphens from ISBN-13', () => {
20    expect(ISBNAdapter.normalize('978-0-262-03384-8')).toBe('9780262033848');
21  });
22  it('passes through compact ISBN-13', () => {
23    expect(ISBNAdapter.normalize('9780262033848')).toBe('9780262033848');
24  });
25  it.each([
26    ['0-262-03384-4', '9780262033848'],
27    ['3-16-148410-X', '9783161484100'],
28    ['0306406152',    '9780306406157'],
29    ['0743273567',    '9780743273565'],
30  ])('converts ISBN-10 %s → %s', (input, expected) => {
31    expect(ISBNAdapter.normalize(input)).toBe(expected);
32  });
33});
34
35describe('ISBNAdapter.validate', () => {
36  it.each([
37    '978-0-262-03384-8', '9783161484100', '978-0-306-40615-7',
38    '978-0-13-110362-7', '9780743273565', '9788932910093',
39    '9789027439642',     '9782070360246', '9781402894626',
40  ])('accepts valid ISBN-13: %s', (input) => {
41    expect(ISBNAdapter.validate(input).valid).toBe(true);
42  });
43
44  it.each([
45    ['978-0-262-03384-7', 'wrong check digit'],
46    ['978-0-262-03384-9', 'wrong check digit'],
47    ['123456789',         'too short'],
48    ['97802620338481',    'too long'],
49    [null,                'null'],
50    [undefined,           'undefined'],
51    ['',                  'empty'],
52  ])('rejects invalid: %s (%s)', (input) => {
53    expect(ISBNAdapter.validate(input as string).valid).toBe(false);
54  });
55});
56
57describe('ISBNAdapter.toLinkedDataUri', () => {
58  it('returns urn:isbn: form', () => {
59    expect(ISBNAdapter.toLinkedDataUri('978-0-262-03384-8')).toBe('urn:isbn:9780262033848');
60  });
61});
62
63describe.skipIf(!process.env.INTEGRATION)('ISBNAdapter live API', () => {
64  it('fetches from Open Library', async () => {
65    const result = await ISBNAdapter.apis.openlibrary.fetch({ isbn: '9780262033848' });
66    expect(result.title).toBeTruthy();
67  });
68});
69