📄 src/models/test/initialLoad.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, it, expect, vi, beforeEach, afterEach } from 'vitest';
16import { initialLoad, getRecordCols, sidecarStore, getCurrentLangs } from '../worker/libs/initialLoad.ts';
17import * as enrichedIndex from '../enriched-index.ts';
18import * as patchStorage from '../indexdb/patch_storage.ts';
19
20// Mock the enriched index parser
21vi.mock('../enriched-index.ts', () => ({
22 parseSidecar: vi.fn(),
23 extractRecordCols: vi.fn()
24}));
25
26// Mock patch_storage
27vi.mock('../indexdb/patch_storage.ts', () => ({
28 getAllPatches: vi.fn().mockResolvedValue({ 'test_user': [] })
29}));
30
31describe('initialLoad', () => {
32 beforeEach(() => {
33 // @ts-ignore
34 globalThis.fetch = vi.fn().mockResolvedValue({
35 ok: true,
36 arrayBuffer: async () => new ArrayBuffer(0)
37 });
38
39 sidecarStore.clear();
40 vi.clearAllMocks();
41 });
42
43 afterEach(() => {
44 vi.restoreAllMocks();
45 });
46
47 it('fetches and parses all tiers', async () => {
48 const mockParsed = { columns: new Map([['ids', new Int32Array([10, 20])]]) };
49 vi.mocked(enrichedIndex.parseSidecar).mockReturnValue(mockParsed as any);
50
51 const result = await initialLoad([{
52 objectType: 1,
53 lang: 'eng',
54 slimUrl: '/slim.bin',
55 extendedUrl: '/ext.bin',
56 provUrl: '/prov.bin'
57 }], ['eng', 'fra']);
58
59 expect(getCurrentLangs()).toEqual(['eng', 'fra']);
60 expect(globalThis.fetch).toHaveBeenCalledTimes(3);
61 expect(result.sidecars.length).toBe(1);
62 expect(result.patches).toEqual({ 'test_user': [] });
63
64 const set = result.sidecars[0];
65 expect(set.idIndex.get(10)).toBe(0);
66 expect(set.idIndex.get(20)).toBe(1);
67 });
68
69 it('throws if fetch fails', async () => {
70 // @ts-ignore
71 globalThis.fetch.mockResolvedValueOnce({ ok: false, status: 404 });
72
73 await expect(initialLoad([{
74 objectType: 1, lang: 'eng', slimUrl: '/slim.bin'
75 }], ['eng'])).rejects.toThrow(/failed to fetch sidecar/);
76 });
77});
78
79describe('getRecordCols', () => {
80 beforeEach(() => {
81 sidecarStore.clear();
82 vi.clearAllMocks();
83 });
84
85 it('extracts record columns accurately based on ID index', async () => {
86 const mockSlim = { columns: new Map([['ids', new Int32Array([100, 200])]]) };
87 vi.mocked(enrichedIndex.parseSidecar).mockReturnValue(mockSlim as any);
88
89 // Mock extractRecordCols
90 vi.mocked(enrichedIndex.extractRecordCols).mockImplementation((sidecar, idx) => ({ extracted: true, idx }));
91
92 // @ts-ignore
93 globalThis.fetch = vi.fn().mockResolvedValue({ ok: true, arrayBuffer: async () => new ArrayBuffer(0) });
94
95 await initialLoad([{
96 objectType: 2,
97 lang: 'eng',
98 slimUrl: '/slim.bin'
99 }], ['eng']);
100
101 const cols = getRecordCols(2, 200); // 200 is at index 1
102 expect(cols.length).toBe(1);
103 expect(cols[0].lang).toBe('eng');
104 expect(cols[0].slim).toEqual({ extracted: true, idx: 1 });
105 expect(cols[0].extended).toBeUndefined();
106 });
107
108 it('returns empty array if record does not exist', async () => {
109 const mockSlim = { columns: new Map([['ids', new Int32Array([100, 200])]]) };
110 vi.mocked(enrichedIndex.parseSidecar).mockReturnValue(mockSlim as any);
111 // @ts-ignore
112 globalThis.fetch = vi.fn().mockResolvedValue({ ok: true, arrayBuffer: async () => new ArrayBuffer(0) });
113
114 await initialLoad([{ objectType: 2, lang: 'eng', slimUrl: '/slim.bin' }], ['eng']);
115
116 const cols = getRecordCols(2, 999);
117 expect(cols.length).toBe(0);
118 });
119});
120