-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathprocess-id.unit.test.ts
More file actions
173 lines (145 loc) · 4.94 KB
/
process-id.unit.test.ts
File metadata and controls
173 lines (145 loc) · 4.94 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { threadId } from 'node:worker_threads';
import {
type Counter,
ID_PATTERNS,
TIME_ID_BASE,
getUniqueInstanceId,
getUniqueProcessThreadId,
getUniqueTimeId,
sortableReadableDateString,
} from './process-id.js';
describe('TIME_ID_BASE', () => {
it.each([
'20231114-221320-000',
'20240101-120000-000',
'20231231-235959-999',
])('should match valid time ID format: %s', timeId => {
expect(timeId).toMatch(TIME_ID_BASE);
});
it.each(['2023-11-14', '20231114', '20231114-221320', 'invalid'])(
'should not match invalid time ID format: %s',
timeId => {
expect(timeId).not.toMatch(TIME_ID_BASE);
},
);
});
describe('ID_PATTERNS', () => {
it.each(['20231114-221320-000', '20240101-120000-000'])(
'TIME_ID should match valid time ID: %s',
timeId => {
expect(timeId).toMatch(ID_PATTERNS.TIME_ID);
},
);
it.each(['20231114-221320-000.123', '20231114-221320'])(
'TIME_ID should not match invalid format: %s',
timeId => {
expect(timeId).not.toMatch(ID_PATTERNS.TIME_ID);
},
);
it('GROUP_ID should match valid group ID', () => {
const groupId = '20231114-221320-000';
expect(groupId).toMatch(ID_PATTERNS.GROUP_ID);
});
it.each(['20231114-221320-000-12345-1', '20240101-120000-000-99999-99'])(
'PROCESS_THREAD_ID should match valid process/thread ID: %s',
processThreadId => {
expect(processThreadId).toMatch(ID_PATTERNS.PROCESS_THREAD_ID);
},
);
it.each(['20231114-221320-000', '20231114-221320-000-12345'])(
'PROCESS_THREAD_ID should not match invalid format: %s',
processThreadId => {
expect(processThreadId).not.toMatch(ID_PATTERNS.PROCESS_THREAD_ID);
},
);
it.each(['20231114-221320-000.12345.1.1', '20240101-120000-000.99999.99.42'])(
'INSTANCE_ID should match valid instance ID: %s',
instanceId => {
expect(instanceId).toMatch(ID_PATTERNS.INSTANCE_ID);
},
);
it.each(['20231114-221320-000', '20231114-221320-000-12345-1'])(
'INSTANCE_ID should not match invalid format: %s',
instanceId => {
expect(instanceId).not.toMatch(ID_PATTERNS.INSTANCE_ID);
},
);
});
describe('sortableReadableDateString', () => {
it('should format timestamp correctly', () => {
const timestamp = 1_700_000_000_000; // 2023-11-14 22:13:20.000
const result = sortableReadableDateString(timestamp);
expect(result).toBe('20231114-221320-000');
expect(result).toMatch(TIME_ID_BASE);
});
});
describe('getUniqueTimeId', () => {
it('should generate time ID with mocked timeOrigin', () => {
const result = getUniqueTimeId();
expect(result).toMatch(ID_PATTERNS.TIME_ID);
expect(result).toMatch(ID_PATTERNS.GROUP_ID);
expect(result).toBe('20231114-221320-000');
});
it('should generate new ID on each call (not idempotent)', () => {
let callCount = 0;
vi.spyOn(performance, 'now').mockImplementation(() => callCount++);
const id1 = getUniqueTimeId();
const id2 = getUniqueTimeId();
expect(id1).not.toBe(id2);
expect(id1).toMatch(ID_PATTERNS.TIME_ID);
expect(id2).toMatch(ID_PATTERNS.TIME_ID);
});
});
describe('getUniqueProcessThreadId', () => {
it('should generate process/thread ID with correct format', () => {
const result = getUniqueProcessThreadId();
expect(result).toMatch(ID_PATTERNS.PROCESS_THREAD_ID);
expect(result).toContain(`-10001-${threadId}`);
expect(result).toStartWith('20231114-221320-000');
});
it('should generate new ID on each call (not idempotent)', () => {
let callCount = 0;
vi.spyOn(performance, 'now').mockImplementation(() => callCount++);
const id1 = getUniqueProcessThreadId();
const id2 = getUniqueProcessThreadId();
expect(id1).not.toBe(id2);
expect(id1).toMatch(ID_PATTERNS.PROCESS_THREAD_ID);
expect(id2).toMatch(ID_PATTERNS.PROCESS_THREAD_ID);
});
});
describe('getUniqueInstanceId', () => {
it('should generate instance ID with correct format', () => {
let counter = 0;
const counterObj: Counter = {
next: () => ++counter,
};
const result = getUniqueInstanceId(counterObj);
expect(result).toMatch(ID_PATTERNS.INSTANCE_ID);
expect(result).toStartWith('20231114-221320-000.');
expect(result).toContain(`.10001.${threadId}.`);
expect(result).toEndWith('.1');
});
it('should use counter to generate incrementing instance IDs', () => {
let counter = 0;
const counterObj: Counter = {
next: () => ++counter,
};
const results = [
getUniqueInstanceId(counterObj),
getUniqueInstanceId(counterObj),
getUniqueInstanceId(counterObj),
];
expect(results[0]).toEndWith('.1');
expect(results[1]).toEndWith('.2');
expect(results[2]).toEndWith('.3');
});
it('should generate different IDs for different calls', () => {
let counter = 0;
const counterObj: Counter = {
next: () => ++counter,
};
expect(getUniqueInstanceId(counterObj)).not.toBe(
getUniqueInstanceId(counterObj),
);
});
});