πŸ“„ src/models/test/Person.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, beforeEach } from 'vitest';
16import { Person, NewPersonWithId } from '../generated-src/Person.ts';
17import { parseSidecar, colsFromSidecar } from '../enriched-index.ts';
18import { clear, configureDb } from '../db.ts';
19import { setDefaultLanguage } from '../orm.ts';
20import { PatchInstance } from '../patcher.ts';
21import { PatchOperation } from '../binary.ts';
22import { createPersonFixture } from './fixtures.ts';
23
24beforeEach(() => {
25    clear();
26    configureDb({ cacheLimit: 10_000 });
27    setDefaultLanguage('eng');
28    PatchInstance.clearQueue();
29});
30
31describe('Person Model - Edge Cases with Fixtures', () => {
32    it('should handle full serialization roundtrip with complex language states', () => {
33        const fixtureEng = createPersonFixture(400);
34        fixtureEng.description = 'English Biography';
35        const fixtureFra = createPersonFixture(400);
36        fixtureFra.description = 'Biographie FranΓ§aise';
37        fixtureFra.title = 'Personne 400';
38
39        const bufferEngSlim = Person.serializeSlim([fixtureEng]);
40        const bufferEngExt = Person.serializeExtended([fixtureEng]);
41        
42        const bufferFraSlim = Person.serializeSlim([fixtureFra]);
43        const bufferFraExt = Person.serializeExtended([fixtureFra]);
44
45        const colsEngSlim = colsFromSidecar(parseSidecar(bufferEngSlim));
46        const colsEngExt = colsFromSidecar(parseSidecar(bufferEngExt));
47        
48        const colsFraSlim = colsFromSidecar(parseSidecar(bufferFraSlim));
49        const colsFraExt = colsFromSidecar(parseSidecar(bufferFraExt));
50
51        const person = NewPersonWithId(401);
52        person.mergeSlim(colsEngSlim as any, 0, 'eng');
53        person.mergeExtended(colsEngExt as any, 0, 'eng');
54        
55        person.mergeSlim(colsFraSlim as any, 0, 'fra');
56        person.mergeExtended(colsFraExt as any, 0, 'fra');
57
58        expect(person.getDescription('eng')).toBe('English Biography');
59        expect(person.getDescription('fra')).toBe('Biographie FranΓ§aise');
60        
61        expect(person.getTitle('eng')).toBe('Person 400');
62        expect(person.getTitle('fra')).toBe('Personne 400');
63        
64        expect(person.nationality).toBe('UK'); // base prop
65    });
66});
67
68describe('Person Model - Advanced Patch Tracking', () => {
69    it('should generate localized string patches when languages differ', () => {
70        const person = NewPersonWithId(500);
71        PatchInstance.clearQueue();
72
73        person.setDescription('Bio', 'eng');
74        person.setDescription('La Bio', 'fra');
75
76        const patches = PatchInstance.patches;
77        expect(patches.length).toBe(2);
78
79        expect(patches[0]).toMatchObject({
80            operation: PatchOperation.EditPropertyString,
81            objectId: 500,
82            propertyId: 1, // description
83            langId: 1, // eng -> 1 in LANG_ID_ENCODE
84            value: 'Bio'
85        });
86
87        expect(patches[1]).toMatchObject({
88            operation: PatchOperation.EditPropertyString,
89            objectId: 500,
90            propertyId: 1, // description
91            langId: 2, // fra -> 2 in LANG_ID_ENCODE
92            value: 'La Bio'
93        });
94    });
95
96    it('should track external identifiers accurately in patches', () => {
97        const person = NewPersonWithId(501);
98        PatchInstance.clearQueue();
99
100        person.isniId = '0000000012345678';
101        person.orcidId = '0000-0002-1825-0097';
102
103        const patches = PatchInstance.patches;
104        expect(patches.length).toBe(2);
105
106        expect(patches[0]).toMatchObject({
107            operation: PatchOperation.EditPropertyString,
108            objectId: 501,
109            propertyId: 47, // isniId
110            value: '0000000012345678'
111        });
112
113        expect(patches[1]).toMatchObject({
114            operation: PatchOperation.EditPropertyString,
115            objectId: 501,
116            propertyId: 48, // orcidId
117            value: '0000-0002-1825-0097'
118        });
119    });
120});
121