-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex-test.js
138 lines (114 loc) · 3.69 KB
/
index-test.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import assert from 'node:assert/strict'
import test from 'node:test'
import HeaderTimers from './index.js'
test('header-timers', async (t) => {
const { key, start, stop, reset, timers, count, values, value, object, string } = HeaderTimers()
await t.test('baseline without timers', () => {
assert.equal(typeof key, 'string')
assert.equal(count(), 0)
assert.equal(timers().length, 0)
assert.equal(values().length, 0)
assert.equal(value(), '')
assert.deepEqual(object(), { [key]: '' })
assert.equal(string(), `${key}: `)
})
await t.test('timer with name and description', () => {
let one
one = start('one', 'description')
assert.equal(typeof one, 'bigint')
assert.equal(count(), 1)
const list = timers()
assert.equal(list[0].name, 'one')
assert.equal(list[0].description, 'description')
one = stop('one')
assert.equal(typeof one, 'number')
const vals = values()
assert(vals[0].startsWith('one;desc="description";dur='))
assert.equal(value(), vals[0])
assert.deepEqual(object(), { [key]: vals[0] })
assert.equal(string(), `${key}: ${vals[0]}`)
start('two')
assert.equal(count(), 2)
stop('two')
assert.equal(count(), 2)
assert.ok(value().indexOf(' ') === -1)
})
await t.test('timer reset', () => {
reset()
assert.equal(count(), 0)
assert.equal(timers().length, 0)
assert.equal(values().length, 0)
})
await t.test('timer without description', () => {
start('three')
const list = timers()
assert.equal(count(), 1)
assert.equal(list[0].name, 'three')
assert.equal(list[0].description, undefined)
stop('three')
const vals = values()
assert(vals[0].startsWith('three;dur='))
})
await t.test('timer with no args', () => {
reset()
start()
const list = timers()
assert.equal(count(), 1)
assert.equal(list[0].name, 'n1')
assert.equal(list[0].description, undefined)
stop()
const vals = values()
assert(vals[0].startsWith('n1;dur='))
})
})
test('header-timers with config', async (t) => {
await t.test('prefix and key', () => {
const { key, start, timers } = HeaderTimers({
prefix: '$',
key: 'x-timer',
})
assert.equal(key, 'x-timer')
start()
const list = timers()
assert.equal(list[0].name, '$1')
})
await t.test('precision', async () => {
let precision = 1
const timers1 = HeaderTimers({ precision })
timers1.start()
await new Promise((resolve) => setTimeout(resolve, 100))
timers1.stop()
const dur1 = timers1.values()[0].split(';')[1].split('=')[1]
assert.equal(typeof dur1, 'string')
assert.equal(dur1, '100')
precision = 100
const timers100 = HeaderTimers({ precision })
timers100.start()
await new Promise((resolve) => setTimeout(resolve, 100))
const ms = timers100.stop()
const dur100 = timers100.values()[0].split(';')[1].split('=')[1]
assert.equal(typeof ms, 'number')
assert.equal(dur100, ms?.toString())
})
await t.test('disabled timers', () => {
const { key, start, stop, timers, count, values, value, object, string, reset } = HeaderTimers({
enabled: false,
})
assert.equal(typeof key, 'string')
assert.equal(typeof start, 'function')
assert.equal(typeof stop, 'function')
assert.equal(start(), 0n)
assert.equal(stop(), 0)
assert.equal(start('one'), 0n)
assert.equal(stop('one'), 0)
assert.equal(start('two'), 0n)
assert.equal(stop('two'), 0)
assert.equal(count(), 0)
assert.equal(timers().length, 0)
assert.equal(values().length, 0)
assert.equal(value(), '')
assert.deepEqual(object(), {})
assert.equal(string(), '')
assert.equal(reset(), null)
})
})