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