Add tests

Sponsored by codiumai
This commit is contained in:
2023-12-05 14:58:30 +01:00
parent f8a8365f90
commit ba0d9bf430
5 changed files with 2145 additions and 48 deletions

View File

@@ -1,26 +1,24 @@
module.exports = { module.exports = {
"env": { env: {
"node": true, node: true,
"commonjs": true, commonjs: true,
"es2021": true es2021: true,
}, "jest/globals": true,
"extends": "eslint:recommended", },
"overrides": [ extends: "eslint:recommended",
{ overrides: [
"env": { {
"node": true env: {
}, node: true,
"files": [ },
".eslintrc.{js,cjs}" files: [".eslintrc.{js,cjs}"],
], parserOptions: {
"parserOptions": { sourceType: "script",
"sourceType": "script" },
} },
} ],
], parserOptions: {
"parserOptions": { ecmaVersion: "latest",
"ecmaVersion": "latest" },
}, rules: {},
"rules": { };
}
}

View File

@@ -8,7 +8,8 @@
"build-windows-client": "nexe -i client.js -o out/client-windows -t windows-x86-18.18.2", "build-windows-client": "nexe -i client.js -o out/client-windows -t windows-x86-18.18.2",
"build-linux-server": "nexe -i server.js -o out/server-linux -t linux-x64-18.18.2", "build-linux-server": "nexe -i server.js -o out/server-linux -t linux-x64-18.18.2",
"build-windows-server": "nexe -i server.js -o out/server-windows -t windows-x86-18.18.2", "build-windows-server": "nexe -i server.js -o out/server-windows -t windows-x86-18.18.2",
"build": "sh build.sh" "build": "sh build.sh",
"test": "jest"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
@@ -22,6 +23,8 @@
"winston": "^3.11.0" "winston": "^3.11.0"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^8.53.0" "@types/jest": "^29.5.10",
"eslint": "^8.55.0",
"jest": "^29.7.0"
} }
} }

2040
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

94
tests/utils.test.js Normal file
View File

@@ -0,0 +1,94 @@
const smpp = require("smpp");
const { splitToParts, verifyExists, getCharacterSizeForEncoding } = require("../utils");
describe("splitToParts", () => {
// A pdu is expected to be one part if it has less than 160 characters and is encoded using GSM7 (data_coding = null or 0)
// Given a pdu with short_message length less than 160 chars, it should return an array with a single pdu.
it("should return an array with a single pdu when short_message length is less than or equal to maxMessageSizeBits", () => {
const pdu = new smpp.PDU("deliver_sm", {
short_message: "test message",
});
const result = splitToParts(pdu);
expect(result.length).toBe(1);
});
// Given a pdu with short_message length greater than 160 chars, it should return an array with 2 pdus.
it("should return an array with two pdus when short_message length is greater than maxMessageSizeBits and less than or equal to maxMessageSizeBits * 2", () => {
const pdu = new smpp.PDU("deliver_sm", {
short_message: "c".repeat(200),
});
const result = splitToParts(pdu);
expect(result.length).toBe(2);
});
// Given a pdu with short_message length greater than 320 chars, it should return an array with 2 pdus.
it("should return an array with three pdus when short_message length is greater than maxMessageSizeBits * 2 and less than or equal to maxMessageSizeBits * 3", () => {
const pdu = new smpp.PDU("deliver_sm", {
short_message: "c".repeat(400),
});
const result = splitToParts(pdu);
expect(result.length).toBe(3);
});
// Given a pdu with short_message length equal to 0, it should return an empty array.
it("should return an empty array when short_message length is equal to 0", () => {
const pdu = new smpp.PDU("deliver_sm", {
short_message: "",
});
const result = splitToParts(pdu);
expect(result.length).toBe(0);
});
// Given a pdu with short_message length equal to 320, it should return an array with two pdus.
it("should return an array with two pdus when short_message length is equal to maxMessageSizeBits", () => {
const pdu = new smpp.PDU("deliver_sm", {
short_message: "c".repeat(320),
});
const result = splitToParts(pdu);
expect(result.length).toBe(2);
});
});
describe("getCharacterSizeForEncoding", () => {
// Returns 7 when data_coding is 0
it("should return 7 when data_coding is 0", () => {
const pdu = { data_coding: 0 };
const result = getCharacterSizeForEncoding(pdu);
expect(result).toBe(7);
});
// Returns 8 when data_coding is 1
it("should return 8 when data_coding is 1", () => {
const pdu = { data_coding: 1 };
const result = getCharacterSizeForEncoding(pdu);
expect(result).toBe(8);
});
// Returns 16 when data_coding is 8
it("should return 16 when data_coding is 8", () => {
const pdu = { data_coding: 8 };
const result = getCharacterSizeForEncoding(pdu);
expect(result).toBe(16);
});
// Returns 7 when data_coding is null
it("should return 0 when data_coding is null", () => {
const pdu = { data_coding: null };
const result = getCharacterSizeForEncoding(pdu);
expect(result).toBe(7);
});
// Returns 0 when data_coding is not a number
it("should return 0 when data_coding is not a number", () => {
const pdu = { data_coding: "abc" };
const result = getCharacterSizeForEncoding(pdu);
expect(result).toBe(0);
});
// Returns 0 when data_coding is negative
it("should return 0 when data_coding is negative", () => {
const pdu = { data_coding: -1 };
const result = getCharacterSizeForEncoding(pdu);
expect(result).toBe(0);
});
});

View File

@@ -36,7 +36,7 @@ function getCharacterSizeForEncoding(pdu) {
return characterSizeBits; return characterSizeBits;
} }
const maxMessageSizeBits = 1072; const maxMessageSizeBits = 1120;
function splitToParts(pdu) { function splitToParts(pdu) {
const charSize = getCharacterSizeForEncoding(pdu); const charSize = getCharacterSizeForEncoding(pdu);
const maxMessageLength = maxMessageSizeBits / charSize; const maxMessageLength = maxMessageSizeBits / charSize;
@@ -99,4 +99,4 @@ async function sendPdu(session, pdu, logger, uselongsms) {
}); });
} }
module.exports = { verifyDefaults, verifyExists, sendPdu }; module.exports = { verifyDefaults, verifyExists, sendPdu, splitToParts, getCharacterSizeForEncoding };