import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import stream, { PassThrough } from 'node:stream'; import { jest } from '@jest/globals'; import Logger from '../../../src/console/logger.js'; import { LogLevel } from '../../../src/console/logLevel.js'; import Defaults from '../../../src/globals/defaults.js'; import Temp from '../../../src/globals/temp.js'; import Igir from '../../../src/igir.js'; import FsPoly, { WalkMode } from '../../../src/polyfill/fsPoly.js'; import IOFile from '../../../src/polyfill/ioFile.js'; import FileChecksums, { ChecksumBitmask } from '../../../src/types/files/fileChecksums.js'; import Options, { ZipFormat, ZipFormatInverted } from '../../../src/types/options.js'; import { ZipReader } from '../../zip/index.js'; import type { ValidationResultValue } from '../src/tzValidator.js'; import TZValidator, { ValidationResult } from '../src/tzValidator.js'; import type { CompressionMethodValue } from '../src/tzWriter.js'; import TZWriter, { CompressionMethod } from '../src/tzWriter.js'; jest.setTimeout(5 * 60 * 1000); // 5min for large files const VALIDATION_MAP: Record = { [CompressionMethod.DEFLATE]: ValidationResult.VALID_TORRENTZIP, [CompressionMethod.ZSTD]: ValidationResult.VALID_RVZSTD, } as const; if (!(await FsPoly.exists(Temp.getTempDir()))) { await FsPoly.mkdir(Temp.getTempDir(), { recursive: true }); } test.each([ [ ZipFormat.TORRENTZIP, [ // Generated by ROMVault v3.7.2 [path.join('One', 'Empty.zip'), '45ccd490'], [path.join('One', 'Fizzbuzz.zip'), '9dfd1d34'], [path.join('One', 'Foobar.zip'), '96d4b5fe'], [path.join('One', 'One Three.zip'), 'ab9a27f2'], [path.join('One', 'Optical Game (Disc 1).zip'), '7c60248b'], [path.join('One', 'Optical Game (Disc 2).zip'), '0a0641d3'], [path.join('One', 'Three Four Five.zip'), '1f83f597'], [path.join('One', 'UMD.zip'), '445d49b1'], ], ], [ ZipFormat.RVZSTD, [ // Generated by ROMVault v3.7.2 [path.join('One', 'Empty.zip'), '6a263dbe'], [path.join('One', 'Fizzbuzz.zip'), 'b519b6f4'], [path.join('One', 'Foobar.zip'), 'cedf7620'], [path.join('One', 'One Three.zip'), 'b371c51a'], [path.join('One', 'Optical Game (Disc 1).zip'), '4dd737d2'], [path.join('One', 'Optical Game (Disc 2).zip'), 'e5d41942'], [path.join('One', 'Three Four Five.zip'), '2777788f'], [path.join('One', 'UMD.zip'), '74b3dd25'], ], ], ])('should write correct zip files: %s', async (zipFormat, expectedChecksums) => { const tempDir = await FsPoly.mkdtemp(Temp.getTempDir()); try { await new Igir( new Options({ commands: ['copy', 'zip'], dat: [path.join('test', 'fixtures', 'dats', 'one.*')], input: [path.join('test', 'fixtures', 'roms')], inputExclude: [path.join('test', 'fixtures', 'roms', '{gcz,rvz,wia}', '**')], output: tempDir, zipFormat: ZipFormatInverted[zipFormat].toLowerCase(), filterRegexExclude: 'Lorem Ipsum', excludeDisks: true, dirDatName: true, disableCache: true, }), new Logger(LogLevel.NEVER, new PassThrough()), ).main(); const writtenFiles = (await FsPoly.walk(tempDir, WalkMode.FILES)).sort(); const writtenFilesHashed = await Promise.all( writtenFiles.map(async (filePath) => { const checksums = await FileChecksums.hashFile(filePath, ChecksumBitmask.CRC32); return [filePath.replace(tempDir + path.sep, ''), checksums.crc32 as string]; }), ); expect(writtenFilesHashed).toEqual(expectedChecksums); } finally { await FsPoly.rm(tempDir, { recursive: true, force: true, }); } }); const assertSingleFileZip = async ( fileName: string, fileSize: number, expectedRawMd5: string, compressionMethod: CompressionMethodValue, expectedZipMd5: string, ): Promise => { const tempFilePath = await FsPoly.mktemp(path.join(Temp.getTempDir(), fileName)); const tempZipPath = await FsPoly.mktemp(path.join(Temp.getTempDir(), `${fileName}.zip`)); try { // Create a file and fill it with zeroes const tempFileDir = path.dirname(tempFilePath); if (!(await FsPoly.exists(tempFileDir))) { await FsPoly.mkdir(tempFileDir, { recursive: true }); } const tempFile = await IOFile.fileOfSize(tempFilePath, 'w', fileSize); const emptyBuffer = Buffer.alloc(Defaults.FILE_READING_CHUNK_SIZE); let position = 0; while (position < fileSize) { await tempFile.writeAt( emptyBuffer.subarray(0, Math.min(emptyBuffer.length, fileSize - position)), position, ); position += emptyBuffer.length; } await tempFile.close(); // Sanity check the temp file await expect(FsPoly.size(tempFilePath)).resolves.toEqual(fileSize); const tempFileMd5 = (await FileChecksums.hashFile(tempFilePath, ChecksumBitmask.MD5)).md5; expect(tempFileMd5).toEqual(expectedRawMd5); // Write the TorrentZip file const tempZip = await TZWriter.open(tempZipPath, compressionMethod); await tempZip.addStream( fs.createReadStream(tempFilePath, { highWaterMark: 33_554_432 * 2 }), fileName, fileSize, os.cpus().length, ); await tempZip.finalize(); // Validate await expect(TZValidator.validate(new ZipReader(tempZipPath))).resolves.toEqual( VALIDATION_MAP[compressionMethod], ); expect((await FileChecksums.hashFile(tempZipPath, ChecksumBitmask.MD5)).md5).toEqual( expectedZipMd5, ); } finally { await FsPoly.rm(tempZipPath, { force: true }); await FsPoly.rm(tempFilePath, { force: true }); } }; // Generated by ROMVault v3.7.2 test.each([ // CP437 ['café', CompressionMethod.DEFLATE, '3afbc02191a7ee92df2a4c757b92668a'], ['café', CompressionMethod.ZSTD, '1649a04339bddad9253f9252c359baf5'], ['☺☻', CompressionMethod.DEFLATE, '41111c300b550951086d4a4267fdb22a'], ['☺☻', CompressionMethod.ZSTD, '01531ba43ff6e297a4a1079fa45dcbdd'], ['♦♣♠♥', CompressionMethod.DEFLATE, 'd3dc1e0c7714480fa26ccf924352c27c'], ['♦♣♠♥', CompressionMethod.ZSTD, '934daefefb8539fbfaad5c2333c34b67'], // UTF-8 ['你好', CompressionMethod.DEFLATE, '69c166dbd72a3689aee86bf22c37b384'], ['你好', CompressionMethod.ZSTD, 'f1c95bb31d0e4b1ea0ff52f137e2ad9a'], ['🌸✨🦊', CompressionMethod.DEFLATE, '525584140748b56ef0e04891bd8baaea'], ['🌸✨🦊', CompressionMethod.ZSTD, 'b32c63001a3b7644a337e0fd4b4c2965'], ['みんな', CompressionMethod.DEFLATE, '2d600a970ff4ba5a023e184e6e7149e7'], ['みんな', CompressionMethod.ZSTD, '10db563ff949a9d6e8c05d2fb04b2f5b'], ])( 'should compress non-ASCII files correctly: %s', async (fileName: string, compressionMethod: CompressionMethodValue, expectedZipMd5: string) => { await assertSingleFileZip( fileName, 4096, '620f0b67a91f7f74151bc5be745b7110', compressionMethod, expectedZipMd5, ); }, ); // Generated by ROMVault v3.7.2 test.each([ [ 0, 'd41d8cd98f00b204e9800998ecf8427e', CompressionMethod.DEFLATE, 'a52c8dc34d9876338e3f2c99658436e5', ], [ 0, 'd41d8cd98f00b204e9800998ecf8427e', CompressionMethod.ZSTD, '197af9c3f77a780fc765993bb02dea2e', ], [ 1, '93b885adfe0da089cdf634904fd59f71', CompressionMethod.DEFLATE, 'e372acecb8a6650901111e3240fec634', ], [ 1, '93b885adfe0da089cdf634904fd59f71', CompressionMethod.ZSTD, 'e86a8d4101c043f2ba00b0a76a72ae71', ], [ 2, 'c4103f122d27677c9db144cae1394a66', CompressionMethod.DEFLATE, 'bdaedb09f7502031f8d6538dfb726a03', ], [ 2, 'c4103f122d27677c9db144cae1394a66', CompressionMethod.ZSTD, 'c89ab052da2cb50a998a7944fc93298c', ], ])( 'should compress small files correctly: %s', async ( fileSize: number, expectedRawMd5: string, compressionMethod: CompressionMethodValue, expectedZipMd5: string, ) => { await assertSingleFileZip('small', fileSize, expectedRawMd5, compressionMethod, expectedZipMd5); }, ); test.each([ [['one/'], CompressionMethod.DEFLATE, 'bcd06b377b442dc43e56c5e75a641ee7'], [['one/'], CompressionMethod.ZSTD, 'b3faf018ff2a10c5809819eabd841e48'], [['one/', 'two/'], CompressionMethod.DEFLATE, 'c9c03c1fa878389cd3f573610c6ceee6'], [['one/', 'two/'], CompressionMethod.ZSTD, '2c9a080899c748b07959a62b3f8a0abb'], [['parent/child/'], CompressionMethod.DEFLATE, 'a436823832cca61bbe9349ea35281b83'], [['parent/child/'], CompressionMethod.ZSTD, '2d4cb1f99cb28a5dc9c83defa96d32a6'], ])( 'should compress directories correctly: %s', async (inputDirectories, compressionMethod: CompressionMethodValue, expectedZipMd5) => { const tempZipPath = await FsPoly.mktemp(path.join(Temp.getTempDir(), 'dir.zip')); try { const tempZip = await TZWriter.open(tempZipPath, compressionMethod); for (const inputDirectory of inputDirectories) { const readable = new stream.Readable({ read(): void { this.push(Buffer.alloc(0)); // eslint-disable-next-line unicorn/no-null this.push(null); }, }); await tempZip.addStream(readable, inputDirectory, 0, os.cpus().length); } await tempZip.finalize(); expect((await FileChecksums.hashFile(tempZipPath, ChecksumBitmask.MD5)).md5).toEqual( expectedZipMd5, ); } finally { await FsPoly.rm(tempZipPath, { force: true }); } }, ); // @ts-expect-error __coverage__ exists if (globalThis.__coverage__) { // Generated by ROMVault v3.7.2 test.each([ // ~4,095MiB [ 0xff_ff_ff_ff - 1, '541249e3205af07b4a03f891185f64a0', CompressionMethod.DEFLATE, '6774743f72258f5ac28d90a7de8c05fb', ], [ 0xff_ff_ff_ff - 1, '541249e3205af07b4a03f891185f64a0', CompressionMethod.ZSTD, 'f2b2a870d4e10b7edfa75c96f20d34b4', ], [ 0xff_ff_ff_ff, 'c654ebc4b3472cfa01ade24bbbbc6d3e', CompressionMethod.DEFLATE, 'd1ae563f29e137c71bb2f20a1d08972d', ], [ 0xff_ff_ff_ff, 'c654ebc4b3472cfa01ade24bbbbc6d3e', CompressionMethod.ZSTD, '4fbbd810cd2dbe3013dae9ff0a60b8cc', ], [ 0xff_ff_ff_ff + 1, 'c9a5a6878d97b48cc965c1e41859f034', CompressionMethod.DEFLATE, '7d3cbf0885c4ca2fea3dd55bb2394c10', ], [ 0xff_ff_ff_ff + 1, 'c9a5a6878d97b48cc965c1e41859f034', CompressionMethod.ZSTD, '069faff50403b8bd2806716bff9c7172', ], ])( 'should compress large files correctly: %s', async ( fileSize: number, expectedRawMd5: string, compressionMethod: CompressionMethodValue, expectedZipMd5: string, ) => { await assertSingleFileZip( 'large', fileSize, expectedRawMd5, compressionMethod, expectedZipMd5, ); }, ); }