diff --git a/lib/countly-common.js b/lib/countly-common.js index 7a961e5..c721bb1 100644 --- a/lib/countly-common.js +++ b/lib/countly-common.js @@ -54,7 +54,6 @@ var cc = { storageTypeEnums: { FILE: "file", MEMORY: "memory", - CUSTOM: "custom", }, /** diff --git a/lib/countly-storage.js b/lib/countly-storage.js index aad6d10..22d7a68 100644 --- a/lib/countly-storage.js +++ b/lib/countly-storage.js @@ -131,7 +131,7 @@ var initStorage = function(userPath, storageType, isBulk = false, persistQueue = if (storageType === StorageTypes.MEMORY) { storageMethod = memoryStorage; } - else if (storageType === StorageTypes.CUSTOM) { + else if (customStorageMethod) { if (hasValidMethods(customStorageMethod)) { storageMethod = customStorageMethod; if (userPath) { @@ -322,7 +322,12 @@ var getStorageType = function() { if (storageMethod === memoryStorage) { return StorageTypes.MEMORY; } - return StorageTypes.FILE; + + if (storageMethod === fileStorage) { + return StorageTypes.FILE; + } + + return null; }; module.exports = { diff --git a/lib/countly.js b/lib/countly.js index 8492bec..51d94d1 100644 --- a/lib/countly.js +++ b/lib/countly.js @@ -335,6 +335,7 @@ Countly.Bulk = Bulk; // device_id Countly.device_id = undefined; + Countly.device_id_type = undefined; Countly.remote_config = undefined; Countly.require_consent = false; Countly.debug = undefined; diff --git a/test/helpers/helper_functions.js b/test/helpers/helper_functions.js index 1728687..83a0777 100644 --- a/test/helpers/helper_functions.js +++ b/test/helpers/helper_functions.js @@ -56,23 +56,30 @@ function clearStorage(keepID = false, isBulk = false, customDir = '') { // Resets Countly Countly.halt(true); // Determine the directory based on isBulk or customDir - const eventDirectory = customDir || (isBulk ? bulkEventDir : eventDir); - const reqDirectory = customDir || (isBulk ? bulkQueueDir : reqDir); - // Helper function to remove directory and files + const eventDirectory = isBulk ? bulkEventDir : eventDir; + const reqDirectory = isBulk ? bulkQueueDir : reqDir; function removeDir(directory) { - if (fs.existsSync(directory)) { - fs.rmSync(directory, { recursive: true, force: true }); + // Remove the .json extension from the directory name, since it will be added in path.resolve + var filePath = path.resolve(__dirname, `${directory}`); + if (fs.existsSync(filePath)) { + fs.rmSync(filePath, { recursive: true, force: true }); } } // Remove event directory if it exists removeDir(eventDirectory); // Remove request directory if it exists removeDir(reqDirectory); + if (customDir) { + removeDir(customDir); + } // Optionally keep the ID directory if (!keepID) { removeDir(idDir); removeDir(idTypeDir); } + return new Promise((resolve, reject) => { + resolve("string"); + }); } /** * bunch of tests specifically gathered for testing events diff --git a/test/tests_storage.js b/test/tests_storage.js index 6c03700..6e3c66d 100644 --- a/test/tests_storage.js +++ b/test/tests_storage.js @@ -1,6 +1,3 @@ -const fs = require('fs'); -const path = require('path'); - const assert = require("assert"); var Countly = require("../lib/countly"); var storage = require("../lib/countly-storage"); @@ -9,702 +6,172 @@ var hp = require("./helpers/helper_functions"); const StorageTypes = cc.storageTypeEnums; -// example event object to use -var eventObj = { - key: "storage_check", - count: 5, - sum: 3.14, - dur: 2000, - segmentation: { - app_version: "1.0", - country: "Zambia", - }, -}; - -var userDetailObj = { - name: "Akira Kurosawa", - username: "a_kurosawa", - email: "akira.kurosawa@filmlegacy.com", - organization: "Toho Studios", - phone: "+81312345678", - picture: "https://example.com/profile_images/akira_kurosawa.jpg", - gender: "Male", - byear: 1910, - custom: { - "known for": "Film Director", - "notable works": "Seven Samurai, Rashomon, Ran", - }, -}; - -// init function -function initMain(device_id) { - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - interval: 10000, - max_events: -1, - device_id: device_id, - }); -} -// TODO: move these to helpers to reduce duplication -function validateSdkGeneratedId(providedDeviceId) { - assert.ok(providedDeviceId); - assert.equal(providedDeviceId.length, 36); - assert.ok(cc.isUUID(providedDeviceId)); -} -function checkRequestsForT(queue, expectedInternalType) { - for (var i = 0; i < queue.length; i++) { - assert.ok(queue[i].t); - assert.equal(queue[i].t, expectedInternalType); - } -} -function validateDeviceId(deviceId, deviceIdType, expectedDeviceId, expectedDeviceIdType) { - var rq = hp.readRequestQueue()[0]; - if (expectedDeviceIdType === cc.deviceIdTypeEnums.SDK_GENERATED) { - validateSdkGeneratedId(deviceId); // for SDK-generated IDs - } - else { - assert.equal(deviceId, expectedDeviceId); // for developer-supplied IDs - } - assert.equal(deviceIdType, expectedDeviceIdType); - checkRequestsForT(rq, expectedDeviceIdType); -} -function recordValuesToStorageAndValidate(userPath, memoryOnly = false, isBulk = false, persistQueue = false) { - // Set values - var deviceIdType = cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED; - storage.initStorage(userPath, memoryOnly, isBulk, persistQueue); - storage.storeSet("cly_id", "SpecialDeviceId"); - storage.storeSet("cly_id_type", deviceIdType); - - // Set values with different data types - storage.storeSet("cly_count", 42); - storage.storeSet("cly_object", { key: "value" }); - storage.storeSet("cly_null", null); - - // Retrieve and assert values - assert.equal(storage.storeGet("cly_id"), "SpecialDeviceId"); - assert.equal(storage.storeGet("cly_id_type"), deviceIdType); - assert.equal(storage.storeGet("cly_count"), 42); - assert.deepEqual(storage.storeGet("cly_object"), { key: "value" }); - assert.equal(storage.storeGet("cly_null"), null); - - // Remove specific items by overriding with null or empty array - storage.storeSet("cly_id", null); - storage.storeSet("cly_object", []); - assert.equal(storage.storeGet("cly_id"), null); - assert.deepEqual(storage.storeGet("cly_object"), []); - - // Reset storage and check if it's empty again - storage.resetStorage(); - assert.equal(storage.storeGet("cly_id"), undefined); - assert.equal(storage.storeGet("cly_id_type"), undefined); - assert.equal(storage.storeGet("cly_count"), undefined); - assert.equal(storage.storeGet("cly_object"), undefined); - assert.equal(storage.storeGet("cly_null"), undefined); -} var __data = {}; -// technically same as defualt file storage method -const customFileStorage = { - storeSet: function(key, value, callback) { - __data[key] = value; - storage.writeFile(key, value, callback); - }, - storeGet: function(key, def) { - cc.log(cc.logLevelEnums.DEBUG, `storeGet, Fetching item from storage with key: [${key}].`); - if (typeof __data[key] === "undefined") { - var ob = storage.readFile(key); - var obLen; - try { - obLen = Object.keys(ob).length; - } - catch (error) { - obLen = 0; - } - if (!ob || obLen === 0) { - __data[key] = def; - } - else { - __data[key] = ob[key]; - } - } - return __data[key]; - }, - storeRemove: function(key) { - delete __data[key]; - var filePath = path.resolve(__dirname, `${storage.getStoragePath()}__${key}.json`); - fs.access(filePath, fs.constants.F_OK, (accessErr) => { - if (accessErr) { - cc.log(cc.logLevelEnums.WARNING, `storeRemove, No file found with key: [${key}]. Nothing to remove.`); - return; - } - fs.unlink(filePath, (err) => { - if (err) { - cc.log(cc.logLevelEnums.ERROR, `storeRemove, Failed to remove file with key: [${key}]. Error: [${err.message}].`); - } - else { - cc.log(cc.logLevelEnums.INFO, `storeRemove, Successfully removed file with key: [${key}].`); - } - }); - }); - }, -}; -const nonValidStorageMethods = { - _storage: {}, - - setInvalid: function(key, value, callback) { - if (key) { - const existingValue = this._storage[key]; - if (typeof value === 'string' && typeof existingValue === 'string') { - this._storage[key] = existingValue + value; - } - else { - this._storage[key] = value; - } - if (typeof callback === "function") { - callback(null); - } - } - }, - getInvalid: function(key, def) { - const value = this._storage[key]; - if (typeof value === 'string') { - return value.split('').reverse().join(''); - } - - return value !== undefined ? value : def; - }, - removeInvalid: function(key) { - delete this._storage[key]; - }, -}; -const funkyMemoryStorage = { - _storage: {}, - - storeSet: function(key, value, callback) { - if (key) { - const existingValue = this._storage[key]; - if (typeof value === 'string' && typeof existingValue === 'string') { - this._storage[key] = existingValue + value; - } - else { - this._storage[key] = value; - } - if (typeof callback === "function") { - callback(null); - } - } - }, - storeGet: function(key, def) { - const value = this._storage[key]; - if (typeof value === 'string') { - return value.split('').reverse().join(''); - } - - return value !== undefined ? value : def; - }, - storeRemove: function(key) { - delete this._storage[key]; - }, -}; const customMemoryStorage = { - _storage: {}, storeSet: function(key, value, callback) { if (key) { - this._storage[key] = value; + __data[key] = value; if (typeof callback === "function") { callback(null); } } }, storeGet: function(key, def) { - return typeof this._storage[key] !== "undefined" ? this._storage[key] : def; + return typeof __data[key] !== "undefined" ? __data[key] : def; }, storeRemove: function(key) { - delete this._storage[key]; + delete __data[key]; }, }; -describe("Storage Tests", () => { - it("1- Store Generated Device ID", (done) => { - // clear previous data - hp.clearStorage(); - // initialize SDK - initMain(); - Countly.begin_session(); - // read request queue - setTimeout(() => { - validateSdkGeneratedId(Countly.get_device_id()); - done(); - }, hp.sWait); - }); - - it("1.1- Validate generated device id after process restart", (done) => { - initMain(); - validateDeviceId(Countly.get_device_id(), Countly.get_device_id_type(), undefined, cc.deviceIdTypeEnums.SDK_GENERATED); - done(); - }); - - it("2.Developer supplied device ID", (done) => { - hp.clearStorage(); - initMain("ID"); - Countly.begin_session(); - setTimeout(() => { - validateDeviceId(Countly.get_device_id(), Countly.get_device_id_type(), "ID", cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED); - done(); - }, hp.sWait); - }); - - it("2.1- Validate generated device id after process restart", (done) => { - validateDeviceId(Countly.get_device_id(), Countly.get_device_id_type(), "ID", cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED); - done(); - }); - - it("3- Record and validate all user details", (done) => { - hp.clearStorage(); - initMain(); - Countly.user_details(userDetailObj); - setTimeout(() => { - var req = hp.readRequestQueue()[0]; - hp.userDetailRequestValidator(userDetailObj, req); - done(); - }, hp.sWait); - }); +var setGetRemoveCustomValue = function() { + storage.storeSet("CustomKey", "CustomValue"); + assert.equal(storage.storeGet("CustomKey", null), "CustomValue"); + storage.storeRemove("CustomKey"); + assert.equal(storage.storeGet("CustomKey", null), null); +}; - it("3.1- Validate stored user detail", (done) => { - var req = hp.readRequestQueue()[0]; - hp.userDetailRequestValidator(userDetailObj, req); - done(); - }); +var ValidateID_IDType = function(devGivenId = null) { + if (!devGivenId) { + var storedId = storage.storeGet("cly_id"); + var storedIdType = storage.storeGet("cly_id_type"); + assert.equal(Countly.get_device_id(), storedId); + assert.equal(Countly.get_device_id_type(), storedIdType); + } + else { + assert.equal(Countly.get_device_id(), devGivenId); + assert.equal(Countly.get_device_id_type(), cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED); + } +}; - it("4- Record event and validate storage", (done) => { +describe("Storage Tests", () => { + // validates if performing storage operations without initializing the SDK is possible with file storage methods + // without initializing SDK, storage should be able to set, get and remove values in file storage + it("1- File Storage with No-Init", (done) => { hp.clearStorage(); - initMain(); - Countly.add_event(eventObj); - setTimeout(() => { - var storedEvents = hp.readEventQueue(); - assert.strictEqual(storedEvents.length, 1, "There should be exactly one event stored"); - - var event = storedEvents[0]; - hp.eventValidator(eventObj, event); - done(); - }, hp.mWait); - }); - - it("4.1- Validate event persistence after process restart", (done) => { - // Initialize SDK - initMain(); - - // Read stored events without clearing storage - var storedEvents = hp.readEventQueue(); - assert.strictEqual(storedEvents.length, 1, "There should be exactly one event stored"); - - var event = storedEvents[0]; - hp.eventValidator(eventObj, event); + storage.initStorage(); + assert.equal(StorageTypes.FILE, storage.getStorageType()); + setGetRemoveCustomValue(); done(); }); - // if storage path is not provided it will be default "../data/" - it("5- Not provide storage path during init", (done) => { + // validates if performing storage operations without initializing the SDK is possible with memory storage methods + // without initializing SDK, storage should be able to set, get and remove values in memory storage + it("2- Memory Storage with No-Init", (done) => { hp.clearStorage(); - initMain(); - assert.equal(storage.getStoragePath(), "../data/"); + storage.initStorage(null, StorageTypes.MEMORY); + assert.equal(StorageTypes.MEMORY, storage.getStorageType()); + assert.equal(undefined, storage.getStoragePath()); + setGetRemoveCustomValue(); done(); }); - // if set to undefined it should be set to default path - it("6- Set storage path to undefined", (done) => { + // validates if performing storage operations without initializing the SDK is possible with custom storage methods + // without initializing SDK, storage should be able to set, get and remove values in memory storage + it("3- Custom Storage with No-Init", (done) => { hp.clearStorage(); - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - storage_path: undefined, - }); - assert.equal(storage.getStoragePath(), "../data/"); + storage.initStorage(null, null, false, false, customMemoryStorage); + assert.equal(null, storage.getStorageType()); + assert.equal(undefined, storage.getStoragePath()); + setGetRemoveCustomValue(); + __data = {}; done(); }); - // if set to null it should be set to default path - it("7- Set storage path to null", (done) => { + // validates the functionality for the configuration time storage options + // sets to file storage with default path and methods during configuration time + it("4- Config Time Storage Options with Default File Storage", (done) => { hp.clearStorage(); Countly.init({ app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - storage_path: null, + url: "https://try.count.ly", + storage_type: StorageTypes.FILE, // for file storage this is not needed, for the readiblitiy purposes it's here }); + assert.equal(StorageTypes.FILE, storage.getStorageType()); assert.equal(storage.getStoragePath(), "../data/"); + setGetRemoveCustomValue(); + ValidateID_IDType(); done(); }); - // it should be set to the custom directory if provided - it("8- Set storage path to custom directory", (done) => { + // validates the functionality for the configuration time storage options + // sets to memory storage with default methods during configuration time + it("5- Config Time Storage Options with Default Memory Storage", (done) => { hp.clearStorage(); Countly.init({ app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - interval: 10000, - max_events: -1, - storage_path: "../test/customStorageDirectory/", - }); - assert.equal(storage.getStoragePath(), "../test/customStorageDirectory/"); - done(); - }); - - // resets the storage path to default and validates that it is set correctly, - // then resets it to undefined and confirms the reset. - it("9- Reset Storage While on Default Path /no-init", (done) => { - // will set to default storage path - storage.setStoragePath(); - assert.equal(storage.getStoragePath(), "../data/"); - // will set to undefined - storage.resetStorage(); - assert.equal(storage.getStoragePath(), undefined); - done(); - }); - - // sets the storage path to default and verifies it, - // then records values to storage and ensures they are stored correctly. - it("10- Recording to Storage with Default Storage Path /no-init", (done) => { - storage.resetStorage(); - // Set to default storage path - storage.setStoragePath(); - assert.equal(storage.getStoragePath(), "../data/"); - recordValuesToStorageAndValidate(); - done(); - }); - - // sets a custom storage path and verifies it, - // then records values to storage and ensures correct storage in the custom path. - it("11- Recording to Storage with Custom Storage Path /no-init", (done) => { - storage.resetStorage(); - // will set to default storage path - storage.setStoragePath("../test/customStorageDirectory/"); - assert.equal(storage.getStoragePath(), "../test/customStorageDirectory/"); - recordValuesToStorageAndValidate("../test/customStorageDirectory/"); - done(); - }); - - // sets the storage path to the default bulk storage path and verifies it, - // then records values to bulk storage and validates proper storage in bulk mode. - it("12- Recording to Bulk Storage with Default Bulk Data Path /no-init", (done) => { - storage.resetStorage(); - // will set to default storage path - // To set the storage path to the default bulk storage path and persist the queue - storage.setStoragePath(null, true, true); - assert.equal(storage.getStoragePath(), "../bulk_data/"); - recordValuesToStorageAndValidate(null, false, true, true); - done(); - }); - - // sets a custom bulk storage path and verifies it, - // then records values to bulk storage and ensures proper recording to the custom path. - it("13- Recording to Bulk Storage with Custom Bulk Storage Path /no-init", (done) => { - storage.resetStorage(); - // will set to default storage path - storage.setStoragePath("../test/customStorageDirectory/", true); - assert.equal(storage.getStoragePath(), "../test/customStorageDirectory/"); - recordValuesToStorageAndValidate("../test/customStorageDirectory/", false, true); - done(); - }); - - it("14- Setting storage path to default path via initStorage /no-init", (done) => { - storage.resetStorage(); - storage.initStorage(); - assert.equal(storage.getStoragePath(), "../data/"); - done(); - }); - - it("15- Setting bulk storage path to default path via initStorage /no-init", (done) => { - storage.resetStorage(); - storage.initStorage(null, false, true); - assert.equal(storage.getStoragePath(), "../bulk_data/"); - done(); - }); - - it("16- Setting custom storage path via initStorage /no-init", (done) => { - storage.resetStorage(); - storage.initStorage("../test/customStorageDirectory/"); - assert.equal(storage.getStoragePath(), "../test/customStorageDirectory/"); - done(); - }); - - it("17- Setting storage method to memory only and checking storage path /no-init", (done) => { - storage.resetStorage(); - storage.initStorage(null, StorageTypes.MEMORY); - assert.equal(storage.getStoragePath(), undefined); - done(); - }); - - // recording device-id in memory only mode - // initializes the SDK in memory only mode, validates that file storage files does not exist - // retrieve the developer supplied device id and id type from storage - it("18- Memory only storage Device-Id", (done) => { - hp.clearStorage(); - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - device_id: "Test-Device-Id", - clear_stored_device_id: true, + url: "https://try.count.ly", storage_type: StorageTypes.MEMORY, }); - hp.doesFileStoragePathsExist((exists) => { - assert.equal(false, exists); - }); + assert.equal(StorageTypes.MEMORY, storage.getStorageType()); assert.equal(storage.getStoragePath(), undefined); - assert.equal(storage.storeGet("cly_id", null), "Test-Device-Id"); - assert.equal(storage.storeGet("cly_id_type", null), cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED); + setGetRemoveCustomValue(); + ValidateID_IDType(); done(); }); - // recording event in memory only mode - // initializes the SDK in memory only mode, validates that file storage files does not exist - // records an event and validates the recorded event - it("19- Record event in memory only mode and validate the record", (done) => { - hp.clearStorage(); - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - device_id: "Test-Device-Id", - clear_stored_device_id: true, - storage_type: StorageTypes.MEMORY, - }); - hp.doesFileStoragePathsExist((exists) => { - assert.equal(false, exists); - }); - Countly.add_event(eventObj); - setTimeout(() => { - const storedData = storage.storeGet("cly_queue", null); - const eventArray = JSON.parse(storedData[0].events); - const eventFromQueue = eventArray[0]; - hp.eventValidator(eventObj, eventFromQueue); - done(); - }, hp.mWait); - }); - - // recording user details in memory only mode - // initializes the SDK in memory only mode, validates that file storage files does not exist - // records user details and validates the recorded details - it("20- Record and validate user details in memory only mode", (done) => { + // validates the functionality for the configuration time storage options + // sets to custom storage during configuration time + it("6- Config Time Storage Options with Custom Storage", (done) => { hp.clearStorage(); Countly.init({ app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - device_id: "Test-Device-Id", - clear_stored_device_id: true, - storage_type: StorageTypes.MEMORY, - }); - hp.doesFileStoragePathsExist((exists) => { - assert.equal(false, exists); + url: "https://try.count.ly", + custom_storage_method: customMemoryStorage, }); - Countly.user_details(userDetailObj); - const storedData = storage.storeGet("cly_queue", null); - const userDetailsReq = storedData[0]; - hp.userDetailRequestValidator(userDetailObj, userDetailsReq); + assert.equal(storage.getStoragePath(), undefined); + assert.equal(null, storage.getStorageType()); + setGetRemoveCustomValue(); + ValidateID_IDType(); + __data = {}; done(); }); - // tests device id changes in memory only storage - // initialize the SDK in memory only mode, check the device id and switch it - // SDK and storage should function properly - it("21- Memory only storage, change SDK Generated Device-Id", (done) => { + // validates the recording of device id correctly if provided by developer + // all validations should succeed like DeviceId, Value recording etc. + it("7- File Storage Init with Dev Supplied Device ID", (done) => { hp.clearStorage(); Countly.init({ app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - clear_stored_device_id: true, - storage_type: StorageTypes.MEMORY, - }); - hp.doesFileStoragePathsExist((exists) => { - assert.equal(false, exists); + url: "https://try.count.ly", + device_id: "ID", + storage_type: StorageTypes.FILE, // for file storage this is not needed, for the readiblitiy purposes it's here }); - assert.equal(storage.getStoragePath(), undefined); - assert.equal(storage.storeGet("cly_id", null), Countly.get_device_id()); - assert.equal(storage.storeGet("cly_id_type", null), Countly.get_device_id_type()); - - Countly.change_id("Test-Id-2"); - assert.equal(storage.storeGet("cly_id", null), "Test-Id-2"); - assert.equal(storage.storeGet("cly_id_type", null), cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED); + assert.equal(StorageTypes.FILE, storage.getStorageType()); + setGetRemoveCustomValue(); + ValidateID_IDType("ID"); done(); }); - // tests switching between storage types after initializing SDK - // passing memory storage type during init and initializing storage afterwards - // SDK should switch to file storage - it("22- Switch to file storage after init", (done) => { + // validates the recording of device id correctly if provided by developer + // all validations should succeed like DeviceId, Value recording etc. + it("8- Memory Storage Init with Dev Supplied Device ID", (done) => { hp.clearStorage(); Countly.init({ app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - clear_stored_device_id: true, + url: "https://try.count.ly", + device_id: "ID2", storage_type: StorageTypes.MEMORY, }); assert.equal(storage.getStoragePath(), undefined); - assert.equal(storage.getStorageType(), StorageTypes.MEMORY); - hp.doesFileStoragePathsExist((exists) => { - assert.equal(false, exists); - }); - - storage.initStorage(); - assert.equal(storage.getStoragePath(), "../data/"); - assert.equal(storage.getStorageType(), StorageTypes.FILE); + assert.equal(StorageTypes.MEMORY, storage.getStorageType()); + setGetRemoveCustomValue(); + ValidateID_IDType("ID2"); done(); }); - // tests storeRemove function in CountlyStorage - // after initializing the memory storage, without initializing SDK, attempts to set, get and remove values - // without initializing SDK storage should function properly - it("23- storeRemove Memory Only /no-init", (done) => { - hp.clearStorage(); - storage.initStorage(null, StorageTypes.MEMORY); - assert.equal(storage.getStoragePath(), undefined); - assert.equal(storage.getStorageType(), StorageTypes.MEMORY); - storage.storeSet("keyToStore", "valueToStore"); - assert.equal(storage.storeGet("keyToStore", null), "valueToStore"); - - storage.storeRemove("keyToStore"); - assert.equal(storage.storeGet("keyToStore", null), null); - done(); - }); - - // tests storeRemove function in CountlyStorage - // after initializing the file storage, without initializing SDK attempts to set, get and remove values - // without initializing SDK storage should function properly - it("24- storeRemove File Storage /no-init", (done) => { - hp.clearStorage(); - storage.initStorage(); - assert.equal(storage.getStoragePath(), "../data/"); - assert.equal(storage.getStorageType(), StorageTypes.FILE); - storage.storeSet("keyToStore", "valueToStore"); - assert.equal(storage.storeGet("keyToStore", null), "valueToStore"); - - storage.storeRemove("keyToStore"); - assert.equal(storage.storeGet("keyToStore", null), null); - done(); - }); - - // tests init time storage config options - // choosing Custom storage type and passing null in storage methods - // passing null as storage method ends up with switching to default file storage - it("25- Null Custom Storage Method", (done) => { + // validates the recording of device id correctly if provided by developer + // all validations should succeed like DeviceId, Value recording etc. + it("9- Custom Storage Init with Dev Supplied Device ID", (done) => { hp.clearStorage(); Countly.init({ app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - device_id: "Test-Device-Id", - clear_stored_device_id: true, - storage_type: StorageTypes.CUSTOM, - custom_storage_method: null, - }); - assert.equal(storage.getStoragePath(), "../data/"); - assert.equal(storage.getStorageType(), StorageTypes.FILE); - done(); - }); - - // tests init time storage config options - // choosing Custom storage type and passing custom storage methods - // SDK should use custom methods as storage method, no File Storage should exist - it("26- Providing Custom Storage Method", (done) => { - hp.clearStorage(); - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - device_id: "Test-Device-Id", - clear_stored_device_id: true, - storage_type: StorageTypes.CUSTOM, + url: "https://try.count.ly", + device_id: "ID3", custom_storage_method: customMemoryStorage, }); - hp.doesFileStoragePathsExist((exists) => { - assert.equal(false, exists); - }); + setGetRemoveCustomValue(); + ValidateID_IDType("ID3"); done(); }); - - // tests init time storage config options - // Recording values in Custom Storage Methods - // SDK should use custom methods as storage methods and values should be recorded correctly - it("27- Record/Remove Values in Custom Storage Method", (done) => { - hp.clearStorage(); - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - device_id: "Test-Device-Id", - clear_stored_device_id: true, - storage_type: StorageTypes.CUSTOM, - custom_storage_method: customMemoryStorage, - }); - hp.doesFileStoragePathsExist((exists) => { - assert.equal(false, exists); - }); - storage.storeSet("CustomStorageKey", "CustomStorageValue"); - assert.equal(storage.storeGet("CustomStorageKey", null), "CustomStorageValue"); - storage.storeRemove("CustomStorageKey"); - assert.equal(storage.storeGet("CustomStorageKey", null), null); - done(); - }); - - // tests init time storage config options - // passes a funky storage method, which does store get as reversing string - // SDK should use custom methods as storage method - it("28- Record/Remove Values in Other Custom Storage Method", (done) => { - hp.clearStorage(); - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - device_id: "Test-Device-Id", - clear_stored_device_id: true, - storage_type: StorageTypes.CUSTOM, - custom_storage_method: funkyMemoryStorage, - }); - hp.doesFileStoragePathsExist((exists) => { - assert.equal(false, exists); - }); - storage.storeSet("CustomStorageKey", "CustomStorageValue"); - storage.storeSet("CustomStorageKey", "CustomStorageValue2"); - assert.equal("2eulaVegarotSmotsuCeulaVegarotSmotsuC", storage.storeGet("CustomStorageKey", null)); - done(); - }); - - // tests init time storage config options - // choosing Custom storage type and passing invalid custom storage methods - // SDK should not use custom methods as storage method, and switch to File Storage - it("29- Providing Invalid Custom Storage Method", (done) => { - hp.clearStorage(); - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - device_id: "Test-Device-Id", - clear_stored_device_id: true, - storage_type: StorageTypes.CUSTOM, - custom_storage_method: nonValidStorageMethods, - }); - assert.equal(storage.getStoragePath(), "../data/"); - assert.equal(storage.getStorageType(), StorageTypes.FILE); - done(); - }); - - // tests init time storage config options - // choosing Custom storage type and passing custom file storage methods - // SDK should use custom methods as storage methods - it("30- Providing File Custom Storage Method", (done) => { - hp.clearStorage(false, false, "../test/customStorageDirectory/"); - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://test.url.ly", - storage_path: "../test/customStorageDirectory/", - storage_type: StorageTypes.CUSTOM, - custom_storage_method: customFileStorage, - clear_stored_device_id: true, - device_id: "ID", - }); - Countly.begin_session(); - setTimeout(() => { - assert.equal(Countly.get_device_id(), "ID"); - assert.equal(Countly.get_device_id_type(), cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED); - assert.equal(storage.getStoragePath(), "../test/customStorageDirectory/"); - storage.storeSet("CustomStorageKey", "CustomStorageValue"); - assert.equal(storage.storeGet("CustomStorageKey", null), "CustomStorageValue"); - storage.storeRemove("CustomStorageKey"); - assert.equal(storage.storeGet("CustomStorageKey", null), null); - done(); - }, hp.sWait); - }); }); \ No newline at end of file