📄 src/models/test/dates.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 { DateUtils, inceptionDate } from '../dates.ts';
17
18describe('DateUtils', () => {
19    it('calculates the correct diff for now', () => {
20        const nowNumber = DateUtils.now();
21        expect(nowNumber).toBeGreaterThan(0);
22        expect(typeof nowNumber).toBe('number');
23    });
24
25    it('serializes a date to ArrayBuffer', () => {
26        const buf = DateUtils.serialize(12345);
27        expect(buf.byteLength).toBe(4);
28        const dv = new DataView(buf);
29        expect(dv.getUint32(0)).toBe(12345);
30    });
31
32    it('converts Date to number', () => {
33        const d = new Date(1000000000);
34        expect(DateUtils.toNumber(d)).toBe(1000000);
35    });
36
37    it('converts number to Date', () => {
38        const d = DateUtils.fromNumber(1000000);
39        expect(d.getTime()).toBe(1000000000);
40    });
41
42    it('calculates now bucket correctly', () => {
43        const bucket = DateUtils.findNowBucket();
44        expect(bucket).toBeGreaterThanOrEqual(0);
45    });
46
47    it('calculates date bucket correctly', () => {
48        // Zero date is May 2025 -> 2025-05-01
49        const futureDate = new Date(2025, 6, 15); // July 2025
50        expect(DateUtils.findDateBucket(futureDate)).toBe(2);
51
52        const pastDate = new Date(2024, 0, 1);
53        expect(() => DateUtils.findDateBucket(pastDate)).toThrow(/is before zeroDate/);
54    });
55});
56