Files
xo-cash-utils/test/extended-json.test.ts

121 lines
4.6 KiB
TypeScript

import { expect, test } from 'vitest';
import { extendedJsonReviver, toExtendedJson, fromExtendedJson } from '../source/index.ts';
/**
* Tests that extendedJsonReviver reconstructs a positive bigint.
*/
const testReviverReconstructsPositiveBigint = (): void => {
// A positive bigint
const reconstructed = extendedJsonReviver('_', '<bigint: 42n>');
// The value is reconstructed as a bigint, not left as a string
expect(reconstructed).toBe(42n);
};
/**
* Tests that extendedJsonReviver reconstructs a negative bigint.
*/
const testReviverReconstructsNegativeBigint = (): void => {
// A negative bigint
const reconstructed = extendedJsonReviver('_', '<bigint: -7n>');
// The sign is preserved
expect(reconstructed).toBe(-7n);
};
/**
* Tests that extendedJsonReviver reconstructs zero as a bigint.
*/
const testReviverReconstructsZeroBigint = (): void => {
const reconstructed = extendedJsonReviver('_', '<bigint: 0n>');
// Zero is reconstructed as bigint 0n, not the number 0 or the string '0'
expect(reconstructed).toBe(0n);
};
/**
* Tests that extendedJsonReviver reconstructs a Uint8Array.
*/
const testReviverReconstructsUint8Array = (): void => {
// A Uint8Array
const reconstructed = extendedJsonReviver('_', '<uint8array: abcd>');
// The value is a Uint8Array with the correct bytes
expect(reconstructed).toStrictEqual(new Uint8Array([ 0xab, 0xcd ]));
};
/**
* Tests that extendedJsonReviver reconstructs an empty Uint8Array when the hex string is empty.
*/
const testReviverReconstructsEmptyUint8Array = (): void => {
const reconstructed = extendedJsonReviver('_', '<uint8array: >');
// An empty Uint8Array is returned, not null, undefined, or an empty string
expect(reconstructed).toStrictEqual(new Uint8Array(0));
};
/**
* Tests that extendedJsonReviver passes through plain strings.
*/
const testReviverPassesThroughPlainString = (): void => {
const reconstructed = extendedJsonReviver('_', 'just a string');
// The value is passed through unchanged
expect(reconstructed).toBe('just a string');
};
/**
* Tests that toExtendedJson serializes an object to a string.
*/
const testToExtendedJsonSerializesObject = (): void => {
// Define an object with a Uint8Array
const extendedObject = { bytes: new Uint8Array([ 0xab, 0xcd ]) };
// Serialize the object to a string
const serialized = toExtendedJson(extendedObject);
// The serialized string should contain the Uint8Array in Extended JSON format (Uint8Arrays are encoded as hex strings)
expect(serialized).toBe('{"bytes":"<uint8array: abcd>"}');
};
/**
* Tests that fromExtendedJson deserializes a string to an object.
*/
const testFromExtendedJsonDeserializesString = (): void => {
// Define a string in Extended JSON format that contains a Uint8Array
const serializedObject = '{"bytes":"<uint8array: abcd>"}';
// Deserialize the string to an object
const deserialized = fromExtendedJson(serializedObject);
// The deserialized object should contain the Uint8Array
expect(deserialized).toStrictEqual({ bytes: new Uint8Array([ 0xab, 0xcd ]) });
};
/**
* Tests that extendedJsonReviver passes through non-string values.
*/
const testReviverPassesThroughNonStringValues = (): void => {
// Numbers, booleans, null, and objects pass through unchanged
expect(extendedJsonReviver('_', 42)).toBe(42);
expect(extendedJsonReviver('_', true)).toBe(true);
expect(extendedJsonReviver('_', false)).toBe(false);
expect(extendedJsonReviver('_', null)).toBe(null);
expect(extendedJsonReviver('_', undefined)).toBe(undefined);
expect(extendedJsonReviver('_', { foo: 'bar' })).toStrictEqual({ foo: 'bar' });
};
const runTests = async (): Promise<void> => {
test('extendedJsonReviver: reconstructs a positive bigint', testReviverReconstructsPositiveBigint);
test('extendedJsonReviver: reconstructs a negative bigint', testReviverReconstructsNegativeBigint);
test('extendedJsonReviver: reconstructs zero as bigint', testReviverReconstructsZeroBigint);
test('extendedJsonReviver: reconstructs a Uint8Array from hex', testReviverReconstructsUint8Array);
test('extendedJsonReviver: reconstructs an empty Uint8Array', testReviverReconstructsEmptyUint8Array);
test('extendedJsonReviver: passes through plain strings', testReviverPassesThroughPlainString);
test('extendedJsonReviver: passes through non-string values', testReviverPassesThroughNonStringValues);
test('toExtendedJson: serializes an object to a string', testToExtendedJsonSerializesObject);
test('fromExtendedJson: deserializes a string to an object', testFromExtendedJsonDeserializesString);
};
await runTests();