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 */ 16import { 17 PacketType, 18 RpcPacket, 19} from 'pigweedjs/protos/pw_rpc/internal/packet_pb'; 20import { Status } from 'pigweedjs/pw_status'; 21 22import * as packets from './packets'; 23 24function addTestData(packet: RpcPacket) { 25 const payload = new RpcPacket(); 26 payload.setStatus(321); 27 packet.setChannelId(1); 28 packet.setServiceId(2); 29 packet.setMethodId(3); 30 packet.setCallId(4); 31 packet.setPayload(payload.serializeBinary()); 32} 33 34describe('Packets', () => { 35 beforeEach(() => { 36 // Do nothing. 37 }); 38 39 it('encodeRequest sets packet fields', () => { 40 const goldenRequest = new RpcPacket(); 41 goldenRequest.setType(PacketType.REQUEST); 42 addTestData(goldenRequest); 43 44 const dataPacket = new RpcPacket(); 45 dataPacket.setStatus(321); 46 const data = packets.encodeRequest([1, 2, 3, 4], dataPacket); 47 const packet = RpcPacket.deserializeBinary(data); 48 49 expect(packet.toObject()).toEqual(goldenRequest.toObject()); 50 }); 51 52 it('encodeResponse sets packet fields', () => { 53 const goldenResponse = new RpcPacket(); 54 goldenResponse.setType(PacketType.RESPONSE); 55 addTestData(goldenResponse); 56 57 const dataPacket = new RpcPacket(); 58 dataPacket.setStatus(321); 59 const data = packets.encodeResponse([1, 2, 3, 4], dataPacket); 60 const packet = RpcPacket.deserializeBinary(data); 61 62 expect(packet.toObject()).toEqual(goldenResponse.toObject()); 63 }); 64 65 it('encodesClientError sets packet fields', () => { 66 const packet = new RpcPacket(); 67 packet.setType(PacketType.REQUEST); 68 addTestData(packet); 69 const data = packets.encodeClientError(packet, Status.NOT_FOUND); 70 const errorPacket = RpcPacket.deserializeBinary(data); 71 72 const golden = new RpcPacket(); 73 golden.setType(PacketType.CLIENT_ERROR); 74 golden.setChannelId(1); 75 golden.setServiceId(2); 76 golden.setMethodId(3); 77 golden.setCallId(4); 78 golden.setStatus(Status.NOT_FOUND); 79 80 expect(errorPacket.toObject()).toEqual(golden.toObject()); 81 }); 82 83 it('encodeCancel sets packet fields', () => { 84 const goldenCancel = new RpcPacket(); 85 goldenCancel.setType(PacketType.CLIENT_ERROR); 86 goldenCancel.setStatus(Status.CANCELLED); 87 goldenCancel.setChannelId(1); 88 goldenCancel.setServiceId(2); 89 goldenCancel.setMethodId(3); 90 goldenCancel.setCallId(4); 91 92 const data = packets.encodeCancel([1, 2, 3, 4]); 93 const packet = RpcPacket.deserializeBinary(data); 94 expect(packet.toObject()).toEqual(goldenCancel.toObject()); 95 }); 96 97 it('decode with serialized request returns request', () => { 98 const request = new RpcPacket(); 99 request.setType(PacketType.REQUEST); 100 addTestData(request); 101 102 expect(request.toObject()).toEqual( 103 packets.decode(request.serializeBinary()).toObject(), 104 ); 105 }); 106 107 it('forServer correctly handles RESPONSE and REQUEST types', () => { 108 const request = new RpcPacket(); 109 request.setType(PacketType.REQUEST); 110 const response = new RpcPacket(); 111 response.setType(PacketType.RESPONSE); 112 113 expect(packets.forServer(request)).toBe(true); 114 expect(packets.forServer(response)).toBe(false); 115 }); 116}); 117