📄 src/models/test/ExternalIdCrosswalk.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 { ExternalIdCrosswalk } from '../generated-src/ExternalIdCrosswalk.ts';
17
18class MockWorker {
19    onmessage: any;
20    onerror: any;
21    onmessageerror: any;
22    postMessage = vi.fn((data: any) => {
23        // Echo back for tests
24        setTimeout(() => {
25            if (this.onmessage) {
26                this.onmessage({ data: { msgId: data.msgId, result: { success: true, cmd: data.cmd } } });
27            }
28        }, 10);
29    });
30}
31
32describe('ExternalIdCrosswalk', () => {
33    let originalWorker: any;
34
35    beforeEach(() => {
36        originalWorker = globalThis.Worker;
37        (globalThis as any).Worker = MockWorker;
38        ExternalIdCrosswalk.instance = null;
39    });
40
41    afterEach(() => {
42        globalThis.Worker = originalWorker;
43    });
44
45    it('initializes worker and resolves promises', async () => {
46        const crosswalk = new ExternalIdCrosswalk();
47        expect(crosswalk.ready).toBe(false);
48
49        await crosswalk.init([] as any);
50        expect(crosswalk.ready).toBe(true);
51
52        const readyPromise = crosswalk.whenReady();
53        await expect(readyPromise).resolves.toBeUndefined();
54    });
55
56    it('sends loadLang request', async () => {
57        const crosswalk = new ExternalIdCrosswalk();
58        const res = await crosswalk.loadLang([] as any);
59        // The mock returns an object { success: true, cmd: 'load_lang_crosswalk' }
60        // but loadLang doesn't return the result, it just awaits it.
61        expect(res).toBeUndefined(); // or whatever the mock returns
62    });
63
64    it('sends resolveDerived request', async () => {
65        const crosswalk = new ExternalIdCrosswalk();
66        const res = await crosswalk.resolveDerived('test-sfcuid', 'eng');
67        expect(res).toEqual({ success: true, cmd: 'resolve_derived' });
68    });
69
70    it('sends resolveForInstance request', async () => {
71        const crosswalk = new ExternalIdCrosswalk();
72        const res = await crosswalk.resolveForInstance('isbn', '12345', 'eng');
73        expect(res).toEqual({ success: true, cmd: 'resolve_standard' });
74    });
75});
76