|
1 | 1 | import { jest } from '@jest/globals'; |
2 | 2 |
|
3 | 3 | const writeFileSyncMock = jest.fn(); |
| 4 | +const renameFileSyncMock = jest.fn(); |
| 5 | +const existsSyncMock = jest.fn(); |
4 | 6 | jest.unstable_mockModule('fs', () => { |
5 | 7 | return { |
6 | 8 | writeFileSync: writeFileSyncMock, |
| 9 | + existsSync: existsSyncMock, |
| 10 | + renameSync: renameFileSyncMock, |
7 | 11 | default: jest.fn(), |
8 | 12 | }; |
9 | 13 | }); |
@@ -100,22 +104,22 @@ describe('LoggerService', () => { |
100 | 104 | const path = logger.getSuggestLogfilePath(); |
101 | 105 | expect(path.includes('/tmpDir')).toBeTruthy(); |
102 | 106 | }); |
| 107 | + }); |
103 | 108 |
|
104 | | - it('should return uniq paths', () => { |
105 | | - const changeDate = (date: string) => |
106 | | - jest.useFakeTimers().setSystemTime(new Date(date)); |
107 | | - changeDate('2026-01-01 10:20:16:36'); |
108 | | - |
109 | | - const path1 = logger.getSuggestLogfilePath(); |
110 | | - changeDate('2026-01-01 10:20:16:37'); |
111 | | - |
112 | | - const path2 = logger.getSuggestLogfilePath(); |
113 | | - changeDate('2026-01-01 10:20:16:38'); |
114 | | - |
115 | | - const path3 = logger.getSuggestLogfilePath(); |
| 109 | + describe('LogFile rotation', () => { |
| 110 | + it('should not rotate file if not exist', () => { |
| 111 | + existsSyncMock.mockReturnValue(false); |
| 112 | + const path = logger.getSuggestLogfilePath(); |
| 113 | + logger.saveToFile(path); |
| 114 | + expect(renameFileSyncMock).not.toBeCalled(); |
| 115 | + }); |
116 | 116 |
|
117 | | - const isSamePath = path1 === path2 || path1 === path3 || path2 === path3; |
118 | | - expect(isSamePath).not.toBeTruthy(); |
| 117 | + it('should rotate file if exist', () => { |
| 118 | + existsSyncMock.mockReturnValue(true); |
| 119 | + const path = logger.getSuggestLogfilePath(); |
| 120 | + logger.saveToFile(path); |
| 121 | + const expectedOldPath = path.replace('latest', 'old'); |
| 122 | + expect(renameFileSyncMock).toBeCalledWith(path, expectedOldPath); |
119 | 123 | }); |
120 | 124 | }); |
121 | 125 |
|
|
0 commit comments