import { describe, expect, it, } from "vitest"; import { getRootKey, NodeKey, } from "./node-key.js"; import { bytesToHex, } from "./utils/bytes.js"; describe("NodeKey", () => { describe("constructor", () => { it("should create NodeKey with bigint version", () => { const nk = new NodeKey(100n, 5); expect(nk.version).toBe(100n); expect(nk.nonce).toBe(5); }); it("should create NodeKey with number version", () => { const nk = new NodeKey(100, 5); expect(nk.version).toBe(100n); expect(nk.nonce).toBe(5); }); it("should ensure nonce is uint32", () => { const nk = new NodeKey(1n, -1); expect(nk.nonce).toBe(0xFFFFFFFF); // -1 as uint32 }); }); describe("toBytes", () => { it("should serialize to 12 bytes", () => { const nk = new NodeKey(100n, 5); const bytes = nk.toBytes(); expect(bytes).toBeInstanceOf(Uint8Array); expect(bytes.length).toBe(12); }); it("should encode version in big-endian", () => { const nk = new NodeKey(0x0102030405060708n, 0); const bytes = nk.toBytes(); expect(bytes[0]).toBe(0x01); expect(bytes[1]).toBe(0x02); expect(bytes[2]).toBe(0x03); expect(bytes[3]).toBe(0x04); expect(bytes[4]).toBe(0x05); expect(bytes[5]).toBe(0x06); expect(bytes[6]).toBe(0x07); expect(bytes[7]).toBe(0x08); }); it("should encode nonce in big-endian", () => { const nk = new NodeKey(0n, 0x01020304); const bytes = nk.toBytes(); expect(bytes[8]).toBe(0x01); expect(bytes[9]).toBe(0x02); expect(bytes[10]).toBe(0x03); expect(bytes[11]).toBe(0x04); }); it("should handle zero values", () => { const nk = new NodeKey(0n, 0); const bytes = nk.toBytes(); expect(bytes).toEqual(new Uint8Array(12)); }); it("should handle max values", () => { const nk = new NodeKey(0xFFFFFFFFFFFFFFFFn, 0xFFFFFFFF); const bytes = nk.toBytes(); expect(bytes).toEqual(new Uint8Array([ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, ])); }); it("should match Go implementation format", () => { // From Go: version=1, nonce=1 should produce specific bytes const nk = new NodeKey(1n, 1); const bytes = nk.toBytes(); // Expected: 8 bytes for version (0x0000000000000001) + 4 bytes for nonce (0x00000001) // version = 1, nonce = 1 expect(bytes).toEqual(new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, ])); }); }); describe("fromBytes", () => { it("should deserialize from 12 bytes", () => { // version = 100, nonce = 5 const bytes = new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 5, ]); const nk = NodeKey.fromBytes(bytes); expect(nk.version).toBe(100n); expect(nk.nonce).toBe(5); }); it("should decode version in big-endian", () => { const bytes = new Uint8Array([ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0, 0, 0, 0, ]); const nk = NodeKey.fromBytes(bytes); expect(nk.version).toBe(0x0102030405060708n); }); it("should decode nonce in big-endian", () => { const bytes = new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02, 0x03, 0x04, ]); const nk = NodeKey.fromBytes(bytes); expect(nk.nonce).toBe(0x01020304); }); it("should throw error for invalid length", () => { expect(() => NodeKey.fromBytes(new Uint8Array(11))).toThrow( "NodeKey must be exactly 12 bytes, got 11", ); expect(() => NodeKey.fromBytes(new Uint8Array(13))).toThrow( "NodeKey must be exactly 12 bytes, got 13", ); }); }); describe("round-trip serialization", () => { it("should serialize and deserialize correctly", () => { const original = new NodeKey(12345n, 67890); const bytes = original.toBytes(); const restored = NodeKey.fromBytes(bytes); expect(restored.version).toBe(original.version); expect(restored.nonce).toBe(original.nonce); expect(restored.equals(original)).toBe(true); }); it("should handle multiple round-trips", () => { const nk1 = new NodeKey(100n, 200); const bytes1 = nk1.toBytes(); const nk2 = NodeKey.fromBytes(bytes1); const bytes2 = nk2.toBytes(); const nk3 = NodeKey.fromBytes(bytes2); expect(nk3.version).toBe(100n); expect(nk3.nonce).toBe(200); expect(bytes1).toEqual(bytes2); }); it("should handle edge cases", () => { const testCases = [ new NodeKey(0n, 0), new NodeKey(1n, 1), new NodeKey(0xFFFFFFFFFFFFFFFFn, 0xFFFFFFFF), new NodeKey(0x8000000000000000n, 0x80000000), ]; for (const original of testCases) { const bytes = original.toBytes(); const restored = NodeKey.fromBytes(bytes); expect(restored.equals(original)).toBe(true); } }); }); describe("rootKey", () => { it("should create root key with nonce 1", () => { const nk = NodeKey.rootKey(100n); expect(nk.version).toBe(100n); expect(nk.nonce).toBe(1); }); it("should accept number version", () => { const nk = NodeKey.rootKey(100); expect(nk.version).toBe(100n); expect(nk.nonce).toBe(1); }); it("should match Go GetRootKey implementation", () => { const nk = NodeKey.rootKey(1n); const bytes = nk.toBytes(); // Go GetRootKey(1) should produce: // version=1 (8 bytes) + nonce=1 (4 bytes) expect(bytes).toEqual(new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, ])); }); }); describe("getRootKey helper", () => { it("should return root key bytes", () => { const bytes = getRootKey(5n); expect(bytes).toBeInstanceOf(Uint8Array); expect(bytes.length).toBe(12); const nk = NodeKey.fromBytes(bytes); expect(nk.version).toBe(5n); expect(nk.nonce).toBe(1); }); }); describe("equals", () => { it("should return true for equal NodeKeys", () => { const nk1 = new NodeKey(100n, 5); const nk2 = new NodeKey(100n, 5); expect(nk1.equals(nk2)).toBe(true); expect(nk2.equals(nk1)).toBe(true); }); it("should return false for different versions", () => { const nk1 = new NodeKey(100n, 5); const nk2 = new NodeKey(101n, 5); expect(nk1.equals(nk2)).toBe(false); }); it("should return false for different nonces", () => { const nk1 = new NodeKey(100n, 5); const nk2 = new NodeKey(100n, 6); expect(nk1.equals(nk2)).toBe(false); }); }); describe("compare", () => { it("should return 0 for equal NodeKeys", () => { const nk1 = new NodeKey(100n, 5); const nk2 = new NodeKey(100n, 5); expect(nk1.compare(nk2)).toBe(0); }); it("should compare by version first", () => { const nk1 = new NodeKey(100n, 10); const nk2 = new NodeKey(101n, 5); expect(nk1.compare(nk2)).toBeLessThan(0); expect(nk2.compare(nk1)).toBeGreaterThan(0); }); it("should compare by nonce if versions equal", () => { const nk1 = new NodeKey(100n, 5); const nk2 = new NodeKey(100n, 10); expect(nk1.compare(nk2)).toBeLessThan(0); expect(nk2.compare(nk1)).toBeGreaterThan(0); }); it("should be transitive", () => { const nk1 = new NodeKey(100n, 1); const nk2 = new NodeKey(100n, 2); const nk3 = new NodeKey(100n, 3); expect(nk1.compare(nk2)).toBeLessThan(0); expect(nk2.compare(nk3)).toBeLessThan(0); expect(nk1.compare(nk3)).toBeLessThan(0); }); }); describe("toString", () => { it("should return string representation", () => { const nk = new NodeKey(100n, 5); expect(nk.toString()).toBe("v100n5"); }); it("should handle zero values", () => { const nk = new NodeKey(0n, 0); expect(nk.toString()).toBe("v0n0"); }); it("should handle large values", () => { const nk = new NodeKey(0xFFFFFFFFFFFFFFFFn, 0xFFFFFFFF); expect(nk.toString()).toBe("v18446744073709551615n4294967295"); }); }); describe("compatibility with Go implementation", () => { it("should match Go binary format for common values", () => { const testCases: Array<{ version: bigint nonce: number expectedHex: string }> = [ { version: 1n, nonce: 1, expectedHex: "000000000000000100000001", }, { version: 100n, nonce: 200, expectedHex: "0000000000000064000000c8", }, { version: 0x123456789ABCDEFn, nonce: 0x12345678, expectedHex: "0123456789abcdef12345678", }, ]; for (const { version, nonce, expectedHex, } of testCases) { const nk = new NodeKey(version, nonce); const bytes = nk.toBytes(); const actualHex = bytesToHex(bytes); expect(actualHex).toBe(expectedHex); } }); }); });