Skip to content

[Rule-based segments] Add tests #863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 16, 2025
1 change: 0 additions & 1 deletion src/__tests__/browserSuites/evaluations-semver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const config = {
export default async function (fetchMock, assert) {

fetchMock.getOnce(config.urls.sdk + '/splitChanges?s=1.3&since=-1&rbSince=-1', { status: 200, body: splitChangesMock1 });
fetchMock.getOnce(config.urls.sdk + '/splitChanges?s=1.3&since=1675259356568&rbSince=-1', { status: 200, body: { ff: { d: [], s: 1675259356568, t: 1675259356568 } } });
fetchMock.getOnce(config.urls.sdk + '/memberships/emi%40split.io', { status: 200, body: { ms: {} } });
fetchMock.getOnce(config.urls.sdk + '/memberships/2nd', { status: 200, body: { ms: {} } });

Expand Down
46 changes: 40 additions & 6 deletions src/__tests__/browserSuites/evaluations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,37 @@ export default function (config, fetchMock, assert) {

};

const evaluationsWithRuleBasedSegments = async (splitio) => {
fetchMock.getOnce('https://sdk.split.io/api/memberships/emi%40split.io', { status: 200, body: { ms: { k: [{ n: 'segment_excluded_by_rbs' }] } } });
fetchMock.getOnce('https://sdk.split.io/api/memberships/mauro%40split.io', { status: 200, body: { ms: {} } });
fetchMock.getOnce('https://sdk.split.io/api/memberships/bilal%40split.io', { status: 200, body: { ms: {} } });
fetchMock.getOnce('https://sdk.split.io/api/memberships/other_key', { status: 200, body: { ms: {} } });

const client1 = splitio.client('[email protected]');
await client1.ready();
assert.equal(client1.getTreatment('rbs_test_flag'), 'v2', 'key in excluded segment');
assert.equal(client1.getTreatment('rbs_test_flag_negated'), 'v1', 'key in excluded segment');
await client1.destroy();

const client2 = splitio.client('[email protected]');
await client2.ready();
assert.equal(client2.getTreatment('rbs_test_flag'), 'v2', 'excluded key');
assert.equal(client2.getTreatment('rbs_test_flag_negated'), 'v1', 'excluded key');
await client2.destroy();

const client3 = splitio.client('[email protected]');
await client3.ready();
assert.equal(client3.getTreatment('rbs_test_flag'), 'v1', 'key satisfies the rbs condition');
assert.equal(client3.getTreatment('rbs_test_flag_negated'), 'v2', 'key satisfies the rbs condition');
await client3.destroy();

const client4 = splitio.client('other_key');
await client4.ready();
assert.equal(client4.getTreatment('rbs_test_flag'), 'v2', 'key not in segment');
assert.equal(client4.getTreatment('rbs_test_flag_negated'), 'v1', 'key not in segment');
await client4.destroy();
};

for (i; i < SDK_INSTANCES_TO_TEST; i++) {
let splitio = SplitFactory(config);

Expand All @@ -376,13 +407,16 @@ export default function (config, fetchMock, assert) {
getTreatmentsTests(client);
getTreatmentsWithConfigTests(client);
getTreatmentsWithInMemoryAttributes(client);
clientTABucket1.destroy();
client.destroy();
tested++;

if (tested === SDK_INSTANCES_TO_TEST) {
assert.end();
}
evaluationsWithRuleBasedSegments(splitio).then(() => {
clientTABucket1.destroy();
client.destroy();
tested++;

if (tested === SDK_INSTANCES_TO_TEST) {
assert.end();
}
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export default function (fetchMock, assert) {
// Mock GET endpoints before creating the client
const settings = settingsFactory(config);
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=-1&rbSince=-1'), { status: 200, body: splitChangesMock1 });
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), { status: 200, body: { ff: { d: [], s: 1457552620999, t: 1457552620999 } } });
fetchMock.getOnce(url(settings, `/memberships/${encodeURIComponent(config.core.key)}`), { status: 200, body: { ms: {} } });

// Init Split client
Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/browserSuites/impressions.debug.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SplitFactory } from '../../';
import { settingsFactory } from '../../settings';
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';
Expand All @@ -25,7 +24,6 @@ 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.3&since=-1&rbSince=-1'), { status: 200, body: splitChangesMock1 });
fetchMock.get(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), { status: 200, body: splitChangesMock2 });
fetchMock.get(url(settings, '/memberships/facundo%40split.io'), { status: 200, body: membershipsFacundo });

const splitio = SplitFactory({
Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/browserSuites/impressions.none.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SplitFactory } from '../..';
import { settingsFactory } from '../../settings/node';
import splitChangesMock1 from '../mocks/splitchanges.since.-1.json';
import splitChangesMock2 from '../mocks/splitchanges.since.1457552620999.json';
import membershipsFacundo from '../mocks/[email protected]';
import { NONE } from '@splitsoftware/splitio-commons/src/utils/constants';
import { truncateTimeFrame } from '@splitsoftware/splitio-commons/src/utils/time';
Expand Down Expand Up @@ -42,7 +41,6 @@ const config = {
export default async 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.3&since=-1&rbSince=-1'), { status: 200, body: splitChangesMock1 });
fetchMock.get(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), { status: 200, body: splitChangesMock2 });
fetchMock.get(url(settings, '/memberships/facundo%40split.io'), { status: 200, body: membershipsFacundo });
fetchMock.get(url(settings, '/memberships/emma%40split.io'), { status: 200, body: membershipsFacundo });

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/browserSuites/impressions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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.3&since=-1&rbSince=-1'), { status: 200, body: splitChangesMock1 });
fetchMock.get(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), { status: 200, body: splitChangesMock2 });
fetchMock.get(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), { status: 200, body: splitChangesMock2 });
fetchMock.get(url(settings, '/memberships/facundo%40split.io'), { status: 200, body: membershipsFacundo });

const splitio = SplitFactory({
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/browserSuites/push-corner-cases.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getStorageHash } from '@splitsoftware/splitio-commons/src/storages/KeyBuilder';
import splitChangesMock1 from '../mocks/splitchanges.since.-1.json';
import splitChangesMock2 from '../mocks/splitchanges.since.1457552620999.json';
import splitKillMessage from '../mocks/message.SPLIT_KILL.1457552650000.json';
import authPushEnabledNicolas from '../mocks/[email protected]';
import { nearlyEqual, url } from '../testUtils';
Expand Down Expand Up @@ -77,7 +78,7 @@ export function testSplitKillOnReadyFromCache(fetchMock, assert) {

// 2 splitChanges request: initial sync and after SSE opened. Sync after SPLIT_KILL is not performed because SplitsSyncTask is "executing"
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=25&rbSince=-1'), { status: 200, body: splitChangesMock1 }, { delay: MILLIS_SPLIT_CHANGES_RESPONSE, /* delay response */ });
fetchMock.getOnce(url(settings, `/splitChanges?s=1.3&since=${splitChangesMock1.ff.t}&rbSince=-1`), { status: 200, body: { ff: { d: [], s: splitChangesMock1.ff.t, t: splitChangesMock1.ff.t } } }, { delay: MILLIS_SPLIT_CHANGES_RESPONSE - 100, /* delay response */ });
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), { status: 200, body: splitChangesMock2 }, { delay: MILLIS_SPLIT_CHANGES_RESPONSE - 100, /* delay response */ });

fetchMock.get(new RegExp('.*'), function (url) {
assert.fail('unexpected GET request with url: ' + url);
Expand Down
12 changes: 6 additions & 6 deletions src/__tests__/browserSuites/push-fallback.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Validate the handling of OCCUPANCY and CONTROL events
*/

import splitChangesMock1 from '../mocks/splitchanges.real.withSegments.json'; // since: -1, till: 1457552620999 (for initial fetch)
import splitChangesMock2 from '../mocks/splitchanges.real.updateWithSegments.json'; // since: 1457552620999, till: 1457552649999 (for SPLIT_UPDATE event)
import splitChangesMock3 from '../mocks/splitchanges.real.updateWithoutSegments.json'; // since: 1457552649999, till: 1457552669999 (for second polling fetch)
import splitChangesMockReal1 from '../mocks/splitchanges.real.withSegments.json'; // since: -1, till: 1457552620999 (for initial fetch)
import splitChangesMockReal2 from '../mocks/splitchanges.real.updateWithSegments.json'; // since: 1457552620999, till: 1457552649999 (for SPLIT_UPDATE event)
import splitChangesMockReal3 from '../mocks/splitchanges.real.updateWithoutSegments.json'; // since: 1457552649999, till: 1457552669999 (for second polling fetch)
import membershipsNicolasMock1 from '../mocks/[email protected]';
import membershipsNicolasMock2 from '../mocks/[email protected]';
import membershipsMarcio from '../mocks/[email protected]';
Expand Down Expand Up @@ -213,7 +213,7 @@ export function testFallback(fetchMock, assert) {
});

// initial split and memberships sync
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=-1&rbSince=-1'), { status: 200, body: splitChangesMock1 });
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=-1&rbSince=-1'), { status: 200, body: splitChangesMockReal1 });
fetchMock.getOnce(url(settings, '/memberships/nicolas%40split.io'), { status: 200, body: membershipsNicolasMock1 });

// split and segment sync after SSE opened
Expand Down Expand Up @@ -249,7 +249,7 @@ export function testFallback(fetchMock, assert) {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, MILLIS_SPLIT_UPDATE_EVENT_DURING_PUSH), 'sync due to SPLIT_UPDATE event');
return { status: 200, body: splitChangesMock2 };
return { status: 200, body: splitChangesMockReal2 };
});

