|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it } from 'node:test'; |
| 21 | +import assert from 'node:assert/strict'; |
| 22 | +import { handleResponse, deserializeVoidResponse, deserializeStatusResponse } from './client.utils.js'; |
| 23 | + |
| 24 | +const SUCCESS = 0; |
| 25 | +const ERROR = 1; |
| 26 | + |
| 27 | +describe('handleResponse', () => { |
| 28 | + |
| 29 | + it('bounds data to the length field, not the full buffer', () => { |
| 30 | + // Server says: status=0, length=0, no payload. |
| 31 | + // But the raw buffer has 4 trailing bytes (e.g. start of next response). |
| 32 | + const buf = Buffer.alloc(12); |
| 33 | + buf.writeUInt32LE(SUCCESS, 0); // status |
| 34 | + buf.writeUInt32LE(0, 4); // length = 0 (void response) |
| 35 | + buf.writeUInt32LE(42, 8); // trailing bytes — NOT part of this response |
| 36 | + |
| 37 | + const r = handleResponse(buf); |
| 38 | + assert.equal(r.data.length, 0); |
| 39 | + }); |
| 40 | + |
| 41 | + it('deserializeVoidResponse returns true for a valid void response with trailing buffer bytes', () => { |
| 42 | + const buf = Buffer.alloc(12); |
| 43 | + buf.writeUInt32LE(SUCCESS, 0); |
| 44 | + buf.writeUInt32LE(0, 4); // length = 0 |
| 45 | + buf.writeUInt32LE(42, 8); // trailing bytes |
| 46 | + |
| 47 | + const r = handleResponse(buf); |
| 48 | + assert.equal(deserializeVoidResponse(r), true); |
| 49 | + }); |
| 50 | + |
| 51 | +}); |
| 52 | + |
| 53 | +describe('deserializeStatusResponse', () => { |
| 54 | + |
| 55 | + it('returns true when status is SUCCESS and data is empty', () => { |
| 56 | + const r = { status: SUCCESS, length: 0, data: Buffer.alloc(0) }; |
| 57 | + assert.equal(deserializeStatusResponse(r), true); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns true when status is SUCCESS and data is non-empty (e.g. SendMessages server payload)', () => { |
| 61 | + // Key difference from deserializeVoidResponse: non-empty data is accepted. |
| 62 | + const r = { status: SUCCESS, length: 4, data: Buffer.from([1, 2, 3, 4]) }; |
| 63 | + assert.equal(deserializeStatusResponse(r), true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns false when status is an error code', () => { |
| 67 | + const r = { status: ERROR, length: 0, data: Buffer.alloc(0) }; |
| 68 | + assert.equal(deserializeStatusResponse(r), false); |
| 69 | + }); |
| 70 | + |
| 71 | +}); |
0 commit comments