/** * Tests for key formatting utilities. */ import { describe, expect, it, } from "vitest"; import { fastKeyFormat, FastPrefixFormatter, formatNodeKey, KeyFormat, legacyNodeKeyFormat, legacyOrphanKeyFormat, legacyRootKeyFormat, metadataKeyFormat, nodeKeyFormat, parseNodeKey, } from "./key-format.js"; describe("KeyFormat", () => { describe("basic functionality", () => { it("should create key with single segment", () => { const kf = new KeyFormat("t".charCodeAt(0), 8); const key = kf.key(100n); expect(key[0]).toBe("t".charCodeAt(0)); expect(key.length).toBe(9); // 1 prefix + 8 bytes // Check big-endian encoding of 100 expect(key[8]).toBe(100); for (let i = 1; i < 8; i++) { expect(key[i]).toBe(0); } }); it("should create key with multiple segments", () => { const kf = new KeyFormat("e".charCodeAt(0), 8, 8, 8); const key = kf.key(100n, 200n, 400n); expect(key[0]).toBe("e".charCodeAt(0)); expect(key.length).toBe(25); // 1 + 8 + 8 + 8 // Check values expect(key[8]).toBe(100); expect(key[16]).toBe(200); expect(key[24]).toBe(400 & 0xFF); expect(key[23]).toBe((400 >> 8) & 0xFF); }); it("should handle byte array segments", () => { const kf = new KeyFormat("b".charCodeAt(0), 4, 8); const bytes1 = new Uint8Array([ 1, 2, 3, 4, ]); const bytes2 = new Uint8Array([ 5, 6, 7, 8, 9, 10, 11, 12, ]); const key = kf.keyBytes(bytes1, bytes2); expect(key[0]).toBe("b".charCodeAt(0)); expect(key.length).toBe(13); expect(Array.from(key.slice(1, 5))).toEqual([ 1, 2, 3, 4, ]); expect(Array.from(key.slice(5, 13))).toEqual([ 5, 6, 7, 8, 9, 10, 11, 12, ]); }); it("should handle unbounded last segment", () => { const kf = new KeyFormat("u".charCodeAt(0), 8, 0); // Short unbounded segment const key1 = kf.keyBytes( new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 1, ]), new Uint8Array([ 1, 2, 3, ]), ); expect(key1.length).toBe(12); // 1 + 8 + 3 // Longer unbounded segment const key2 = kf.keyBytes( new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 1, ]), new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ]), ); expect(key2.length).toBe(19); // 1 + 8 + 10 }); it("should throw if non-last segment is 0", () => { expect(() => new KeyFormat("x".charCodeAt(0), 0, 8)).toThrow( "Only the last item in a key format can be 0", ); }); it("should throw if segment is too long", () => { const kf = new KeyFormat("x".charCodeAt(0), 4); const longSegment = new Uint8Array([ 1, 2, 3, 4, 5, ]); expect(() => kf.keyBytes(longSegment)).toThrow( "Length of segment 0 (5 bytes) is longer than the 4 bytes required", ); }); it("should throw if too many arguments provided", () => { const kf = new KeyFormat("x".charCodeAt(0), 8, 8); expect(() => kf.key(1n, 2n, 3n)).toThrow( "keyFormat.key() is provided with 3 args but format only has 2 segments", ); }); }); describe("scanBytes", () => { it("should scan single segment", () => { const kf = new KeyFormat("t".charCodeAt(0), 8); const key = kf.key(12345n); const segments = kf.scanBytes(key); expect(segments.length).toBe(1); expect(segments[0].length).toBe(8); }); it("should scan multiple segments", () => { const kf = new KeyFormat("m".charCodeAt(0), 8, 4, 8); const key = kf.key(100n, 200, 300n); const segments = kf.scanBytes(key); expect(segments.length).toBe(3); expect(segments[0].length).toBe(8); expect(segments[1].length).toBe(4); expect(segments[2].length).toBe(8); }); it("should scan unbounded segment", () => { const kf = new KeyFormat("u".charCodeAt(0), 8, 0); const unboundedData = new Uint8Array([ 1, 2, 3, 4, 5, ]); const key = kf.keyBytes( new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 1, ]), unboundedData, ); const segments = kf.scanBytes(key); expect(segments.length).toBe(2); expect(segments[0].length).toBe(8); expect(Array.from(segments[1])).toEqual([ 1, 2, 3, 4, 5, ]); }); it("should handle partial keys", () => { const kf = new KeyFormat("p".charCodeAt(0), 8, 8, 8); const key = new Uint8Array(9); // Only prefix + first segment key[0] = "p".charCodeAt(0); const segments = kf.scanBytes(key); expect(segments.length).toBe(1); }); }); describe("scan values", () => { it("should scan bigint values", () => { const kf = new KeyFormat("b".charCodeAt(0), 8, 8); const key = kf.key(100n, 200n); const [a, b] = kf.scan(key, ["bigint", "bigint"]); expect(a).toBe(100n); expect(b).toBe(200n); }); it("should scan uint32 values", () => { const kf = new KeyFormat("u".charCodeAt(0), 4, 4); const key = kf.key(100, 4294967295); const [a, b] = kf.scan(key, ["uint32", "uint32"]); expect(a).toBe(100); expect(b).toBe(4294967295); }); it("should scan int32 values", () => { const kf = new KeyFormat("i".charCodeAt(0), 4, 4); const key = kf.key(-100, 2147483647); const [a, b] = kf.scan(key, ["int32", "int32"]); expect(a).toBe(-100); expect(b).toBe(2147483647); }); it("should scan bytes", () => { const kf = new KeyFormat("b".charCodeAt(0), 8, 0); const data = new Uint8Array([ 1, 2, 3, 4, 5, ]); const key = kf.keyBytes( new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 1, ]), data, ); const [version, bytes] = kf.scan(key, ["bigint", "bytes"]); expect(version).toBe(1n); expect(bytes).toBeInstanceOf(Uint8Array); expect(Array.from(bytes as Uint8Array)).toEqual([ 1, 2, 3, 4, 5, ]); }); }); describe("negative values", () => { it("should handle negative bigint values", () => { const kf = new KeyFormat("n".charCodeAt(0), 8, 8); const key = kf.key(-100n, -200n); // Scan back const [a, b] = kf.scan(key, ["bigint", "bigint"]); // In two's complement, these will be large positive values when read as unsigned // We need to check if they need to be converted back const expected_a = (1n << 64n) - 100n; const expected_b = (1n << 64n) - 200n; // Our scan returns them as large positive values expect(a).toBe(expected_a); expect(b).toBe(expected_b); }); }); describe("prefix and length", () => { it("should return correct prefix string", () => { const kf = new KeyFormat("x".charCodeAt(0), 8); expect(kf.getPrefix()).toBe("x"); }); it("should return correct length", () => { const kf = new KeyFormat("x".charCodeAt(0), 8, 4, 8); expect(kf.getLength()).toBe(21); // 1 + 8 + 4 + 8 }); }); }); describe("FastPrefixFormatter", () => { it("should create and scan keys", () => { const formatter = new FastPrefixFormatter("n".charCodeAt(0), 32); const hash = new Uint8Array(32); for (let i = 0; i < 32; i++) { hash[i] = i; } const key = formatter.key(hash); expect(key[0]).toBe("n".charCodeAt(0)); expect(key.length).toBe(33); const scanned = formatter.scan(key); expect(Array.from(scanned)).toEqual(Array.from(hash)); }); it("should handle variable length when length is 0", () => { const formatter = new FastPrefixFormatter("v".charCodeAt(0), 0); const short = new Uint8Array([ 1, 2, 3, ]); const key1 = formatter.key(short); expect(key1.length).toBe(4); const long = new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, ]); const key2 = formatter.key(long); expect(key2.length).toBe(9); }); it("should throw on wrong length", () => { const formatter = new FastPrefixFormatter("f".charCodeAt(0), 8); const wrongLength = new Uint8Array([ 1, 2, 3, ]); expect(() => formatter.key(wrongLength)).toThrow( "Expected value of length 8, got 3", ); }); it("should throw on invalid key when scanning", () => { const formatter = new FastPrefixFormatter("x".charCodeAt(0), 8); const invalidKey = new Uint8Array([ 0, 1, 2, 3, ]); expect(() => formatter.scan(invalidKey)).toThrow("Invalid key format"); }); }); describe("formatNodeKey and parseNodeKey", () => { it("should format and parse node keys correctly", () => { const version = 12345n; const nonce = 67890; const key = formatNodeKey(version, nonce); expect(key[0]).toBe("s".charCodeAt(0)); expect(key.length).toBe(13); // 1 + 8 + 4 const { version: v, nonce: n, } = parseNodeKey(key); expect(v).toBe(version); expect(n).toBe(nonce); }); it("should handle negative versions", () => { const version = -100n; const nonce = 42; const key = formatNodeKey(version, nonce); const { version: v, nonce: n, } = parseNodeKey(key); expect(v).toBe(version); expect(n).toBe(nonce); }); it("should handle negative nonces", () => { const version = 100n; const nonce = -42; const key = formatNodeKey(version, nonce); const { version: v, nonce: n, } = parseNodeKey(key); expect(v).toBe(version); expect(n).toBe(nonce); }); it("should handle max values", () => { const version = (1n << 63n) - 1n; // Max signed int64 const nonce = 2147483647; // Max int32 const key = formatNodeKey(version, nonce); const { version: v, nonce: n, } = parseNodeKey(key); expect(v).toBe(version); expect(n).toBe(nonce); }); }); describe("predefined key formats", () => { it("should have correct node key format", () => { expect(nodeKeyFormat).toBeInstanceOf(FastPrefixFormatter); const key = formatNodeKey(1n, 1); expect(key[0]).toBe("s".charCodeAt(0)); }); it("should have correct fast key format", () => { expect(fastKeyFormat).toBeInstanceOf(KeyFormat); const key = fastKeyFormat.key(new Uint8Array([ 1, 2, 3, ])); expect(key[0]).toBe("f".charCodeAt(0)); expect(key.slice(1)).toEqual(new Uint8Array([ 1, 2, 3, ])); }); it("should have correct metadata key format", () => { expect(metadataKeyFormat).toBeInstanceOf(KeyFormat); const key = metadataKeyFormat.key(new Uint8Array([ 1, 2, 3, ])); expect(key[0]).toBe("m".charCodeAt(0)); }); it("should have correct legacy node key format", () => { expect(legacyNodeKeyFormat).toBeInstanceOf(FastPrefixFormatter); const hash = new Uint8Array(32); const key = legacyNodeKeyFormat.key(hash); expect(key[0]).toBe("n".charCodeAt(0)); }); it("should have correct legacy orphan key format", () => { expect(legacyOrphanKeyFormat).toBeInstanceOf(KeyFormat); const hash = new Uint8Array(32); const key = legacyOrphanKeyFormat.key(100n, 50n, hash); expect(key[0]).toBe("o".charCodeAt(0)); expect(key.length).toBe(49); // 1 + 8 + 8 + 32 }); it("should have correct legacy root key format", () => { expect(legacyRootKeyFormat).toBeInstanceOf(KeyFormat); const key = legacyRootKeyFormat.key(42n); expect(key[0]).toBe("r".charCodeAt(0)); expect(key.length).toBe(9); }); }); describe("compatibility tests from Go", () => { it("should match Go test vectors for simple 3 int key format", () => { const kf = new KeyFormat("e".charCodeAt(0), 8, 8, 8); // Empty test vector const empty = kf.keyBytes(); expect(Array.from(empty)).toEqual(["e".charCodeAt(0)]); // Three byte test vector const threeBytes = kf.keyBytes(new Uint8Array([ 1, 2, 3, ])); expect(Array.from(threeBytes)).toEqual([ "e".charCodeAt(0), 0, 0, 0, 0, 0, 1, 2, 3, ]); // Eight byte test vector const eightBytes = kf.keyBytes(new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, ])); expect(Array.from(eightBytes)).toEqual([ "e".charCodeAt(0), 1, 2, 3, 4, 5, 6, 7, 8, ]); // Multiple segments const multi = kf.keyBytes( new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, ]), new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, ]), new Uint8Array([ 1, 1, 2, 2, 3, 3, ]), ); expect(Array.from(multi)).toEqual([ "e".charCodeAt(0), 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 1, 1, 2, 2, 3, 3, ]); }); it("should match Go test vectors for zero suffix key format", () => { const kf = new KeyFormat("e".charCodeAt(0), 8, 0); // Variable length suffix const variableLength = kf.keyBytes( new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, ]), new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, ]), ); expect(Array.from(variableLength)).toEqual([ "e".charCodeAt(0), 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, ]); // String suffix const stringData = new TextEncoder().encode("hellohello"); const withString = kf.keyBytes( new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, ]), stringData, ); expect(Array.from(withString)).toEqual([ "e".charCodeAt(0), 1, 2, 3, 4, 5, 6, 7, 8, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, ]); }); it("should match Go test for scan and key", () => { const kf = new KeyFormat("e".charCodeAt(0), 8, 8, 8); const key = new Uint8Array([ "e".charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 1, 144, // 400 in big-endian ]); const generatedKey = kf.key(100n, 200n, 400n); expect(Array.from(generatedKey)).toEqual(Array.from(key)); const [ a, b, c, ] = kf.scan(key, [ "bigint", "bigint", "bigint", ]); expect(a).toBe(100n); expect(b).toBe(200n); expect(c).toBe(400n); // Partial scan const [ ao, bo, bs, ] = kf.scan(key, [ "bigint", "bigint", "bytes", ]); expect(ao).toBe(100n); expect(bo).toBe(200n); expect(Array.from(bs as Uint8Array)).toEqual([ 0, 0, 0, 0, 0, 0, 1, 144, ]); }); });