mirror of
https://github.com/codedread/bitjs
synced 2025-10-04 18:19:15 +02:00
Fix compress after previous refactor and add a unit test.
This commit is contained in:
parent
65db5bdbd2
commit
8e1a7f3d0f
5 changed files with 81 additions and 10 deletions
59
tests/compress.spec.js
Normal file
59
tests/compress.spec.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
import * as fs from 'node:fs';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { Unarchiver, getUnarchiver } from '../archive/decompress.js';
|
||||
import { CompressStatus, Zipper, ZipCompressionMethod } from '../archive/compress.js';
|
||||
|
||||
/**
|
||||
* @typedef {import('./archive/compress.js').FileInfo} FileInfo
|
||||
*/
|
||||
|
||||
const PATH = `tests/archive-testfiles/`;
|
||||
|
||||
const INPUT_FILENAMES = [
|
||||
'sample-1.txt',
|
||||
'sample-2.csv',
|
||||
'sample-3.json',
|
||||
];
|
||||
|
||||
describe('bitjs.archive.compress', () => {
|
||||
/** @type {Map<string, FileInfo>} */
|
||||
let inputFileInfos = new Map();
|
||||
let decompressedFileSize = 0;
|
||||
|
||||
before(() => {
|
||||
for (const fileName of INPUT_FILENAMES) {
|
||||
const fullFilename = `${PATH}${fileName}`;
|
||||
const fd = fs.openSync(fullFilename, 'r');
|
||||
const lastModTime = fs.fstatSync(fd).mtimeMs;
|
||||
const nodeBuf = fs.readFileSync(fullFilename);
|
||||
const fileData = new Uint8Array(
|
||||
nodeBuf.buffer.slice(nodeBuf.byteOffset, nodeBuf.byteOffset + nodeBuf.length));
|
||||
inputFileInfos.set(fileName, {fileName, lastModTime, fileData});
|
||||
decompressedFileSize += fileData.byteLength;
|
||||
fs.closeSync(fd);
|
||||
}
|
||||
});
|
||||
|
||||
it('zipper works', (done) => {
|
||||
const files = new Map(inputFileInfos);
|
||||
const zipper = new Zipper({zipCompressionMethod: ZipCompressionMethod.STORE});
|
||||
zipper.start(Array.from(files.values()), true).then(byteArray => {
|
||||
expect(zipper.compressState).equals(CompressStatus.COMPLETE);
|
||||
expect(byteArray.byteLength > decompressedFileSize).equals(true);
|
||||
|
||||
const unarchiver = getUnarchiver(byteArray.buffer);
|
||||
unarchiver.addEventListener('extract', evt => {
|
||||
const {filename, fileData} = evt.unarchivedFile;
|
||||
expect(files.has(filename)).equals(true);
|
||||
const inputFile = files.get(filename).fileData;
|
||||
expect(inputFile.byteLength).equals(fileData.byteLength);
|
||||
for (let b = 0; b < inputFile.byteLength; ++b) {
|
||||
expect(inputFile[b]).equals(fileData[b]);
|
||||
}
|
||||
});
|
||||
unarchiver.addEventListener('finish', evt => done());
|
||||
unarchiver.start();
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue