|
| 1 | + |
| 2 | +var Countly = require("../../lib/countly"); |
| 3 | +var hp = require("../support/helper.js"); |
| 4 | + |
| 5 | +function initMain(val) { |
| 6 | + Countly.init({ |
| 7 | + app_key: "spp", |
| 8 | + url: "https://hey.some.ly", |
| 9 | + debug: true, |
| 10 | + behavior_settings: val |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | +function sentReqList(size, expectedKeywords, nonexistingKeywords) { |
| 15 | + var directReqs = Countly._internals.testingGetRequests(); // get direct requests |
| 16 | + var functionNames = directReqs.map((item) => item.functionName); |
| 17 | + cy.log("Direct Requests: " + JSON.stringify(directReqs)); |
| 18 | + |
| 19 | + if (typeof size !== 'undefined') { |
| 20 | + expect(directReqs.length).to.eq(size); |
| 21 | + } |
| 22 | + if (expectedKeywords) { |
| 23 | + for (var i = 0; i < expectedKeywords.length; i++) { |
| 24 | + expect(functionNames.includes(expectedKeywords[i])).to.be.true; |
| 25 | + } |
| 26 | + } |
| 27 | + if (nonexistingKeywords) { |
| 28 | + for (var j = 0; j < nonexistingKeywords.length; j++) { |
| 29 | + expect(functionNames.includes(nonexistingKeywords[j])).to.be.false; |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | +function queues(eqsize, rqsize, expectedKeywords, nonexistingKeywords) { |
| 34 | + var queues = Countly._internals.getLocalQueues(); // get local queues |
| 35 | + const allKeysE = queues.eventQ.flatMap(item => Object.keys(item)); |
| 36 | + const allKeysQ = queues.requestQ.flatMap(item => Object.keys(item)); |
| 37 | + const uniqueKeys = [...new Set(allKeysE), ...new Set(allKeysQ)]; |
| 38 | + |
| 39 | + const allValuesE = queues.eventQ.flatMap(item => Object.values(item)); |
| 40 | + const allValuesQ = queues.requestQ.flatMap(item => Object.values(item)); |
| 41 | + const uniqueValues = [...new Set(allValuesE), ...new Set(allValuesQ)]; |
| 42 | + |
| 43 | + cy.log("Unique Keys: " + JSON.stringify(uniqueKeys)); |
| 44 | + cy.log("Queues: " + JSON.stringify(queues)); |
| 45 | + |
| 46 | + if (typeof eqsize !== 'undefined') { |
| 47 | + expect(queues.eventQ.length).to.eq(eqsize); |
| 48 | + } |
| 49 | + if (typeof rqsize !== 'undefined') { |
| 50 | + expect(queues.requestQ.length).to.eq(rqsize); |
| 51 | + } |
| 52 | + if (expectedKeywords) { |
| 53 | + for (var i = 0; i < expectedKeywords.length; i++) { |
| 54 | + expect(uniqueKeys.includes(expectedKeywords[i])).to.be.true; |
| 55 | + } |
| 56 | + } |
| 57 | + if (nonexistingKeywords) { |
| 58 | + for (var j = 0; j < nonexistingKeywords.length; j++) { |
| 59 | + expect(uniqueValues.includes(nonexistingKeywords[j])).to.be.false; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +const waitT = 2000; |
| 65 | + |
| 66 | +describe("SDK Behavior test", () => { |
| 67 | + it("Basic initialization with default config", () => { |
| 68 | + hp.haltAndClearStorage(() => { |
| 69 | + initMain(); |
| 70 | + cy.wait(waitT).then(() => { |
| 71 | + cy.fetch_local_request_queue().then((rq) => { |
| 72 | + cy.log("Request Queue: " + JSON.stringify(rq)); |
| 73 | + sentReqList(2, ["server_config", "[healthCheck]"]); |
| 74 | + queues(0, 0); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it("Initialization with default config and integration methods", () => { |
| 81 | + hp.haltAndClearStorage(() => { |
| 82 | + initMain(); |
| 83 | + hp.integrationMethods(); |
| 84 | + cy.wait(waitT).then(() => { |
| 85 | + cy.fetch_local_request_queue().then((rq) => { |
| 86 | + cy.log("Request Queue: " + JSON.stringify(rq)); |
| 87 | + sentReqList(9, ["server_config", "[healthCheck]", "enrollUserToAb", "fetch_remote_config_explicit", "send_request_queue", "get_available_feedback_widgets,"]); |
| 88 | + queues(0, 15); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("Config with networking disabled", () => { |
| 95 | + hp.haltAndClearStorage(() => { |
| 96 | + var settings = {}; |
| 97 | + settings.c = { |
| 98 | + networking: false |
| 99 | + }; |
| 100 | + initMain(settings); |
| 101 | + hp.integrationMethods(); |
| 102 | + cy.wait(waitT).then(() => { |
| 103 | + cy.fetch_local_request_queue().then((rq) => { |
| 104 | + cy.log("Request Queue: " + JSON.stringify(rq)); |
| 105 | + sentReqList(1, ["server_config"]); |
| 106 | + queues(0, 15); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it("Config with tracking disabled", () => { |
| 113 | + hp.haltAndClearStorage(() => { |
| 114 | + var settings = {}; |
| 115 | + settings.c = { |
| 116 | + tracking: false |
| 117 | + }; |
| 118 | + initMain(settings); |
| 119 | + hp.integrationMethods(); |
| 120 | + cy.wait(waitT).then(() => { |
| 121 | + cy.fetch_local_request_queue().then((rq) => { |
| 122 | + cy.log("Request Queue: " + JSON.stringify(rq)); |
| 123 | + sentReqList(8, ["server_config", "[healthCheck]", "enrollUserToAb", "fetch_remote_config_explicit", "get_available_feedback_widgets,"]); |
| 124 | + queues(0, 0); |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it("Config with both tracking and networking disabled", () => { |
| 131 | + hp.haltAndClearStorage(() => { |
| 132 | + var settings = {}; |
| 133 | + settings.c = { |
| 134 | + tracking: false, |
| 135 | + networking: false |
| 136 | + }; |
| 137 | + initMain(settings); |
| 138 | + hp.integrationMethods(); |
| 139 | + cy.wait(waitT).then(() => { |
| 140 | + cy.fetch_local_request_queue().then((rq) => { |
| 141 | + cy.log("Request Queue: " + JSON.stringify(rq)); |
| 142 | + sentReqList(1, ["server_config"]); |
| 143 | + queues(0, 0); |
| 144 | + }); |
| 145 | + }); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + it("Config with consent required true", () => { |
| 150 | + hp.haltAndClearStorage(() => { |
| 151 | + var settings = {}; |
| 152 | + settings.c = { |
| 153 | + cr: true |
| 154 | + }; |
| 155 | + initMain(settings); |
| 156 | + hp.integrationMethods(); |
| 157 | + cy.wait(waitT).then(() => { |
| 158 | + cy.fetch_local_request_queue().then((rq) => { |
| 159 | + cy.log("Request Queue: " + JSON.stringify(rq)); |
| 160 | + sentReqList(4, ["server_config", "[healthCheck]", "enrollUserToAb", "send_request_queue"]); // device id change sent |
| 161 | + queues(0, 2, ["old_device_id", "consent"]); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + it("Limited queue sizes with networking disabled", () => { |
| 168 | + hp.haltAndClearStorage(() => { |
| 169 | + var settings = {}; |
| 170 | + settings.c = { |
| 171 | + rqs: 1, |
| 172 | + eqs: 1, |
| 173 | + networking: false, |
| 174 | + }; |
| 175 | + initMain(settings); |
| 176 | + Countly.add_event({ key: "test_1" }); |
| 177 | + Countly.add_event({ key: "test_2" }); |
| 178 | + Countly.add_event({ key: "test_3" }); |
| 179 | + Countly.add_event({ key: "test_4" }); |
| 180 | + Countly.add_event({ key: "test_5" }); |
| 181 | + cy.wait(3000).then(() => { |
| 182 | + cy.fetch_local_request_queue().then((rq) => { |
| 183 | + cy.log("Request Queue: " + JSON.stringify(rq)); |
| 184 | + sentReqList(1, ["server_config"]); |
| 185 | + queues(0, 2, ["events"], ["test_1", "test_2", "test_3"]); // 1+1 |
| 186 | + }); |
| 187 | + }); |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + it("Session update interval configuration", () => { |
| 192 | + hp.haltAndClearStorage(() => { |
| 193 | + var settings = {}; |
| 194 | + settings.c = { |
| 195 | + sui: 1, |
| 196 | + }; |
| 197 | + initMain(settings); |
| 198 | + Countly.track_sessions(); |
| 199 | + cy.wait(4000).then(() => { |
| 200 | + cy.fetch_local_request_queue().then((rq) => { |
| 201 | + cy.log("Request Queue: " + JSON.stringify(rq)); |
| 202 | + sentReqList(3, ["server_config", "[healthCheck]", "send_request_queue"]); |
| 203 | + queues(0, 4, ["session_duration", "begin_session"], ["session_end"]); |
| 204 | + }); |
| 205 | + }); |
| 206 | + }); |
| 207 | + }); |
| 208 | + |
| 209 | + it("Disabled tracking types configuration", () => { |
| 210 | + hp.haltAndClearStorage(() => { |
| 211 | + var settings = {}; |
| 212 | + settings.c = { |
| 213 | + st: false, |
| 214 | + crt: false, |
| 215 | + lt: false, |
| 216 | + vt: false, |
| 217 | + cet: false, |
| 218 | + }; |
| 219 | + initMain(settings); |
| 220 | + Countly.track_sessions(); |
| 221 | + Countly.add_event({ key: "test_1" }); |
| 222 | + Countly.track_pageview(); |
| 223 | + Countly.log_error({ message: "test" }); |
| 224 | + cy.wait(4000).then(() => { |
| 225 | + cy.fetch_local_request_queue().then((rq) => { |
| 226 | + cy.log("Request Queue: " + JSON.stringify(rq)); |
| 227 | + sentReqList(2, ["server_config", "[healthCheck]"]); |
| 228 | + queues(0, 0); |
| 229 | + }); |
| 230 | + }); |
| 231 | + }); |
| 232 | + }); |
| 233 | +}); |
0 commit comments