diff --git a/src/EventProcessor.js b/src/EventProcessor.js index 063695e..e8273cf 100644 --- a/src/EventProcessor.js +++ b/src/EventProcessor.js @@ -4,7 +4,6 @@ const ContextFilter = require('./ContextFilter'); const errors = require('./errors'); const messages = require('./messages'); const utils = require('./utils'); -const { getContextKeys } = require('./context'); function EventProcessor( platform, @@ -47,16 +46,10 @@ function EventProcessor( // Transform an event from its internal format to the format we use when sending a payload. function makeOutputEvent(e) { const ret = utils.extend({}, e); - if (e.kind === 'identify') { - // identify events always have an inline context - ret.context = contextFilter.filter(e.context); - } else if (e.kind === 'feature') { - // feature events always have an inline context - ret.context = contextFilter.filter(e.context, true); - } else { - ret.contextKeys = getContextKeysFromEvent(e); - delete ret['context']; - } + + // This method is used for identify, feature, and custom events, which always have an inline context. + ret.context = contextFilter.filter(e.context); + if (e.kind === 'feature') { delete ret['trackEvents']; delete ret['debugEventsUntilDate']; @@ -64,10 +57,6 @@ function EventProcessor( return ret; } - function getContextKeysFromEvent(event) { - return getContextKeys(event.context, logger); - } - function addToOutbox(event) { if (queue.length < eventCapacity) { queue.push(event); diff --git a/src/__tests__/EventProcessor-test.js b/src/__tests__/EventProcessor-test.js index 9288c97..5eb994d 100644 --- a/src/__tests__/EventProcessor-test.js +++ b/src/__tests__/EventProcessor-test.js @@ -59,16 +59,6 @@ describe.each([ } } - function checkUserInline(e, source, inlineUser) { - if (inlineUser) { - expect(e.context).toEqual(inlineUser); - expect(e.contextKeys).toBeUndefined(); - } else { - expect(e.contextKeys).toEqual({ user: source.context.key || source.context.user.key }); - expect(e.context).toBeUndefined(); - } - } - function checkFeatureEvent(e, source, debug, inlineUser) { expect(e.kind).toEqual(debug ? 'debug' : 'feature'); expect(e.creationDate).toEqual(source.creationDate); @@ -87,7 +77,7 @@ describe.each([ expect(e.key).toEqual(source.key); expect(e.data).toEqual(source.data); expect(e.metricValue).toEqual(source.metricValue); - checkUserInline(e, source); + expect(e.context).toEqual(source.context); } function checkSummaryEvent(e) { @@ -219,6 +209,31 @@ describe.each([ }); }); + it('filters context in feature event', async () => { + const config = { ...defaultConfig, allAttributesPrivate: true }; + await withProcessorAndSender(config, async (ep, mockEventSender) => { + const e = { + kind: 'feature', + creationDate: 1000, + context: eventContext, + key: 'flagkey', + version: 11, + variation: 1, + value: 'value', + default: 'default', + trackEvents: true, + }; + ep.enqueue(e); + await ep.flush(); + + expect(mockEventSender.calls.length()).toEqual(1); + const output = (await mockEventSender.calls.take()).events; + expect(output.length).toEqual(2); + checkFeatureEvent(output[0], e, false, filteredContext); + checkSummaryEvent(output[1]); + }); + }); + it('can both track and debug an event', async () => { await withProcessorAndSender(defaultConfig, async (ep, mockEventSender) => { const futureTime = new Date().getTime() + 1000000; @@ -380,6 +395,27 @@ describe.each([ }); }); + it('filters context in custom event', async () => { + const config = { ...defaultConfig, allAttributesPrivate: true }; + await withProcessorAndSender(config, async (ep, mockEventSender) => { + const e = { + kind: 'custom', + creationDate: 1000, + context: eventContext, + key: 'eventkey', + data: { thing: 'stuff' }, + metricValue: 1.5, + }; + ep.enqueue(e); + await ep.flush(); + + expect(mockEventSender.calls.length()).toEqual(1); + const output = (await mockEventSender.calls.take()).events; + expect(output.length).toEqual(1); + checkCustomEvent(output[0], { ...e, context: filteredContext }); + }); + }); + it('enforces event capacity', async () => { const config = { ...defaultConfig, eventCapacity: 1, logger: stubPlatform.logger() }; const e0 = { kind: 'custom', creationDate: 1000, context: eventContext, key: 'key0' };