|
| 1 | +import path from 'path'; |
| 2 | + |
| 3 | +const logsPath = path.join(require('os').homedir(), 'sg_stats', 'logs'); |
| 4 | + |
| 5 | +const importLogsFolderPath = async ( |
| 6 | + workerData: unknown, |
| 7 | +): Promise<string> => { |
| 8 | + let logsFolderPathValue: string = ''; |
| 9 | + |
| 10 | + jest.isolateModules(() => { |
| 11 | + jest.doMock('worker_threads', () => ({ workerData })); |
| 12 | + |
| 13 | + // eslint-disable-next-line global-require |
| 14 | + const { logsFolderPath } = require('../../../0 - utils/logger') as { logsFolderPath: string }; |
| 15 | + |
| 16 | + logsFolderPathValue = logsFolderPath; |
| 17 | + }); |
| 18 | + |
| 19 | + return logsFolderPathValue; |
| 20 | +}; |
| 21 | + |
| 22 | +describe('logger logsFolderPath in worker context', () => { |
| 23 | + test('should use logsFolderPath from workerData when valid', async () => { |
| 24 | + const expectedPath = '/home/user/sg_stats/logs/17.02.2026 13:00'; |
| 25 | + |
| 26 | + const result = await importLogsFolderPath({ logsFolderPath: expectedPath }); |
| 27 | + |
| 28 | + expect(result).toBe(expectedPath); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should not contain current timestamp when workerData has logsFolderPath', async () => { |
| 32 | + const mainThreadPath = '/home/user/sg_stats/logs/01.01.2020 00:00'; |
| 33 | + |
| 34 | + const result = await importLogsFolderPath({ logsFolderPath: mainThreadPath }); |
| 35 | + |
| 36 | + expect(result).toBe(mainThreadPath); |
| 37 | + expect(result).not.toContain(new Date().getFullYear().toString()); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should generate new path when workerData is null', async () => { |
| 41 | + const result = await importLogsFolderPath(null); |
| 42 | + |
| 43 | + expect(result).not.toBe(''); |
| 44 | + expect(result.startsWith(logsPath)).toBe(true); |
| 45 | + }); |
| 46 | + |
| 47 | + test('should generate new path when workerData is undefined', async () => { |
| 48 | + const result = await importLogsFolderPath(undefined); |
| 49 | + |
| 50 | + expect(result).not.toBe(''); |
| 51 | + expect(result.startsWith(logsPath)).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + test('should generate new path when workerData has invalid shape', async () => { |
| 55 | + const result = await importLogsFolderPath({ wrongField: 123 }); |
| 56 | + |
| 57 | + expect(result).not.toBe(''); |
| 58 | + expect(result.startsWith(logsPath)).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + test('should generate new path when workerData has non-string logsFolderPath', async () => { |
| 62 | + const result = await importLogsFolderPath({ logsFolderPath: 42 }); |
| 63 | + |
| 64 | + expect(result).not.toBe(''); |
| 65 | + expect(result.startsWith(logsPath)).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + test('should use exact workerData path without appending timestamp', async () => { |
| 69 | + const exactPath = '/custom/logs/fixed-folder'; |
| 70 | + |
| 71 | + const result = await importLogsFolderPath({ logsFolderPath: exactPath }); |
| 72 | + |
| 73 | + expect(result).toBe(exactPath); |
| 74 | + }); |
| 75 | +}); |
0 commit comments