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