import { describe, expect, it, } from "vitest"; import { decodeBytes, decodeUvarint, decodeVarint, decodeVarintBigInt, encodeBytes, encodedBytesSize, encodeUvarint, encodeVarint, uvarintLength, varintLength, } from "./encoding.js"; describe("unsigned varint encoding (uvarint)", () => { describe("encodeUvarint", () => { it("should encode single byte values (0-127)", () => { expect(encodeUvarint(0)).toEqual(new Uint8Array([0])); expect(encodeUvarint(1)).toEqual(new Uint8Array([1])); expect(encodeUvarint(127)).toEqual(new Uint8Array([127])); }); it("should encode two byte values", () => { expect(encodeUvarint(128)).toEqual(new Uint8Array([0x80, 0x01])); expect(encodeUvarint(255)).toEqual(new Uint8Array([0xFF, 0x01])); expect(encodeUvarint(300)).toEqual(new Uint8Array([0xAC, 0x02])); }); it("should encode multi-byte values", () => { expect(encodeUvarint(16384)).toEqual(new Uint8Array([ 0x80, 0x80, 0x01, ])); expect(encodeUvarint(2097151)).toEqual(new Uint8Array([ 0xFF, 0xFF, 0x7F, ])); }); it("should encode large numbers", () => { const encoded = encodeUvarint(Number.MAX_SAFE_INTEGER); expect(encoded.length).toBeGreaterThan(1); // Verify it can be decoded back const decoded = decodeUvarint(encoded); expect(decoded.value).toBe(Number.MAX_SAFE_INTEGER); }); it("should encode bigint values", () => { expect(encodeUvarint(0n)).toEqual(new Uint8Array([0])); expect(encodeUvarint(128n)).toEqual(new Uint8Array([0x80, 0x01])); expect(encodeUvarint(BigInt(Number.MAX_SAFE_INTEGER))).toEqual( encodeUvarint(Number.MAX_SAFE_INTEGER), ); }); it("should throw on negative numbers", () => { expect(() => encodeUvarint(-1)).toThrow("value must be non-negative"); expect(() => encodeUvarint(-1n)).toThrow("value must be non-negative"); }); it("should throw on unsafe integers", () => { expect(() => encodeUvarint(Number.MAX_SAFE_INTEGER + 1)).toThrow( "exceeds safe integer range", ); }); }); describe("decodeUvarint", () => { it("should decode single byte values", () => { expect(decodeUvarint(new Uint8Array([0]))).toEqual({ value: 0, bytesRead: 1, }); expect(decodeUvarint(new Uint8Array([1]))).toEqual({ value: 1, bytesRead: 1, }); expect(decodeUvarint(new Uint8Array([127]))).toEqual({ value: 127, bytesRead: 1, }); }); it("should decode multi-byte values", () => { expect(decodeUvarint(new Uint8Array([0x80, 0x01]))).toEqual({ value: 128, bytesRead: 2, }); expect(decodeUvarint(new Uint8Array([0xFF, 0x01]))).toEqual({ value: 255, bytesRead: 2, }); expect(decodeUvarint(new Uint8Array([0xAC, 0x02]))).toEqual({ value: 300, bytesRead: 2, }); }); it("should decode with offset", () => { const bytes = new Uint8Array([ 0xFF, 0x80, 0x01, 0xAA, ]); expect(decodeUvarint(bytes, 1)).toEqual({ value: 128, bytesRead: 2, }); }); it("should handle extra bytes after varint", () => { const bytes = new Uint8Array([ 0x01, 0xFF, 0xFF, ]); expect(decodeUvarint(bytes)).toEqual({ value: 1, bytesRead: 1, }); }); it("should throw on incomplete varint", () => { expect(() => decodeUvarint(new Uint8Array([0x80]))).toThrow( "unexpected end of data", ); expect(() => decodeUvarint(new Uint8Array([0x80, 0x80]))).toThrow( "unexpected end of data", ); }); it("should throw on varint that's too long", () => { // Create a varint with 11 continuation bytes (more than max of 10) const tooLong = new Uint8Array(11).fill(0x80); expect(() => decodeUvarint(tooLong)).toThrow("varint too long"); }); }); describe("encode/decode round-trip", () => { it("should round-trip various unsigned values", () => { const testValues = [ 0, 1, 127, 128, 255, 256, 16383, 16384, 2097151, 2097152, 1000000, Number.MAX_SAFE_INTEGER, ]; for (const value of testValues) { const encoded = encodeUvarint(value); const decoded = decodeUvarint(encoded); expect(decoded.value).toBe(value); expect(decoded.bytesRead).toBe(encoded.length); } }); it("should round-trip bigint unsigned values", () => { const testValues = [ 0n, 1n, 127n, 128n, BigInt(Number.MAX_SAFE_INTEGER), ]; for (const value of testValues) { const encoded = encodeUvarint(value); const decoded = decodeUvarint(encoded); // Convert to bigint for comparison since decodeUvarint returns number expect(BigInt(decoded.value)).toBe(value); expect(decoded.bytesRead).toBe(encoded.length); } }); it("should round-trip very large bigint values (beyond safe integer)", () => { // For values beyond Number.MAX_SAFE_INTEGER, decodeUvarint will throw // These would require a decodeUvarintBigInt function (not implemented) const largeValue = BigInt(Number.MAX_SAFE_INTEGER) + 1n; const encoded = encodeUvarint(largeValue); expect(() => decodeUvarint(encoded)).toThrow("exceeds safe integer range"); }); }); describe("compatibility with known vectors", () => { // These test vectors are from the Go implementation it("should match Go binary.PutUvarint/Uvarint behavior", () => { // Test cases: [value, expected bytes] const testCases: Array<[number, number[]]> = [ [0, [0x00]], [1, [0x01]], [127, [0x7F]], [128, [0x80, 0x01]], [255, [0xFF, 0x01]], [256, [0x80, 0x02]], [16383, [0xFF, 0x7F]], [ 16384, [ 0x80, 0x80, 0x01, ], ], ]; for (const [value, expectedBytes] of testCases) { const encoded = encodeUvarint(value); expect(Array.from(encoded)).toEqual(expectedBytes); const decoded = decodeUvarint(new Uint8Array(expectedBytes)); expect(decoded.value).toBe(value); } }); }); describe("Edge cases for uncovered lines", () => { it("should throw error when decoding varint exceeds safe integer (line 99)", () => { // Create a varint that exceeds Number.MAX_SAFE_INTEGER // This is 2^53 which requires more than 7 bytes const largeVarint = encodeUvarint(BigInt(Number.MAX_SAFE_INTEGER) + 100n); // decodeUvarint should throw for values > MAX_SAFE_INTEGER expect(() => decodeUvarint(largeVarint)).toThrow("exceeds safe integer range"); }); }); }); describe("signed varint encoding (ZigZag)", () => { describe("encodeVarint - ZigZag signed encoding", () => { it("should encode positive values with ZigZag", () => { // ZigZag: 0→0, 1→2, 2→4, 3→6 expect(encodeVarint(0)).toEqual(new Uint8Array([0x00])); // 0 * 2 = 0 expect(encodeVarint(1)).toEqual(new Uint8Array([0x02])); // 1 * 2 = 2 expect(encodeVarint(2)).toEqual(new Uint8Array([0x04])); // 2 * 2 = 4 expect(encodeVarint(3)).toEqual(new Uint8Array([0x06])); // 3 * 2 = 6 }); it("should encode negative values with ZigZag", () => { // ZigZag: -1→1, -2→3, -3→5, -4→7 expect(encodeVarint(-1)).toEqual(new Uint8Array([0x01])); // (-1 * 2) - 1 = 1 expect(encodeVarint(-2)).toEqual(new Uint8Array([0x03])); // (-2 * 2) - 1 = 3 expect(encodeVarint(-3)).toEqual(new Uint8Array([0x05])); // (-3 * 2) - 1 = 5 expect(encodeVarint(-4)).toEqual(new Uint8Array([0x07])); // (-4 * 2) - 1 = 7 }); it("should encode larger positive values with ZigZag", () => { expect(encodeVarint(64)).toEqual(new Uint8Array([0x80, 0x01])); // 64 * 2 = 128 expect(encodeVarint(127)).toEqual(new Uint8Array([0xFE, 0x01])); // 127 * 2 = 254 }); it("should encode larger negative values with ZigZag", () => { expect(encodeVarint(-64)).toEqual(new Uint8Array([0x7F])); // (-64 * 2) - 1 = 127 expect(encodeVarint(-65)).toEqual(new Uint8Array([0x81, 0x01])); // (-65 * 2) - 1 = 129 }); it("should encode bigint values with ZigZag", () => { expect(encodeVarint(0n)).toEqual(new Uint8Array([0])); expect(encodeVarint(1n)).toEqual(new Uint8Array([0x02])); expect(encodeVarint(-1n)).toEqual(new Uint8Array([0x01])); expect(encodeVarint(-2n)).toEqual(new Uint8Array([0x03])); }); it("should throw on unsafe integers", () => { expect(() => encodeVarint(Number.MAX_SAFE_INTEGER + 1)).toThrow( "exceeds safe integer range", ); }); }); describe("decodeVarint - ZigZag signed decoding", () => { it("should decode positive ZigZag values", () => { expect(decodeVarint(new Uint8Array([0x00]))).toEqual({ value: 0, bytesRead: 1, }); expect(decodeVarint(new Uint8Array([0x02]))).toEqual({ value: 1, bytesRead: 1, }); expect(decodeVarint(new Uint8Array([0x04]))).toEqual({ value: 2, bytesRead: 1, }); }); it("should decode negative ZigZag values", () => { expect(decodeVarint(new Uint8Array([0x01]))).toEqual({ value: -1, bytesRead: 1, }); expect(decodeVarint(new Uint8Array([0x03]))).toEqual({ value: -2, bytesRead: 1, }); expect(decodeVarint(new Uint8Array([0x05]))).toEqual({ value: -3, bytesRead: 1, }); }); it("should decode larger positive ZigZag values", () => { expect(decodeVarint(new Uint8Array([0x80, 0x01]))).toEqual({ value: 64, bytesRead: 2, }); expect(decodeVarint(new Uint8Array([0xFE, 0x01]))).toEqual({ value: 127, bytesRead: 2, }); }); it("should decode larger negative ZigZag values", () => { expect(decodeVarint(new Uint8Array([0x7F]))).toEqual({ value: -64, bytesRead: 1, }); expect(decodeVarint(new Uint8Array([0x81, 0x01]))).toEqual({ value: -65, bytesRead: 2, }); }); it("should throw on incomplete varint", () => { expect(() => decodeVarint(new Uint8Array([0x80]))).toThrow( "unexpected end of data", ); }); it("should throw on varint that's too long", () => { const tooLong = new Uint8Array(11).fill(0x80); expect(() => decodeVarint(tooLong)).toThrow("varint too long"); }); }); describe("decodeVarintBigInt - ZigZag BigInt decoding", () => { it("should decode positive values as BigInt", () => { expect(decodeVarintBigInt(new Uint8Array([0x00]))).toEqual({ value: 0n, bytesRead: 1, }); expect(decodeVarintBigInt(new Uint8Array([0x02]))).toEqual({ value: 1n, bytesRead: 1, }); }); it("should decode negative values as BigInt", () => { expect(decodeVarintBigInt(new Uint8Array([0x01]))).toEqual({ value: -1n, bytesRead: 1, }); expect(decodeVarintBigInt(new Uint8Array([0x03]))).toEqual({ value: -2n, bytesRead: 1, }); }); it("should decode large values beyond safe integer range", () => { const largeValue = BigInt(Number.MAX_SAFE_INTEGER) + 1000n; const encoded = encodeVarint(largeValue); const decoded = decodeVarintBigInt(encoded); expect(decoded.value).toBe(largeValue); }); it("should throw on incomplete varint", () => { expect(() => decodeVarintBigInt(new Uint8Array([0x80]))).toThrow( "unexpected end of data", ); }); it("should throw on varint that's too long", () => { const tooLong = new Uint8Array(11).fill(0x80); tooLong[10] = 0x01; expect(() => decodeVarintBigInt(tooLong)).toThrow("varint too long"); }); }); describe("encode/decode round-trip with ZigZag", () => { it("should round-trip positive and negative values", () => { const testValues = [ 0, 1, -1, 2, -2, 63, -63, 64, -64, 127, -127, 128, -128, 1000, -1000, 10000, -10000, ]; for (const value of testValues) { const encoded = encodeVarint(value); const decoded = decodeVarint(encoded); expect(decoded.value).toBe(value); expect(decoded.bytesRead).toBe(encoded.length); } }); it("should round-trip bigint values", () => { const testValues = [ 0n, 1n, -1n, 127n, -127n, 128n, -128n, BigInt(Number.MAX_SAFE_INTEGER), BigInt(Number.MIN_SAFE_INTEGER), ]; for (const value of testValues) { const encoded = encodeVarint(value); const decoded = decodeVarintBigInt(encoded); expect(decoded.value).toBe(value); expect(decoded.bytesRead).toBe(encoded.length); } }); }); describe("compatibility with Go binary.PutVarint/Varint", () => { it("should match Go ZigZag encoding", () => { // Test cases: [value, expected bytes] const testCases: Array<[number, number[]]> = [ [0, [0x00]], [1, [0x02]], [-1, [0x01]], [2, [0x04]], [-2, [0x03]], [64, [0x80, 0x01]], [-64, [0x7F]], [127, [0xFE, 0x01]], [-65, [0x81, 0x01]], ]; for (const [value, expectedBytes] of testCases) { const encoded = encodeVarint(value); expect(Array.from(encoded)).toEqual(expectedBytes); const decoded = decodeVarint(new Uint8Array(expectedBytes)); expect(decoded.value).toBe(value); } }); }); describe("Edge cases", () => { it("should throw error when signed varint exceeds safe integer range", () => { // Create a ZigZag varint that exceeds Number.MAX_SAFE_INTEGER const largeVarint = encodeVarint(BigInt(Number.MAX_SAFE_INTEGER) + 100n); expect(() => decodeVarint(largeVarint)).toThrow("use decodeVarintBigInt"); }); }); describe("varintLength - ZigZag length calculation", () => { it("should calculate correct length for positive values", () => { // ZigZag doubles positive values: 0→0, 1→2, 2→4 expect(varintLength(0)).toBe(1); // 0 * 2 = 0 expect(varintLength(1)).toBe(1); // 1 * 2 = 2 expect(varintLength(2)).toBe(1); // 2 * 2 = 4 expect(varintLength(63)).toBe(1); // 63 * 2 = 126 }); it("should calculate correct length for negative values", () => { // ZigZag: -1→1, -2→3, -64→127 expect(varintLength(-1)).toBe(1); // (-1 * 2) - 1 = 1 expect(varintLength(-2)).toBe(1); // (-2 * 2) - 1 = 3 expect(varintLength(-64)).toBe(1); // (-64 * 2) - 1 = 127 }); it("should calculate correct length for larger values", () => { // ZigZag: 64→128 (2 bytes), 127→254 (2 bytes) expect(varintLength(64)).toBe(2); expect(varintLength(127)).toBe(2); expect(varintLength(-65)).toBe(2); }); it("should match actual encoded length", () => { const testValues = [ 0, 1, -1, 63, -63, 64, -64, 127, -127, 128, -128, 1000, -1000, ]; for (const value of testValues) { const encoded = encodeVarint(value); expect(varintLength(value)).toBe(encoded.length); } }); it("should work with bigint", () => { expect(varintLength(0n)).toBe(1); expect(varintLength(1n)).toBe(1); expect(varintLength(-1n)).toBe(1); const encoded = encodeVarint(BigInt(Number.MAX_SAFE_INTEGER)); expect(varintLength(BigInt(Number.MAX_SAFE_INTEGER))).toBe(encoded.length); }); }); }); describe("uvarintLength - unsigned length calculation", () => { it("should calculate correct length for single byte values", () => { expect(uvarintLength(0)).toBe(1); expect(uvarintLength(1)).toBe(1); expect(uvarintLength(127)).toBe(1); }); it("should calculate correct length for multi-byte values", () => { expect(uvarintLength(128)).toBe(2); expect(uvarintLength(255)).toBe(2); expect(uvarintLength(16383)).toBe(2); expect(uvarintLength(16384)).toBe(3); }); it("should match actual encoded length", () => { const testValues = [ 0, 1, 127, 128, 255, 256, 16383, 16384, 1000000, ]; for (const value of testValues) { const encoded = encodeUvarint(value); expect(uvarintLength(value)).toBe(encoded.length); } }); it("should work with bigint", () => { expect(uvarintLength(0n)).toBe(1); expect(uvarintLength(128n)).toBe(2); const encoded = encodeUvarint(BigInt(Number.MAX_SAFE_INTEGER)); expect(uvarintLength(BigInt(Number.MAX_SAFE_INTEGER))).toBe(encoded.length); }); it("should throw on negative values", () => { expect(() => uvarintLength(-1)).toThrow("value must be non-negative"); expect(() => uvarintLength(-1n)).toThrow("value must be non-negative"); }); it("should handle large values", () => { const largeValue = 0x10000000; // 268435456 const length = uvarintLength(largeValue); expect(length).toBe(5); // Should need 5 bytes // Verify by encoding and checking const encoded = encodeUvarint(largeValue); expect(encoded.length).toBe(length); // Even larger value (use bigint for values > MAX_SAFE_INTEGER) const veryLargeValue = 0x100000000n; // 4294967296 (2^32) const length2 = uvarintLength(veryLargeValue); expect(length2).toBeGreaterThan(4); const encoded2 = encodeUvarint(veryLargeValue); expect(encoded2.length).toBe(length2); }); }); describe("bytes encoding (encodeBytes/decodeBytes)", () => { describe("decodeBytes and encodeBytes", () => { it("should encode bytes with length prefix", () => { const data = new Uint8Array([ 0x01, 0x02, 0x03, 0x04, ]); const encoded = encodeBytes(data); // Should have varint length prefix + data expect(encoded.length).toBeGreaterThan(data.length); expect(encoded[0]).toBe(4); // Length is 4, fits in 1 byte }); it("should decode bytes with length prefix", () => { const data = new Uint8Array([ 0xAA, 0xBB, 0xCC, ]); const encoded = encodeBytes(data); const { bytes: decoded, bytesRead, } = decodeBytes(encoded, 0); expect(decoded).toEqual(data); expect(bytesRead).toBe(encoded.length); }); it("should handle encodeBytes/decodeBytes round trip", () => { const testCases = [ new Uint8Array([]), new Uint8Array([0x00]), new Uint8Array([0xFF]), new Uint8Array([ 0x01, 0x02, 0x03, ]), new Uint8Array(100).fill(0xAB), new Uint8Array(1000).fill(0xCD), ]; for (const original of testCases) { const encoded = encodeBytes(original); const { bytes: decoded, } = decodeBytes(encoded, 0); expect(decoded).toEqual(original); } }); it("should calculate encodedBytesSize correctly", () => { const data = new Uint8Array([ 0x01, 0x02, 0x03, ]); const encoded = encodeBytes(data); const calculatedSize = encodedBytesSize(data); expect(calculatedSize).toBe(encoded.length); }); it("should handle encodedBytesSize for various lengths", () => { // Small data (length < 128, varint is 1 byte) expect(encodedBytesSize(new Uint8Array(10))).toBe(11); // 1 + 10 // Medium data (length 128, varint is 2 bytes) expect(encodedBytesSize(new Uint8Array(128))).toBe(130); // 2 + 128 // Large data (length 16384, varint is 3 bytes) expect(encodedBytesSize(new Uint8Array(16384))).toBe(16387); // 3 + 16384 }); it("should throw error when decodeBytes has insufficient data", () => { // Create encoded bytes but truncate it const data = new Uint8Array([ 0x01, 0x02, 0x03, 0x04, ]); const encoded = encodeBytes(data); // Truncate to fewer bytes than indicated by length prefix const truncated = encoded.slice(0, 3); // Says 4 bytes but only has 2 expect(() => decodeBytes(truncated, 0)).toThrow("Insufficient bytes"); }); it("should handle decodeBytes with offset", () => { const data1 = new Uint8Array([0xAA, 0xBB]); const data2 = new Uint8Array([ 0xCC, 0xDD, 0xEE, ]); const encoded1 = encodeBytes(data1); const encoded2 = encodeBytes(data2); // Concatenate const combined = new Uint8Array(encoded1.length + encoded2.length); combined.set(encoded1, 0); combined.set(encoded2, encoded1.length); // Decode first const { bytes: decoded1, bytesRead: read1, } = decodeBytes(combined, 0); expect(decoded1).toEqual(data1); // Decode second using offset const { bytes: decoded2, bytesRead: read2, } = decodeBytes(combined, read1); expect(decoded2).toEqual(data2); expect(read1 + read2).toBe(combined.length); }); it("should handle empty byte array encoding", () => { const empty = new Uint8Array([]); const encoded = encodeBytes(empty); expect(encoded.length).toBe(1); // Just the length prefix (0) expect(encoded[0]).toBe(0); const { bytes: decoded, } = decodeBytes(encoded, 0); expect(decoded).toEqual(empty); }); it("should handle large byte array encoding", () => { const large = new Uint8Array(500).fill(0x42); const encoded = encodeBytes(large); // 500 in varint is [0xF4, 0x03], so 2 bytes expect(encoded.length).toBe(502); // 2 + 500 const { bytes: decoded, } = decodeBytes(encoded, 0); expect(decoded).toEqual(large); }); }); });