Skip to content

Commit 4677fbb

Browse files
test: Add unit tests for DataView and TypedArray serialization (#90)
- Adds tests for `DataView` serialization in `serializeValue`. - Adds tests for other `TypedArray` (e.g., `Float32Array`) serialization. - Adds tests for `ArrayBufferView` with offset and length. - Verifies that these types are correctly serialized to `Uint8Array` marker objects and deserialized back to `Uint8Array`. - Ensures coverage for existing `ArrayBuffer.isView` handling in `src/core/serialization.ts`. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent c93cc9e commit 4677fbb

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

tests/unit/serialization.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,46 @@ describe('RPC Serialization', () => {
2020
assert.strictEqual(typeof result.base64, 'string');
2121
});
2222

23+
it('should serialize DataView to marker object', () => {
24+
const buffer = new ArrayBuffer(4);
25+
const view = new DataView(buffer);
26+
view.setUint8(0, 10);
27+
view.setUint8(1, 20);
28+
29+
const result = serializeValue(view) as any;
30+
assert.strictEqual(result.__type, 'Uint8Array');
31+
32+
// Verify content
33+
const decoded = base64ToUint8Array(result.base64);
34+
assert.deepStrictEqual(decoded, new Uint8Array([10, 20, 0, 0]));
35+
});
36+
37+
it('should serialize other TypedArrays (Float32Array) to marker object', () => {
38+
const floatArr = new Float32Array([1.5]);
39+
const result = serializeValue(floatArr) as any;
40+
41+
assert.strictEqual(result.__type, 'Uint8Array');
42+
43+
// Verify content by reinterpreting bytes
44+
const decoded = base64ToUint8Array(result.base64);
45+
const decodedFloat = new Float32Array(decoded.buffer);
46+
assert.strictEqual(decodedFloat[0], 1.5);
47+
});
48+
49+
it('should handle ArrayBuffer views with offset and length', () => {
50+
// Create a buffer with [0, 1, 2, 3, 4, 5]
51+
const buffer = new Uint8Array([0, 1, 2, 3, 4, 5]).buffer;
52+
53+
// Create a view on the middle part: [2, 3]
54+
const view = new Uint8Array(buffer, 2, 2);
55+
56+
const result = serializeValue(view) as any;
57+
assert.strictEqual(result.__type, 'Uint8Array');
58+
59+
const decoded = base64ToUint8Array(result.base64);
60+
assert.deepStrictEqual(decoded, new Uint8Array([2, 3]));
61+
});
62+
2363
it('should recursively serialize objects', () => {
2464
const obj = {
2565
a: 1,

0 commit comments

Comments
 (0)