Files
ForgeFlow/tests/zip-writer.test.mjs
2026-07-24 20:29:23 +02:00

42 lines
1.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import zlib from 'node:zlib';
import zipModule from '../src/shared/zip-writer.cjs';
const { createZip, crc32 } = zipModule;
function unzipLocalEntries(buffer) {
const entries = new Map();
let offset = 0;
while (offset + 4 <= buffer.length && buffer.readUInt32LE(offset) === 0x04034b50) {
const method = buffer.readUInt16LE(offset + 8);
const expectedCrc = buffer.readUInt32LE(offset + 14);
const compressedSize = buffer.readUInt32LE(offset + 18);
const nameLength = buffer.readUInt16LE(offset + 26);
const extraLength = buffer.readUInt16LE(offset + 28);
const nameStart = offset + 30;
const dataStart = nameStart + nameLength + extraLength;
const name = buffer.subarray(nameStart, nameStart + nameLength).toString('utf8');
const compressed = buffer.subarray(dataStart, dataStart + compressedSize);
const data = method === 8 ? zlib.inflateRawSync(compressed) : compressed;
assert.equal(crc32(data), expectedCrc);
entries.set(name, data);
offset = dataStart + compressedSize;
}
return entries;
}
test('creates a valid deflated ZIP with UTF-8 entry names and CRCs', () => {
const archive = createZip([
{ name: 'manifest.json', data: '{"ok":true}\n' },
{ name: 'logs/diagnostics.jsonl', data: Buffer.from('hello diagnostics\n') },
{ name: 'unicode/één.txt', data: 'veilig' }
]);
assert.equal(archive.readUInt32LE(0), 0x04034b50);
assert.equal(archive.readUInt32LE(archive.length - 22), 0x06054b50);
const entries = unzipLocalEntries(archive);
assert.equal(entries.size, 3);
assert.equal(entries.get('manifest.json').toString(), '{"ok":true}\n');
assert.equal(entries.get('unicode/één.txt').toString(), 'veilig');
});