// fetches due to second fallback to polling
Expand Down Expand Up @@ -288,7 +288,7 @@ export function testFallback(fetchMock, assert) {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552649999&rbSince=-1'), function () {
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, MILLIS_STREAMING_DISABLED_CONTROL + settings.scheduler.featuresRefreshRate, 100), 'fetch due to fourth fallback to polling');
return { status: 200, body: splitChangesMock3 };
return { status: 200, body: splitChangesMockReal3 };
});
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552669999&rbSince=-1'), function () {
const lapse = Date.now() - start;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ function testInitializationFail(fetchMock, assert, fallbackToPolling) {
});

if (fallbackToPolling) {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
assert.true(ready, 'client ready');
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, 0), 'polling (first fetch)');
return { status: 200, body: splitChangesMock2 };
});
}

fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
assert.true(ready, 'client ready');
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, settings.scheduler.featuresRefreshRate), 'polling (second fetch)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ export function testPushRetriesDueToAuthErrors(fetchMock, assert) {
assert.true(nearlyEqual(lapse, 0), 'initial sync');
return { status: 200, body: splitChangesMock1 };
});
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
assert.true(ready, 'client ready before first polling fetch');
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, 0), 'fallback to polling');
return { status: 200, body: splitChangesMock2 };
});
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, settings.scheduler.featuresRefreshRate), 'polling');
return { status: 200, body: splitChangesMock2 };
});
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, settings.scheduler.featuresRefreshRate * 2), 'keep polling since auth success buth with push disabled');
client.destroy().then(() => {
Expand Down Expand Up @@ -142,18 +142,18 @@ export function testPushRetriesDueToSseErrors(fetchMock, assert) {
assert.true(nearlyEqual(lapse, 0), 'initial sync');
return { status: 200, body: splitChangesMock1 };
});
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
assert.true(ready, 'client ready before first polling fetch');
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, 0), 'fallback to polling');
return { status: 200, body: splitChangesMock2 };
});
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, settings.scheduler.featuresRefreshRate), 'polling');
return { status: 200, body: splitChangesMock2 };
});
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, expectedTimeToSSEsuccess), 'sync due to success SSE connection');
client.destroy().then(() => {
Expand Down Expand Up @@ -260,7 +260,7 @@ export function testSdkDestroyWhileAuthRetries(fetchMock, assert) {

fetchMock.get({ url: url(settings, '/memberships/nicolas%40split.io'), repeat: 2 }, { status: 200, body: membershipsNicolasMock });
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=-1&rbSince=-1'), { status: 200, body: splitChangesMock1 });
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), { status: 200, body: splitChangesMock2 });
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), { status: 200, body: splitChangesMock2 });

