2021-02-04 19:15:23 +01:00
|
|
|
import { strict as assert } from 'assert';
|
|
|
|
import sinon from 'sinon';
|
2023-06-09 22:48:48 +02:00
|
|
|
import { toHex } from '@metamask/controller-utils';
|
2021-03-16 22:00:08 +01:00
|
|
|
import { ENVIRONMENT_TYPE_BACKGROUND } from '../../../shared/constants/app';
|
|
|
|
import { createSegmentMock } from '../lib/segment';
|
2020-12-02 22:41:30 +01:00
|
|
|
import {
|
|
|
|
METAMETRICS_ANONYMOUS_ID,
|
|
|
|
METAMETRICS_BACKGROUND_PAGE_OBJECT,
|
2023-04-03 17:31:04 +02:00
|
|
|
MetaMetricsUserTrait,
|
2021-03-16 22:00:08 +01:00
|
|
|
} from '../../../shared/constants/metametrics';
|
|
|
|
import waitUntilCalled from '../../../test/lib/wait-until-called';
|
2023-06-22 20:46:09 +02:00
|
|
|
import { CHAIN_IDS, CURRENCY_SYMBOLS } from '../../../shared/constants/network';
|
2022-11-08 00:03:03 +01:00
|
|
|
import * as Utils from '../lib/util';
|
2021-03-16 22:00:08 +01:00
|
|
|
import MetaMetricsController from './metametrics';
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const segment = createSegmentMock(2, 10000);
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const VERSION = '0.0.1-test';
|
|
|
|
const FAKE_CHAIN_ID = '0x1338';
|
|
|
|
const LOCALE = 'en_US';
|
|
|
|
const TEST_META_METRICS_ID = '0xabc';
|
2022-11-08 19:08:08 +01:00
|
|
|
const DUMMY_ACTION_ID = 'DUMMY_ACTION_ID';
|
2023-05-23 16:16:23 +02:00
|
|
|
const MOCK_EXTENSION_ID = 'testid';
|
|
|
|
|
|
|
|
const MOCK_EXTENSION = {
|
|
|
|
runtime: {
|
|
|
|
id: MOCK_EXTENSION_ID,
|
|
|
|
setUninstallURL: () => undefined,
|
|
|
|
},
|
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2022-03-31 21:21:01 +02:00
|
|
|
const MOCK_TRAITS = {
|
|
|
|
test_boolean: true,
|
|
|
|
test_string: 'abc',
|
|
|
|
test_number: 123,
|
|
|
|
test_bool_array: [true, true, false],
|
|
|
|
test_string_array: ['test', 'test', 'test'],
|
|
|
|
test_boolean_array: [1, 2, 3],
|
|
|
|
};
|
|
|
|
|
|
|
|
const MOCK_INVALID_TRAITS = {
|
|
|
|
test_null: null,
|
|
|
|
test_array_multi_types: [true, 'a', 1],
|
|
|
|
};
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
const DEFAULT_TEST_CONTEXT = {
|
2023-05-23 16:16:23 +02:00
|
|
|
app: {
|
|
|
|
name: 'MetaMask Extension',
|
|
|
|
version: VERSION,
|
|
|
|
extensionId: MOCK_EXTENSION_ID,
|
|
|
|
},
|
2020-12-02 22:41:30 +01:00
|
|
|
page: METAMETRICS_BACKGROUND_PAGE_OBJECT,
|
|
|
|
referrer: undefined,
|
|
|
|
userAgent: window.navigator.userAgent,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
const DEFAULT_SHARED_PROPERTIES = {
|
|
|
|
chain_id: FAKE_CHAIN_ID,
|
|
|
|
locale: LOCALE.replace('_', '-'),
|
|
|
|
environment_type: 'background',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
const DEFAULT_EVENT_PROPERTIES = {
|
|
|
|
category: 'Unit Test',
|
|
|
|
revenue: undefined,
|
|
|
|
value: undefined,
|
|
|
|
currency: undefined,
|
2023-05-23 16:16:23 +02:00
|
|
|
extensionId: MOCK_EXTENSION_ID,
|
2020-12-02 22:41:30 +01:00
|
|
|
...DEFAULT_SHARED_PROPERTIES,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
const DEFAULT_PAGE_PROPERTIES = {
|
|
|
|
...DEFAULT_SHARED_PROPERTIES,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
function getMockPreferencesStore({ currentLocale = LOCALE } = {}) {
|
|
|
|
let preferencesStore = {
|
|
|
|
currentLocale,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const subscribe = sinon.stub();
|
2020-12-02 22:41:30 +01:00
|
|
|
const updateState = (newState) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
preferencesStore = { ...preferencesStore, ...newState };
|
|
|
|
subscribe.getCall(0).args[0](preferencesStore);
|
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
return {
|
|
|
|
getState: sinon.stub().returns(preferencesStore),
|
|
|
|
updateState,
|
|
|
|
subscribe,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
|
|
|
|
2022-01-12 20:31:54 +01:00
|
|
|
const SAMPLE_PERSISTED_EVENT = {
|
|
|
|
id: 'testid',
|
|
|
|
persist: true,
|
|
|
|
category: 'Unit Test',
|
|
|
|
successEvent: 'sample persisted event success',
|
|
|
|
failureEvent: 'sample persisted event failure',
|
|
|
|
properties: {
|
|
|
|
test: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const SAMPLE_NON_PERSISTED_EVENT = {
|
|
|
|
id: 'testid2',
|
|
|
|
persist: false,
|
|
|
|
category: 'Unit Test',
|
|
|
|
successEvent: 'sample non-persisted event success',
|
|
|
|
failureEvent: 'sample non-persisted event failure',
|
2023-03-07 16:46:41 +01:00
|
|
|
uniqueIdentifier: 'sample-non-persisted-event',
|
2022-01-12 20:31:54 +01:00
|
|
|
properties: {
|
|
|
|
test: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
function getMetaMetricsController({
|
|
|
|
participateInMetaMetrics = true,
|
|
|
|
metaMetricsId = TEST_META_METRICS_ID,
|
|
|
|
preferencesStore = getMockPreferencesStore(),
|
2023-06-22 20:46:09 +02:00
|
|
|
getCurrentChainId = () => FAKE_CHAIN_ID,
|
|
|
|
onNetworkDidChange = () => {
|
|
|
|
// do nothing
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
segmentInstance,
|
2020-12-02 22:41:30 +01:00
|
|
|
} = {}) {
|
|
|
|
return new MetaMetricsController({
|
2022-11-08 00:03:03 +01:00
|
|
|
segment: segmentInstance || segment,
|
2023-06-22 20:46:09 +02:00
|
|
|
getCurrentChainId,
|
|
|
|
onNetworkDidChange,
|
2020-12-02 22:41:30 +01:00
|
|
|
preferencesStore,
|
|
|
|
version: '0.0.1',
|
|
|
|
environment: 'test',
|
|
|
|
initState: {
|
|
|
|
participateInMetaMetrics,
|
|
|
|
metaMetricsId,
|
2022-01-12 20:31:54 +01:00
|
|
|
fragments: {
|
|
|
|
testid: SAMPLE_PERSISTED_EVENT,
|
|
|
|
testid2: SAMPLE_NON_PERSISTED_EVENT,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
events: {},
|
2020-12-02 22:41:30 +01:00
|
|
|
},
|
2023-05-23 16:16:23 +02:00
|
|
|
extension: MOCK_EXTENSION,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
2023-06-22 20:46:09 +02:00
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
describe('MetaMetricsController', function () {
|
2022-11-08 00:03:03 +01:00
|
|
|
const now = new Date();
|
|
|
|
let clock;
|
|
|
|
beforeEach(function () {
|
2023-07-26 14:13:28 +02:00
|
|
|
globalThis.sentry = {
|
|
|
|
startSession: sinon.fake(() => {
|
|
|
|
/** NOOP */
|
|
|
|
}),
|
|
|
|
endSession: sinon.fake(() => {
|
|
|
|
/** NOOP */
|
|
|
|
}),
|
|
|
|
};
|
2022-11-08 00:03:03 +01:00
|
|
|
clock = sinon.useFakeTimers(now.getTime());
|
|
|
|
sinon.stub(Utils, 'generateRandomId').returns('DUMMY_RANDOM_ID');
|
|
|
|
});
|
2023-01-05 15:49:55 +01:00
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
describe('constructor', function () {
|
|
|
|
it('should properly initialize', function () {
|
2022-01-12 20:31:54 +01:00
|
|
|
const mock = sinon.mock(segment);
|
|
|
|
mock
|
|
|
|
.expects('track')
|
|
|
|
.once()
|
|
|
|
.withArgs({
|
|
|
|
event: 'sample non-persisted event failure',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
test: true,
|
|
|
|
},
|
2023-03-07 16:46:41 +01:00
|
|
|
messageId: 'sample-non-persisted-event-failure',
|
2022-11-08 00:03:03 +01:00
|
|
|
timestamp: new Date(),
|
2022-01-12 20:31:54 +01:00
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
assert.strictEqual(metaMetricsController.version, VERSION);
|
|
|
|
assert.strictEqual(metaMetricsController.chainId, FAKE_CHAIN_ID);
|
2020-12-02 22:41:30 +01:00
|
|
|
assert.strictEqual(
|
|
|
|
metaMetricsController.state.participateInMetaMetrics,
|
|
|
|
true,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-12-02 22:41:30 +01:00
|
|
|
assert.strictEqual(
|
|
|
|
metaMetricsController.state.metaMetricsId,
|
|
|
|
TEST_META_METRICS_ID,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
metaMetricsController.locale,
|
|
|
|
LOCALE.replace('_', '-'),
|
|
|
|
);
|
2022-01-12 20:31:54 +01:00
|
|
|
assert.deepStrictEqual(metaMetricsController.state.fragments, {
|
|
|
|
testid: SAMPLE_PERSISTED_EVENT,
|
|
|
|
});
|
|
|
|
mock.verify();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should update when network changes', function () {
|
2023-06-22 20:46:09 +02:00
|
|
|
let chainId = '0x111';
|
|
|
|
let networkDidChangeListener;
|
|
|
|
const onNetworkDidChange = (listener) => {
|
|
|
|
networkDidChangeListener = listener;
|
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController({
|
2023-06-22 20:46:09 +02:00
|
|
|
getCurrentChainId: () => chainId,
|
|
|
|
onNetworkDidChange,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2023-06-22 20:46:09 +02:00
|
|
|
|
|
|
|
chainId = '0x222';
|
|
|
|
networkDidChangeListener();
|
|
|
|
|
|
|
|
assert.strictEqual(metaMetricsController.chainId, '0x222');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should update when preferences changes', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const preferencesStore = getMockPreferencesStore();
|
2020-12-02 22:41:30 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
preferencesStore,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
preferencesStore.updateState({
|
|
|
|
currentLocale: 'en_UK',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
assert.strictEqual(metaMetricsController.locale, 'en-UK');
|
|
|
|
});
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
describe('generateMetaMetricsId', function () {
|
|
|
|
it('should generate an 0x prefixed hex string', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController();
|
2020-12-02 22:41:30 +01:00
|
|
|
assert.equal(
|
|
|
|
metaMetricsController.generateMetaMetricsId().startsWith('0x'),
|
|
|
|
true,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2022-03-31 21:21:01 +02:00
|
|
|
describe('identify', function () {
|
|
|
|
it('should call segment.identify for valid traits if user is participating in metametrics', async function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
participateInMetaMetrics: true,
|
|
|
|
metaMetricsId: TEST_META_METRICS_ID,
|
|
|
|
});
|
|
|
|
const mock = sinon.mock(segment);
|
|
|
|
|
2022-11-08 00:03:03 +01:00
|
|
|
mock.expects('identify').once().withArgs({
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
traits: MOCK_TRAITS,
|
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
|
|
|
});
|
2022-03-31 21:21:01 +02:00
|
|
|
|
|
|
|
metaMetricsController.identify({
|
|
|
|
...MOCK_TRAITS,
|
|
|
|
...MOCK_INVALID_TRAITS,
|
|
|
|
});
|
2022-11-08 00:03:03 +01:00
|
|
|
|
2022-03-31 21:21:01 +02:00
|
|
|
mock.verify();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should transform date type traits into ISO-8601 timestamp strings', async function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
participateInMetaMetrics: true,
|
|
|
|
metaMetricsId: TEST_META_METRICS_ID,
|
|
|
|
});
|
|
|
|
const mock = sinon.mock(segment);
|
|
|
|
|
|
|
|
const mockDate = new Date();
|
|
|
|
const mockDateISOString = mockDate.toISOString();
|
|
|
|
|
|
|
|
mock
|
|
|
|
.expects('identify')
|
|
|
|
.once()
|
|
|
|
.withArgs({
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
traits: {
|
|
|
|
test_date: mockDateISOString,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
2022-03-31 21:21:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
metaMetricsController.identify({
|
|
|
|
test_date: mockDate,
|
|
|
|
});
|
|
|
|
mock.verify();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call segment.identify if user is not participating in metametrics', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
participateInMetaMetrics: false,
|
|
|
|
});
|
|
|
|
const mock = sinon.mock(segment);
|
|
|
|
|
|
|
|
mock.expects('identify').never();
|
|
|
|
|
|
|
|
metaMetricsController.identify(MOCK_TRAITS);
|
|
|
|
mock.verify();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call segment.identify if there are no valid traits to identify', async function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
participateInMetaMetrics: true,
|
|
|
|
metaMetricsId: TEST_META_METRICS_ID,
|
|
|
|
});
|
|
|
|
const mock = sinon.mock(segment);
|
|
|
|
|
|
|
|
mock.expects('identify').never();
|
|
|
|
|
|
|
|
metaMetricsController.identify(MOCK_INVALID_TRAITS);
|
|
|
|
mock.verify();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
describe('setParticipateInMetaMetrics', function () {
|
2023-07-31 23:19:32 +02:00
|
|
|
it('should update the value of participateInMetaMetrics', async function () {
|
2020-12-02 22:41:30 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
participateInMetaMetrics: null,
|
|
|
|
metaMetricsId: null,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
assert.equal(metaMetricsController.state.participateInMetaMetrics, null);
|
2023-07-31 23:19:32 +02:00
|
|
|
await metaMetricsController.setParticipateInMetaMetrics(true);
|
2023-07-26 14:13:28 +02:00
|
|
|
assert.ok(globalThis.sentry.startSession.calledOnce);
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(metaMetricsController.state.participateInMetaMetrics, true);
|
2023-07-31 23:19:32 +02:00
|
|
|
await metaMetricsController.setParticipateInMetaMetrics(false);
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(metaMetricsController.state.participateInMetaMetrics, false);
|
|
|
|
});
|
2023-07-31 23:19:32 +02:00
|
|
|
it('should generate and update the metaMetricsId when set to true', async function () {
|
2020-12-02 22:41:30 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
participateInMetaMetrics: null,
|
|
|
|
metaMetricsId: null,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
assert.equal(metaMetricsController.state.metaMetricsId, null);
|
2023-07-31 23:19:32 +02:00
|
|
|
await metaMetricsController.setParticipateInMetaMetrics(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(typeof metaMetricsController.state.metaMetricsId, 'string');
|
|
|
|
});
|
2023-07-31 23:19:32 +02:00
|
|
|
it('should nullify the metaMetricsId when set to false', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController();
|
2023-07-31 23:19:32 +02:00
|
|
|
await metaMetricsController.setParticipateInMetaMetrics(false);
|
2023-07-26 14:13:28 +02:00
|
|
|
assert.ok(globalThis.sentry.endSession.calledOnce);
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(metaMetricsController.state.metaMetricsId, null);
|
|
|
|
});
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2021-11-11 04:27:04 +01:00
|
|
|
describe('submitEvent', function () {
|
2020-12-02 22:41:30 +01:00
|
|
|
it('should not track an event if user is not participating in metametrics', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mock = sinon.mock(segment);
|
2020-12-02 22:41:30 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
participateInMetaMetrics: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
mock.expects('track').never();
|
2021-11-11 04:27:04 +01:00
|
|
|
metaMetricsController.submitEvent({
|
2020-12-02 22:41:30 +01:00
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
properties: {
|
|
|
|
test: 1,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
mock.verify();
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should track an event if user has not opted in, but isOptIn is true', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mock = sinon.mock(segment);
|
2020-12-02 22:41:30 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController({
|
2022-11-23 19:00:05 +01:00
|
|
|
participateInMetaMetrics: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
mock
|
|
|
|
.expects('track')
|
|
|
|
.once()
|
|
|
|
.withArgs({
|
|
|
|
event: 'Fake Event',
|
|
|
|
anonymousId: METAMETRICS_ANONYMOUS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
test: 1,
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-11-11 04:27:04 +01:00
|
|
|
metaMetricsController.submitEvent(
|
2020-12-02 22:41:30 +01:00
|
|
|
{
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
properties: {
|
|
|
|
test: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ isOptIn: true },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
mock.verify();
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should track an event during optin and allow for metaMetricsId override', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mock = sinon.mock(segment);
|
2020-12-02 22:41:30 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController({
|
2022-11-23 19:00:05 +01:00
|
|
|
participateInMetaMetrics: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
mock
|
|
|
|
.expects('track')
|
|
|
|
.once()
|
|
|
|
.withArgs({
|
|
|
|
event: 'Fake Event',
|
|
|
|
userId: 'TESTID',
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
test: 1,
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-11-11 04:27:04 +01:00
|
|
|
metaMetricsController.submitEvent(
|
2020-12-02 22:41:30 +01:00
|
|
|
{
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
properties: {
|
|
|
|
test: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ isOptIn: true, metaMetricsId: 'TESTID' },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
mock.verify();
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should track a legacy event', function () {
|
2021-04-26 18:05:43 +02:00
|
|
|
const mock = sinon.mock(segment);
|
2021-02-04 19:15:23 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController();
|
2020-12-02 22:41:30 +01:00
|
|
|
mock
|
|
|
|
.expects('track')
|
|
|
|
.once()
|
|
|
|
.withArgs({
|
|
|
|
event: 'Fake Event',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
test: 1,
|
2021-04-26 18:05:43 +02:00
|
|
|
legacy_event: true,
|
2020-12-02 22:41:30 +01:00
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-11-11 04:27:04 +01:00
|
|
|
metaMetricsController.submitEvent(
|
2020-12-02 22:41:30 +01:00
|
|
|
{
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
properties: {
|
|
|
|
test: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ matomoEvent: true },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
mock.verify();
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should track a non legacy event', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mock = sinon.mock(segment);
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
2020-12-02 22:41:30 +01:00
|
|
|
mock
|
|
|
|
.expects('track')
|
|
|
|
.once()
|
|
|
|
.withArgs({
|
|
|
|
event: 'Fake Event',
|
|
|
|
properties: {
|
|
|
|
test: 1,
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-11-11 04:27:04 +01:00
|
|
|
metaMetricsController.submitEvent({
|
2020-12-02 22:41:30 +01:00
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
properties: {
|
|
|
|
test: 1,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
mock.verify();
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should immediately flush queue if flushImmediately set to true', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
const flushStub = sinon.stub(segment, 'flush');
|
|
|
|
const flushCalled = waitUntilCalled(flushStub, segment);
|
2021-11-11 04:27:04 +01:00
|
|
|
metaMetricsController.submitEvent(
|
2020-12-02 22:41:30 +01:00
|
|
|
{
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
},
|
|
|
|
{ flushImmediately: true },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.doesNotReject(flushCalled());
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should throw if event or category not provided', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController();
|
2020-12-02 22:41:30 +01:00
|
|
|
assert.rejects(
|
2021-11-11 04:27:04 +01:00
|
|
|
() => metaMetricsController.submitEvent({ event: 'test' }),
|
2020-12-02 22:41:30 +01:00
|
|
|
/Must specify event and category\./u,
|
|
|
|
'must specify category',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
assert.rejects(
|
2021-11-11 04:27:04 +01:00
|
|
|
() => metaMetricsController.submitEvent({ category: 'test' }),
|
2020-12-02 22:41:30 +01:00
|
|
|
/Must specify event and category\./u,
|
|
|
|
'must specify event',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should throw if provided sensitiveProperties, when excludeMetaMetricsId is true', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController();
|
2020-12-02 22:41:30 +01:00
|
|
|
assert.rejects(
|
|
|
|
() =>
|
2021-11-11 04:27:04 +01:00
|
|
|
metaMetricsController.submitEvent(
|
2020-12-02 22:41:30 +01:00
|
|
|
{
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
sensitiveProperties: { foo: 'bar' },
|
|
|
|
},
|
|
|
|
{ excludeMetaMetricsId: true },
|
|
|
|
),
|
|
|
|
/sensitiveProperties was specified in an event payload that also set the excludeMetaMetricsId flag/u,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should track sensitiveProperties in a separate, anonymous event', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
const spy = sinon.spy(segment, 'track');
|
2021-11-11 04:27:04 +01:00
|
|
|
metaMetricsController.submitEvent({
|
2020-12-02 22:41:30 +01:00
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
sensitiveProperties: { foo: 'bar' },
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
assert.ok(spy.calledTwice);
|
2020-12-02 22:41:30 +01:00
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
anonymousId: METAMETRICS_ANONYMOUS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
foo: 'bar',
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
2020-12-02 22:41:30 +01:00
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-12-02 22:41:30 +01:00
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: DEFAULT_EVENT_PROPERTIES,
|
2022-11-08 00:03:03 +01:00
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
2020-12-02 22:41:30 +01:00
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
describe('trackPage', function () {
|
|
|
|
it('should track a page view', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mock = sinon.mock(segment);
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
2020-12-02 22:41:30 +01:00
|
|
|
mock
|
|
|
|
.expects('page')
|
|
|
|
.once()
|
|
|
|
.withArgs({
|
|
|
|
name: 'home',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
params: null,
|
|
|
|
...DEFAULT_PAGE_PROPERTIES,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
metaMetricsController.trackPage({
|
|
|
|
name: 'home',
|
|
|
|
params: null,
|
|
|
|
environmentType: ENVIRONMENT_TYPE_BACKGROUND,
|
|
|
|
page: METAMETRICS_BACKGROUND_PAGE_OBJECT,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
mock.verify();
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should not track a page view if user is not participating in metametrics', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mock = sinon.mock(segment);
|
2020-12-02 22:41:30 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
participateInMetaMetrics: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
mock.expects('page').never();
|
2020-12-02 22:41:30 +01:00
|
|
|
metaMetricsController.trackPage({
|
|
|
|
name: 'home',
|
|
|
|
params: null,
|
|
|
|
environmentType: ENVIRONMENT_TYPE_BACKGROUND,
|
|
|
|
page: METAMETRICS_BACKGROUND_PAGE_OBJECT,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
mock.verify();
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
it('should track a page view if isOptInPath is true and user not yet opted in', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mock = sinon.mock(segment);
|
2020-12-02 22:41:30 +01:00
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
preferencesStore: getMockPreferencesStore({
|
|
|
|
participateInMetaMetrics: null,
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
mock
|
|
|
|
.expects('page')
|
|
|
|
.once()
|
|
|
|
.withArgs({
|
|
|
|
name: 'home',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
params: null,
|
|
|
|
...DEFAULT_PAGE_PROPERTIES,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
messageId: Utils.generateRandomId(),
|
|
|
|
timestamp: new Date(),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
metaMetricsController.trackPage(
|
|
|
|
{
|
|
|
|
name: 'home',
|
|
|
|
params: null,
|
|
|
|
environmentType: ENVIRONMENT_TYPE_BACKGROUND,
|
|
|
|
page: METAMETRICS_BACKGROUND_PAGE_OBJECT,
|
|
|
|
},
|
|
|
|
{ isOptInPath: true },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
mock.verify();
|
|
|
|
});
|
2022-11-08 19:08:08 +01:00
|
|
|
|
|
|
|
it('multiple trackPage call with same actionId should result in same messageId being sent to segment', function () {
|
|
|
|
const mock = sinon.mock(segment);
|
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
preferencesStore: getMockPreferencesStore({
|
|
|
|
participateInMetaMetrics: null,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
mock
|
|
|
|
.expects('page')
|
|
|
|
.twice()
|
|
|
|
.withArgs({
|
|
|
|
name: 'home',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
params: null,
|
|
|
|
...DEFAULT_PAGE_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: DUMMY_ACTION_ID,
|
|
|
|
timestamp: new Date(),
|
|
|
|
});
|
|
|
|
metaMetricsController.trackPage(
|
|
|
|
{
|
|
|
|
name: 'home',
|
|
|
|
params: null,
|
|
|
|
actionId: DUMMY_ACTION_ID,
|
|
|
|
environmentType: ENVIRONMENT_TYPE_BACKGROUND,
|
|
|
|
page: METAMETRICS_BACKGROUND_PAGE_OBJECT,
|
|
|
|
},
|
|
|
|
{ isOptInPath: true },
|
|
|
|
);
|
|
|
|
metaMetricsController.trackPage(
|
|
|
|
{
|
|
|
|
name: 'home',
|
|
|
|
params: null,
|
|
|
|
actionId: DUMMY_ACTION_ID,
|
|
|
|
environmentType: ENVIRONMENT_TYPE_BACKGROUND,
|
|
|
|
page: METAMETRICS_BACKGROUND_PAGE_OBJECT,
|
|
|
|
},
|
|
|
|
{ isOptInPath: true },
|
|
|
|
);
|
|
|
|
mock.verify();
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2023-01-05 15:49:55 +01:00
|
|
|
describe('deterministic messageId', function () {
|
|
|
|
it('should use the actionId as messageId when provided', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
const spy = sinon.spy(segment, 'track');
|
|
|
|
metaMetricsController.submitEvent({
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
properties: { foo: 'bar' },
|
|
|
|
actionId: '0x001',
|
|
|
|
});
|
|
|
|
assert.ok(spy.calledOnce);
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
foo: 'bar',
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: '0x001',
|
|
|
|
timestamp: new Date(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should append 0x000 to the actionId of anonymized event when tracking sensitiveProperties', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
const spy = sinon.spy(segment, 'track');
|
|
|
|
metaMetricsController.submitEvent({
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
sensitiveProperties: { foo: 'bar' },
|
|
|
|
actionId: '0x001',
|
|
|
|
});
|
|
|
|
assert.ok(spy.calledTwice);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
anonymousId: METAMETRICS_ANONYMOUS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
foo: 'bar',
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: '0x001-0x000',
|
|
|
|
timestamp: new Date(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: '0x001',
|
|
|
|
timestamp: new Date(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use the uniqueIdentifier as messageId when provided', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
const spy = sinon.spy(segment, 'track');
|
|
|
|
metaMetricsController.submitEvent({
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
properties: { foo: 'bar' },
|
|
|
|
uniqueIdentifier: 'transaction-submitted-0000',
|
|
|
|
});
|
|
|
|
assert.ok(spy.calledOnce);
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
foo: 'bar',
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: 'transaction-submitted-0000',
|
|
|
|
timestamp: new Date(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should append 0x000 to the uniqueIdentifier of anonymized event when tracking sensitiveProperties', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
const spy = sinon.spy(segment, 'track');
|
|
|
|
metaMetricsController.submitEvent({
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
sensitiveProperties: { foo: 'bar' },
|
|
|
|
uniqueIdentifier: 'transaction-submitted-0000',
|
|
|
|
});
|
|
|
|
assert.ok(spy.calledTwice);
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
anonymousId: METAMETRICS_ANONYMOUS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
foo: 'bar',
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: 'transaction-submitted-0000-0x000',
|
|
|
|
timestamp: new Date(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: 'transaction-submitted-0000',
|
|
|
|
timestamp: new Date(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should combine the uniqueIdentifier and actionId as messageId when both provided', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
const spy = sinon.spy(segment, 'track');
|
|
|
|
metaMetricsController.submitEvent({
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
properties: { foo: 'bar' },
|
|
|
|
actionId: '0x001',
|
|
|
|
uniqueIdentifier: 'transaction-submitted-0000',
|
|
|
|
});
|
|
|
|
assert.ok(spy.calledOnce);
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
foo: 'bar',
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: 'transaction-submitted-0000-0x001',
|
|
|
|
timestamp: new Date(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should append 0x000 to the combined uniqueIdentifier and actionId of anonymized event when tracking sensitiveProperties', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
const spy = sinon.spy(segment, 'track');
|
|
|
|
metaMetricsController.submitEvent({
|
|
|
|
event: 'Fake Event',
|
|
|
|
category: 'Unit Test',
|
|
|
|
sensitiveProperties: { foo: 'bar' },
|
|
|
|
actionId: '0x001',
|
|
|
|
uniqueIdentifier: 'transaction-submitted-0000',
|
|
|
|
});
|
|
|
|
assert.ok(spy.calledTwice);
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
anonymousId: METAMETRICS_ANONYMOUS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
foo: 'bar',
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: 'transaction-submitted-0000-0x001-0x000',
|
|
|
|
timestamp: new Date(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith({
|
|
|
|
event: 'Fake Event',
|
|
|
|
userId: TEST_META_METRICS_ID,
|
|
|
|
context: DEFAULT_TEST_CONTEXT,
|
|
|
|
properties: {
|
|
|
|
...DEFAULT_EVENT_PROPERTIES,
|
|
|
|
},
|
|
|
|
messageId: 'transaction-submitted-0000-0x001',
|
|
|
|
timestamp: new Date(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-28 23:56:56 +02:00
|
|
|
describe('_buildUserTraitsObject', function () {
|
|
|
|
it('should return full user traits object on first call', function () {
|
2022-04-15 18:35:07 +02:00
|
|
|
const MOCK_ALL_TOKENS = {
|
2023-06-09 22:48:48 +02:00
|
|
|
[toHex(1)]: {
|
2022-04-15 18:35:07 +02:00
|
|
|
'0x1235ce91d74254f29d4609f25932fe6d97bf4842': [
|
|
|
|
{
|
|
|
|
address: '0xd2cea331e5f5d8ee9fb1055c297795937645de91',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: '0xabc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
'0xe364b0f9d1879e53e8183055c9d7dd2b7375d86b': [
|
|
|
|
{
|
|
|
|
address: '0xd2cea331e5f5d8ee9fb1055c297795937645de91',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-06-09 22:48:48 +02:00
|
|
|
[toHex(4)]: {
|
2022-04-15 18:35:07 +02:00
|
|
|
'0x1235ce91d74254f29d4609f25932fe6d97bf4842': [
|
|
|
|
{
|
|
|
|
address: '0xd2cea331e5f5d8ee9fb1055c297795937645de91',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: '0x12317F958D2ee523a2206206994597C13D831ec7',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-03-28 23:56:56 +02:00
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
const traits = metaMetricsController._buildUserTraitsObject({
|
2022-04-15 15:39:55 +02:00
|
|
|
addressBook: {
|
2022-09-14 16:55:31 +02:00
|
|
|
[CHAIN_IDS.MAINNET]: [{ address: '0x' }],
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: [{ address: '0x' }, { address: '0x0' }],
|
2022-04-15 15:39:55 +02:00
|
|
|
},
|
2022-11-15 19:49:42 +01:00
|
|
|
allNfts: {
|
2022-04-10 09:42:00 +02:00
|
|
|
'0xac706cE8A9BF27Afecf080fB298d0ee13cfb978A': {
|
2023-06-09 22:48:48 +02:00
|
|
|
[toHex(56)]: [
|
2022-04-10 09:42:00 +02:00
|
|
|
{
|
|
|
|
address: '0xd2cea331e5f5d8ee9fb1055c297795937645de91',
|
|
|
|
tokenId: '100',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: '0xd2cea331e5f5d8ee9fb1055c297795937645de91',
|
|
|
|
tokenId: '101',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: '0x7488d2ce5deb26db021285b50b661d655eb3d3d9',
|
|
|
|
tokenId: '99',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
'0xe04AB39684A24D8D4124b114F3bd6FBEB779cacA': {
|
2023-06-09 22:48:48 +02:00
|
|
|
[toHex(59)]: [
|
2022-04-10 09:42:00 +02:00
|
|
|
{
|
|
|
|
address: '0x63d646bc7380562376d5de205123a57b1718184d',
|
|
|
|
tokenId: '14',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2022-04-15 18:35:07 +02:00
|
|
|
allTokens: MOCK_ALL_TOKENS,
|
2023-03-09 22:00:28 +01:00
|
|
|
networkConfigurations: {
|
|
|
|
'network-configuration-id-1': {
|
|
|
|
chainId: CHAIN_IDS.MAINNET,
|
|
|
|
ticker: CURRENCY_SYMBOLS.ETH,
|
|
|
|
},
|
|
|
|
'network-configuration-id-2': {
|
|
|
|
chainId: CHAIN_IDS.GOERLI,
|
|
|
|
ticker: CURRENCY_SYMBOLS.TEST_ETH,
|
|
|
|
},
|
|
|
|
'network-configuration-id-3': { chainId: '0xaf' },
|
|
|
|
},
|
2022-04-15 15:39:55 +02:00
|
|
|
identities: [{}, {}],
|
|
|
|
ledgerTransportType: 'web-hid',
|
|
|
|
openSeaEnabled: true,
|
2022-11-15 19:49:42 +01:00
|
|
|
useNftDetection: false,
|
2022-04-18 18:31:44 +02:00
|
|
|
theme: 'default',
|
2022-05-11 22:27:58 +02:00
|
|
|
useTokenDetection: true,
|
2023-03-02 21:55:27 +01:00
|
|
|
desktopEnabled: false,
|
2023-03-23 18:01:51 +01:00
|
|
|
security_providers: [],
|
2022-03-28 23:56:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(traits, {
|
2023-04-03 17:31:04 +02:00
|
|
|
[MetaMetricsUserTrait.AddressBookEntries]: 3,
|
|
|
|
[MetaMetricsUserTrait.InstallDateExt]: '',
|
|
|
|
[MetaMetricsUserTrait.LedgerConnectionType]: 'web-hid',
|
|
|
|
[MetaMetricsUserTrait.NetworksAdded]: [
|
|
|
|
CHAIN_IDS.MAINNET,
|
|
|
|
CHAIN_IDS.GOERLI,
|
|
|
|
'0xaf',
|
|
|
|
],
|
|
|
|
[MetaMetricsUserTrait.NetworksWithoutTicker]: ['0xaf'],
|
|
|
|
[MetaMetricsUserTrait.NftAutodetectionEnabled]: false,
|
|
|
|
[MetaMetricsUserTrait.NumberOfAccounts]: 2,
|
|
|
|
[MetaMetricsUserTrait.NumberOfNftCollections]: 3,
|
|
|
|
[MetaMetricsUserTrait.NumberOfNfts]: 4,
|
|
|
|
[MetaMetricsUserTrait.NumberOfTokens]: 5,
|
|
|
|
[MetaMetricsUserTrait.OpenseaApiEnabled]: true,
|
|
|
|
[MetaMetricsUserTrait.ThreeBoxEnabled]: false,
|
|
|
|
[MetaMetricsUserTrait.Theme]: 'default',
|
|
|
|
[MetaMetricsUserTrait.TokenDetectionEnabled]: true,
|
|
|
|
[MetaMetricsUserTrait.DesktopEnabled]: false,
|
|
|
|
[MetaMetricsUserTrait.SecurityProviders]: [],
|
2023-05-23 16:16:23 +02:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
|
|
|
[MetaMetricsUserTrait.MmiExtensionId]: 'testid',
|
|
|
|
[MetaMetricsUserTrait.MmiAccountAddress]: null,
|
|
|
|
[MetaMetricsUserTrait.MmiIsCustodian]: false,
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
2022-03-28 23:56:56 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return only changed traits object on subsequent calls', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
metaMetricsController._buildUserTraitsObject({
|
2022-04-15 15:39:55 +02:00
|
|
|
addressBook: {
|
2022-09-14 16:55:31 +02:00
|
|
|
[CHAIN_IDS.MAINNET]: [{ address: '0x' }],
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: [{ address: '0x' }, { address: '0x0' }],
|
2022-04-15 15:39:55 +02:00
|
|
|
},
|
2022-04-15 18:35:07 +02:00
|
|
|
allTokens: {},
|
2023-03-09 22:00:28 +01:00
|
|
|
networkConfigurations: {
|
|
|
|
'network-configuration-id-1': { chainId: CHAIN_IDS.MAINNET },
|
|
|
|
'network-configuration-id-2': { chainId: CHAIN_IDS.GOERLI },
|
|
|
|
},
|
2022-03-28 23:56:56 +02:00
|
|
|
ledgerTransportType: 'web-hid',
|
2022-04-15 15:39:55 +02:00
|
|
|
openSeaEnabled: true,
|
2022-03-28 23:56:56 +02:00
|
|
|
identities: [{}, {}],
|
2022-11-15 19:49:42 +01:00
|
|
|
useNftDetection: false,
|
2022-04-18 18:31:44 +02:00
|
|
|
theme: 'default',
|
2022-05-11 22:27:58 +02:00
|
|
|
useTokenDetection: true,
|
2023-03-02 21:55:27 +01:00
|
|
|
desktopEnabled: false,
|
2022-03-28 23:56:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const updatedTraits = metaMetricsController._buildUserTraitsObject({
|
2022-04-15 15:39:55 +02:00
|
|
|
addressBook: {
|
2022-09-14 16:55:31 +02:00
|
|
|
[CHAIN_IDS.MAINNET]: [{ address: '0x' }, { address: '0x1' }],
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: [{ address: '0x' }, { address: '0x0' }],
|
2022-04-15 15:39:55 +02:00
|
|
|
},
|
2022-04-15 18:35:07 +02:00
|
|
|
allTokens: {
|
2023-06-09 22:48:48 +02:00
|
|
|
[toHex(1)]: {
|
|
|
|
'0xabcde': [{ '0x12345': { address: '0xtestAddress' } }],
|
|
|
|
},
|
2022-04-15 18:35:07 +02:00
|
|
|
},
|
2023-03-09 22:00:28 +01:00
|
|
|
networkConfigurations: {
|
|
|
|
'network-configuration-id-1': { chainId: CHAIN_IDS.MAINNET },
|
|
|
|
'network-configuration-id-2': { chainId: CHAIN_IDS.GOERLI },
|
|
|
|
},
|
2022-03-28 23:56:56 +02:00
|
|
|
ledgerTransportType: 'web-hid',
|
2022-04-15 15:39:55 +02:00
|
|
|
openSeaEnabled: false,
|
2022-03-28 23:56:56 +02:00
|
|
|
identities: [{}, {}, {}],
|
2022-11-15 19:49:42 +01:00
|
|
|
useNftDetection: false,
|
2022-04-18 18:31:44 +02:00
|
|
|
theme: 'default',
|
2022-05-11 22:27:58 +02:00
|
|
|
useTokenDetection: true,
|
2023-03-02 21:55:27 +01:00
|
|
|
desktopEnabled: false,
|
2022-03-28 23:56:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(updatedTraits, {
|
2023-04-03 17:31:04 +02:00
|
|
|
[MetaMetricsUserTrait.AddressBookEntries]: 4,
|
|
|
|
[MetaMetricsUserTrait.NumberOfAccounts]: 3,
|
|
|
|
[MetaMetricsUserTrait.NumberOfTokens]: 1,
|
|
|
|
[MetaMetricsUserTrait.OpenseaApiEnabled]: false,
|
2022-03-28 23:56:56 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return null if no traits changed', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController();
|
|
|
|
metaMetricsController._buildUserTraitsObject({
|
2022-04-15 15:39:55 +02:00
|
|
|
addressBook: {
|
2022-09-14 16:55:31 +02:00
|
|
|
[CHAIN_IDS.MAINNET]: [{ address: '0x' }],
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: [{ address: '0x' }, { address: '0x0' }],
|
2022-04-15 15:39:55 +02:00
|
|
|
},
|
2022-04-15 18:35:07 +02:00
|
|
|
allTokens: {},
|
2023-03-09 22:00:28 +01:00
|
|
|
networkConfigurations: {
|
|
|
|
'network-configuration-id-1': { chainId: CHAIN_IDS.MAINNET },
|
|
|
|
'network-configuration-id-2': { chainId: CHAIN_IDS.GOERLI },
|
|
|
|
},
|
2022-03-28 23:56:56 +02:00
|
|
|
ledgerTransportType: 'web-hid',
|
2022-04-15 15:39:55 +02:00
|
|
|
openSeaEnabled: true,
|
2022-03-28 23:56:56 +02:00
|
|
|
identities: [{}, {}],
|
2022-11-15 19:49:42 +01:00
|
|
|
useNftDetection: true,
|
2022-04-18 18:31:44 +02:00
|
|
|
theme: 'default',
|
2022-05-11 22:27:58 +02:00
|
|
|
useTokenDetection: true,
|
2023-03-02 21:55:27 +01:00
|
|
|
desktopEnabled: false,
|
2022-04-15 15:39:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const updatedTraits = metaMetricsController._buildUserTraitsObject({
|
2022-04-13 18:46:49 +02:00
|
|
|
addressBook: {
|
2022-09-14 16:55:31 +02:00
|
|
|
[CHAIN_IDS.MAINNET]: [{ address: '0x' }],
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: [{ address: '0x' }, { address: '0x0' }],
|
2022-04-13 18:46:49 +02:00
|
|
|
},
|
2022-04-15 18:35:07 +02:00
|
|
|
allTokens: {},
|
2023-03-09 22:00:28 +01:00
|
|
|
networkConfigurations: {
|
|
|
|
'network-configuration-id-1': { chainId: CHAIN_IDS.MAINNET },
|
|
|
|
'network-configuration-id-2': { chainId: CHAIN_IDS.GOERLI },
|
|
|
|
},
|
2022-03-28 23:56:56 +02:00
|
|
|
ledgerTransportType: 'web-hid',
|
2022-04-15 15:39:55 +02:00
|
|
|
openSeaEnabled: true,
|
2022-03-28 23:56:56 +02:00
|
|
|
identities: [{}, {}],
|
2022-11-15 19:49:42 +01:00
|
|
|
useNftDetection: true,
|
2022-04-18 18:31:44 +02:00
|
|
|
theme: 'default',
|
2022-05-11 22:27:58 +02:00
|
|
|
useTokenDetection: true,
|
2023-03-02 21:55:27 +01:00
|
|
|
desktopEnabled: false,
|
2022-03-28 23:56:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(updatedTraits, null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-08 00:03:03 +01:00
|
|
|
describe('submitting segmentApiCalls to segment SDK', function () {
|
|
|
|
it('should add event to store when submitting to SDK', function () {
|
|
|
|
const metaMetricsController = getMetaMetricsController({});
|
|
|
|
metaMetricsController.trackPage({}, { isOptIn: true });
|
|
|
|
const { segmentApiCalls } = metaMetricsController.store.getState();
|
|
|
|
assert(Object.keys(segmentApiCalls).length > 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove event from store when callback is invoked', function () {
|
|
|
|
const segmentInstance = createSegmentMock(2, 10000);
|
|
|
|
const stubFn = (_, cb) => {
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
sinon.stub(segmentInstance, 'track').callsFake(stubFn);
|
|
|
|
sinon.stub(segmentInstance, 'page').callsFake(stubFn);
|
|
|
|
|
|
|
|
const metaMetricsController = getMetaMetricsController({
|
|
|
|
segmentInstance,
|
|
|
|
});
|
|
|
|
metaMetricsController.trackPage({}, { isOptIn: true });
|
|
|
|
const { segmentApiCalls } = metaMetricsController.store.getState();
|
|
|
|
assert(Object.keys(segmentApiCalls).length === 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
afterEach(function () {
|
|
|
|
// flush the queues manually after each test
|
2021-02-04 19:15:23 +01:00
|
|
|
segment.flush();
|
2022-11-08 00:03:03 +01:00
|
|
|
clock.restore();
|
2021-02-04 19:15:23 +01:00
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
});
|