import { expect } from 'chai'; import { rm } from 'fs/promises'; import { createFile, getFileStats, getSampledData } from '../../src/utils/file'; import { getTestPath, randomString } from './utils'; describe('utils/file', () => { let inputFilePath: string; let fileName: string; beforeEach((done) => { const content = []; fileName = `${randomString()}.log`; inputFilePath = getTestPath(fileName); for (let index = 1; index <= 100; index += 1) { content.push(`测试数据 ${index}`); } content.push(''); createFile(inputFilePath, content).then(() => { done(); }); }); afterEach(async () => { await rm(inputFilePath); }); it('获取文件信息', async () => { const fileInfo = await getFileStats(inputFilePath); expect(fileInfo).to.deep.includes({ name: fileName, fullPath: inputFilePath, size: '1.55 KB', rowCount: 100, }); }); it('获取文件数据采样(首位中)', async () => { const sampledData = await getSampledData(inputFilePath, 3); expect(sampledData).to.deep.equal({ head: [ '测试数据 1', '测试数据 2', '测试数据 3', ], middle: [ '测试数据 49', '测试数据 50', '测试数据 51', ], tail: [ '测试数据 98', '测试数据 99', '测试数据 100', ], }); }); });