import { describe, expect, it, } from "vitest"; import { stringToBytes, } from "../utils/bytes.js"; import { FastNode, } from "./fast-node.js"; describe("FastNode", () => { describe("constructor", () => { it("should create a FastNode with given key, value, and version", () => { const key = stringToBytes("mykey"); const value = stringToBytes("myvalue"); const version = 42n; const fastNode = new FastNode(key, value, version); expect(fastNode.key).toEqual(key); expect(fastNode.value).toEqual(value); expect(fastNode.versionLastUpdatedAt).toBe(version); }); it("should create a FastNode with empty value", () => { const key = stringToBytes("key"); const value = new Uint8Array(0); const version = 1n; const fastNode = new FastNode(key, value, version); expect(fastNode.key).toEqual(key); expect(fastNode.value).toEqual(value); expect(fastNode.versionLastUpdatedAt).toBe(version); }); it("should create a FastNode with version 0", () => { const key = stringToBytes("key"); const value = stringToBytes("value"); const version = 0n; const fastNode = new FastNode(key, value, version); expect(fastNode.versionLastUpdatedAt).toBe(0n); }); it("should be immutable after construction", () => { const key = stringToBytes("key"); const value = stringToBytes("value"); const version = 1n; const fastNode = new FastNode(key, value, version); expect(Object.isFrozen(fastNode)).toBe(true); }); }); describe("encodedSize", () => { it("should calculate correct size for small values", () => { const key = stringToBytes("a"); const value = stringToBytes("b"); const version = 1n; const fastNode = new FastNode(key, value, version); const size = fastNode.encodedSize(); // version: 1 byte (varint for 1) // value: 1 byte (varint for length 1) + 1 byte (data) expect(size).toBe(3); }); it("should calculate correct size for larger values", () => { const key = stringToBytes("key"); const value = new Uint8Array(100); // 100 bytes of zeros const version = 1000n; const fastNode = new FastNode(key, value, version); const size = fastNode.encodedSize(); // version: 2 bytes (varint for 1000) // value: 1 byte (varint for length 100) + 100 bytes (data) expect(size).toBe(103); }); it("should calculate correct size for empty value", () => { const key = stringToBytes("key"); const value = new Uint8Array(0); const version = 1n; const fastNode = new FastNode(key, value, version); const size = fastNode.encodedSize(); // version: 1 byte (varint for 1) // value: 1 byte (varint for length 0) + 0 bytes (data) expect(size).toBe(2); }); it("should calculate correct size for large version", () => { const key = stringToBytes("key"); const value = stringToBytes("value"); const version = 1_000_000_000n; const fastNode = new FastNode(key, value, version); const size = fastNode.encodedSize(); // version: 5 bytes (varint for 1 billion) // value: 1 byte (varint for length 5) + 5 bytes (data) expect(size).toBe(11); }); }); describe("toBytes and fromBytes", () => { it("should serialize and deserialize correctly", () => { const key = stringToBytes("mykey"); const value = stringToBytes("myvalue"); const version = 42n; const original = new FastNode(key, value, version); const bytes = original.toBytes(); const deserialized = FastNode.fromBytes(key, bytes); expect(deserialized.key).toEqual(original.key); expect(deserialized.value).toEqual(original.value); expect(deserialized.versionLastUpdatedAt).toBe(original.versionLastUpdatedAt); }); it("should serialize and deserialize with empty value", () => { const key = stringToBytes("key"); const value = new Uint8Array(0); const version = 1n; const original = new FastNode(key, value, version); const bytes = original.toBytes(); const deserialized = FastNode.fromBytes(key, bytes); expect(deserialized.key).toEqual(original.key); expect(deserialized.value).toEqual(value); expect(deserialized.versionLastUpdatedAt).toBe(version); }); it("should serialize and deserialize with version 0", () => { const key = stringToBytes("key"); const value = stringToBytes("value"); const version = 0n; const original = new FastNode(key, value, version); const bytes = original.toBytes(); const deserialized = FastNode.fromBytes(key, bytes); expect(deserialized.versionLastUpdatedAt).toBe(0n); }); it("should serialize and deserialize with large version", () => { const key = stringToBytes("key"); const value = stringToBytes("value"); const version = 9_223_372_036_854_775_807n; // Max int64 const original = new FastNode(key, value, version); const bytes = original.toBytes(); const deserialized = FastNode.fromBytes(key, bytes); expect(deserialized.versionLastUpdatedAt).toBe(version); }); it("should serialize and deserialize with large value", () => { const key = stringToBytes("key"); const value = new Uint8Array(1000); for (let i = 0; i < value.length; i++) { value[i] = i % 256; } const version = 123n; const original = new FastNode(key, value, version); const bytes = original.toBytes(); const deserialized = FastNode.fromBytes(key, bytes); expect(deserialized.key).toEqual(original.key); expect(deserialized.value).toEqual(original.value); expect(deserialized.versionLastUpdatedAt).toBe(version); }); it("should produce bytes of correct length", () => { const key = stringToBytes("key"); const value = stringToBytes("value"); const version = 42n; const fastNode = new FastNode(key, value, version); const bytes = fastNode.toBytes(); const expectedSize = fastNode.encodedSize(); expect(bytes.length).toBe(expectedSize); }); it("should throw error on malformed bytes (truncated)", () => { const key = stringToBytes("key"); const value = stringToBytes("value"); const version = 42n; const original = new FastNode(key, value, version); const bytes = original.toBytes(); // Truncate the bytes const truncated = bytes.slice(0, bytes.length - 1); expect(() => FastNode.fromBytes(key, truncated)).toThrow(); }); it("should throw error on malformed bytes (extra data)", () => { const key = stringToBytes("key"); const value = stringToBytes("value"); const version = 42n; const original = new FastNode(key, value, version); const bytes = original.toBytes(); // Add extra bytes const extra = new Uint8Array(bytes.length + 10); extra.set(bytes, 0); expect(() => FastNode.fromBytes(key, extra)).toThrow( /unexpected trailing bytes/, ); }); }); describe("keyString and valueString", () => { it("should return hex string for key", () => { const key = new Uint8Array([ 0xDE, 0xAD, 0xBE, 0xEF, ]); const value = stringToBytes("value"); const version = 1n; const fastNode = new FastNode(key, value, version); expect(fastNode.keyString()).toBe("deadbeef"); }); it("should return hex string for value", () => { const key = stringToBytes("key"); const value = new Uint8Array([ 0xCA, 0xFE, 0xBA, 0xBE, ]); const version = 1n; const fastNode = new FastNode(key, value, version); expect(fastNode.valueString()).toBe("cafebabe"); }); it("should return empty string for empty key", () => { const key = new Uint8Array(0); const value = stringToBytes("value"); const version = 1n; const fastNode = new FastNode(key, value, version); expect(fastNode.keyString()).toBe(""); }); it("should return empty string for empty value", () => { const key = stringToBytes("key"); const value = new Uint8Array(0); const version = 1n; const fastNode = new FastNode(key, value, version); expect(fastNode.valueString()).toBe(""); }); }); describe("comparison with Go implementation", () => { it("should match Go serialization format", () => { // This test verifies the serialization format matches Go's implementation const key = stringToBytes("testkey"); const value = stringToBytes("testvalue"); const version = 100n; const fastNode = new FastNode(key, value, version); const bytes = fastNode.toBytes(); // Manually verify the format: // - First byte(s): signed varint (ZigZag) encoding of version // Version 100 with ZigZag: 100 << 1 = 200 = 0xC8 0x01 in varint // - Next byte(s): varint encoding of value length (9 = 0x09) // - Remaining bytes: value data expect(bytes[0]).toBe(0xC8); // version = 100, ZigZag encoded (first byte) expect(bytes[1]).toBe(0x01); // version = 100, ZigZag encoded (second byte) expect(bytes[2]).toBe(0x09); // length = 9 expect(bytes.length).toBe(12); // 2 (version) + 1 (length) + 9 (value) }); it("should round-trip with multiple FastNodes", () => { // Test multiple nodes to ensure no state leakage const nodes = [ new FastNode(stringToBytes("key1"), stringToBytes("value1"), 1n), new FastNode(stringToBytes("key2"), stringToBytes("value2"), 2n), new FastNode(stringToBytes("key3"), stringToBytes("value3"), 3n), ]; for (const original of nodes) { const bytes = original.toBytes(); const deserialized = FastNode.fromBytes(original.key, bytes); expect(deserialized.key).toEqual(original.key); expect(deserialized.value).toEqual(original.value); expect(deserialized.versionLastUpdatedAt).toBe(original.versionLastUpdatedAt); } }); }); });