// Copyright (C) 2026 Whiterun LLC, // This software is licensed under the GNU Lesser General Public License (LGPL), version 3.0 or later. // A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/lgpl-3.0.html import { describe, it, expect } from "vitest"; import { binToHex } from "@bitauth/libauth"; import { parseExtendedJson, parseExtendedJsonValue, isExtendedJsonFormat, toUint8Array, toBigInt, } from "./serialize.js"; describe("wc-serialize", () => { describe("parseExtendedJsonValue", () => { it("parses bigint format", () => { expect(parseExtendedJsonValue("")).toBe(546n); expect(parseExtendedJsonValue("")).toBe(0n); expect(parseExtendedJsonValue("")).toBe(100000000n); }); it("parses Uint8Array format", () => { const result = parseExtendedJsonValue(""); expect(result).toBeInstanceOf(Uint8Array); expect(binToHex(result as Uint8Array)).toBe("abcd"); }); it("returns original string for non-extended values", () => { expect(parseExtendedJsonValue("hello")).toBe("hello"); expect(parseExtendedJsonValue("123")).toBe("123"); }); }); describe("isExtendedJsonFormat", () => { it("detects bigint format", () => { expect(isExtendedJsonFormat("")).toBe(true); }); it("detects Uint8Array format", () => { expect(isExtendedJsonFormat("")).toBe(true); }); it("rejects plain strings", () => { expect(isExtendedJsonFormat("hello")).toBe(false); expect(isExtendedJsonFormat("123")).toBe(false); }); }); describe("parseExtendedJson", () => { it("parses JSON with mixed extended values", () => { const json = JSON.stringify({ value: "", data: "", name: "test", count: 42, }); const result = parseExtendedJson(json); expect(result.value).toBe(200000n); expect(result.data).toBeInstanceOf(Uint8Array); expect(binToHex(result.data)).toBe("76a9"); expect(result.name).toBe("test"); expect(result.count).toBe(42); }); }); describe("toUint8Array", () => { it("passes through Uint8Array", () => { const bytes = new Uint8Array([1, 2, 3]); expect(toUint8Array(bytes)).toBe(bytes); }); it("converts hex string", () => { const result = toUint8Array("abcd"); expect(binToHex(result)).toBe("abcd"); }); it("converts extended JSON format", () => { const result = toUint8Array(""); expect(binToHex(result)).toBe("abcd"); }); }); describe("toBigInt", () => { it("passes through bigint", () => { expect(toBigInt(546n)).toBe(546n); }); it("converts number", () => { expect(toBigInt(546)).toBe(546n); }); it("converts numeric string", () => { expect(toBigInt("546")).toBe(546n); }); it("converts extended JSON format", () => { expect(toBigInt("")).toBe(546n); }); }); });