|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { defaultClock, epochClock } from './clock-epoch.js'; |
| 3 | + |
| 4 | +describe('epochClock', () => { |
| 5 | + it('should create epoch clock with defaults', () => { |
| 6 | + const c = epochClock(); |
| 7 | + expect(c.timeOriginMs).toBe(500_000); |
| 8 | + expect(c.tid).toBe(2); |
| 9 | + expect(c.pid).toBe(10_001); |
| 10 | + expect(c.fromEpochMs).toBeFunction(); |
| 11 | + expect(c.fromEpochUs).toBeFunction(); |
| 12 | + expect(c.fromPerfMs).toBeFunction(); |
| 13 | + expect(c.fromEntryStartTimeMs).toBeFunction(); |
| 14 | + expect(c.fromDateNowMs).toBeFunction(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should use pid options', () => { |
| 18 | + expect(epochClock({ pid: 999 })).toStrictEqual( |
| 19 | + expect.objectContaining({ |
| 20 | + pid: 999, |
| 21 | + }), |
| 22 | + ); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should use tid options', () => { |
| 26 | + expect(epochClock({ tid: 888 })).toStrictEqual( |
| 27 | + expect.objectContaining({ |
| 28 | + tid: 888, |
| 29 | + }), |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should support performance clock by default for epochNowUs', () => { |
| 34 | + const c = epochClock(); |
| 35 | + expect(c.timeOriginMs).toBe(500_000); |
| 36 | + expect(c.epochNowUs()).toBe(1_000_000_000); // timeOrigin + (Date.now() - timeOrigin) = Date.now() |
| 37 | + }); |
| 38 | + |
| 39 | + it.each([ |
| 40 | + [1_000_000_000, 1_000_000_000], |
| 41 | + [1_001_000_000, 1_001_000_000], |
| 42 | + [999_000_000, 999_000_000], |
| 43 | + ])('should convert epoch microseconds to microseconds', (us, result) => { |
| 44 | + const c = epochClock(); |
| 45 | + expect(c.fromEpochUs(us)).toBe(result); |
| 46 | + }); |
| 47 | + |
| 48 | + it.each([ |
| 49 | + [1_000_000, 1_000_000_000], |
| 50 | + [1_001_000.5, 1_001_000_500], |
| 51 | + [999_000.4, 999_000_400], |
| 52 | + ])('should convert epoch milliseconds to microseconds', (ms, result) => { |
| 53 | + const c = epochClock(); |
| 54 | + expect(c.fromEpochMs(ms)).toBe(result); |
| 55 | + }); |
| 56 | + |
| 57 | + it.each([ |
| 58 | + [0, 500_000_000], |
| 59 | + [1000, 501_000_000], |
| 60 | + ])( |
| 61 | + 'should convert performance milliseconds to microseconds', |
| 62 | + (perfMs, expected) => { |
| 63 | + expect(epochClock().fromPerfMs(perfMs)).toBe(expected); |
| 64 | + }, |
| 65 | + ); |
| 66 | + |
| 67 | + it('should convert entry start time to microseconds', () => { |
| 68 | + const c = epochClock(); |
| 69 | + expect([ |
| 70 | + c.fromEntryStartTimeMs(0), |
| 71 | + c.fromEntryStartTimeMs(1000), |
| 72 | + ]).toStrictEqual([c.fromPerfMs(0), c.fromPerfMs(1000)]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should convert Date.now() milliseconds to microseconds', () => { |
| 76 | + const c = epochClock(); |
| 77 | + expect([ |
| 78 | + c.fromDateNowMs(1_000_000), |
| 79 | + c.fromDateNowMs(2_000_000), |
| 80 | + ]).toStrictEqual([1_000_000_000, 2_000_000_000]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should maintain conversion consistency', () => { |
| 84 | + const c = epochClock(); |
| 85 | + |
| 86 | + expect({ |
| 87 | + fromEpochUs_2B: c.fromEpochUs(2_000_000_000), |
| 88 | + fromEpochMs_2M: c.fromEpochMs(2_000_000), |
| 89 | + fromEpochUs_1B: c.fromEpochUs(1_000_000_000), |
| 90 | + fromEpochMs_1M: c.fromEpochMs(1_000_000), |
| 91 | + }).toStrictEqual({ |
| 92 | + fromEpochUs_2B: 2_000_000_000, |
| 93 | + fromEpochMs_2M: 2_000_000_000, |
| 94 | + fromEpochUs_1B: 1_000_000_000, |
| 95 | + fromEpochMs_1M: 1_000_000_000, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it.each([ |
| 100 | + [1_000_000_000.1, 1_000_000_000], |
| 101 | + [1_000_000_000.4, 1_000_000_000], |
| 102 | + [1_000_000_000.5, 1_000_000_001], |
| 103 | + [1_000_000_000.9, 1_000_000_001], |
| 104 | + ])('should round microseconds correctly', (value, result) => { |
| 105 | + const c = epochClock(); |
| 106 | + expect(c.fromEpochUs(value)).toBe(result); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe('defaultClock', () => { |
| 111 | + it('should have valid defaultClock export', () => { |
| 112 | + expect({ |
| 113 | + tid: typeof defaultClock.tid, |
| 114 | + timeOriginMs: typeof defaultClock.timeOriginMs, |
| 115 | + }).toStrictEqual({ |
| 116 | + tid: 'number', |
| 117 | + timeOriginMs: 'number', |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments