📄 src/models/test/file_storage.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 { fileStore } from '../indexdb/file_storage.ts';
18import { ClearDbData, Db, jsonStoreName } from '../indexdb/common.ts';
19import type { FileRecord } from '../interfaces.ts';
20
21describe('file_storage', () => {
22    beforeEach(async () => {
23        await ClearDbData();
24        await Db();
25    });
26
27    it('returns false for has when file not present', async () => {
28        const has = await fileStore.has('non-existent-file');
29        expect(has).toBe(false);
30    });
31
32    it('can store and load a file record', async () => {
33        const record: FileRecord = {
34            id: 'file-123',
35            size: 1024,
36            content: new Uint8Array([1, 2, 3]).buffer,
37            ts: Date.now()
38        };
39
40        const stored = await fileStore.store(record);
41        expect(stored).toBe(true);
42
43        const has = await fileStore.has('file-123');
44        expect(has).toBe(true);
45
46        const loaded = await fileStore.load('file-123');
47        expect(loaded).toBeDefined();
48        expect(loaded?.id).toBe('file-123');
49        expect(loaded?.size).toBe(1024);
50        
51        const contentArr = new Uint8Array(loaded?.content as ArrayBuffer);
52        expect(contentArr[0]).toBe(1);
53    });
54
55    it('returns undefined when loading non-existent file', async () => {
56        const loaded = await fileStore.load('missing-file');
57        expect(loaded).toBeUndefined();
58    });
59
60    it('can update existing file data', async () => {
61        const record: FileRecord = {
62            id: 'file-update',
63            size: 50,
64            content: new ArrayBuffer(50),
65            ts: Date.now()
66        };
67
68        await fileStore.store(record);
69        
70        record.size = 100;
71        await fileStore.store(record);
72
73        const loaded = await fileStore.load('file-update');
74        expect(loaded?.size).toBe(100);
75    });
76});
77