|
| 1 | +import process from 'node:process'; |
| 2 | +import { threadId } from 'node:worker_threads'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { defaultClock, epochClock } from './clock-epoch.js'; |
| 5 | + |
| 6 | +describe('epochClock', () => { |
| 7 | + it('should create epoch clock with defaults', () => { |
| 8 | + const c = epochClock(); |
| 9 | + expect(c).toStrictEqual( |
| 10 | + expect.objectContaining({ |
| 11 | + tid: threadId, |
| 12 | + pid: process.pid, |
| 13 | + timeOriginMs: performance.timeOrigin, |
| 14 | + }), |
| 15 | + ); |
| 16 | + expect(typeof c.fromEpochMs).toBe('function'); |
| 17 | + expect(typeof c.fromEpochUs).toBe('function'); |
| 18 | + expect(typeof c.fromPerfMs).toBe('function'); |
| 19 | + expect(typeof c.fromEntryStartTimeMs).toBe('function'); |
| 20 | + expect(typeof c.fromDateNowMs).toBe('function'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should support performance clock by default for epochNowUs', () => { |
| 24 | + const c = epochClock(); |
| 25 | + expect(c.timeOriginMs).toBe(performance.timeOrigin); |
| 26 | + const nowUs = c.epochNowUs(); |
| 27 | + expect(nowUs).toBe(Math.round(nowUs)); |
| 28 | + const expectedUs = Date.now() * 1000; |
| 29 | + |
| 30 | + expect(nowUs).toBeGreaterThan(expectedUs - 1000); |
| 31 | + expect(nowUs).toBeLessThan(expectedUs + 1000); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should convert epoch milliseconds to microseconds correctly', () => { |
| 35 | + const c = epochClock(); |
| 36 | + const epochMs = Date.now(); |
| 37 | + |
| 38 | + const result = c.fromEpochMs(epochMs); |
| 39 | + expect(result).toBe(Math.round(epochMs * 1000)); |
| 40 | + expect(result).toBe(Math.round(result)); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should convert epoch microseconds to microseconds correctly', () => { |
| 44 | + const c = epochClock(); |
| 45 | + const epochUs = Date.now() * 1000; |
| 46 | + const expectedUs = Math.round(epochUs); |
| 47 | + |
| 48 | + const result = c.fromEpochUs(epochUs); |
| 49 | + expect(result).toBe(expectedUs); |
| 50 | + expect(result).toBe(Math.round(result)); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should convert performance milliseconds to epoch microseconds correctly', () => { |
| 54 | + const c = epochClock(); |
| 55 | + const perfMs = performance.now(); |
| 56 | + const expectedUs = Math.round((c.timeOriginMs + perfMs) * 1000); |
| 57 | + |
| 58 | + const result = c.fromPerfMs(perfMs); |
| 59 | + expect(result).toBe(expectedUs); |
| 60 | + expect(result).toBe(Math.round(result)); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should convert entry start time milliseconds to epoch microseconds correctly', () => { |
| 64 | + const c = epochClock(); |
| 65 | + const entryStartMs = performance.mark('fromPerfMs').startTime; |
| 66 | + const expectedUs = Math.round((c.timeOriginMs + entryStartMs) * 1000); |
| 67 | + |
| 68 | + const result = c.fromEntryStartTimeMs(entryStartMs); |
| 69 | + expect(result).toBe(expectedUs); |
| 70 | + expect(result).toBe(Math.round(result)); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should convert Date.now milliseconds to epoch microseconds correctly', () => { |
| 74 | + const c = epochClock(); |
| 75 | + const dateNowMs = Date.now(); |
| 76 | + |
| 77 | + const result = c.fromDateNowMs(dateNowMs); |
| 78 | + expect(result).toBe(Math.round(dateNowMs * 1000)); |
| 79 | + expect(result).toBe(Math.round(result)); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('defaultClock', () => { |
| 84 | + it('should have valid defaultClock export', () => { |
| 85 | + const c = defaultClock; |
| 86 | + expect(c).toStrictEqual( |
| 87 | + expect.objectContaining({ |
| 88 | + tid: threadId, |
| 89 | + pid: process.pid, |
| 90 | + timeOriginMs: performance.timeOrigin, |
| 91 | + }), |
| 92 | + ); |
| 93 | + |
| 94 | + expect(typeof c.fromEpochMs).toBe('function'); |
| 95 | + expect(typeof c.fromEpochUs).toBe('function'); |
| 96 | + expect(typeof c.fromPerfMs).toBe('function'); |
| 97 | + expect(typeof c.fromEntryStartTimeMs).toBe('function'); |
| 98 | + expect(typeof c.fromDateNowMs).toBe('function'); |
| 99 | + }); |
| 100 | +}); |
0 commit comments