|
| 1 | +import { FileHelper } from '../../src/app/helpers/file/files-helper'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import { IFile } from '../../src/app/interfaces/file-helper.interface'; |
| 4 | +import { LogCategory } from '../../src/app/interfaces/reports-handlers.interface'; |
| 5 | +import { isLinux, isMac } from '../../src/common/env'; |
| 6 | + |
| 7 | +describe('files-helper', () => { |
| 8 | + let fileHelper: FileHelper; |
| 9 | + let folderPath = 'C:\\abc'; |
| 10 | + let fsMock; |
| 11 | + let logs = [ |
| 12 | + 'C9Zeus.2529612.log', |
| 13 | + 'C9Zeus.1.log', |
| 14 | + 'C9Trader.25296.log', |
| 15 | + 'C9Trader.25.log', |
| 16 | + 'HEHE.txt', |
| 17 | + 'HAH.txt', |
| 18 | + ]; |
| 19 | + beforeEach(() => { |
| 20 | + fileHelper = new FileHelper(); |
| 21 | + fileHelper.createFiles(); |
| 22 | + fsMock = jest.spyOn(fs, 'readdirSync').mockImplementation(() => logs); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + fsMock.mockRestore(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should clean file names', () => { |
| 30 | + const validatedFileName = fileHelper.validateFilename( |
| 31 | + 'console.log("hello world").txt', |
| 32 | + ); |
| 33 | + |
| 34 | + expect(validatedFileName).toBe('console.log__hello_world__.txt'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should clean file names', () => { |
| 38 | + const validatedPath = fileHelper.validateFilePath( |
| 39 | + 'SELEC*FROM ABC/hello-world/haha', |
| 40 | + ); |
| 41 | + |
| 42 | + expect(validatedPath).toBe(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should clean file path', () => { |
| 46 | + const validatedPath = fileHelper.validateFilePath( |
| 47 | + 'SELEC*FROM ABC/hello-world/haha', |
| 48 | + ); |
| 49 | + |
| 50 | + expect(validatedPath).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return enough logs in folder - old modified', () => { |
| 54 | + const stats = { |
| 55 | + dev: 2114, |
| 56 | + ino: 48064969, |
| 57 | + mode: 33188, |
| 58 | + nlink: 1, |
| 59 | + uid: 85, |
| 60 | + gid: 100, |
| 61 | + rdev: 0, |
| 62 | + size: 527, |
| 63 | + blksize: 4096, |
| 64 | + blocks: 8, |
| 65 | + atimeMs: 1318289051000.1, |
| 66 | + mtimeMs: 1318289051000.1, |
| 67 | + ctimeMs: 1318289051000.1, |
| 68 | + birthtimeMs: 1318289051000.1, |
| 69 | + atime: new Date('Mon, 10 Oct 2011 23:24:11 GMT'), |
| 70 | + mtime: new Date('Mon, 10 Oct 2011 23:24:11 GMT'), |
| 71 | + ctime: new Date('Mon, 10 Oct 2011 23:24:11 GMT'), |
| 72 | + birthtime: new Date('Mon, 10 Oct 2011 23:24:11 GMT'), |
| 73 | + isFile: () => true, |
| 74 | + }; |
| 75 | + const now = new Date(); |
| 76 | + |
| 77 | + jest.spyOn(fs, 'statSync').mockImplementation((path) => { |
| 78 | + switch (path) { |
| 79 | + case `${folderPath}\\${logs[0]}`: { |
| 80 | + const newStats = { ...stats }; |
| 81 | + newStats.mtime = new Date(now.getTime() - 360 * 60 * 1000); |
| 82 | + return newStats; |
| 83 | + } |
| 84 | + case `${folderPath}\\${logs[2]}`: { |
| 85 | + const newStats = { ...stats }; |
| 86 | + newStats.mtime = new Date(now.getTime() - 380 * 60 * 1000); |
| 87 | + return stats; |
| 88 | + } |
| 89 | + default: { |
| 90 | + return stats; |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + const files = fileHelper.getLatestModifiedFiles(folderPath); |
| 96 | + const firstModifiedFile: IFile = { |
| 97 | + fileName: logs[0], |
| 98 | + platform: '', |
| 99 | + modifiedTimestamp: new Date(now.getTime() - 360 * 60 * 1000).getTime(), |
| 100 | + }; |
| 101 | + |
| 102 | + if (isLinux || isMac) { |
| 103 | + expect(files.get(LogCategory.RECENT)).toEqual(undefined); |
| 104 | + } else { |
| 105 | + expect(files.get(LogCategory.RECENT)).toEqual(firstModifiedFile); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + it('should return enough logs in folder - mixed modified but get latest ones', () => { |
| 110 | + const stats = { |
| 111 | + dev: 2114, |
| 112 | + ino: 48064969, |
| 113 | + mode: 33188, |
| 114 | + nlink: 1, |
| 115 | + uid: 85, |
| 116 | + gid: 100, |
| 117 | + rdev: 0, |
| 118 | + size: 527, |
| 119 | + blksize: 4096, |
| 120 | + blocks: 8, |
| 121 | + atimeMs: 1318289051000.1, |
| 122 | + mtimeMs: 1318289051000.1, |
| 123 | + ctimeMs: 1318289051000.1, |
| 124 | + birthtimeMs: 1318289051000.1, |
| 125 | + atime: new Date('Mon, 10 Oct 2011 23:24:11 GMT'), |
| 126 | + mtime: new Date('Mon, 10 Oct 2011 23:24:11 GMT'), |
| 127 | + ctime: new Date('Mon, 10 Oct 2011 23:24:11 GMT'), |
| 128 | + birthtime: new Date('Mon, 10 Oct 2011 23:24:11 GMT'), |
| 129 | + isFile: () => true, |
| 130 | + }; |
| 131 | + const now = new Date(); |
| 132 | + |
| 133 | + jest.spyOn(fs, 'statSync').mockImplementation((path) => { |
| 134 | + switch (path) { |
| 135 | + case `${folderPath}\\${logs[0]}`: { |
| 136 | + const newStats = { ...stats }; |
| 137 | + newStats.mtime = new Date(now.getTime() - 15 * 60 * 1000); |
| 138 | + return newStats; |
| 139 | + } |
| 140 | + case `${folderPath}\\${logs[2]}`: { |
| 141 | + const newStats = { ...stats }; |
| 142 | + newStats.mtime = new Date(now.getTime() - 15 * 60 * 1000); |
| 143 | + return newStats; |
| 144 | + } |
| 145 | + case `${folderPath}\\${logs[3]}`: { |
| 146 | + const newStats = { ...stats }; |
| 147 | + newStats.mtime = new Date(now.getTime() - 380 * 60 * 1000); |
| 148 | + return newStats; |
| 149 | + } |
| 150 | + default: { |
| 151 | + return stats; |
| 152 | + } |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + const files = fileHelper.getLatestModifiedFiles(folderPath); |
| 157 | + const result = new Map<string, IFile>(); |
| 158 | + const firstModifiedFile: IFile = { |
| 159 | + fileName: logs[0], |
| 160 | + platform: '', |
| 161 | + modifiedTimestamp: new Date(now.getTime() - 15 * 60 * 1000).getTime(), |
| 162 | + }; |
| 163 | + const secondModifiedFile: IFile = { |
| 164 | + fileName: logs[2], |
| 165 | + platform: '', |
| 166 | + modifiedTimestamp: new Date(now.getTime() - 15 * 60 * 1000).getTime(), |
| 167 | + }; |
| 168 | + |
| 169 | + result.set(LogCategory.LATEST + '_0', firstModifiedFile); |
| 170 | + result.set(LogCategory.LATEST + '_2', secondModifiedFile); |
| 171 | + |
| 172 | + if (isLinux || isMac) { |
| 173 | + expect(files.get(LogCategory.LATEST + '_0')).toEqual(undefined); |
| 174 | + expect(files.get(LogCategory.LATEST + '_2')).toEqual(undefined); |
| 175 | + } else { |
| 176 | + expect(files.get(LogCategory.LATEST + '_0')).toEqual(firstModifiedFile); |
| 177 | + expect(files.get(LogCategory.LATEST + '_2')).toEqual(secondModifiedFile); |
| 178 | + } |
| 179 | + }); |
| 180 | +}); |
0 commit comments