1// Copyright (C) 2023 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import { 16 deserializeStateObject, 17 isSerializedBigint, 18 serializeStateObject, 19} from './upload_utils'; 20 21describe('isSerializedBigint', () => { 22 it('should return true for a valid serialized bigint', () => { 23 const value = { 24 __kind: 'bigint', 25 value: '1234567890', 26 }; 27 expect(isSerializedBigint(value)).toBeTruthy(); 28 }); 29 30 it('should return false for a null value', () => { 31 expect(isSerializedBigint(null)).toBeFalsy(); 32 }); 33 34 it('should return false for a non-object value', () => { 35 expect(isSerializedBigint(123)).toBeFalsy(); 36 }); 37 38 it('should return false for a non-serialized bigint value', () => { 39 const value = { 40 __kind: 'not-bigint', 41 value: '1234567890', 42 }; 43 expect(isSerializedBigint(value)).toBeFalsy(); 44 }); 45}); 46 47describe('serializeStateObject', () => { 48 it('should serialize a simple object', () => { 49 const object = { 50 a: 1, 51 b: 2, 52 c: 3, 53 }; 54 const expectedJson = `{"a":1,"b":2,"c":3}`; 55 expect(serializeStateObject(object)).toEqual(expectedJson); 56 }); 57 58 it('should serialize a bigint', () => { 59 const object = { 60 a: 123456789123456789n, 61 }; 62 const expectedJson = 63 `{"a":{"__kind":"bigint","value":"123456789123456789"}}`; 64 expect(serializeStateObject(object)).toEqual(expectedJson); 65 }); 66 67 it('should not serialize a non-serializable property', () => { 68 const object = { 69 a: 1, 70 b: 2, 71 c: 3, 72 nonSerializableState: 4, 73 }; 74 const expectedJson = `{"a":1,"b":2,"c":3}`; 75 expect(serializeStateObject(object)).toEqual(expectedJson); 76 }); 77}); 78 79describe('deserializeStateObject', () => { 80 it('should deserialize a simple object', () => { 81 const json = `{"a":1,"b":2,"c":3}`; 82 const expectedObject = { 83 a: 1, 84 b: 2, 85 c: 3, 86 }; 87 expect(deserializeStateObject(json)).toEqual(expectedObject); 88 }); 89 90 it('should deserialize a bigint', () => { 91 const json = `{"a":{"__kind":"bigint","value":"123456789123456789"}}`; 92 const expectedObject = { 93 a: 123456789123456789n, 94 }; 95 expect(deserializeStateObject(json)).toEqual(expectedObject); 96 }); 97 98 it('should deserialize a null', () => { 99 const json = `{"a":null}`; 100 const expectedObject = { 101 a: null, 102 }; 103 expect(deserializeStateObject(json)).toEqual(expectedObject); 104 }); 105}); 106