harb/kraiken-lib/src/tests/subgraph.test.ts
openhands 26a9645b1f fix: kraiken-lib: fix broken tests + raise coverage to 95% (#286)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-26 00:10:45 +00:00

72 lines
2.5 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import { bytesToUint256LittleEndian, uint256ToBytesLittleEndian } from '../subgraph.js';
describe('uint256ToBytesLittleEndian', () => {
test('converts zero to four zero bytes', () => {
expect(uint256ToBytesLittleEndian(0n)).toEqual(new Uint8Array([0, 0, 0, 0]));
});
test('converts 1 to [1, 0, 0, 0]', () => {
expect(uint256ToBytesLittleEndian(1n)).toEqual(new Uint8Array([1, 0, 0, 0]));
});
test('converts 3 to [3, 0, 0, 0]', () => {
const result = uint256ToBytesLittleEndian(3n);
let hexString = '0x';
for (let i = 0; i < result.length; i++) {
hexString += result[i].toString(16).padStart(2, '0');
}
expect(hexString).toEqual('0x03000000');
});
test('converts 256 to [0, 1, 0, 0] (second byte)', () => {
expect(uint256ToBytesLittleEndian(256n)).toEqual(new Uint8Array([0, 1, 0, 0]));
});
test('converts 65536 to [0, 0, 1, 0] (third byte)', () => {
expect(uint256ToBytesLittleEndian(65536n)).toEqual(new Uint8Array([0, 0, 1, 0]));
});
test('converts 16777216 (2^24) to [0, 0, 0, 1] (fourth byte)', () => {
expect(uint256ToBytesLittleEndian(16777216n)).toEqual(new Uint8Array([0, 0, 0, 1]));
});
test('returns a 4-byte Uint8Array', () => {
const result = uint256ToBytesLittleEndian(12345n);
expect(result).toBeInstanceOf(Uint8Array);
expect(result.length).toBe(4);
});
});
describe('bytesToUint256LittleEndian', () => {
test('converts four zero bytes to zero', () => {
expect(bytesToUint256LittleEndian(new Uint8Array([0, 0, 0, 0]))).toBe(0n);
});
test('converts [1, 0, 0, 0] to 1', () => {
expect(bytesToUint256LittleEndian(new Uint8Array([1, 0, 0, 0]))).toBe(1n);
});
test('converts [0, 1, 0, 0] to 256', () => {
expect(bytesToUint256LittleEndian(new Uint8Array([0, 1, 0, 0]))).toBe(256n);
});
test('converts [255, 255, 255, 255] to 0xffffffff', () => {
expect(bytesToUint256LittleEndian(new Uint8Array([255, 255, 255, 255]))).toBe(0xffffffffn);
});
test('returns a bigint', () => {
const result = bytesToUint256LittleEndian(new Uint8Array([3, 0, 0, 0]));
expect(typeof result).toBe('bigint');
});
});
describe('roundtrip: uint256ToBytesLittleEndian -> bytesToUint256LittleEndian', () => {
test('roundtrip preserves value for common position IDs', () => {
const values = [0n, 1n, 2n, 3n, 100n, 255n, 256n, 12345n, 65535n, 16777215n];
for (const val of values) {
const bytes = uint256ToBytesLittleEndian(val);
expect(bytesToUint256LittleEndian(bytes)).toBe(val);
}
});
});