Skip to content

Commit

Permalink
Crash Test and Clear File Function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Rıza Kat committed Sep 4, 2024
1 parent f605360 commit 160f4c7
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 27 deletions.
45 changes: 42 additions & 3 deletions test/helpers/helper_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,39 @@ function readBulkReqQueue() {
return a;
}

function clearStorageJsonFiles(directory, files) {
files.forEach((file) => {
const filePath = path.join(directory, file);
// Check if the file exists before attempting to clear it
fs.access(filePath, fs.constants.F_OK, (err) => {
if (!err) {
// Write an empty JSON object to the file
// logs can be added here
fs.writeFile(filePath, JSON.stringify({}), 'utf8', (er) => { });
}
});
});
}

function clearJsonFiles(isBulk = true, customDir = '') {
// Determine the directory and files based on isBulk
const directory = customDir || (isBulk ? path.join(__dirname, 'bulk_data') : path.join(__dirname, 'data'));
const files = isBulk ? [
'__cly_id.json',
'__cly_req_queue.json',
'__cly_bulk_event.json',
] : [
'__cly_event.json',
'__cly_id_type.json',
'__cly_id.json',
'__cly_queue.json',
'__cly_remote_configs.json',
];

// Clear files in the specified directory
clearStorageJsonFiles(directory, files);
}

