|
| 1 | +import {RmRecordDecoder} from '../RmRecordDecoder'; |
| 2 | + |
| 3 | +describe('RmRecordDecoder', () => { |
| 4 | + describe('.readRecord()', () => { |
| 5 | + test('returns undefined when no data available', () => { |
| 6 | + const decoder = new RmRecordDecoder(); |
| 7 | + const result = decoder.readRecord(); |
| 8 | + expect(result).toBeUndefined(); |
| 9 | + }); |
| 10 | + |
| 11 | + test('decodes empty record', () => { |
| 12 | + const decoder = new RmRecordDecoder(); |
| 13 | + decoder.push(new Uint8Array([0, 0, 0, 0])); |
| 14 | + expect(decoder.readRecord()).toBeUndefined(); |
| 15 | + }); |
| 16 | + |
| 17 | + test('decodes empty record', () => { |
| 18 | + const decoder = new RmRecordDecoder(); |
| 19 | + decoder.push(new Uint8Array([0, 0, 0, 0, 0])); |
| 20 | + expect(decoder.readRecord()).toBeUndefined(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('decodes empty record - 2', () => { |
| 24 | + const decoder = new RmRecordDecoder(); |
| 25 | + expect(decoder.readRecord()).toBeUndefined(); |
| 26 | + decoder.push(new Uint8Array([0])); |
| 27 | + expect(decoder.readRecord()).toBeUndefined(); |
| 28 | + decoder.push(new Uint8Array([0])); |
| 29 | + expect(decoder.readRecord()).toBeUndefined(); |
| 30 | + decoder.push(new Uint8Array([0])); |
| 31 | + expect(decoder.readRecord()).toBeUndefined(); |
| 32 | + decoder.push(new Uint8Array([0])); |
| 33 | + expect(decoder.readRecord()).toBeUndefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + test('decodes two records streamed one byte at a time', () => { |
| 37 | + const decoder = new RmRecordDecoder(); |
| 38 | + expect(decoder.readRecord()).toBeUndefined(); |
| 39 | + decoder.push(new Uint8Array([0b10000000])); |
| 40 | + expect(decoder.readRecord()).toBeUndefined(); |
| 41 | + decoder.push(new Uint8Array([0])); |
| 42 | + expect(decoder.readRecord()).toBeUndefined(); |
| 43 | + decoder.push(new Uint8Array([0])); |
| 44 | + expect(decoder.readRecord()).toBeUndefined(); |
| 45 | + decoder.push(new Uint8Array([1])); |
| 46 | + expect(decoder.readRecord()).toBeUndefined(); |
| 47 | + decoder.push(new Uint8Array([42])); |
| 48 | + expect(decoder.readRecord()?.buf()).toEqual(new Uint8Array([42])); |
| 49 | + expect(decoder.readRecord()).toBeUndefined(); |
| 50 | + decoder.push(new Uint8Array([0b10000000, 0, 0])); |
| 51 | + expect(decoder.readRecord()).toBeUndefined(); |
| 52 | + expect(decoder.readRecord()).toBeUndefined(); |
| 53 | + decoder.push(new Uint8Array([1, 43])); |
| 54 | + expect(decoder.readRecord()?.buf()).toEqual(new Uint8Array([43])); |
| 55 | + expect(decoder.readRecord()).toBeUndefined(); |
| 56 | + }); |
| 57 | + |
| 58 | + test('decodes single-byte record', () => { |
| 59 | + const decoder = new RmRecordDecoder(); |
| 60 | + decoder.push(new Uint8Array([0b10000000, 0, 0, 1, 42])); |
| 61 | + const result = decoder.readRecord()?.buf(); |
| 62 | + expect(result).toBeInstanceOf(Uint8Array); |
| 63 | + expect(result!.length).toBe(1); |
| 64 | + expect(result![0]).toBe(42); |
| 65 | + }); |
| 66 | + |
| 67 | + test('decodes multi-byte record', () => { |
| 68 | + const decoder = new RmRecordDecoder(); |
| 69 | + const data = new Uint8Array([1, 2, 3, 4, 5]); |
| 70 | + decoder.push(new Uint8Array([0b10000000, 0, 0, data.length, ...data])); |
| 71 | + const result = decoder.readRecord()?.buf(); |
| 72 | + expect(result).toBeInstanceOf(Uint8Array); |
| 73 | + expect(result!.length).toBe(data.length); |
| 74 | + expect(result).toEqual(data); |
| 75 | + }); |
| 76 | + |
| 77 | + test('decodes ASCII string data', () => { |
| 78 | + const text = 'hello world'; |
| 79 | + const data = new TextEncoder().encode(text); |
| 80 | + const decoder = new RmRecordDecoder(); |
| 81 | + decoder.push(new Uint8Array([0b10000000, 0, 0, data.length, ...data])); |
| 82 | + const result = decoder.readRecord()?.buf(); |
| 83 | + expect(result).toBeInstanceOf(Uint8Array); |
| 84 | + expect(result!.length).toBe(data.length); |
| 85 | + expect(result).toEqual(data); |
| 86 | + }); |
| 87 | + |
| 88 | + test('decodes large record', () => { |
| 89 | + const size = 10000; |
| 90 | + const data = new Uint8Array(size); |
| 91 | + for (let i = 0; i < size; i++) data[i] = i % 256; |
| 92 | + const decoder = new RmRecordDecoder(); |
| 93 | + decoder.push(new Uint8Array([0b10000000, (size >> 16) & 0xff, (size >> 8) & 0xff, size & 0xff, ...data])); |
| 94 | + const result = decoder.readRecord()?.buf(); |
| 95 | + expect(result).toBeInstanceOf(Uint8Array); |
| 96 | + expect(result!.length).toBe(data.length); |
| 97 | + expect(result).toEqual(data); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('fragmented records', () => { |
| 102 | + test('decodes record with two fragments', () => { |
| 103 | + const part1 = new Uint8Array([1, 2, 3]); |
| 104 | + const part2 = new Uint8Array([4, 5, 6]); |
| 105 | + const decoder = new RmRecordDecoder(); |
| 106 | + decoder.push(new Uint8Array([0b00000000, 0, 0, part1.length, ...part1])); |
| 107 | + expect(decoder.readRecord()).toBeUndefined(); |
| 108 | + decoder.push(new Uint8Array([0b10000000, 0, 0, part2.length, ...part2])); |
| 109 | + const result = decoder.readRecord()?.buf(); |
| 110 | + expect(result).toBeInstanceOf(Uint8Array); |
| 111 | + expect(result!.length).toBe(part1.length + part2.length); |
| 112 | + expect(result).toEqual(new Uint8Array([...part1, ...part2])); |
| 113 | + }); |
| 114 | + |
| 115 | + test('decodes record with three fragments', () => { |
| 116 | + const part1 = new Uint8Array([1, 2]); |
| 117 | + const part2 = new Uint8Array([3, 4]); |
| 118 | + const part3 = new Uint8Array([5, 6]); |
| 119 | + const decoder = new RmRecordDecoder(); |
| 120 | + decoder.push(new Uint8Array([0b00000000, 0, 0, part1.length, ...part1])); |
| 121 | + expect(decoder.readRecord()).toBeUndefined(); |
| 122 | + decoder.push(new Uint8Array([0b00000000, 0, 0, part2.length, ...part2])); |
| 123 | + expect(decoder.readRecord()).toBeUndefined(); |
| 124 | + decoder.push(new Uint8Array([0b10000000, 0, 0, part3.length, ...part3])); |
| 125 | + const result = decoder.readRecord()?.buf(); |
| 126 | + expect(result).toBeInstanceOf(Uint8Array); |
| 127 | + expect(result!.length).toBe(part1.length + part2.length + part3.length); |
| 128 | + expect(result).toEqual(new Uint8Array([...part1, ...part2, ...part3])); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments