📄 src/models/test/serializer.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, beforeAll } from 'vitest';
16import { Book, type BookInput, NewBookWithId } from '../generated-src/Book.ts';
17import { Tag, type TagInput, NewTagWithId } from '../generated-src/Tag.ts';
18import { Person, type PersonInput, NewPersonWithId } from '../generated-src/Person.ts';
19import { parseSidecar, colsFromSidecar } from '../enriched-index.ts';
20import { setDefaultLanguage } from '../orm.ts';
21
22beforeAll(() => {
23    setDefaultLanguage('eng');
24});
25
26const mockBooks: BookInput[] = [
27    {
28        id: 5,
29        title: "Test Book",
30        description: "A test description",
31        originalLang: "eng",
32        previewTxId: "tx-preview",
33        ageGradeBand: 2,
34        safe: true,
35        deleted: false,
36        textQuality: 4,
37        authorIds: [1, 2],
38        defaultLanguage: "eng",
39        defaultDownloadTxId: "tx-download",
40        defaultMethod: 1,
41        defaultIsAiTranslation: false,
42        defaultAiEngine: null,
43        defaultAddedAt: 10000,
44        defaultNbPages: 100,
45        defaultSizeBytes: 1024,
46        defaultIsbn: null,
47        defaultPublisher: null,
48        defaultPublicationDate: null,
49        defaultPublishedAt: null,
50        defaultQualityScore: 4,
51        prerequisites: null,
52        deweyDecimal: null,
53        locCallNumber: null,
54        copyrightStatus: 0,
55        illustrations: 0,
56        educationQuality: 0,
57        practicality: 0,
58        accessibilityLevel: 0,
59        teacherMaterials: false,
60        pedagogicalScaffolding: 0,
61        clarityOfExplanation: 0,
62        workedExamples: 0,
63        exercisesAndAssessment: 0,
64        selfStudyReadiness: 0,
65        referenceApparatus: 0,
66        safetyGuidance: 0,
67        contentCurrency: 0,
68        ecoRegeneration: 0,
69        communityLiving: 0,
70        promotesPeace: 0,
71        businessDevelopment: 0,
72        instances: [{
73            language: "eng",
74            downloadTxId: "tx-download",
75            method: 1,
76            isAiTranslation: false,
77            aiEngine: null,
78            addedAt: 10000,
79            nbPages: 100,
80            sizeBytes: 1024,
81            publisher: null,
82            publicationDate: null,
83            publishedAt: null,
84            qualityScore: 4,
85            isbn: null,
86            isDefault: true
87        }]
88    }
89];
90
91const mockTags: TagInput[] = [
92    {
93        id: 42,
94        title: "Science",
95        description: "Science tag description",
96        originalLang: "eng",
97        previewTxId: null,
98        ageGradeBand: 0,
99        safe: true,
100        deleted: false,
101        tags: [10],
102        books: [5],
103        ddcNotation: null
104    }
105];
106
107const mockPersons: PersonInput[] = [
108    {
109        id: 1,
110        title: "Jane Doe",
111        description: "A famous author",
112        originalLang: "eng",
113        previewTxId: null,
114        ageGradeBand: 0,
115        safe: true,
116        deleted: false,
117        birthDate: "1980-01-01",
118        deathDate: null,
119        nationality: "US",
120        wikiUrl: "https://en.wikipedia.org/wiki/Jane_Doe",
121        isniId: null,
122        orcidId: null
123    }
124];
125
126describe('Code-Generated Serializers & ORM Merge', () => {
127    it('Book round-trip: serializes, parses, and merges into ORM correctly', () => {
128        const slimBuf = Book.serializeSlim(mockBooks);
129        const extBuf = Book.serializeExtended(mockBooks);
130        
131        const slimCols = colsFromSidecar(parseSidecar(slimBuf)) as any;
132        const extCols = colsFromSidecar(parseSidecar(extBuf)) as any;
133        
134        const book = NewBookWithId(5);
135        book.mergeSlim(slimCols, 0);
136        book.mergeExtended(extCols, 0, 'eng'); // lang = 'eng'
137        
138        expect(book.id).toBe(5);
139        expect(book.title).toBe("Test Book");
140        expect(book.description).toBe("A test description");
141        expect(book.ageGradeBand).toBe(2);
142        expect(book.safe).toBe(true);
143        expect(book.deleted).toBe(false);
144        expect(book.getInstances('eng')[0].downloadTxId).toBe("tx-download");
145        expect(book.getInstances('eng')[0].method).toBe(1);
146        expect(book.getInstances('eng')[0].nbPages).toBe(100);
147        
148        expect(book.authorsIds).toEqual([1, 2]);
149        
150        const instances = book.getInstances('eng');
151        expect(instances.length).toBe(1);
152        expect(instances[0].downloadTxId).toBe("tx-download");
153    });
154
155    it('Tag round-trip: serializes, parses, and merges into ORM correctly', () => {
156        const slimBuf = Tag.serializeSlim(mockTags);
157        const extBuf = Tag.serializeExtended(mockTags);
158        
159        const slimCols = colsFromSidecar(parseSidecar(slimBuf)) as any;
160        const extCols = colsFromSidecar(parseSidecar(extBuf)) as any;
161        
162        const tag = NewTagWithId(42);
163        tag.mergeSlim(slimCols, 0);
164        tag.mergeExtended(extCols, 0, 'eng');
165        
166        expect(tag.id).toBe(42);
167        expect(tag.title).toBe("Science");
168        expect(tag.description).toBe("Science tag description");
169        expect(tag.tagsIds).toEqual([10]);
170        expect(tag.booksIds).toEqual([5]);
171    });
172
173    it('Person round-trip: serializes, parses, and merges into ORM correctly', () => {
174        const slimBuf = Person.serializeSlim(mockPersons);
175        const extBuf = Person.serializeExtended(mockPersons);
176        
177        const slimCols = colsFromSidecar(parseSidecar(slimBuf)) as any;
178        const extCols = colsFromSidecar(parseSidecar(extBuf)) as any;
179        
180        const person = NewPersonWithId(1);
181        person.mergeSlim(slimCols, 0);
182        person.mergeExtended(extCols, 0, 'eng');
183        
184        expect(person.id).toBe(1);
185        expect(person.title).toBe("Jane Doe");
186        expect(person.description).toBe("A famous author");
187        expect(person.birthDate).toBe("1980-01-01");
188        expect(person.deathDate).toBe("");
189        expect(person.nationality).toBe("US");
190        expect(person.wikiUrl).toBe("https://en.wikipedia.org/wiki/Jane_Doe");
191    });
192});
193