// queue files clearing logic
function clearStorage(keepID) {
keepID = keepID || false;
Expand Down Expand Up @@ -77,11 +110,16 @@ function eventValidator(eventObject, eventQueue, time) {
}
assert.equal(eventObject.dur, eventQueue.dur);
}
// check if segmentation exists. If it is add test(s)
// check if segmentation exists. If it does, add tests
if (typeof eventObject.segmentation !== 'undefined') {
// loop through segmentation keys and create tets
// loop through segmentation keys and create tests
for (var key in eventObject.segmentation) {
assert.equal(eventObject.segmentation[key], eventQueue.segmentation[key]);
if (Array.isArray(eventObject.segmentation[key]) || typeof eventObject.segmentation[key] === 'object') {
assert.deepStrictEqual(eventObject.segmentation[key], eventQueue.segmentation[key]);
}
else {
assert.equal(eventObject.segmentation[key], eventQueue.segmentation[key]);
}
}
}
// common parameter validation
Expand Down Expand Up @@ -202,6 +240,7 @@ module.exports = {
readBulkReqQueue,
eventValidator,
crashRequestValidator,
clearJsonFiles,
sessionRequestValidator,
userDetailRequestValidator,
viewEventValidator,
Expand Down
130 changes: 106 additions & 24 deletions test/tests_bulk.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-console */
/* global runthis */
const assert = require("assert");
const CountlyBulk = require("../lib/countly-bulk");
var hp = require("./helpers/helper_functions");
Expand All @@ -13,24 +14,67 @@ function createBulk(storagePath) {
return bulk;
}

function validateCrash(validator, nonfatal) {
assert.ok(validator.crash._os);
assert.ok(validator.crash._os_version);
assert.ok(validator.crash._error);
assert.ok(validator.crash._app_version);
assert.ok(typeof validator.crash._run !== 'undefined');
assert.ok(typeof validator.crash._custom !== 'undefined');
assert.equal(nonfatal, validator.crash._nonfatal);
assert.equal(true, validator.crash._javascript);
assert.equal(true, validator.crash._not_os_specific);
}

// note: this can replace the current one in the helper functions
function validateUserDetails(actual, expected) {
const keys = ['name', 'username', 'email', 'organization', 'phone', 'picture', 'gender', 'byear', 'custom'];
let isValid = true;

keys.forEach((key) => {
if (JSON.stringify(actual[key]) !== JSON.stringify(expected[key])) {
if (typeof actual[key] === 'object' && actual[key] !== null) {
if (Array.isArray(actual[key])) {
if (!Array.isArray(expected[key]) || JSON.stringify(actual[key]) !== JSON.stringify(expected[key])) {
console.error(`Mismatch for key "${key}": expected "${JSON.stringify(expected[key])}", but got "${JSON.stringify(actual[key])}"`);
isValid = false;
}
}
else {
if (JSON.stringify(actual[key]) !== JSON.stringify(expected[key])) {
console.error(`Mismatch for key "${key}": expected "${JSON.stringify(expected[key])}", but got "${JSON.stringify(actual[key])}"`);
isValid = false;
}
}
}
else if (actual[key] !== expected[key]) {
console.error(`Mismatch for key "${key}": expected "${expected[key]}", but got "${actual[key]}"`);
isValid = false;
}
});
// Validate nested custom object separately
const customKeys = Object.keys(expected.custom || {});
customKeys.forEach((key) => {
if (actual.custom[key] !== expected.custom[key]) {
console.error(`Mismatch in custom object for key "${key}": expected "${expected.custom[key]}", but got "${actual.custom[key]}"`);
isValid = false;
}
});
if (expected.custom && actual.custom) {
const customKeys = Object.keys(expected.custom);
customKeys.forEach((key) => {
if (typeof actual.custom[key] === 'object' && actual.custom[key] !== null) {
if (Array.isArray(actual.custom[key])) {
if (!Array.isArray(expected.custom[key]) || JSON.stringify(actual.custom[key]) !== JSON.stringify(expected.custom[key])) {
console.error(`Mismatch in custom object for key "${key}": expected "${JSON.stringify(expected.custom[key])}", but got "${JSON.stringify(actual.custom[key])}"`);
isValid = false;
}
}
else {
if (JSON.stringify(actual.custom[key]) !== JSON.stringify(expected.custom[key])) {
console.error(`Mismatch in custom object for key "${key}": expected "${JSON.stringify(expected.custom[key])}", but got "${JSON.stringify(actual.custom[key])}"`);
isValid = false;
}
}
}
else if (actual.custom[key] !== expected.custom[key]) {
console.error(`Mismatch in custom object for key "${key}": expected "${expected.custom[key]}", but got "${actual.custom[key]}"`);
isValid = false;
}
});
}
return isValid;
}

Expand All @@ -40,8 +84,13 @@ var eventObj = {
sum: 3.14,
dur: 2000,
segmentation: {
app_version: "1.0",
country: "Zambia",
string_value: "example",
number_value: 42,
boolean_value: true,
array_value: ["item1", "item2"],
object_value: { nested_key: "nested_value" },
null_value: null,
undefined_value: undefined,
},
};

Expand All @@ -55,28 +104,33 @@ var userDetailObj = {
gender: "Female",
byear: 1992, // birth year
custom: {
key1: "value1 segment",
key2: "value2 segment",
string_value: "example",
number_value: 42,
boolean_value: true,
array_value: ["item1", "item2"],
object_value: { nested_key: "nested_value" },
null_value: null,
undefined_value: undefined,
},
};

describe("Bulk Tests", () => {
it("1- Bulk with Default Storage Path", (done) => {
storage.resetStorage();
hp.clearJsonFiles(true);
createBulk();
assert.equal(storage.getStoragePath(), "../bulk_data/");
done();
});

it("2- Bulk with Custom Storage Path", (done) => {
storage.resetStorage();
hp.clearJsonFiles(true);
createBulk("../test/customStorageDirectory/");
assert.equal(storage.getStoragePath(), "../test/customStorageDirectory/");
done();
});

it("3- Bulk add_user with Record Event", (done) => {
storage.resetStorage();
hp.clearJsonFiles(true);
var bulk = createBulk();
var user = bulk.add_user({ device_id: "testUser1" });
user.add_event(eventObj);
Expand All @@ -90,7 +144,7 @@ describe("Bulk Tests", () => {
});

it("4- Bulk add_user with User Details", (done) => {
storage.resetStorage();
hp.clearJsonFiles(true);
var bulk = createBulk();
var user = bulk.add_user({ device_id: "testUser2" });
user.user_details(userDetailObj);
Expand All @@ -109,18 +163,46 @@ describe("Bulk Tests", () => {
});

it("5- Bulk add_request", (done) => {
storage.resetStorage();
hp.clearJsonFiles(true);
var bulk = createBulk();
bulk.add_request({ device_id: "TestUser4" });
bulk.add_request({ device_id: "TestUser3" });
setTimeout(() => {
var reqQueue = hp.readBulkReqQueue();
var testUser3Request = reqQueue.find((req) => req.device_id === "TestUser3");
assert.ok(testUser3Request);
assert.strictEqual(testUser3Request.device_id, "TestUser3");
assert.strictEqual(testUser3Request.app_key, "YOUR_APP_KEY");
assert.strictEqual(testUser3Request.sdk_name, "javascript_native_nodejs_bulk");
done();
}, hp.sWait);
});

it("6- Bulk add_user Report Crash", (done) => {
hp.clearJsonFiles(true);
var bulk = createBulk();
var user = bulk.add_user({ device_id: "TestUser4" });
try {
runthis();
}
catch (ex) {
user.report_crash({
_os: "Android",
_os_version: "7",
_error: "Stack trace goes here",
_app_version: "1.0",
_run: 12345,
_custom: {},
_nonfatal: true,
_javascript: true,
_not_os_specific: true,
}, 1500645200);
}
// read event queue
setTimeout(() => {
var reqQueue = hp.readBulkReqQueue();
var testUser4Request = reqQueue.find((req) => req.device_id === "TestUser4");
assert.ok(testUser4Request);
assert.strictEqual(testUser4Request.device_id, "TestUser4");
assert.strictEqual(testUser4Request.app_key, "YOUR_APP_KEY");
assert.strictEqual(testUser4Request.sdk_name, "javascript_native_nodejs_bulk");
validateCrash(testUser4Request, true);
done();
}, hp.sWait);
});
});
/* eslint-enable no-console */
});

0 comments on commit 160f4c7

Please sign in to comment.