1// Copyright 2022 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://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, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14 15/* eslint-env browser */ 16 17import { Decoder } from './decoder'; 18import * as protocol from './protocol'; 19import * as util from './util'; 20 21const FLAG = Uint8Array.from([protocol.FLAG]); 22 23function withFCS(data: Uint8Array): Uint8Array { 24 return util.concatenate(data, protocol.frameCheckSequence(data)); 25} 26 27function withFlags(data: Uint8Array): Uint8Array { 28 return util.concatenate(FLAG, data, FLAG); 29} 30 31describe('Decoder', () => { 32 let decoder: Decoder; 33 let textEncoder: TextEncoder; 34 35 beforeEach(() => { 36 decoder = new Decoder(); 37 textEncoder = new TextEncoder(); 38 }); 39 40 it('parses a correct UI frame peoperly', () => { 41 const expectedData = textEncoder.encode('123456789'); 42 const expectedAddress = 128; 43 44 const frameData = withFlags( 45 withFCS(textEncoder.encode('\x00\x03\x03123456789')), 46 ); 47 48 const frames = Array.from(decoder.process(frameData)); 49 50 expect(frames.length).toEqual(1); 51 expect(frames[0].address).toEqual(expectedAddress); 52 expect(frames[0].data).toEqual(expectedData); 53 expect(frames[0].ok()).toBe(true); 54 }); 55}); 56