|
| 1 | +import { SOLANA_ERROR__CODECS__EXPECTED_DECODER_TO_CONSUME_ENTIRE_BYTE_ARRAY, SolanaError } from '@solana/errors'; |
| 2 | + |
| 3 | +import { createDecoder, Decoder } from '../codec'; |
| 4 | +import { createDecoderThatConsumesEntireByteArray } from '../decoder-entire-byte-array'; |
| 5 | + |
| 6 | +describe('createDecoderThatConsumesEntireByteArray', () => { |
| 7 | + // Given: a decoder that consumes exactly 4 bytes |
| 8 | + const outputNumber = 1234; |
| 9 | + const innerDecoder: Decoder<number> = createDecoder({ |
| 10 | + fixedSize: 4, |
| 11 | + read: (_bytes, offset) => [outputNumber, offset + 4], |
| 12 | + }); |
| 13 | + |
| 14 | + describe('decode function', () => { |
| 15 | + describe('with no offset', () => { |
| 16 | + it('returns the same value as the inner decoder when the entire byte array is consumed', () => { |
| 17 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 18 | + const bytes = new Uint8Array(4); |
| 19 | + const value = decoder.decode(bytes); |
| 20 | + expect(value).toBe(outputNumber); |
| 21 | + }); |
| 22 | + |
| 23 | + it('fatals when the inner decoder does not consume the entire byte array', () => { |
| 24 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 25 | + const bytes = new Uint8Array(5); |
| 26 | + expect(() => decoder.decode(bytes)).toThrow( |
| 27 | + new SolanaError(SOLANA_ERROR__CODECS__EXPECTED_DECODER_TO_CONSUME_ENTIRE_BYTE_ARRAY, { |
| 28 | + expectedLength: 4, |
| 29 | + numExcessBytes: 1, |
| 30 | + }), |
| 31 | + ); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('with an offset', () => { |
| 36 | + it('returns the same value as the inner decoder when the entire byte array is consumed', () => { |
| 37 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 38 | + const bytes = new Uint8Array(6); |
| 39 | + const value = decoder.decode(bytes, 2); |
| 40 | + expect(value).toBe(outputNumber); |
| 41 | + }); |
| 42 | + |
| 43 | + it('fatals when the inner decoder does not consume the entire byte array', () => { |
| 44 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 45 | + const bytes = new Uint8Array(7); |
| 46 | + expect(() => decoder.decode(bytes, 2)).toThrow( |
| 47 | + new SolanaError(SOLANA_ERROR__CODECS__EXPECTED_DECODER_TO_CONSUME_ENTIRE_BYTE_ARRAY, { |
| 48 | + expectedLength: 6, |
| 49 | + numExcessBytes: 1, |
| 50 | + }), |
| 51 | + ); |
| 52 | + }); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('read function', () => { |
| 57 | + describe('with no offset', () => { |
| 58 | + it('returns the same value as the inner decoder when the entire byte array is consumed', () => { |
| 59 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 60 | + const bytes = new Uint8Array(4); |
| 61 | + const [value] = decoder.read(bytes, 0); |
| 62 | + expect(value).toBe(outputNumber); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns the same offset as the inner decoder when the entire byte array is consumed', () => { |
| 66 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 67 | + const bytes = new Uint8Array(4); |
| 68 | + const [, offset] = decoder.read(bytes, 0); |
| 69 | + expect(offset).toBe(4); |
| 70 | + }); |
| 71 | + |
| 72 | + it('fatals when the inner decoder does not consume the entire byte array', () => { |
| 73 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 74 | + const bytes = new Uint8Array(5); |
| 75 | + expect(() => decoder.read(bytes, 0)).toThrow( |
| 76 | + new SolanaError(SOLANA_ERROR__CODECS__EXPECTED_DECODER_TO_CONSUME_ENTIRE_BYTE_ARRAY, { |
| 77 | + expectedLength: 4, |
| 78 | + numExcessBytes: 1, |
| 79 | + }), |
| 80 | + ); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('with an offset', () => { |
| 85 | + it('returns the same value as the inner decoder when the entire byte array is consumed', () => { |
| 86 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 87 | + const bytes = new Uint8Array(6); |
| 88 | + const [value] = decoder.read(bytes, 2); |
| 89 | + expect(value).toBe(outputNumber); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns the same offset as the inner decoder when the entire byte array is consumed', () => { |
| 93 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 94 | + const bytes = new Uint8Array(6); |
| 95 | + const [, offset] = decoder.read(bytes, 2); |
| 96 | + expect(offset).toBe(6); |
| 97 | + }); |
| 98 | + |
| 99 | + it('fatals when the inner decoder does not consume the entire byte array', () => { |
| 100 | + const decoder = createDecoderThatConsumesEntireByteArray(innerDecoder); |
| 101 | + const bytes = new Uint8Array(7); |
| 102 | + expect(() => decoder.read(bytes, 2)).toThrow( |
| 103 | + new SolanaError(SOLANA_ERROR__CODECS__EXPECTED_DECODER_TO_CONSUME_ENTIRE_BYTE_ARRAY, { |
| 104 | + expectedLength: 6, |
| 105 | + numExcessBytes: 1, |
| 106 | + }), |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments