|
| 1 | +import { threadId } from 'node:worker_threads'; |
| 2 | +import { vi } from 'vitest'; |
| 3 | +import { |
| 4 | + type Counter, |
| 5 | + ID_PATTERNS, |
| 6 | + TIME_ID_BASE, |
| 7 | + getUniqueInstanceId, |
| 8 | + getUniqueProcessThreadId, |
| 9 | + getUniqueTimeId, |
| 10 | + sortableReadableDateString, |
| 11 | +} from './process-id.js'; |
| 12 | + |
| 13 | +describe('TIME_ID_BASE', () => { |
| 14 | + it.each([ |
| 15 | + '20231114-221320-000', |
| 16 | + '20240101-120000-000', |
| 17 | + '20231231-235959-999', |
| 18 | + ])('should match valid time ID format: %s', timeId => { |
| 19 | + expect(timeId).toMatch(TIME_ID_BASE); |
| 20 | + }); |
| 21 | + |
| 22 | + it.each(['2023-11-14', '20231114', '20231114-221320', 'invalid'])( |
| 23 | + 'should not match invalid time ID format: %s', |
| 24 | + timeId => { |
| 25 | + expect(timeId).not.toMatch(TIME_ID_BASE); |
| 26 | + }, |
| 27 | + ); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('ID_PATTERNS', () => { |
| 31 | + it.each(['20231114-221320-000', '20240101-120000-000'])( |
| 32 | + 'TIME_ID should match valid time ID: %s', |
| 33 | + timeId => { |
| 34 | + expect(timeId).toMatch(ID_PATTERNS.TIME_ID); |
| 35 | + }, |
| 36 | + ); |
| 37 | + |
| 38 | + it.each(['20231114-221320-000.123', '20231114-221320'])( |
| 39 | + 'TIME_ID should not match invalid format: %s', |
| 40 | + timeId => { |
| 41 | + expect(timeId).not.toMatch(ID_PATTERNS.TIME_ID); |
| 42 | + }, |
| 43 | + ); |
| 44 | + |
| 45 | + it.each(['20231114-221320-000'])( |
| 46 | + 'GROUP_ID should match valid group ID: %s', |
| 47 | + groupId => { |
| 48 | + expect(groupId).toMatch(ID_PATTERNS.GROUP_ID); |
| 49 | + }, |
| 50 | + ); |
| 51 | + |
| 52 | + it.each(['20231114-221320-000-12345-1', '20240101-120000-000-99999-99'])( |
| 53 | + 'PROCESS_THREAD_ID should match valid process/thread ID: %s', |
| 54 | + processThreadId => { |
| 55 | + expect(processThreadId).toMatch(ID_PATTERNS.PROCESS_THREAD_ID); |
| 56 | + }, |
| 57 | + ); |
| 58 | + |
| 59 | + it.each(['20231114-221320-000', '20231114-221320-000-12345'])( |
| 60 | + 'PROCESS_THREAD_ID should not match invalid format: %s', |
| 61 | + processThreadId => { |
| 62 | + expect(processThreadId).not.toMatch(ID_PATTERNS.PROCESS_THREAD_ID); |
| 63 | + }, |
| 64 | + ); |
| 65 | + |
| 66 | + it.each(['20231114-221320-000.12345.1.1', '20240101-120000-000.99999.99.42'])( |
| 67 | + 'INSTANCE_ID should match valid instance ID: %s', |
| 68 | + instanceId => { |
| 69 | + expect(instanceId).toMatch(ID_PATTERNS.INSTANCE_ID); |
| 70 | + }, |
| 71 | + ); |
| 72 | + |
| 73 | + it.each(['20231114-221320-000', '20231114-221320-000-12345-1'])( |
| 74 | + 'INSTANCE_ID should not match invalid format: %s', |
| 75 | + instanceId => { |
| 76 | + expect(instanceId).not.toMatch(ID_PATTERNS.INSTANCE_ID); |
| 77 | + }, |
| 78 | + ); |
| 79 | + |
| 80 | + it.each(['20231114-221320-000.12345.1.1'])( |
| 81 | + 'SHARD_ID should match valid shard ID (deprecated alias): %s', |
| 82 | + shardId => { |
| 83 | + expect(shardId).toMatch(ID_PATTERNS.SHARD_ID); |
| 84 | + }, |
| 85 | + ); |
| 86 | + |
| 87 | + it.each(['20231114-221320-000'])( |
| 88 | + 'READABLE_DATE should match valid readable date (deprecated alias): %s', |
| 89 | + readableDate => { |
| 90 | + expect(readableDate).toMatch(ID_PATTERNS.READABLE_DATE); |
| 91 | + }, |
| 92 | + ); |
| 93 | +}); |
| 94 | + |
| 95 | +describe('sortableReadableDateString', () => { |
| 96 | + it('should format timestamp correctly', () => { |
| 97 | + const timestamp = 1_700_000_000_000; // 2023-11-14 22:13:20.000 |
| 98 | + const result = sortableReadableDateString(timestamp); |
| 99 | + expect(result).toBe('20231114-221320-000'); |
| 100 | + expect(result).toMatch(TIME_ID_BASE); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('getUniqueTimeId', () => { |
| 105 | + it('should generate time ID with mocked timeOrigin', () => { |
| 106 | + const result = getUniqueTimeId(); |
| 107 | + |
| 108 | + expect(result).toMatch(ID_PATTERNS.TIME_ID); |
| 109 | + expect(result).toMatch(ID_PATTERNS.GROUP_ID); |
| 110 | + expect(result).toBe('20231114-221320-000'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should generate new ID on each call (not idempotent)', () => { |
| 114 | + let callCount = 0; |
| 115 | + vi.spyOn(performance, 'now').mockImplementation(() => callCount++); |
| 116 | + |
| 117 | + const id1 = getUniqueTimeId(); |
| 118 | + const id2 = getUniqueTimeId(); |
| 119 | + |
| 120 | + expect(id1).not.toBe(id2); |
| 121 | + expect(id1).toMatch(ID_PATTERNS.TIME_ID); |
| 122 | + expect(id2).toMatch(ID_PATTERNS.TIME_ID); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('getUniqueProcessThreadId', () => { |
| 127 | + it('should generate process/thread ID with correct format', () => { |
| 128 | + const result = getUniqueProcessThreadId(); |
| 129 | + |
| 130 | + expect(result).toMatch(ID_PATTERNS.PROCESS_THREAD_ID); |
| 131 | + expect(result).toContain(`-10001-${threadId}`); |
| 132 | + expect(result).toStartWith('20231114-221320-000'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should generate new ID on each call (not idempotent)', () => { |
| 136 | + let callCount = 0; |
| 137 | + vi.spyOn(performance, 'now').mockImplementation(() => callCount++); |
| 138 | + |
| 139 | + const id1 = getUniqueProcessThreadId(); |
| 140 | + const id2 = getUniqueProcessThreadId(); |
| 141 | + |
| 142 | + expect(id1).not.toBe(id2); |
| 143 | + expect(id1).toMatch(ID_PATTERNS.PROCESS_THREAD_ID); |
| 144 | + expect(id2).toMatch(ID_PATTERNS.PROCESS_THREAD_ID); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe('getUniqueInstanceId', () => { |
| 149 | + it('should generate instance ID with correct format', () => { |
| 150 | + let counter = 0; |
| 151 | + const counterObj: Counter = { |
| 152 | + next: () => ++counter, |
| 153 | + }; |
| 154 | + |
| 155 | + const result = getUniqueInstanceId(counterObj); |
| 156 | + |
| 157 | + expect(result).toMatch(ID_PATTERNS.INSTANCE_ID); |
| 158 | + expect(result).toStartWith('20231114-221320-000.'); |
| 159 | + expect(result).toContain(`.10001.${threadId}.`); |
| 160 | + expect(result).toEndWith('.1'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should use counter to generate incrementing instance IDs', () => { |
| 164 | + let counter = 0; |
| 165 | + const counterObj: Counter = { |
| 166 | + next: () => ++counter, |
| 167 | + }; |
| 168 | + |
| 169 | + const results = [ |
| 170 | + getUniqueInstanceId(counterObj), |
| 171 | + getUniqueInstanceId(counterObj), |
| 172 | + getUniqueInstanceId(counterObj), |
| 173 | + ]; |
| 174 | + |
| 175 | + const counters = results.map(r => |
| 176 | + Number.parseInt(r.split('.').at(-1)!, 10), |
| 177 | + ); |
| 178 | + expect(counters).toEqual([1, 2, 3]); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should generate different IDs for different calls', () => { |
| 182 | + let counter = 0; |
| 183 | + const counterObj: Counter = { |
| 184 | + next: () => ++counter, |
| 185 | + }; |
| 186 | + |
| 187 | + expect(getUniqueInstanceId(counterObj)).not.toBe( |
| 188 | + getUniqueInstanceId(counterObj), |
| 189 | + ); |
| 190 | + }); |
| 191 | +}); |
0 commit comments