Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it } from "vitest";
import { assertSize } from "./assert-size.js";

describe("assertSize", () => {
it("should not throw an error for hex strings within the specified size", () => {
expect(() => assertSize("0x1a4", { size: 2 })).not.toThrow();
expect(() => assertSize("0x1234", { size: 2 })).not.toThrow();
});

it("should throw an error for hex strings exceeding the specified size", () => {
expect(() => assertSize("0x123456", { size: 2 })).toThrow(
"Size overflow: 3 > 2",
);
expect(() => assertSize("0xabcdef", { size: 2 })).toThrow(
"Size overflow: 3 > 2",
);
});

it("should not throw an error for Uint8Array within the specified size", () => {
expect(() =>
assertSize(new Uint8Array([1, 2, 3]), { size: 3 }),
).not.toThrow();
expect(() => assertSize(new Uint8Array([]), { size: 0 })).not.toThrow();
});

it("should throw an error for Uint8Array exceeding the specified size", () => {
expect(() => assertSize(new Uint8Array([1, 2, 3, 4]), { size: 3 })).toThrow(
"Size overflow: 4 > 3",
);
});

it("should not throw an error for empty hex strings", () => {
expect(() => assertSize("0x", { size: 0 })).not.toThrow();
});

it("should handle boundary conditions correctly", () => {
expect(() => assertSize("0x12", { size: 1 })).not.toThrow();
expect(() => assertSize("0x12", { size: 0 })).toThrow(
"Size overflow: 1 > 0",
);
});

it("should not throw an error for hex strings exactly at the specified size", () => {
expect(() => assertSize("0x12", { size: 1 })).not.toThrow();
expect(() => assertSize("0x1234", { size: 2 })).not.toThrow();
});
});
36 changes: 36 additions & 0 deletions packages/thirdweb/src/utils/encoding/helpers/byte-size.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from "vitest";
import { byteSize } from "./byte-size.js";

describe("byteSize", () => {
it('should calculate the byte size for a valid hex string with "0x" prefix', () => {
expect(byteSize("0x1a4")).toBe(2); // "1a4" is 1 byte + 1 nibble = 2 bytes
expect(byteSize("0x123456")).toBe(3); // "123456" is 3 bytes
});

it("should return 0 for an empty hex string", () => {
expect(byteSize("0x")).toBe(0);
});

it("should calculate the byte size for a Uint8Array", () => {
expect(byteSize(new Uint8Array([1, 2, 3]))).toBe(3);
expect(byteSize(new Uint8Array([]))).toBe(0);
});

it('should handle a single byte hex string with "0x" prefix', () => {
expect(byteSize("0x1")).toBe(1); // "1" is half a byte, treated as 1 byte
expect(byteSize("0x12")).toBe(1); // "12" is 1 byte
});

it("should handle longer hex strings", () => {
expect(byteSize("0x1234567890abcdef")).toBe(8); // "1234567890abcdef" is 8 bytes
expect(byteSize("0xabcdef")).toBe(3); // "abcdef" is 3 bytes
});

it("should return 0 for non-hex string inputs in Uint8Array form", () => {
expect(byteSize(new Uint8Array([]))).toBe(0); // Empty Uint8Array
});

it('should handle strings that are not valid hex but start with "0x"', () => {
expect(byteSize("0xg")).toBe(1); // Not a valid hex string
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from "vitest";
import { charCodeToBase16 } from "./charcode-to-base-16.js";

describe("charCodeToBase16", () => {
it("should return correct values for digits (0-9)", () => {
expect(charCodeToBase16("0".charCodeAt(0))).toBe(0);
expect(charCodeToBase16("5".charCodeAt(0))).toBe(5);
expect(charCodeToBase16("9".charCodeAt(0))).toBe(9);
});

it("should return correct values for uppercase letters (A-F)", () => {
expect(charCodeToBase16("A".charCodeAt(0))).toBe(10);
expect(charCodeToBase16("C".charCodeAt(0))).toBe(12);
expect(charCodeToBase16("F".charCodeAt(0))).toBe(15);
});

it("should return correct values for lowercase letters (a-f)", () => {
expect(charCodeToBase16("a".charCodeAt(0))).toBe(10);
expect(charCodeToBase16("c".charCodeAt(0))).toBe(12);
expect(charCodeToBase16("f".charCodeAt(0))).toBe(15);
});

// Test cases for invalid inputs
it("should return undefined for invalid inputs", () => {
expect(charCodeToBase16("G".charCodeAt(0))).toBeUndefined();
expect(charCodeToBase16("z".charCodeAt(0))).toBeUndefined();
expect(charCodeToBase16(" ".charCodeAt(0))).toBeUndefined();
expect(charCodeToBase16("!".charCodeAt(0))).toBeUndefined();
});

it("should handle edge cases correctly", () => {
expect(charCodeToBase16("0".charCodeAt(0) - 1)).toBeUndefined(); // Just below '0'
expect(charCodeToBase16("9".charCodeAt(0) + 1)).toBeUndefined(); // Just above '9'
expect(charCodeToBase16("A".charCodeAt(0) - 1)).toBeUndefined(); // Just below 'A'
expect(charCodeToBase16("F".charCodeAt(0) + 1)).toBeUndefined(); // Just above 'F'
expect(charCodeToBase16("a".charCodeAt(0) - 1)).toBeUndefined(); // Just below 'a'
expect(charCodeToBase16("f".charCodeAt(0) + 1)).toBeUndefined(); // Just above 'f'
});
});
54 changes: 54 additions & 0 deletions packages/thirdweb/src/utils/encoding/helpers/is-hext.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { describe, expect, it } from "vitest";
import { isHex } from "./is-hex.js";

describe("isHex", () => {
it('should return true for valid hex strings with "0x" prefix in strict mode', () => {
expect(isHex("0x1a4", { strict: true })).toBe(true);
expect(isHex("0xABCDEF", { strict: true })).toBe(true);
});

it('should return false for hex strings without "0x" prefix in strict mode', () => {
expect(isHex("1a4", { strict: true })).toBe(false);
expect(isHex("abcdef", { strict: true })).toBe(false);
});

it('should return true for valid hex strings with or without "0x" prefix in non-strict mode', () => {
expect(isHex("0x1a4", { strict: false })).toBe(true);
expect(isHex("1a4", { strict: false })).toBe(false);
});

it("should return false for invalid hex strings in strict mode", () => {
expect(isHex("0x1g4", { strict: true })).toBe(false);
expect(isHex("0xZXY", { strict: true })).toBe(false);
});

it("should return false for invalid hex strings in non-strict mode", () => {
expect(isHex("0x1g4", { strict: false })).toBe(true);
expect(isHex("0xZXY", { strict: false })).toBe(true);
});

it("should return false for non-string inputs", () => {
expect(isHex(123, { strict: true })).toBe(false);
expect(isHex(null, { strict: true })).toBe(false);
expect(isHex(undefined, { strict: true })).toBe(false);
expect(isHex({}, { strict: true })).toBe(false);
});

it("should return false for empty strings", () => {
expect(isHex("", { strict: true })).toBe(false);
expect(isHex("", { strict: false })).toBe(false);
});

it("should return true for valid empty hex prefix in non-strict mode", () => {
expect(isHex("0x", { strict: false })).toBe(true);
});

it("should return true for valid empty hex prefix in strict mode", () => {
expect(isHex("0x", { strict: true })).toBe(true);
});

it("should use strict mode by default", () => {
expect(isHex("0x1a4")).toBe(true);
expect(isHex("1a4")).toBe(false);
});
});
Loading
Loading