UNPKG

@zondax/ledger-js

Version:

TS / Node API for apps running on Ledger devices

57 lines 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /****************************************************************************** * (c) 2018 - 2024 Zondax AG * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *****************************************************************************/ const buffer_1 = require("buffer"); const consts_1 = require("./consts"); const payload_1 = require("./payload"); const responseError_1 = require("./responseError"); describe('ResponsePayload', () => { let payload; let responsePayload; beforeEach(() => { payload = buffer_1.Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05]); responsePayload = new payload_1.ResponsePayload(payload); }); test('getCompleteBuffer should return a complete buffer', () => { expect(responsePayload.getCompleteBuffer()).toEqual(buffer_1.Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05])); }); test('getAvailableBuffer should return the available buffer', () => { responsePayload.readBytes(3); expect(responsePayload.getAvailableBuffer()).toEqual(buffer_1.Buffer.from([0x04, 0x05])); }); test('readBytes should return the correct bytes and increase offset', () => { const readBuffer = responsePayload.readBytes(2); expect(readBuffer).toEqual(buffer_1.Buffer.from([0x01, 0x02])); expect(responsePayload.readBytes(1)).toEqual(buffer_1.Buffer.from([0x03])); }); test('skipBytes should increase the offset correctly', () => { responsePayload.skipBytes(2); expect(responsePayload.readBytes(1)).toEqual(buffer_1.Buffer.from([0x03])); }); test('resetOffset should reset the offset to zero', () => { responsePayload.readBytes(3); responsePayload.resetOffset(); expect(responsePayload.readBytes(2)).toEqual(buffer_1.Buffer.from([0x01, 0x02])); }); test('readBytes should throw an error when reading beyond the buffer length', () => { expect(() => responsePayload.readBytes(10)).toThrow(new responseError_1.ResponseError(consts_1.LedgerError.UnknownError, 'Attempt to read beyond buffer length')); }); test('skipBytes should throw an error when skipping beyond the buffer length', () => { expect(() => responsePayload.skipBytes(10)).toThrow(new responseError_1.ResponseError(consts_1.LedgerError.UnknownError, 'Attempt to skip beyond buffer length')); }); }); //# sourceMappingURL=payload.test.js.map