📄 src/models/test/pid.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 } from 'vitest';
16import { encodePID, decodePID, base34, unbase34 } from '../pid.js';
17
18describe('pid codec', () => {
19 it('base34 correctly converts 32-bit bounds', () => {
20 expect(base34(0)).toBe('0000000');
21 expect(base34(4740)).toBe('000043E');
22 });
23
24 it('unbase34 round-trips correctly', () => {
25 const testCases = [0, 1, 100, 4740, 2147483647, 4294967295];
26 for (const wireId of testCases) {
27 const b34 = base34(wireId);
28 expect(unbase34(b34)).toBe(wireId);
29 }
30 });
31
32 it('verifies round-trip: decodePID(type, encodePID(type, wireId)).wireId === wireId', () => {
33 const testCases = [0, 1, 100, 4740, 2147483647, 4294967295];
34 for (const wireId of testCases) {
35 const encoded = encodePID('BK', wireId);
36 const decoded = decodePID('BK', encoded);
37 expect(decoded.wireId).toBe(wireId);
38 }
39 });
40
41 it('encodePID("BK", 4740) starts with "BK000043E"', () => {
42 const encoded = encodePID('BK', 4740);
43 expect(encoded.startsWith('BK000043E')).toBe(true);
44 expect(encoded.length).toBe(10);
45 });
46
47 it('no encoded PID contains "I" or "O" at position 9 (the check char)', () => {
48 let foundIOrO = false;
49 for (let i = 0; i < 10000; i++) {
50 const encoded = encodePID('BK', i);
51 const check = encoded[9];
52 if (check === 'I' || check === 'O') {
53 foundIOrO = true;
54 }
55 }
56 expect(foundIOrO).toBe(false);
57 });
58});
59