📄 src/models/test/book_data.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 'fake-indexeddb/auto';
16import { describe, it, expect, beforeEach } from 'vitest';
17import { hasBookData, loadBookData, storeBookData } from '../indexdb/book_data.ts';
18import { ClearDbData, Db, pdfDataStoreName } from '../indexdb/common.ts';
19import type { BookData } from '../interfaces.ts';
20
21describe('book_data', () => {
22    beforeEach(async () => {
23        await ClearDbData();
24        await Db();
25    });
26
27    it('returns false for hasBookData when not present', async () => {
28        const has = await hasBookData('non-existent-id');
29        expect(has).toBe(false);
30    });
31
32    it('can store and load book data', async () => {
33        const data: BookData = {
34            fileId: 'test-file-123',
35            pages: 10,
36            covers: [],
37            toc: [],
38            extractedWords: [],
39            pdfRef: {} as any
40        };
41
42        const stored = await storeBookData(data);
43        expect(stored).toBe(true);
44
45        const has = await hasBookData('test-file-123');
46        expect(has).toBe(true);
47
48        const loaded = await loadBookData('test-file-123');
49        expect(loaded).toBeDefined();
50        expect(loaded?.fileId).toBe('test-file-123');
51        expect(loaded?.pages).toBe(10);
52    });
53
54    it('returns undefined when loading non-existent data', async () => {
55        const loaded = await loadBookData('missing-data');
56        expect(loaded).toBeUndefined();
57    });
58
59    it('can update existing book data', async () => {
60        const data: BookData = {
61            fileId: 'test-file-update',
62            pages: 5,
63            covers: [],
64            toc: [],
65            extractedWords: [],
66            pdfRef: {} as any
67        };
68
69        await storeBookData(data);
70        
71        data.pages = 25;
72        await storeBookData(data); // update
73
74        const loaded = await loadBookData('test-file-update');
75        expect(loaded?.pages).toBe(25);
76    });
77});
78