41 lines
1.7 KiB
TypeScript
41 lines
1.7 KiB
TypeScript
import {
|
|
protocolFieldsToJson,
|
|
protocolUint32,
|
|
protocolUint32FromDb,
|
|
protocolUint32ToDb,
|
|
parseProtocolSequence,
|
|
} from './protocol-uint32';
|
|
|
|
describe('CMPP unsigned protocol fields', () => {
|
|
it.each([0, 2147483647, 2147483648, 4294967295])(
|
|
'round trips %s without changing the JSON number contract',
|
|
(value) => {
|
|
expect(protocolUint32FromDb(protocolUint32ToDb(value))).toBe(value);
|
|
expect(
|
|
JSON.parse(
|
|
JSON.stringify(protocolFieldsToJson({ rows: [{ sequenceId: BigInt(value), ackResult: BigInt(value) }] })),
|
|
),
|
|
).toEqual({ rows: [{ sequenceId: value, ackResult: value }] });
|
|
},
|
|
);
|
|
it.each([-1, 4294967296, 1.5, NaN, Infinity, '', '0', ' ', {}, true])('rejects invalid wire value %s', (value) => {
|
|
expect(() => protocolUint32(value)).toThrow();
|
|
expect(() => protocolUint32ToDb(value)).toThrow();
|
|
});
|
|
it('preserves optional historical nulls and unrelated serializers', () => {
|
|
expect(protocolUint32ToDb(null)).toBeUndefined();
|
|
expect(protocolUint32FromDb(null)).toBeUndefined();
|
|
const date = new Date();
|
|
expect(
|
|
protocolFieldsToJson({ date, money: 10000n, sequenceId: null, gatewayMessageId: '18446744073709551615' }),
|
|
).toEqual({ date, money: 10000n, sequenceId: null, gatewayMessageId: '18446744073709551615' });
|
|
expect(() => protocolUint32FromDb(4294967296n)).toThrow();
|
|
});
|
|
it('distinguishes text zero from missing or malformed historical sequences', () => {
|
|
for (const value of [null, undefined, '', ' ', '-1', '1.5', '1e2', '4294967296'])
|
|
expect(parseProtocolSequence(value)).toBeUndefined();
|
|
expect(parseProtocolSequence('0')).toBe(0);
|
|
expect(parseProtocolSequence('4294967295')).toBe(4294967295);
|
|
});
|
|
});
|