-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathidentifier.spec.ts
More file actions
45 lines (39 loc) · 1.51 KB
/
identifier.spec.ts
File metadata and controls
45 lines (39 loc) · 1.51 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
import { createSpanIdentifier, createTraceIdentifier, toPaddedHexadecimalString } from './identifier'
describe('identifier', () => {
describe('TraceIdentifier', () => {
it('generates a random id', () => {
const identifier = createTraceIdentifier()
expect(identifier.toString()).toMatch(/^\d+$/)
})
it('formats using base 16', () => {
mockRandomValues((buffer) => (buffer[0] = 0xff))
const identifier = createTraceIdentifier()
expect(identifier.toString(16)).toEqual('ff')
})
it('should generate a max value of 64 bits', () => {
mockRandomValues((buffer) => buffer.fill(0xff))
const identifier = createTraceIdentifier()
expect(identifier.toString(16)).toEqual('ffffffffffffffff')
})
})
describe('SpanIdentifier', () => {
it('generates a max value of 63 bits', () => {
mockRandomValues((buffer) => buffer.fill(0xff))
const identifier = createSpanIdentifier()
expect(identifier.toString(16)).toEqual('7fffffffffffffff')
})
})
})
describe('toPaddedHexadecimalString', () => {
it('should pad the string to 16 characters', () => {
mockRandomValues((buffer) => (buffer[0] = 0x01))
const identifier = createTraceIdentifier()
expect(toPaddedHexadecimalString(identifier)).toEqual('0000000000000001')
})
})
function mockRandomValues(cb: (buffer: Uint8Array) => void) {
spyOn(window.crypto, 'getRandomValues').and.callFake((bufferView: ArrayBufferView) => {
cb(new Uint8Array(bufferView.buffer))
return bufferView
})
}