fetchMock.get(new RegExp('.*'), function (url) {
assert.fail('unexpected GET request with url: ' + url);
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/browserSuites/push-refresh-token.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function testRefreshToken(fetchMock, assert) {
});

// sync after SSE opened
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), { status: 200, body: splitChangesMock2 });
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), { status: 200, body: splitChangesMock2 });
fetchMock.getOnce(url(settings, '/memberships/nicolas%40split.io'), { status: 200, body: membershipsNicolasMock1 });

// re-auth due to refresh token, with connDelay of 0.5 seconds
Expand All @@ -100,7 +100,7 @@ export function testRefreshToken(fetchMock, assert) {
});

// sync after SSE reopened
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, MILLIS_REFRESH_TOKEN + MILLIS_CONNDELAY), 'sync after SSE connection is reopened');
return { status: 200, body: { ff: { d: [], s: 1457552620999, t: 1457552620999 } } };
Expand All @@ -116,7 +116,7 @@ export function testRefreshToken(fetchMock, assert) {
});

// split sync after SSE closed due to push disabled
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=-1'), function () {
fetchMock.getOnce(url(settings, '/splitChanges?s=1.3&since=1457552620999&rbSince=100'), function () {
const lapse = Date.now() - start;
assert.true(nearlyEqual(lapse, MILLIS_REFRESH_TOKEN * 2), 'sync after SSE connection is reopened a second time');
setTimeout(() => {
Expand Down
Loading