Files
2026-10-30 23:54:07 +07:00

50 lines
2.6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {lz4, asbIndex, encrDirectory} from './level_bundles.mjs';
test('LZ4 validates literal sizes and supports overlapping matches', () => {
assert.equal(lz4(Buffer.from([0x50, ...Buffer.from('hello')]), 5).toString(), 'hello');
assert.equal(lz4(Buffer.from([0x11, 97, 1, 0]), 6).toString(), 'aaaaaa');
assert.throws(() => lz4(Buffer.from([0xf0]), 15), /Truncated/);
assert.throws(() => lz4(Buffer.from([0x11, 97, 0, 0]), 6), /match/);
assert.throws(() => lz4(Buffer.from([0x50, 97]), 5), /literal/);
assert.throws(() => lz4(Buffer.from([0]), 0x4000001), /limit/);
});
test('SRAM bundle ranges use block Hash128 word order and reject corrupt sections', () => {
const data = Buffer.alloc(224);
Buffer.from('5352414d00050001', 'hex').copy(data);
data.writeUInt32LE(40, 8); data.writeUInt16LE(8, 12); data.writeUInt16LE(16, 14);
let offset = 168;
[24, 4, 32, 4, 8, 12, 4, 8].forEach((stride, i) => {
const count = [1, 0, 1, 0, 0, 0, 0, 0][i], p = 44 + i * 16;
data.writeUInt32LE(offset, p); data.writeUInt32LE(count, p + 4); data.writeUInt32LE(stride, p + 8);
offset += count * stride;
});
Buffer.from('000102030405060708090a0b0c0d0e0f', 'hex').copy(data, 168);
data.writeUInt32LE(12, 200); data.writeUInt32LE(100, 204);
assert.deepEqual(asbIndex(data).bundle(0), {index: 0, block: '03020100070605040b0a09080f0e0d0c.block', offset: 12, size: 100});
assert.throws(() => asbIndex(data).bundle(1), /bundle index/);
data.writeUInt32LE(0xffffffff, 48);
assert.throws(() => asbIndex(data), /section/);
});
test('ENCR directory parsing exposes codec 6 without pretending to decode its payload', () => {
const info = Buffer.alloc(42);
info.writeUInt32BE(1, 0); info.writeUInt32BE(4, 4); info.writeUInt32BE(2, 8); info.writeUInt16BE(6, 12);
info.writeUInt32BE(1, 14); info.writeBigUInt64BE(4n, 26); info.writeUInt32BE(4, 34);
info.write('CAB\0', 38);
const compressed = Buffer.concat([Buffer.from([0xf0, info.length - 15]), info]);
const raw = Buffer.alloc(25 + compressed.length + 2);
raw.write('ENCR\0'); raw.writeBigUInt64BE(BigInt(raw.length), 5);
raw.writeUInt32BE(compressed.length, 13); raw.writeUInt32BE(info.length, 17); raw.writeUInt32BE(0x43, 21);
compressed.copy(raw, 25);
const result = encrDirectory(raw);
assert.deepEqual(result.unsupported_payload_compressions, [6]);
assert.equal(result.payload_decoded, false);
assert.equal(result.nodes[0].path, 'CAB');
assert.throws(() => encrDirectory(raw.subarray(0, -1)), /header/);
raw.writeUInt32BE(0x44, 21);
assert.throws(() => encrDirectory(raw), /layout/);
});