@pyro-labs/utils
Version:
Utility functions for Pyro SDK
102 lines • 4.45 kB
JavaScript
;
/**
* Tests for @pyro/utils
*/
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const index_1 = require("./index");
(0, vitest_1.describe)("toHex", () => {
(0, vitest_1.it)("should convert bigint to hex", () => {
(0, vitest_1.expect)((0, index_1.toHex)(BigInt(255))).toBe("0xff");
(0, vitest_1.expect)((0, index_1.toHex)(BigInt(0))).toBe("0x0");
(0, vitest_1.expect)((0, index_1.toHex)(BigInt(256))).toBe("0x100");
});
(0, vitest_1.it)("should convert number to hex", () => {
(0, vitest_1.expect)((0, index_1.toHex)(255)).toBe("0xff");
(0, vitest_1.expect)((0, index_1.toHex)(0)).toBe("0x0");
});
(0, vitest_1.it)("should convert string to hex", () => {
(0, vitest_1.expect)((0, index_1.toHex)("255")).toBe("0xff");
(0, vitest_1.expect)((0, index_1.toHex)("0x100")).toBe("0x100");
});
(0, vitest_1.it)("should convert Uint8Array to hex", () => {
const bytes = new Uint8Array([255, 0, 128]);
(0, vitest_1.expect)((0, index_1.toHex)(bytes)).toBe("0xff0080");
});
(0, vitest_1.it)("should throw error for invalid type", () => {
(0, vitest_1.expect)(() => (0, index_1.toHex)(null)).toThrow();
(0, vitest_1.expect)(() => (0, index_1.toHex)(undefined)).toThrow();
});
});
(0, vitest_1.describe)("hexToBigInt", () => {
(0, vitest_1.it)("should convert hex string to bigint", () => {
(0, vitest_1.expect)((0, index_1.hexToBigInt)("0xff")).toBe(BigInt(255));
(0, vitest_1.expect)((0, index_1.hexToBigInt)("ff")).toBe(BigInt(255));
(0, vitest_1.expect)((0, index_1.hexToBigInt)("0x0")).toBe(BigInt(0));
});
});
(0, vitest_1.describe)("bytesToHex", () => {
(0, vitest_1.it)("should convert bytes to hex", () => {
const bytes = new Uint8Array([255, 0, 128]);
(0, vitest_1.expect)((0, index_1.bytesToHex)(bytes)).toBe("0xff0080");
});
(0, vitest_1.it)("should handle empty array", () => {
(0, vitest_1.expect)((0, index_1.bytesToHex)(new Uint8Array([]))).toBe("0x");
});
});
(0, vitest_1.describe)("hexToBytes", () => {
(0, vitest_1.it)("should convert hex to bytes", () => {
const bytes = (0, index_1.hexToBytes)("0xff0080");
(0, vitest_1.expect)(bytes).toEqual(new Uint8Array([255, 0, 128]));
});
(0, vitest_1.it)("should handle hex without 0x prefix", () => {
const bytes = (0, index_1.hexToBytes)("ff0080");
(0, vitest_1.expect)(bytes).toEqual(new Uint8Array([255, 0, 128]));
});
});
(0, vitest_1.describe)("sleep", () => {
(0, vitest_1.it)("should wait for specified time", async () => {
const start = Date.now();
await (0, index_1.sleep)(100);
const end = Date.now();
(0, vitest_1.expect)(end - start).toBeGreaterThanOrEqual(90);
(0, vitest_1.expect)(end - start).toBeLessThan(200);
});
});
(0, vitest_1.describe)("retry", () => {
(0, vitest_1.it)("should retry on failure", async () => {
let attempts = 0;
const fn = async () => {
attempts++;
if (attempts < 3) {
throw new Error("Failed");
}
return "success";
};
const result = await (0, index_1.retry)(fn, 3, 10);
(0, vitest_1.expect)(result).toBe("success");
(0, vitest_1.expect)(attempts).toBe(3);
});
(0, vitest_1.it)("should throw after max retries", async () => {
const fn = async () => {
throw new Error("Always fails");
};
await (0, vitest_1.expect)((0, index_1.retry)(fn, 2, 10)).rejects.toThrow("Always fails");
});
(0, vitest_1.it)("should succeed on first try", async () => {
const fn = async () => "success";
const result = await (0, index_1.retry)(fn, 3, 10);
(0, vitest_1.expect)(result).toBe("success");
});
});
(0, vitest_1.describe)("logger", () => {
(0, vitest_1.it)("should have default log level", () => {
(0, vitest_1.expect)(index_1.logger.getLevel()).toBe(index_1.LogLevel.INFO);
});
(0, vitest_1.it)("should allow setting log level", () => {
index_1.logger.setLevel(index_1.LogLevel.DEBUG);
(0, vitest_1.expect)(index_1.logger.getLevel()).toBe(index_1.LogLevel.DEBUG);
index_1.logger.setLevel(index_1.LogLevel.INFO); // Reset
});
});
//# sourceMappingURL=index.test.js.map