From 516e2e89dc4fa291d16b45a464a5fa28c9b537db Mon Sep 17 00:00:00 2001 From: T1B0 Date: Fri, 19 Jan 2024 14:28:26 +0100 Subject: [PATCH] fix: fix var naming, add some test --- src/tcp.client.ts | 12 +++---- src/wire/token/delete-token.command.test.ts | 39 +++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/wire/token/delete-token.command.test.ts diff --git a/src/tcp.client.ts b/src/tcp.client.ts index 5bb467d..f594bf2 100644 --- a/src/tcp.client.ts +++ b/src/tcp.client.ts @@ -41,17 +41,17 @@ export const sendCommandWithResponse = (s: Socket) => (command: number, payload: Buffer): Promise => { const payloadSize = payload.length + COMMAND_LENGTH; - let buffer = Buffer.alloc(8); + const head = Buffer.alloc(8); - buffer.writeUint32LE(payloadSize, 0); - buffer.writeUint32LE(command, 4); + head.writeUint32LE(payloadSize, 0); + head.writeUint32LE(command, 4); console.log( - '==> CMD', buffer.readInt32LE(4), + '==> CMD', head.readInt32LE(4), translateCommandCode(command), - 'LENGTH', buffer.readInt32LE(0)); + 'LENGTH', head.readInt32LE(0)); - const cmd = Buffer.concat([buffer, payload]); + const cmd = Buffer.concat([head, payload]); console.log('==> sending cmd', command, cmd /**, cmd.toString()*/); console.log('==> socket write', s.write(cmd)); diff --git a/src/wire/token/delete-token.command.test.ts b/src/wire/token/delete-token.command.test.ts new file mode 100644 index 0000000..d5bdfad --- /dev/null +++ b/src/wire/token/delete-token.command.test.ts @@ -0,0 +1,39 @@ + +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { DELETE_TOKEN } from './delete-token.command.js'; + +describe('DeleteToken', () => { + + describe('serialize', () => { + + const name = 'test-token'; + + it('serialize 1 name into buffer', () => { + + assert.deepEqual( + DELETE_TOKEN.serialize(name).length, + 1 + name.length + ); + }); + + it('throw on name < 1', () => { + assert.throws( + () => DELETE_TOKEN.serialize('') + ); + }); + + it("throw on name > 255 bytes", () => { + assert.throws( + () => DELETE_TOKEN.serialize("YoLo".repeat(65)) + ); + }); + + it("throw on name > 255 bytes - utf8 version", () => { + assert.throws( + () => DELETE_TOKEN.serialize("¥Ø£Ø".repeat(33)) + ); + }); + + }); +});