Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Jan 17, 2025
1 parent 27894b0 commit b43c8f0
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 71 deletions.
8 changes: 6 additions & 2 deletions src/__tests__/browserSuites/impressions-listener.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,21 @@ export default function (assert) {
keyName: '[email protected]',
treatment: 'no',
bucketingKey: 'impr_bucketing_2',
label: 'default rule'
label: 'default rule',
pt: undefined
};

assert.equal(listener.logImpression.callCount, 4, 'Impression listener logImpression method should be called after we call client.getTreatment, once per each impression generated.');
assert.true(listener.logImpression.getCall(0).calledWithMatch({
assert.true(listener.logImpression.getCall(0).calledWithExactly({
impression: {
feature: 'hierarchical_splits_test',
keyName: '[email protected]',
treatment: 'on',
time: listener.logImpression.getCall(0).args[0].impression.time,
bucketingKey: undefined,
label: 'expected label',
changeNumber: 2828282828,
pt: undefined
},
attributes: undefined,
...metaData
Expand Down
66 changes: 34 additions & 32 deletions src/__tests__/browserSuites/impressions.debug.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import splitChangesMock1 from '../mocks/splitchanges.since.-1.json';
import splitChangesMock2 from '../mocks/splitchanges.since.1457552620999.json';
import membershipsFacundo from '../mocks/[email protected]';
import { DEBUG } from '@splitsoftware/splitio-commons/src/utils/constants';
import { truncateTimeFrame } from '@splitsoftware/splitio-commons/src/utils/time';
import { url } from '../testUtils';

const baseUrls = {
Expand All @@ -19,6 +20,8 @@ const settings = settingsFactory({
streamingEnabled: false
});

let truncatedTimeFrame;

export default function (fetchMock, assert) {
// Mocking this specific route to make sure we only get the items we want to test from the handlers.
fetchMock.getOnce(url(settings, '/splitChanges?s=1.2&since=-1'), { status: 200, body: splitChangesMock1 });
Expand Down Expand Up @@ -47,41 +50,21 @@ export default function (fetchMock, assert) {
});

const client = splitio.client();
const assertPayload = req => {
const resp = JSON.parse(req.body);

assert.equal(resp.length, 1, 'We performed three evaluations so we should have 1 impressions type');

const alwaysOnWithConfigImpr = resp.filter(e => e.f === 'split_with_config')[0];

assert.equal(alwaysOnWithConfigImpr.i.length, 3);

function validateImpressionData(output, expected) {
assert.equal(output.k, expected.keyName, 'Present impressions should have the correct key.');
assert.equal(output.b, expected.bucketingKey, 'Present impressions should have the correct bucketingKey.');
assert.equal(output.t, expected.treatment, 'Present impressions should have the correct treatment.');
assert.equal(output.r, expected.label, 'Present impressions should have the correct label.');
assert.equal(output.c, expected.changeNumber, 'Present impressions should have the correct changeNumber.');
assert.equal(output.pt, expected.pt, 'Present impressions should have the correct previousTime.');
}

validateImpressionData(alwaysOnWithConfigImpr.i[0], {
keyName: '[email protected]', label: 'another expected label', treatment: 'o.n',
bucketingKey: undefined, changeNumber: 828282828282, pt: undefined
});
validateImpressionData(alwaysOnWithConfigImpr.i[1], {
keyName: '[email protected]', label: 'another expected label', treatment: 'o.n',
bucketingKey: undefined, changeNumber: 828282828282, pt: alwaysOnWithConfigImpr.i[0].m
});
validateImpressionData(alwaysOnWithConfigImpr.i[2], {
keyName: '[email protected]', label: 'another expected label', treatment: 'o.n',
bucketingKey: undefined, changeNumber: 828282828282, pt: alwaysOnWithConfigImpr.i[1].m
});
};

fetchMock.postOnce(url(settings, '/testImpressions/bulk'), (url, req) => {
assert.equal(req.headers.SplitSDKImpressionsMode, DEBUG);
assertPayload(req);
const data = JSON.parse(req.body);

assert.deepEqual(data, [{
f: 'split_with_config',
i: [{
k: '[email protected]', t: 'o.n', m: data[0].i[0].m, c: 828282828282, r: 'another expected label'
}, {
k: '[email protected]', t: 'o.n', m: data[0].i[1].m, c: 828282828282, r: 'another expected label', pt: data[0].i[0].m,
}, {
k: '[email protected]', t: 'o.n', m: data[0].i[2].m, c: 828282828282, r: 'another expected label', pt: data[0].i[1].m
}]
}]);

client.destroy().then(() => {
assert.end();
Expand All @@ -90,9 +73,28 @@ export default function (fetchMock, assert) {
return 200;
});

fetchMock.postOnce(url(settings, '/testImpressions/count'), (url, opts) => {
assert.deepEqual(JSON.parse(opts.body), {
pf: [{ f: 'always_on_track_impressions_false', m: truncatedTimeFrame, rc: 1 }]
}, 'We should generate impression count for the feature with track impressions disabled.');

return 200;
});

fetchMock.postOnce(url(settings, '/v1/keys/cs'), (url, opts) => {
assert.deepEqual(JSON.parse(opts.body), {
keys: [{ fs: ['always_on_track_impressions_false'], k: '[email protected]' }]
}, 'We should track unique keys for the feature with track impressions disabled.');

return 200;
});

client.ready().then(() => {
truncatedTimeFrame = truncateTimeFrame(Date.now());

client.getTreatment('split_with_config');
client.getTreatment('split_with_config');
client.getTreatment('split_with_config');
assert.equal(client.getTreatment('always_on_track_impressions_false'), 'on');
});
}
41 changes: 27 additions & 14 deletions src/__tests__/browserSuites/impressions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ export default function (fetchMock, assert) {
const assertPayload = req => {
const resp = JSON.parse(req.body);

assert.equal(resp.length, 2, 'We performed three evaluations so we should have 2 impressions type');
assert.equal(resp.length, 2, 'We performed evaluations for 3 features, but one with `impressionsDisabled` true, so we should have 2 items total');

const dependencyChildImpr = resp.filter(e => e.f === 'hierarchical_splits_test')[0];
const alwaysOnWithConfigImpr = resp.filter(e => e.f === 'split_with_config')[0];
const splitWithConfigImpr = resp.filter(e => e.f === 'split_with_config')[0];
const alwaysOnWithTrackImpressionsFalse = resp.filter(e => e.f === 'always_on_track_impressions_false');

assert.true(dependencyChildImpr, 'Split we wanted to evaluate should be present on the impressions.');
assert.false(resp.some(e => e.f === 'hierarchical_dep_always_on'), 'Parent split evaluations should not result in impressions.');
assert.false(resp.some(e => e.f === 'hierarchical_dep_hierarchical'), 'No matter how deep is the chain.');
assert.true(alwaysOnWithConfigImpr, 'Split evaluated with config should have generated an impression too.');
assert.false(Object.prototype.hasOwnProperty.call(alwaysOnWithConfigImpr.i[0], 'configuration'), 'Impressions do not change with configuration evaluations.');
assert.false(Object.prototype.hasOwnProperty.call(alwaysOnWithConfigImpr.i[0], 'config'), 'Impressions do not change with configuration evaluations.');
assert.true(splitWithConfigImpr, 'Split evaluated with config should have generated an impression too.');
assert.false(Object.prototype.hasOwnProperty.call(splitWithConfigImpr.i[0], 'configuration'), 'Impressions do not change with configuration evaluations.');
assert.false(Object.prototype.hasOwnProperty.call(splitWithConfigImpr.i[0], 'config'), 'Impressions do not change with configuration evaluations.');
assert.equal(alwaysOnWithTrackImpressionsFalse.length, 0);

const {
k,
Expand Down Expand Up @@ -94,18 +96,26 @@ export default function (fetchMock, assert) {
fetchMock.postOnce(url(settings, '/testImpressions/count'), (url, opts) => {
const data = JSON.parse(opts.body);

assert.equal(data.pf.length, 1, 'We should generate impressions count for one feature.');
assert.equal(data.pf.length, 2, 'We should generate impressions count for 2 features.');

// finding these validate the feature names collection too
const dependencyChildImpr = data.pf.filter(e => e.f === 'hierarchical_splits_test')[0];
const alwaysOnWithConfigImpr = data.pf.filter(e => e.f === 'split_with_config')[0];
const splitWithConfigImpr = data.pf.filter(e => e.f === 'split_with_config')[0];
const alwaysOnWithTrackImpressionsFalse = data.pf.filter(e => e.f === 'always_on_track_impressions_false')[0];

assert.equal(dependencyChildImpr.rc, 1);
assert.equal(typeof dependencyChildImpr.m, 'number');
assert.equal(dependencyChildImpr.m, truncatedTimeFrame);
assert.equal(alwaysOnWithConfigImpr.rc, 3);
assert.equal(typeof alwaysOnWithConfigImpr.m, 'number');
assert.equal(alwaysOnWithConfigImpr.m, truncatedTimeFrame);
assert.equal(splitWithConfigImpr.rc, 2);
assert.equal(typeof splitWithConfigImpr.m, 'number');
assert.equal(splitWithConfigImpr.m, truncatedTimeFrame);
assert.equal(alwaysOnWithTrackImpressionsFalse.rc, 1);
assert.equal(typeof alwaysOnWithTrackImpressionsFalse.m, 'number');
assert.equal(alwaysOnWithTrackImpressionsFalse.m, truncatedTimeFrame);

return 200;
});

fetchMock.postOnce(url(settings, '/v1/keys/cs'), (url, opts) => {
assert.deepEqual(JSON.parse(opts.body), {
keys: [{ fs: [ 'always_on_track_impressions_false' ], k: '[email protected]' }]
}, 'We should only track unique keys for features flags with track impressions disabled.');

return 200;
});
Expand All @@ -120,5 +130,8 @@ export default function (fetchMock, assert) {
}, 'We should get an evaluation as always.');
client.getTreatmentWithConfig('split_with_config');
client.getTreatmentWithConfig('split_with_config');

// Impression should not be tracked
assert.equal(client.getTreatment('always_on_track_impressions_false'), 'on');
});
}
3 changes: 2 additions & 1 deletion src/__tests__/browserSuites/manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export default async function (settings, fetchMock, assert) {
'treatments': map(mockSplits.splits[index].conditions[0].partitions, partition => partition.treatment),
'configs': mockSplits.splits[index].configurations || {},
'sets': mockSplits.splits[index].sets,
'defaultTreatment': mockSplits.splits[index].defaultTreatment
'defaultTreatment': mockSplits.splits[index].defaultTreatment,
'impressionsDisabled': false
});

assert.equal(manager.split('non_existent'), null, 'Trying to get a manager.split() of a Split that does not exist returns null.');
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/browserSuites/telemetry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default async function telemetryBrowserSuite(fetchMock, assert) {

// @TODO check if iDe value is correct
assert.deepEqual(data, {
mE: {}, hE: { sp: { 500: 1 }, ms: { 500: 1 } }, tR: 0, aR: 0, iQ: 4, iDe: 1, iDr: 0, spC: 32, seC: 1, skC: 1, eQ: 1, eD: 0, sE: [], t: [], ufs: {}
mE: {}, hE: { sp: { 500: 1 }, ms: { 500: 1 } }, tR: 0, aR: 0, iQ: 4, iDe: 1, iDr: 0, spC: 33, seC: 1, skC: 1, eQ: 1, eD: 0, sE: [], t: [], ufs: {}
}, 'metrics/usage JSON payload should be the expected');

finish.next();
Expand All @@ -91,7 +91,7 @@ export default async function telemetryBrowserSuite(fetchMock, assert) {
// @TODO check if iDe value is correct
assert.deepEqual(data, {
mL: {}, mE: {}, hE: {}, hL: {}, // errors and latencies were popped
tR: 0, aR: 0, iQ: 4, iDe: 1, iDr: 0, spC: 32, seC: 1, skC: 1, eQ: 1, eD: 0, sE: [], t: [], ufs: {}
tR: 0, aR: 0, iQ: 4, iDe: 1, iDr: 0, spC: 33, seC: 1, skC: 1, eQ: 1, eD: 0, sE: [], t: [], ufs: {}
}, '2nd metrics/usage JSON payload should be the expected');
return 200;
});
Expand Down
15 changes: 8 additions & 7 deletions src/__tests__/consumer/browser_consumer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { version } from '../../../package.json';
import { SplitFactory, PluggableStorage } from '../../';

const expectedSplitName = 'hierarchical_splits_testing_on';
const expectedSplitView = { name: 'hierarchical_splits_testing_on', trafficType: 'user', killed: false, changeNumber: 1487277320548, treatments: ['on', 'off'], configs: {}, sets: [], defaultTreatment: 'off' };
const expectedSplitView = { name: 'hierarchical_splits_testing_on', trafficType: 'user', killed: false, changeNumber: 1487277320548, treatments: ['on', 'off'], configs: {}, sets: [], defaultTreatment: 'off', impressionsDisabled: false };

const wrapperPrefix = 'PLUGGABLE_STORAGE_UT';
const wrapperInstance = inMemoryWrapperFactory();
Expand Down Expand Up @@ -111,11 +111,12 @@ tape('Browser Consumer mode with pluggable storage', function (t) {

assert.equal(await client.getTreatment('always-on'), 'on', 'Evaluations using pluggable storage should be correct.');

// Below splits were added manually.
// Below feature flags were added manually.
// They are all_keys (always evaluate to on) which depend from always-on split. the _on/off is what treatment they are expecting there.
assert.equal(await client.getTreatment('hierarchical_splits_testing_on'), 'on', 'Evaluations using pluggable storage should be correct.');
assert.equal(await client.getTreatment('hierarchical_splits_testing_off'), 'off', 'Evaluations using pluggable storage should be correct.');
assert.equal(await client.getTreatment('hierarchical_splits_testing_on_negated'), 'off', 'Evaluations using pluggable storage should be correct.');
assert.equal(await client.getTreatment('always-on-impressions-disabled-true'), 'on', 'Evaluations using pluggable storage should be correct.');

assert.equal(typeof client.track('user', 'test.event', 18).then, 'function', 'Track calls should always return a promise on Consumer mode.');
assert.equal(typeof client.track().then, 'function', 'Track calls should always return a promise on Consumer mode, even when parameters are incorrect.');
Expand All @@ -125,11 +126,11 @@ tape('Browser Consumer mode with pluggable storage', function (t) {

// Manager methods
const splitNames = await manager.names();
assert.equal(splitNames.length, 25, 'manager `names` method returns the list of split names asynchronously');
assert.equal(splitNames.length, 26, 'manager `names` method returns the list of split names asynchronously');
assert.equal(splitNames.indexOf(expectedSplitName) > -1, true, 'list of split names should contain expected splits');
assert.deepEqual(await manager.split(expectedSplitName), expectedSplitView, 'manager `split` method returns the split view of the given split name asynchronously');
const splitViews = await manager.splits();
assert.equal(splitViews.length, 25, 'manager `splits` method returns the list of split views asynchronously');
assert.equal(splitViews.length, 26, 'manager `splits` method returns the list of split views asynchronously');
assert.deepEqual(splitViews.find(splitView => splitView.name === expectedSplitName), expectedSplitView, 'manager `split` method returns the split view of the given split name asynchronously');

// New shared client created
Expand Down Expand Up @@ -164,7 +165,7 @@ tape('Browser Consumer mode with pluggable storage', function (t) {

// Assert impressionsListener
setTimeout(() => {
assert.equal(impressions.length, TOTAL_RAW_IMPRESSIONS, 'Each evaluation has its corresponding impression');
assert.equal(impressions.length, TOTAL_RAW_IMPRESSIONS + 1 /* One evaluation with impressionsDisabled true */, 'Each evaluation has its corresponding impression');
assert.equal(impressions[0].impression.label, SDK_NOT_READY, 'The first impression is control with label "sdk not ready"');

assert.end();
Expand Down Expand Up @@ -251,7 +252,7 @@ tape('Browser Consumer mode with pluggable storage', function (t) {

assert.equal(await client.getTreatment('always-on'), 'on', 'Evaluations using pluggable storage should be correct.');

// Below splits were added manually.
// Below feature flags were added manually.
// They are all_keys (always evaluate to on) which depend from always-on split. the _on/off is what treatment they are expecting there.
assert.equal(await client.getTreatment('hierarchical_splits_testing_on'), 'on', 'Evaluations using pluggable storage should be correct.');
assert.equal(await client.getTreatment('hierarchical_splits_testing_off'), 'off', 'Evaluations using pluggable storage should be correct.');
Expand Down Expand Up @@ -383,7 +384,7 @@ tape('Browser Consumer mode with pluggable storage', function (t) {

assert.equal(await client.getTreatment('always-on'), 'on', 'Evaluations using pluggable storage should be correct.');

// Below splits were added manually.
// Below feature flags were added manually.
// They are all_keys (always evaluate to on) which depend from always-on split. the _on/off is what treatment they are expecting there.
assert.equal(await client.getTreatment('hierarchical_splits_testing_on'), 'on', 'Evaluations using pluggable storage should be correct.');
assert.equal(await client.getTreatment('hierarchical_splits_testing_off'), 'off', 'Evaluations using pluggable storage should be correct.');
Expand Down
Loading

0 comments on commit b43c8f0

Please sign in to comment.