68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { base64ToBytes, BufferReader } from '../src/parser';
|
|
|
|
describe('base64ToBytes', () => {
|
|
it('decodes a simple base64 string', () => {
|
|
const bytes = base64ToBytes('aGVsbG8='); // "hello"
|
|
expect(Array.from(bytes)).toEqual([104, 101, 108, 108, 111]);
|
|
});
|
|
|
|
it('handles empty string', () => {
|
|
const bytes = base64ToBytes('');
|
|
expect(bytes).toBeInstanceOf(Uint8Array);
|
|
expect(bytes.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('BufferReader', () => {
|
|
it('readByte and peekByte advance/inspect correctly', () => {
|
|
const buf = new Uint8Array([1, 2, 3]);
|
|
const r = new BufferReader(buf);
|
|
expect(r.peekByte()).toBe(1);
|
|
expect(r.readByte()).toBe(1);
|
|
expect(r.peekByte()).toBe(2);
|
|
});
|
|
|
|
it('readBytes with and without length', () => {
|
|
const buf = new Uint8Array([10, 11, 12, 13]);
|
|
const r = new BufferReader(buf);
|
|
const a = r.readBytes(2);
|
|
expect(Array.from(a)).toEqual([10, 11]);
|
|
const b = r.readBytes();
|
|
expect(Array.from(b)).toEqual([12, 13]);
|
|
});
|
|
|
|
it('hasMore and remainingBytes reflect position', () => {
|
|
const buf = new Uint8Array([5, 6]);
|
|
const r = new BufferReader(buf);
|
|
expect(r.hasMore()).toBe(true);
|
|
expect(r.remainingBytes()).toBe(2);
|
|
r.readByte();
|
|
expect(r.remainingBytes()).toBe(1);
|
|
r.readByte();
|
|
expect(r.hasMore()).toBe(false);
|
|
});
|
|
|
|
it('reads little-endian unsigned ints', () => {
|
|
const r16 = new BufferReader(new Uint8Array([0x34, 0x12]));
|
|
expect(r16.readUint16LE()).toBe(0x1234);
|
|
|
|
const r32 = new BufferReader(new Uint8Array([0x78, 0x56, 0x34, 0x12]));
|
|
expect(r32.readUint32LE()).toBe(0x12345678);
|
|
});
|
|
|
|
it('reads signed ints with two/four bytes (negative)', () => {
|
|
const r16 = new BufferReader(new Uint8Array([0xff, 0xff]));
|
|
expect(r16.readInt16LE()).toBe(-1);
|
|
|
|
const r32 = new BufferReader(new Uint8Array([0xff, 0xff, 0xff, 0xff]));
|
|
expect(r32.readInt32LE()).toBe(-1);
|
|
});
|
|
|
|
it('readTimestamp returns Date with seconds->ms conversion', () => {
|
|
const r = new BufferReader(new Uint8Array([0x01, 0x00, 0x00, 0x00]));
|
|
const d = r.readTimestamp();
|
|
expect(d.getTime()).toBe(1000);
|
|
});
|
|
});
|