Merge branch 'development' into sse-and-backoff

This commit is contained in:
2026-07-20 10:21:33 +00:00
2 changed files with 51 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { extendedJsonReviver } from '../source/index.ts';
import { extendedJsonReviver, toExtendedJson, fromExtendedJson } from '../source/index.ts';
/**
* Tests that extendedJsonReviver reconstructs a positive bigint.
@@ -64,6 +64,34 @@ const testReviverPassesThroughPlainString = (): void => {
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.
*/
@@ -85,6 +113,8 @@ const runTests = async (): Promise<void> => {
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();